Skip to content

Commit 997c30e

Browse files
committed
Merge branch 'dev' of github.com:resyon/leetcode-master into dev
2 parents 1516dc3 + 1b39577 commit 997c30e

25 files changed

+875
-78
lines changed

problems/0015.三数之和.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,23 @@ var threeSum = function(nums) {
336336
```
337337

338338

339+
ruby:
340+
```ruby
341+
def is_valid(strs)
342+
symbol_map = {')' => '(', '}' => '{', ']' => '['}
343+
stack = []
344+
strs.size.times {|i|
345+
c = strs[i]
346+
if symbol_map.has_key?(c)
347+
top_e = stack.shift
348+
return false if symbol_map[c] != top_e
349+
else
350+
stack.unshift(c)
351+
end
352+
}
353+
stack.empty?
354+
end
355+
```
339356

340357
-----------------------
341358
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/0019.删除链表的倒数第N个节点.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,30 @@ class Solution {
112112
}
113113
}
114114
```
115+
116+
Python:
117+
```python
118+
# Definition for singly-linked list.
119+
# class ListNode:
120+
# def __init__(self, val=0, next=None):
121+
# self.val = val
122+
# self.next = next
123+
class Solution:
124+
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
125+
head_dummy = ListNode()
126+
head_dummy.next = head
127+
128+
slow, fast = head_dummy, head_dummy
129+
while(n!=0): #fast先往前走n步
130+
fast = fast.next
131+
n -= 1
132+
while(fast.next!=None):
133+
slow = slow.next
134+
fast = fast.next
135+
#fast 走到结尾后,slow的下一个节点为倒数第N个节点
136+
slow.next = slow.next.next #删除
137+
return head_dummy.next
138+
```
115139
Go:
116140
```Go
117141
/**

problems/0027.移除元素.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,20 @@ var removeElement = (nums, val) => {
186186
};
187187
```
188188

189+
Ruby:
190+
```ruby
191+
def remove_element(nums, val)
192+
i = 0
193+
nums.each_index do |j|
194+
if nums[j] != val
195+
nums[i] = nums[j]
196+
i+=1
197+
end
198+
end
199+
i
200+
end
201+
```
202+
189203
-----------------------
190204
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
191205
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

problems/0039.组合总和.md

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -237,34 +237,28 @@ public:
237237

238238
Java:
239239
```Java
240+
// 剪枝优化
240241
class Solution {
241-
List<List<Integer>> lists = new ArrayList<>();
242-
Deque<Integer> deque = new LinkedList<>();
243-
244-
public List<List<Integer>> combinationSum3(int k, int n) {
245-
int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
246-
backTracking(arr, n, k, 0);
247-
return lists;
242+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
243+
List<List<Integer>> res = new ArrayList<>();
244+
Arrays.sort(candidates); // 先进行排序
245+
backtracking(res, new ArrayList<>(), candidates, target, 0, 0);
246+
return res;
248247
}
249248

250-
public void backTracking(int[] arr, int n, int k, int startIndex) {
251-
//如果 n 小于0,没必要继续本次递归,已经不符合要求了
252-
if (n < 0) {
253-
return;
254-
}
255-
if (deque.size() == k) {
256-
if (n == 0) {
257-
lists.add(new ArrayList(deque));
258-
}
249+
public void backtracking(List<List<Integer>> res, List<Integer> path, int[] candidates, int target, int sum, int idx) {
250+
// 找到了数字和为 target 的组合
251+
if (sum == target) {
252+
res.add(new ArrayList<>(path));
259253
return;
260254
}
261-
for (int i = startIndex; i < arr.length - (k - deque.size()) + 1; i++) {
262-
deque.push(arr[i]);
263-
//减去当前元素
264-
n -= arr[i];
265-
backTracking(arr, n, k, i + 1);
266-
//恢复n
267-
n += deque.pop();
255+
256+
for (int i = idx; i < candidates.length; i++) {
257+
// 如果 sum + candidates[i] > target 就终止遍历
258+
if (sum + candidates[i] > target) break;
259+
path.add(candidates[i]);
260+
backtracking(res, path, candidates, target, sum + candidates[i], i);
261+
path.remove(path.size() - 1); // 回溯,移除路径 path 最后一个元素
268262
}
269263
}
270264
}

problems/0046.全排列.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,45 @@ class Solution {
182182
```
183183

184184
Python:
185+
```python3
186+
class Solution:
187+
def permute(self, nums: List[int]) -> List[List[int]]:
188+
res = [] #存放符合条件结果的集合
189+
path = [] #用来存放符合条件的结果
190+
used = [] #用来存放已经用过的数字
191+
def backtrack(nums,used):
192+
if len(path) == len(nums):
193+
return res.append(path[:]) #此时说明找到了一组
194+
for i in range(0,len(nums)):
195+
if nums[i] in used:
196+
continue #used里已经收录的元素,直接跳过
197+
path.append(nums[i])
198+
used.append(nums[i])
199+
backtrack(nums,used)
200+
used.pop()
201+
path.pop()
202+
backtrack(nums,used)
203+
return res
204+
```
185205

206+
Python(优化,不用used数组):
207+
```python3
208+
class Solution:
209+
def permute(self, nums: List[int]) -> List[List[int]]:
210+
res = [] #存放符合条件结果的集合
211+
path = [] #用来存放符合条件的结果
212+
def backtrack(nums):
213+
if len(path) == len(nums):
214+
return res.append(path[:]) #此时说明找到了一组
215+
for i in range(0,len(nums)):
216+
if nums[i] in path: #path里已经收录的元素,直接跳过
217+
continue
218+
path.append(nums[i])
219+
backtrack(nums) #递归
220+
path.pop() #回溯
221+
backtrack(nums)
222+
return res
223+
```
186224

187225
Go:
188226
```Go

problems/0055.跳跃游戏.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,20 @@ func canJUmp(nums []int) bool {
141141
}
142142
```
143143

144-
144+
Javascript:
145+
```Javascript
146+
var canJump = function(nums) {
147+
if(nums.length === 1) return true
148+
let cover = 0
149+
for(let i = 0; i <= cover; i++) {
150+
cover = Math.max(cover, i + nums[i])
151+
if(cover >= nums.length - 1) {
152+
return true
153+
}
154+
}
155+
return false
156+
};
157+
```
145158

146159

147160
-----------------------

problems/0078.子集.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,20 @@ class Solution {
205205
```
206206

207207
Python:
208-
208+
```python3
209+
class Solution:
210+
def subsets(self, nums: List[int]) -> List[List[int]]:
211+
res = []
212+
path = []
213+
def backtrack(nums,startIndex):
214+
res.append(path[:]) #收集子集,要放在终止添加的上面,否则会漏掉自己
215+
for i in range(startIndex,len(nums)): #当startIndex已经大于数组的长度了,就终止了,for循环本来也结束了,所以不需要终止条件
216+
path.append(nums[i])
217+
backtrack(nums,i+1) #递归
218+
path.pop() #回溯
219+
backtrack(nums,0)
220+
return res
221+
```
209222

210223
Go:
211224
```Go

problems/0090.子集II.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,23 @@ class Solution {
208208
```
209209

210210
Python:
211-
211+
```python3
212+
class Solution:
213+
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
214+
res = [] #存放符合条件结果的集合
215+
path = [] #用来存放符合条件结果
216+
def backtrack(nums,startIndex):
217+
res.append(path[:])
218+
for i in range(startIndex,len(nums)):
219+
if i > startIndex and nums[i] == nums[i - 1]: #我们要对同一树层使用过的元素进行跳过
220+
continue
221+
path.append(nums[i])
222+
backtrack(nums,i+1) #递归
223+
path.pop() #回溯
224+
nums = sorted(nums) #去重需要排序
225+
backtrack(nums,0)
226+
return res
227+
```
212228

213229
Go:
214230
```Go

problems/0096.不同的二叉搜索树.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,16 @@ class Solution {
186186
```
187187

188188
Python:
189-
189+
```python
190+
class Solution:
191+
def numTrees(self, n: int) -> int:
192+
dp = [0] * (n + 1)
193+
dp[0], dp[1] = 1, 1
194+
for i in range(2, n + 1):
195+
for j in range(1, i + 1):
196+
dp[i] += dp[j - 1] * dp[i - j]
197+
return dp[-1]
198+
```
190199

191200
Go:
192201
```Go

0 commit comments

Comments
 (0)