Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 59f19ab

Browse files
Merge pull request #1 from doocs/master
update
2 parents fc3c9ad + 1c8b53e commit 59f19ab

File tree

67 files changed

+3019
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+3019
-6
lines changed

‎README.md‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<p align="center">
2-
<a href="https://github.com/doocs/leetcode"><img src="http://p9ucdlghd.bkt.clouddn.com/leetcode-github.png" alt="LeetCode-GitHub"></a>
2+
<a href="https://github.com/doocs/leetcode"><img src="http://p9ucdlghd.bkt.clouddn.com/leetcode-github-yanglbme.png" alt="LeetCode-GitHub-yanglbme"></a>
33
</p>
44

55
<p align="center">
6-
<a href="https://github.com/doocs/leetcode"><img src="https://img.shields.io/badge/Lang-Java%2FPython%2FJS%2FCPP%2FGo%2F...-blue.svg" alt="Language"></a>
6+
<a href="https://github.com/doocs/leetcode"><img src="https://img.shields.io/badge/lang-Java%2FPython%2FRuby%2FCPP%2FJS%2FGo...-blue.svg" alt="Language"></a>
77
<a href="http://makeapullrequest.com"><img src="https://img.shields.io/badge/PRs-Welcome-brightgreen.svg" alt="PRs Welcome"></a>
88
<a href="https://github.com/doocs/leetcode/issues"><img src="https://img.shields.io/github/issues/doocs/leetcode.svg" alt="issues"></a>
99
<a href="https://github.com/doocs/leetcode/stargazers"><img src="https://img.shields.io/github/stars/doocs/leetcode.svg" alt="stars"></a>
1010
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://img.shields.io/github/forks/doocs/leetcode.svg" alt="forks"></a>
1111
</p>
1212

1313
## Introduction
14-
Complete solutions to Leetcode problems, updated daily.
14+
Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to Leetcode problems, [updated daily](https://github.com/doocs/leetcode/projects/1).
1515

1616
## Solution List
1717

@@ -21,22 +21,30 @@ Complete solutions to Leetcode problems, updated daily.
2121
|---|---|---|
2222
| 001 | [Two Sum](https://github.com/doocs/leetcode/tree/master/solution/001.Two%20Sum) | `Array`, `Hash Table` |
2323
| 007 | [Reverse Integer](https://github.com/doocs/leetcode/tree/master/solution/007.Reverse%20Integer) | `Math` |
24+
| 009 | [Palindrome Number](https://github.com/doocs/leetcode/tree/master/solution/009.Palindrome%20Number) | `Math` |
2425
| 013 | [Roman to Integer](https://github.com/doocs/leetcode/tree/master/solution/013.Roman%20to%20Integer) | `Math`, `String` |
2526
| 014 | [Longest Common Prefix](https://github.com/doocs/leetcode/tree/master/solution/014.Longest%20Common%20Prefix) | `String` |
2627
| 020 | [Valid Parentheses](https://github.com/doocs/leetcode/tree/master/solution/020.Valid%20Parentheses) | `String`, `Stack` |
2728
| 021 | [Merge Two Sorted Lists](https://github.com/doocs/leetcode/tree/master/solution/021.Merge%20Two%20Sorted%20Lists) | `Linked List` |
29+
| 028 | [Implement strStr()](https://github.com/doocs/leetcode/tree/master/solution/028.Implement%20strStr()) | `Two Pointers`, `String` |
30+
| 035 | [Search Insert Position](https://github.com/doocs/leetcode/tree/master/solution/035.Search%20Insert%20Position) | `Array`, `Binary Search` |
2831
| 053 | [Maximum Subarray](https://github.com/doocs/leetcode/tree/master/solution/053.Maximum%20Subarray) | `Array`, `Divide and Conquer`, `Dynamic Programming` |
2932
| 070 | [Climbing Stairs](https://github.com/doocs/leetcode/tree/master/solution/070.Climbing%20Stairs) | `Dynamic Programming` |
3033
| 083 | [Remove Duplicates from Sorted List](https://github.com/doocs/leetcode/tree/master/solution/083.Remove%20Duplicates%20from%20Sorted%20List) | `Linked List` |
3134
| 136 | [Single Number](https://github.com/doocs/leetcode/tree/master/solution/136.Single%20Number) | `Hash Table`, `Bit Manipulation` |
35+
| 141 | [Linked List Cycle](https://github.com/doocs/leetcode/tree/master/solution/141.Linked%20List%20Cycle) | `Linked List`, `Two Pointers` |
3236
| 189 | [Rotate Array](https://github.com/doocs/leetcode/tree/master/solution/189.Rotate%20Array) | `Array` |
3337
| 198 | [House Robber](https://github.com/doocs/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
3438
| 203 | [Remove Linked List Elements](https://github.com/doocs/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
3539
| 231 | [Power of Two](https://github.com/doocs/leetcode/tree/master/solution/231.Power%20of%20Two) | `Math`, `Bit Manipulation` |
40+
| 234 | [Palindrome Linked List](https://github.com/doocs/leetcode/tree/master/solution/234.Palindrome%20Linked%20List) | `Linked List`, `Two Pointers` |
3641
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://github.com/doocs/leetcode/tree/master/solution/235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree) | `Tree` |
3742
| 237 | [Delete Node in a Linked List](https://github.com/doocs/leetcode/tree/master/solution/237.Delete%20Node%20in%20a%20Linked%20List) | `Linked List` |
3843
| 344 | [Reverse String](https://github.com/doocs/leetcode/tree/master/solution/344.Reverse%20String) | `Two Pointers`, `String` |
44+
| 581 | [Shortest Unsorted Continuous Subarray](https://github.com/doocs/leetcode/tree/master/solution/581.Shortest%20Unsorted%20Continuous%20Subarray) | `Array` |
45+
| 695 | [Max Area of Island](https://github.com/doocs/leetcode/tree/master/solution/695.Max%20Area%20of%20Island) | `Array`, `Depth-first Search` |
3946
| 703 | [Kth Largest Element in a Stream](https://github.com/doocs/leetcode/tree/master/solution/703.Kth%20Largest%20Element%20in%20a%20Stream) | `Heap` |
47+
| 707 | [Design Linked List](https://github.com/doocs/leetcode/tree/master/solution/707.Design%20Linked%20List) | `Linked List`, `Design` |
4048
| 876 | [Middle of the Linked List](https://github.com/doocs/leetcode/tree/master/solution/876.Middle%20of%20the%20Linked%20List) | `Linked List` |
4149

4250

@@ -45,12 +53,18 @@ Complete solutions to Leetcode problems, updated daily.
4553
| # | Title | Tags |
4654
|---|---|---|
4755
| 002 | [Add Two Numbers](https://github.com/doocs/leetcode/tree/master/solution/002.Add%20Two%20Numbers) | `Linked List`, `Math` |
56+
| 003 | [Longest Substring Without Repeating Characters](https://github.com/doocs/leetcode/tree/master/solution/003.Longest%20Substring%20Without%20Repeating%20Characters) | `Hash Table`, `Two Pointers`, `String` |
57+
| 005 | [Longest Palindromic Substring](https://github.com/doocs/leetcode/tree/master/solution/005.Longest%20Palindromic%20Substring) | `String`, `Dynamic Programming` |
4858
| 015 | [3Sum](https://github.com/doocs/leetcode/tree/master/solution/015.3Sum) | `Array`, `Two Pointers` |
59+
| 018 | [4Sum](https://github.com/doocs/leetcode/tree/master/solution/018.4Sum) | `Array`, `Hash Table`, `Two Pointers` |
4960
| 019 | [Remove Nth Node From End of List](https://github.com/doocs/leetcode/tree/master/solution/019.Remove%20Nth%20Node%20From%20End%20of%20List) | `Linked List`, `Two Pointers` |
5061
| 024 | [Swap Nodes in Pairs](https://github.com/doocs/leetcode/tree/master/solution/024.Swap%20Nodes%20in%20Pairs) | `Linked List` |
5162
| 031 | [Next Permutation](https://github.com/doocs/leetcode/tree/master/solution/031.Next%20Permutation) | `Array` |
5263
| 046 | [Permutations](https://github.com/doocs/leetcode/tree/master/solution/046.Permutations) | `Backtracking` |
5364
| 047 | [Permutations II](https://github.com/doocs/leetcode/tree/master/solution/047.Permutations%20II) | `Backtracking` |
65+
| 054 | [Spiral Matrix](https://github.com/doocs/leetcode/tree/master/solution/054.Spiral%20Matrix) | `Array` |
66+
| 059 | [Spiral Matrix II](https://github.com/doocs/leetcode/tree/master/solution/059.Spiral%20Matrix%20II) | `Array` |
67+
| 061 | [Rotate List](https://github.com/doocs/leetcode/tree/master/solution/061.Rotate%20List) | `Linked List`, `Two Pointers` |
5468
| 062 | [Unique Paths](https://github.com/doocs/leetcode/tree/master/solution/062.Unique%20Paths) | `Array`, `Dynamic Programming` |
5569
| 063 | [Unique Paths II](https://github.com/doocs/leetcode/tree/master/solution/063.Unique%20Paths%20II) | `Array`, `Dynamic Programming` |
5670
| 075 | [Sort Colors](https://github.com/doocs/leetcode/tree/master/solution/075.Sort%20Colors) | `Array`, `Two Pointers`, `Sort` |
@@ -62,30 +76,38 @@ Complete solutions to Leetcode problems, updated daily.
6276
| 127 | [Word Ladder](https://github.com/doocs/leetcode/tree/master/solution/127.Word%20Ladder) | `Breadth-first Search` |
6377
| 130 | [Surrounded Regions](https://github.com/doocs/leetcode/tree/master/solution/130.Surrounded%20Regions) | `Depth-first Search`, `Breadth-first Search`, `Union Find` |
6478
| 137 | [Single Number II](https://github.com/doocs/leetcode/tree/master/solution/137.Single%20Number%20II) | `Bit Manipulation` |
79+
| 142 | [Linked List Cycle II](https://github.com/doocs/leetcode/tree/master/solution/142.Linked%20List%20Cycle%20II) | `Linked List`, `Two Pointers` |
80+
| 143| [Reorder List](https://github.com/doocs/leetcode/tree/master/solution/143.Reorder%20List) | `Linked List` |
6581
| 144 | [Binary Tree Preorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/144.Binary%20Tree%20Preorder%20Traversal) | `Stack`, `Tree` |
6682
| 150 | [Evaluate Reverse Polish Notation](https://github.com/doocs/leetcode/tree/master/solution/150.Evaluate%20Reverse%20Polish%20Notation) | `Stack` |
6783
| 153 | [Find Minimum in Rotated Sorted Array](https://github.com/doocs/leetcode/tree/master/solution/153.Find%20Minimum%20in%20Rotated%20Sorted%20Array) | `Array`, `Binary Search` |
84+
| 328 | [Odd Even Linked List](https://github.com/doocs/leetcode/tree/master/solution/328.Odd%20Even%20Linked%20List) | `Linked List` |
6885

6986

7087
### Hard
7188

7289
| # | Title | Tags |
7390
|---|---|---|
91+
| 004 | [Median of Two Sorted Arrays](https://github.com/doocs/leetcode/tree/master/solution/004.Median%20of%20Two%20Sorted%20Arrays) | `Array`, `Binary Search`, `Divide and Conquer` |
7492
| 023 | [Merge k Sorted Lists](https://github.com/doocs/leetcode/tree/master/solution/023.Merge%20k%20Sorted%20Lists) | `Linked List`, `Divide and Conquer`, `Heap` |
7593
| 032 | [Longest Valid Parentheses](https://github.com/doocs/leetcode/tree/master/solution/032.Longest%20Valid%20Parentheses) | `String`, `Dynamic Programming` |
7694
| 084 | [Largest Rectangle in Histogram](https://github.com/doocs/leetcode/tree/master/solution/084.Largest%20Rectangle%20in%20Histogram) | `Array`, `Stack` |
7795
| 145 | [Binary Tree Postorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/145.Binary%20Tree%20Postorder%20Traversal) | `Stack`, `Tree` |
7896
| 295 | [Find Median from Data Stream](https://github.com/doocs/leetcode/tree/master/solution/295.Find%20Median%20from%20Data%20Stream) | `Heap`, `Design` |
7997

98+
8099
## Contributions
81-
I'm looking for long-term contributors/partners to this repo! Send me PRs if you're interested! See the following:
100+
I'm looking for long-term contributors/partners to this repo! Send me [PRs](https://github.com/doocs/leetcode/pulls) if you're interested! See the following:
82101
- Fork this repository to your own GitHub account and then [clone](https://help.github.com/articles/cloning-a-repository/) it to your local device.
83102
- Submit a pull request with your changes!
84103

85104
## Contributors
86105

106+
" If you want to go fast, go alone. If you want to go far, go together. And that's the spirit of [teamwork](https://github.com/doocs/leetcode/graphs/contributors) ".
107+
87108
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
88-
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/doocs/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="100px;"/><br /><sub><b>chakyam</b></sub>](https://github.com/chakyam)<br />[💻](https://github.com/doocs/leetcode/commits?author=chakyam "Code") </center> |
89-
|---|---|
109+
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="80px;"/>](https://github.com/yanglbme) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="80px;"/>](https://github.com/chakyam) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/10081554?v=4" width="80px;"/>](https://github.com/zhkmxx9302013) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/40383345?v=4" width="80px;"/>](https://github.com/MarkKuang1991) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/12371194?v=4" width="80px;"/>](https://github.com/fonxian) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/25222367?v=4" width="80px;"/>](https://github.com/zhanary) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/42396616?v=4" width="80px;"/>](https://github.com/ZhouTingZhaobiu) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/31923541?v=4" width="80px;"/>](https://github.com/zouwx2cs) </center> |
110+
|---|---|---|---|---|---|---|---|
111+
| <center> [<img src="https://avatars3.githubusercontent.com/u/20679510?v=4" width="80px;"/>](https://github.com/Mrzhudky) </center> |
90112

91113
<!-- ALL-CONTRIBUTORS-LIST:END -->

‎solution/001.Two Sum/Solution.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target)
4+
{
5+
vector<int> res ;
6+
unordered_map<int, int> hash ;
7+
8+
for (int i = 0; i < nums.size(); ++i)
9+
{
10+
int aim = target - nums[i] ;
11+
int local = hash[aim] ;
12+
if (local != NULL)
13+
{
14+
res.push_back(local-1) ;
15+
res.push_back(i) ;
16+
return res ;
17+
}
18+
else
19+
hash[nums[i]] = i+1 ;
20+
}
21+
22+
return res ;
23+
}
24+
};

‎solution/001.Two Sum/Solution.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func twoSum(nums []int, target int) []int {
2+
var numsMap map[int]int
3+
numsMap = make(map[int]int)
4+
for i := 0; i < len(nums); i++ {
5+
if _, ok := numsMap[target-nums[i]]; ok {
6+
7+
return []int{numsMap[target-nums[i]], i}
8+
}
9+
numsMap[nums[i]] = i
10+
}
11+
return nil
12+
}

‎solution/001.Two Sum/Solution.rb‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# @param {Integer[]} nums
2+
# @param {Integer} target
3+
# @return {Integer[]}
4+
def two_sum(nums, target)
5+
nums.each_with_index do |x, idx|
6+
if nums.include? target - x
7+
return [idx, nums.index(target - x)] if nums.index(target - x) != idx
8+
end
9+
next
10+
end
11+
end

‎solution/001.Two Sum/Solution.scala‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.collection.mutable
2+
3+
object Solution {
4+
def twoSum(nums: Array[Int], target: Int): Array[Int] = {
5+
var map = new mutable.HashMap[Int, Int]()
6+
for (i <- 0 to nums.length) {
7+
if (map.contains(target - nums(i))) {
8+
return Array(map(target - nums(i)), i)
9+
} else {
10+
map += (nums(i) -> i)
11+
}
12+
}
13+
Array(0, 0)
14+
}
15+
}

‎solution/001.Two Sum/Solution2.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#time 32ms
2+
class Solution(object):
3+
def twoSum(self, nums, target):
4+
buff = {}
5+
for i in range(len(nums)):
6+
if nums[i] in buff: #如果这个数在字典里,证明之前有数可以和他匹配
7+
return buff[nums[i]],i #返回之前留信息的数的编号,和当前这个数的编号
8+
else:
9+
buff[target - nums[i]] = i #这个数字不在字典里,那么就存一下他需要匹配的数,并记录编号
10+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Definition for singly-linked list.
2+
# class ListNode
3+
# attr_accessor :val, :next
4+
# def initialize(val)
5+
# @val = val
6+
# @next = nil
7+
# end
8+
# end
9+
10+
# @param {ListNode} l1
11+
# @param {ListNode} l2
12+
# @return {ListNode}
13+
14+
def add_two_numbers(l1, l2)
15+
return l2 if l1 == nil
16+
return l1 if l2 == nil
17+
cur_val = l1.val + l2.val
18+
l3 = ListNode.new(cur_val % 10)
19+
add = cur_val >= 10 ? 1 : 0
20+
tmp = l3
21+
22+
l1 = l1.next
23+
l2 = l2.next
24+
while !l1.nil? || !l2.nil? || add > 0
25+
cur_val = add
26+
cur_val += l1.nil? ? 0 : l1.val
27+
cur_val += l2.nil? ? 0 : l2.val
28+
tmp.next = ListNode.new(cur_val % 10)
29+
tmp = tmp.next
30+
add = cur_val >= 10 ? 1 : 0
31+
32+
l1 = l1.nil? ? l1 : l1.next
33+
l2 = l2.nil? ? l2 : l2.next
34+
end
35+
36+
l3
37+
end
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## 无重复字符的最长子串
2+
### 题目描述
3+
4+
给定一个字符串,找出不含有重复字符的**最长子串**的长度。
5+
6+
**示例 1:**
7+
```
8+
输入: "abcabcbb"
9+
输出: 3
10+
解释: 无重复字符的最长子串是 "abc",其长度为 3。
11+
```
12+
13+
**示例 2:**
14+
```
15+
输入: "bbbbb"
16+
输出: 1
17+
解释: 无重复字符的最长子串是 "b",其长度为 1。
18+
```
19+
20+
**示例 3:**
21+
```
22+
输入: "pwwkew"
23+
输出: 3
24+
解释: 无重复字符的最长子串是 "wke",其长度为 3。
25+
请注意,答案必须是一个子串,"pwke" 是一个子序列 而不是子串。
26+
```
27+
28+
### 解法
29+
利用指针 `p`, `q`,初始指向字符串开头。遍历字符串,`q` 向右移动,若指向的字符在 map 中,说明出现了重复字符,此时,`p` 要在出现**重复字符的下一个位置** `map.get(chars[q]) + 1`**当前位置** `p` 之间取较大值,防止 `p` 指针回溯。循环的过程中,要将 chars[q] 及对应位置放入 map 中,也需要不断计算出`max``q - p + 1` 的较大值,赋给 `max`。最后输出 `max` 即可。
30+
31+
```java
32+
class Solution {
33+
public int lengthOfLongestSubstring(String s) {
34+
if (s == null || s.length() == 0) {
35+
return 0;
36+
}
37+
char[] chars = s.toCharArray();
38+
int len = chars.length;
39+
int p = 0, q = 0;
40+
int max = 0;
41+
Map<Character, Integer> map = new HashMap<>();
42+
while (q < len) {
43+
if (map.containsKey(chars[q])) {
44+
// 防止p指针回溯,导致计算到重复字符的长度
45+
// eg. abba,当p指向最右的a时,若简单把p赋为map.get(chars[q] + 1),则出现指针回溯
46+
p = Math.max(p, map.get(chars[q]) + 1);
47+
}
48+
map.put(chars[q], q);
49+
max = Math.max(max, q - p + 1);
50+
++q;
51+
}
52+
53+
return max;
54+
}
55+
}
56+
```
57+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int lengthOfLongestSubstring(String s) {
3+
if (s == null || s.length() == 0) {
4+
return 0;
5+
}
6+
char[] chars = s.toCharArray();
7+
int len = chars.length;
8+
int p = 0, q = 0;
9+
int max = 0;
10+
Map<Character, Integer> map = new HashMap<>();
11+
while (q < len) {
12+
if (map.containsKey(chars[q])) {
13+
// 防止p指针回溯,导致计算到重复字符的长度
14+
p = Math.max(p, map.get(chars[q]) + 1);
15+
}
16+
map.put(chars[q], q);
17+
max = Math.max(max, q - p + 1);
18+
++q;
19+
}
20+
21+
return max;
22+
}
23+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /