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 83c4a31

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents a64f7a1 + fa66994 commit 83c4a31

22 files changed

+445
-189
lines changed

‎problems/0028.实现strStr.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
# 28. 实现 strStr()
1111

12-
[力扣题目链接](https://leetcode.cn/problems/implement-strstr/)
12+
[力扣题目链接](https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/)
1313

1414
实现 strStr() 函数。
1515

@@ -699,7 +699,7 @@ class Solution(object):
699699
if haystack[i:i+n]==needle:
700700
return i
701701
return -1
702-
```
702+
```
703703
```python
704704
// 方法一
705705
class Solution:

‎problems/0031.下一个排列.md‎

Lines changed: 19 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -160,73 +160,34 @@ class Solution {
160160
```
161161
162162
## Python
163-
>直接使用sorted()不符合题意
163+
>直接使用sorted()会开辟新的空间并返回一个新的list,故补充一个原地反转函数
164164
```python
165165
class Solution:
166166
def nextPermutation(self, nums: List[int]) -> None:
167167
"""
168168
Do not return anything, modify nums in-place instead.
169169
"""
170-
for i in range(len(nums)-1, -1, -1):
171-
for j in range(len(nums)-1, i, -1):
170+
length = len(nums)
171+
for i in range(length - 1, -1, -1):
172+
for j in range(length - 1, i, -1):
172173
if nums[j] > nums[i]:
173174
nums[j], nums[i] = nums[i], nums[j]
174-
nums[i+1:len(nums)] = sorted(nums[i+1:len(nums)])
175-
return
176-
nums.sort()
177-
```
178-
>另一种思路
179-
```python
180-
class Solution:
181-
'''
182-
抛砖引玉:因题目要求"必须原地修改,只允许使用额外常数空间",python内置sorted函数以及数组切片+sort()无法使用。
183-
故选择另一种算法暂且提供一种python思路
184-
'''
185-
def nextPermutation(self, nums: List[int]) -> None:
186-
"""
187-
Do not return anything, modify nums in-place instead.
188-
"""
189-
length = len(nums)
190-
for i in range(length-1, 0, -1):
191-
if nums[i-1] < nums[i]:
192-
for j in range(length-1, 0, -1):
193-
if nums[j] > nums[i-1]:
194-
nums[i-1], nums[j] = nums[j], nums[i-1]
195-
break
196-
self.reverse(nums, i, length-1)
197-
break
198-
if n == 1:
199-
# 若正常结束循环,则对原数组直接翻转
200-
self.reverse(nums, 0, length-1)
201-
202-
def reverse(self, nums: List[int], low: int, high: int) -> None:
203-
while low < high:
204-
nums[low], nums[high] = nums[high], nums[low]
205-
low += 1
206-
high -= 1
207-
```
208-
>上一版本简化版
209-
```python
210-
class Solution(object):
211-
def nextPermutation(self, nums: List[int]) -> None:
212-
n = len(nums)
213-
i = n-2
214-
while i >= 0 and nums[i] >= nums[i+1]:
215-
i -= 1
216-
217-
if i > -1: // i==-1,不存在下一个更大的排列
218-
j = n-1
219-
while j >= 0 and nums[j] <= nums[i]:
220-
j -= 1
221-
nums[i], nums[j] = nums[j], nums[i]
175+
self.reverse(nums, i + 1, length - 1)
176+
return
177+
self.reverse(nums, 0, length - 1)
222178
223-
start, end = i+1, n-1
224-
while start < end:
225-
nums[start], nums[end] = nums[end], nums[start]
226-
start += 1
227-
end -= 1
228-
229-
return nums
179+
def reverse(self, nums: List[int], left: int, right: int) -> None:
180+
while left < right:
181+
nums[left], nums[right] = nums[right], nums[left]
182+
left += 1
183+
right -= 1
184+
185+
"""
186+
265 / 265 个通过测试用例
187+
状态:通过
188+
执行用时: 36 ms
189+
内存消耗: 14.9 MB
190+
"""
230191
```
231192

232193
## Go

‎problems/0040.组合总和II.md‎

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -258,40 +258,45 @@ public:
258258
**使用标记数组**
259259
```Java
260260
class Solution {
261-
List<List<Integer>> lists = new ArrayList<>();
262-
Deque<Integer> deque = new LinkedList<>();
263-
int sum = 0;
264-
265-
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
266-
//为了将重复的数字都放到一起,所以先进行排序
267-
Arrays.sort(candidates);
268-
//加标志数组,用来辅助判断同层节点是否已经遍历
269-
boolean[] flag = new boolean[candidates.length];
270-
backTracking(candidates, target, 0, flag);
271-
return lists;
272-
}
261+
LinkedList<Integer> path = new LinkedList<>();
262+
List<List<Integer>> ans = new ArrayList<>();
263+
boolean[] used;
264+
int sum = 0;
273265

274-
public void backTracking(int[] arr, int target, int index, boolean[] flag) {
275-
if (sum == target) {
276-
lists.add(new ArrayList(deque));
277-
return;
278-
}
279-
for (int i = index; i < arr.length && arr[i] + sum <= target; i++) {
280-
//出现重复节点,同层的第一个节点已经被访问过,所以直接跳过
281-
if (i > 0 && arr[i] == arr[i - 1] && !flag[i - 1]) {
282-
continue;
283-
}
284-
flag[i] = true;
285-
sum += arr[i];
286-
deque.push(arr[i]);
287-
//每个节点仅能选择一次,所以从下一位开始
288-
backTracking(arr, target, i + 1, flag);
289-
int temp = deque.pop();
290-
flag[i] = false;
291-
sum -= temp;
292-
}
266+
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
267+
used = new boolean[candidates.length];
268+
// 加标志数组,用来辅助判断同层节点是否已经遍历
269+
Arrays.fill(used, false);
270+
// 为了将重复的数字都放到一起,所以先进行排序
271+
Arrays.sort(candidates);
272+
backTracking(candidates, target, 0);
273+
return ans;
274+
}
275+
276+
private void backTracking(int[] candidates, int target, int startIndex) {
277+
if (sum == target) {
278+
ans.add(new ArrayList(path));
279+
}
280+
for (int i = startIndex; i < candidates.length; i++) {
281+
if (sum + candidates[i] > target) {
282+
break;
283+
}
284+
// 出现重复节点,同层的第一个节点已经被访问过,所以直接跳过
285+
if (i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]) {
286+
continue;
287+
}
288+
used[i] = true;
289+
sum += candidates[i];
290+
path.add(candidates[i]);
291+
// 每个节点仅能选择一次,所以从下一位开始
292+
backTracking(candidates, target, i + 1);
293+
used[i] = false;
294+
sum -= candidates[i];
295+
path.removeLast();
293296
}
297+
}
294298
}
299+
295300
```
296301
**不使用标记数组**
297302
```Java

‎problems/0046.全排列.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ var permute = function(nums) {
331331

332332
```
333333

334-
## TypeScript
334+
### TypeScript
335335

336336
```typescript
337337
function permute(nums: number[]): number[][] {

‎problems/0063.不同路径II.md‎

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,24 +210,54 @@ public:
210210
```java
211211
class Solution {
212212
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
213-
int n = obstacleGrid.length, m = obstacleGrid[0].length;
214-
int[][] dp = new int[n][m];
215-
216-
for (int i = 0; i < m; i++) {
217-
if (obstacleGrid[0][i] == 1) break; //一旦遇到障碍,后续都到不了
218-
dp[0][i] = 1;
213+
int m = obstacleGrid.length;
214+
int n = obstacleGrid[0].length;
215+
int[][] dp = new int[m][n];
216+
217+
//如果在起点或终点出现了障碍,直接返回0
218+
if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) {
219+
return 0;
219220
}
220-
for (int i =0; i < n; i++) {
221-
if (obstacleGrid[i][0] == 1) break; ////一旦遇到障碍,后续都到不了
222-
dp[i][0] = 1;
221+
222+
for (int i =0; i < m &&obstacleGrid[i][0] == 0; i++) {
223+
dp[i][0] = 1;
223224
}
224-
for (int i = 1; i < n; i++) {
225-
for (int j = 1; j < m; j++) {
226-
if (obstacleGrid[i][j] == 1) continue;
227-
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
225+
for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) {
226+
dp[0][j] = 1;
227+
}
228+
229+
for (int i = 1; i < m; i++) {
230+
for (int j = 1; j < n; j++) {
231+
dp[i][j] = (obstacleGrid[i][j] == 0) ? dp[i - 1][j] + dp[i][j - 1] : 0;
232+
}
233+
}
234+
return dp[m - 1][n - 1];
235+
}
236+
}
237+
```
238+
239+
```java
240+
// 空间优化版本
241+
class Solution {
242+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
243+
int m = obstacleGrid.length;
244+
int n = obstacleGrid[0].length;
245+
int[] dp = new int[n];
246+
247+
for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) {
248+
dp[j] = 1;
249+
}
250+
251+
for (int i = 1; i < m; i++) {
252+
for (int j = 0; j < n; j++) {
253+
if (obstacleGrid[i][j] == 1) {
254+
dp[j] = 0;
255+
} else if (j != 0) {
256+
dp[j] += dp[j - 1];
257+
}
228258
}
229259
}
230-
return dp[n - 1][m -1];
260+
return dp[n - 1];
231261
}
232262
}
233263
```

‎problems/0070.爬楼梯.md‎

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -213,22 +213,6 @@ public:
213213
214214
215215
### Java
216-
```Java
217-
class Solution {
218-
public int climbStairs(int n) {
219-
// 跟斐波那契数列一样
220-
if(n <= 2) return n;
221-
int a = 1, b = 2, sum = 0;
222-
223-
for(int i = 3; i <= n; i++){
224-
sum = a + b;
225-
a = b;
226-
b = sum;
227-
}
228-
return b;
229-
}
230-
}
231-
```
232216
233217
```java
234218
// 常规方式
@@ -241,15 +225,22 @@ public int climbStairs(int n) {
241225
}
242226
return dp[n];
243227
}
228+
```
229+
230+
```Java
244231
// 用变量记录代替数组
245-
public int climbStairs(int n) {
246-
int a = 0, b = 1, c = 0; // 默认需要1次
247-
for (int i = 1; i <= n; i++) {
248-
c = a + b; // f(i - 1) + f(n - 2)
249-
a = b; // 记录上一轮的值
250-
b = c; // 向后步进1个数
232+
class Solution {
233+
public int climbStairs(int n) {
234+
if(n <= 2) return n;
235+
int a = 1, b = 2, sum = 0;
236+
237+
for(int i = 3; i <= n; i++){
238+
sum = a + b; // f(i - 1) + f(i - 2)
239+
a = b; // 记录f(i - 1),即下一轮的f(i - 2)
240+
b = sum; // 记录f(i),即下一轮的f(i - 1)
241+
}
242+
return b;
251243
}
252-
return c;
253244
}
254245
```
255246

‎problems/0078.子集.md‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,6 @@ class Solution {
180180
List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
181181
LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
182182
public List<List<Integer>> subsets(int[] nums) {
183-
if (nums.length == 0){
184-
result.add(new ArrayList<>());
185-
return result;
186-
}
187183
subsetsHelper(nums, 0);
188184
return result;
189185
}

‎problems/0112.路径总和.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ class solution:
499499
if not cur_node.left and not cur_node.right:
500500
if remain == 0:
501501
result.append(path[:])
502-
return
502+
return
503503

504504
if cur_node.left:
505505
path.append(cur_node.left.val)
@@ -508,7 +508,7 @@ class solution:
508508

509509
if cur_node.right:
510510
path.append(cur_node.right.val)
511-
traversal(cur_node.right, remain-cur_node.left.val)
511+
traversal(cur_node.right, remain-cur_node.right.val)
512512
path.pop()
513513

514514
result, path = [], []

‎problems/0135.分发糖果.md‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,25 +135,22 @@ class Solution {
135135
2、起点下标 ratings.length - 2 从右往左, 只要左边 比 右边 大,此时 左边的糖果应该 取本身的糖果数(符合比它左边大) 和 右边糖果数 + 1 二者的最大值,这样才符合 它比它左边的大,也比它右边大
136136
*/
137137
public int candy(int[] ratings) {
138-
int[] candyVec = new int[ratings.length];
138+
int len = ratings.length;
139+
int[] candyVec = new int[len];
139140
candyVec[0] = 1;
140-
for (int i = 1; i < ratings.length; i++) {
141-
if (ratings[i] > ratings[i - 1]) {
142-
candyVec[i] = candyVec[i - 1] + 1;
143-
} else {
144-
candyVec[i] = 1;
145-
}
141+
for (int i = 1; i < len; i++) {
142+
candyVec[i] = (ratings[i] > ratings[i - 1]) ? candyVec[i - 1] + 1 : 1;
146143
}
147144
148-
for (int i = ratings.length - 2; i >= 0; i--) {
145+
for (int i = len - 2; i >= 0; i--) {
149146
if (ratings[i] > ratings[i + 1]) {
150147
candyVec[i] = Math.max(candyVec[i], candyVec[i + 1] + 1);
151148
}
152149
}
153150
154151
int ans = 0;
155-
for (int s : candyVec) {
156-
ans += s;
152+
for (int num : candyVec) {
153+
ans += num;
157154
}
158155
return ans;
159156
}

0 commit comments

Comments
(0)

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