Skip to content

Commit 4f0aa8b

Browse files
Merge branch 'master' into patch-4
2 parents 94bf891 + 42c78d9 commit 4f0aa8b

File tree

109 files changed

+1664
-772
lines changed

Some content is hidden

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

109 files changed

+1664
-772
lines changed

.DS_Store

-8 KB
Binary file not shown.

problems/0001.两数之和.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ class Solution:
118118
return [records[target - val], idx] # 如果存在就返回字典记录索引和当前索引
119119
```
120120

121+
Python (v2):
122+
123+
```python
124+
class Solution:
125+
def twoSum(self, nums: List[int], target: int) -> List[int]:
126+
rec = {}
127+
for i in range(len(nums)):
128+
rest = target - nums[i]
129+
# Use get to get the index of the data, making use of one of the dictionary properties.
130+
if rec.get(rest, None) is not None: return [rec[rest], i]
131+
rec[nums[i]] = i
132+
```
121133

122134
Go:
123135

problems/0005.最长回文子串.md

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,26 @@ public:
260260

261261
# 其他语言版本
262262

263-
## Java
263+
Java:
264+
265+
```java
266+
public int[] twoSum(int[] nums, int target) {
267+
int[] res = new int[2];
268+
if(nums == null || nums.length == 0){
269+
return res;
270+
}
271+
Map<Integer, Integer> map = new HashMap<>();
272+
for(int i = 0; i < nums.length; i++){
273+
int temp = target - nums[i];
274+
if(map.containsKey(temp)){
275+
res[1] = i;
276+
res[0] = map.get(temp);
277+
}
278+
map.put(nums[i], i);
279+
}
280+
return res;
281+
}
282+
```
264283

265284
```java
266285
// 双指针 中心扩散法
@@ -291,7 +310,7 @@ class Solution {
291310
}
292311
```
293312

294-
## Python
313+
Python
295314

296315
```python
297316
class Solution:
@@ -312,7 +331,8 @@ class Solution:
312331
return s[left:right + 1]
313332

314333
```
315-
> 双指针法:
334+
双指针:
335+
316336
```python
317337
class Solution:
318338
def longestPalindrome(self, s: str) -> str:
@@ -340,13 +360,13 @@ class Solution:
340360
return s[start:end]
341361

342362
```
343-
## Go
363+
Go:
344364

345365
```go
346366

347367
```
348368

349-
## JavaScript
369+
JavaScript
350370

351371
```js
352372
//动态规划解法
@@ -462,8 +482,9 @@ var longestPalindrome = function(s) {
462482
};
463483
```
464484

465-
## C
466-
动态规划:
485+
C:
486+
487+
动态规划:
467488
```c
468489
//初始化dp数组,全部初始为false
469490
bool **initDP(int strLen) {
@@ -513,7 +534,7 @@ char * longestPalindrome(char * s){
513534
}
514535
```
515536
516-
双指针:
537+
双指针
517538
```c
518539
int left, maxLength;
519540
void extend(char *str, int i, int j, int size) {

problems/0015.三数之和.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,34 @@ class Solution:
247247
right -= 1
248248
return ans
249249
```
250+
Python (v2):
251+
252+
```python
253+
class Solution:
254+
def threeSum(self, nums: List[int]) -> List[List[int]]:
255+
if len(nums) < 3: return []
256+
nums, res = sorted(nums), []
257+
for i in range(len(nums) - 2):
258+
cur, l, r = nums[i], i + 1, len(nums) - 1
259+
if res != [] and res[-1][0] == cur: continue # Drop duplicates for the first time.
260+
261+
while l < r:
262+
if cur + nums[l] + nums[r] == 0:
263+
res.append([cur, nums[l], nums[r]])
264+
# Drop duplicates for the second time in interation of l & r. Only used when target situation occurs, because that is the reason for dropping duplicates.
265+
while l < r - 1 and nums[l] == nums[l + 1]:
266+
l += 1
267+
while r > l + 1 and nums[r] == nums[r - 1]:
268+
r -= 1
269+
if cur + nums[l] + nums[r] > 0:
270+
r -= 1
271+
else:
272+
l += 1
273+
return res
274+
```
275+
250276
Go:
277+
251278
```Go
252279
func threeSum(nums []int)[][]int{
253280
sort.Ints(nums)

problems/0018.四数之和.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ class Solution(object):
216216

217217
# good thing about using python is you can use set to drop duplicates.
218218
ans = set()
219+
# ans = [] # save results by list()
219220
for i in range(len(nums)):
220221
for j in range(i + 1, len(nums)):
221222
for k in range(j + 1, len(nums)):
@@ -224,10 +225,16 @@ class Solution(object):
224225
# make sure no duplicates.
225226
count = (nums[i] == val) + (nums[j] == val) + (nums[k] == val)
226227
if hashmap[val] > count:
227-
ans.add(tuple(sorted([nums[i], nums[j], nums[k], val])))
228-
else:
229-
continue
230-
return ans
228+
ans_tmp = tuple(sorted([nums[i], nums[j], nums[k], val]))
229+
ans.add(ans_tmp)
230+
# Avoiding duplication in list manner but it cause time complexity increases
231+
# if ans_tmp not in ans:
232+
# ans.append(ans_tmp)
233+
else:
234+
continue
235+
return list(ans)
236+
# if used list() to save results, just
237+
# return ans
231238

232239
```
233240

problems/0024.两两交换链表中的节点.md

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -254,32 +254,20 @@ TypeScript:
254254

255255
```typescript
256256
function swapPairs(head: ListNode | null): ListNode | null {
257-
/**
258-
* 初始状态:
259-
* curNode -> node1 -> node2 -> tmepNode
260-
* 转换过程:
261-
* curNode -> node2
262-
* curNode -> node2 -> node1
263-
* curNode -> node2 -> node1 -> tempNode
264-
* curNode = node1
265-
*/
266-
let retNode: ListNode | null = new ListNode(0, head),
267-
curNode: ListNode | null = retNode,
268-
node1: ListNode | null = null,
269-
node2: ListNode | null = null,
270-
tempNode: ListNode | null = null;
271-
272-
while (curNode && curNode.next && curNode.next.next) {
273-
node1 = curNode.next;
274-
node2 = curNode.next.next;
275-
tempNode = node2.next;
276-
curNode.next = node2;
277-
node2.next = node1;
278-
node1.next = tempNode;
279-
curNode = node1;
280-
}
281-
return retNode.next;
282-
};
257+
const dummyHead: ListNode = new ListNode(0, head);
258+
let cur: ListNode = dummyHead;
259+
while(cur.next !== null && cur.next.next !== null) {
260+
const tem: ListNode = cur.next;
261+
const tem1: ListNode = cur.next.next.next;
262+
263+
cur.next = cur.next.next; // step 1
264+
cur.next.next = tem; // step 2
265+
cur.next.next.next = tem1; // step 3
266+
267+
cur = cur.next.next;
268+
}
269+
return dummyHead.next;
270+
}
283271
```
284272

285273
Kotlin:

problems/0042.接雨水.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public:
365365

366366
## 其他语言版本
367367

368-
Java:
368+
### Java:
369369

370370
双指针法
371371
```java
@@ -468,7 +468,7 @@ class Solution {
468468
}
469469
```
470470

471-
Python:
471+
### Python:
472472

473473
双指针法
474474
```python3
@@ -575,7 +575,7 @@ class Solution:
575575

576576
```
577577

578-
Go:
578+
### Go
579579

580580
```go
581581
func trap(height []int) int {
@@ -642,7 +642,7 @@ func min(a,b int)int{
642642

643643

644644

645-
JavaScript:
645+
### JavaScript:
646646

647647
```javascript
648648
//双指针
@@ -744,7 +744,7 @@ var trap = function(height) {
744744
};
745745
```
746746

747-
C:
747+
### C:
748748

749749
一种更简便的双指针方法:
750750

problems/0053.最大子序和(动态规划).md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ const maxSubArray = nums => {
174174
// 数组长度,dp初始化
175175
const len = nums.length;
176176
let dp = new Array(len).fill(0);
177+
dp[0] = nums[0];
177178
// 最大值初始化为dp[0]
178179
let max = dp[0];
179180
for (let i = 1; i < len; i++) {

problems/0059.螺旋矩阵II.md

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -192,47 +192,31 @@ python3:
192192

193193
```python
194194
class Solution:
195-
196195
def generateMatrix(self, n: int) -> List[List[int]]:
197-
# 初始化要填充的正方形
198-
matrix = [[0] * n for _ in range(n)]
199-
200-
left, right, up, down = 0, n - 1, 0, n - 1
201-
number = 1 # 要填充的数字
202-
203-
while left < right and up < down:
204-
205-
# 从左到右填充上边
206-
for x in range(left, right):
207-
matrix[up][x] = number
208-
number += 1
209-
210-
# 从上到下填充右边
211-
for y in range(up, down):
212-
matrix[y][right] = number
213-
number += 1
214-
215-
# 从右到左填充下边
216-
for x in range(right, left, -1):
217-
matrix[down][x] = number
218-
number += 1
219-
220-
# 从下到上填充左边
221-
for y in range(down, up, -1):
222-
matrix[y][left] = number
223-
number += 1
224-
225-
# 缩小要填充的范围
226-
left += 1
227-
right -= 1
228-
up += 1
229-
down -= 1
230-
231-
# 如果阶数为奇数,额外填充一次中心
232-
if n % 2:
233-
matrix[n // 2][n // 2] = number
234-
235-
return matrix
196+
nums = [[0] * n for _ in range(n)]
197+
startx, starty = 0, 0 # 起始点
198+
loop, mid = n // 2, n // 2 # 迭代次数、n为奇数时,矩阵的中心点
199+
count = 1 # 计数
200+
201+
for offset in range(1, loop + 1) : # 每循环一层偏移量加1,偏移量从1开始
202+
for i in range(starty, n - offset) : # 从左至右,左闭右开
203+
nums[startx][i] = count
204+
count += 1
205+
for i in range(startx, n - offset) : # 从上至下
206+
nums[i][n - offset] = count
207+
count += 1
208+
for i in range(n - offset, starty, -1) : # 从右至左
209+
nums[n - offset][i] = count
210+
count += 1
211+
for i in range(n - offset, startx, -1) : # 从下至上
212+
nums[i][starty] = count
213+
count += 1
214+
startx += 1 # 更新起始点
215+
starty += 1
216+
217+
if n % 2 != 0 : # n为奇数时,填充中心点
218+
nums[mid][mid] = count
219+
return nums
236220
```
237221

238222
javaScript

problems/0063.不同路径II.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,39 @@ public:
155155
* 时间复杂度:$O(n × m)$,n、m 分别为obstacleGrid 长度和宽度
156156
* 空间复杂度:$O(n × m)$
157157
158+
159+
同样我们给出空间优化版本:
160+
```CPP
161+
class Solution {
162+
public:
163+
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
164+
if (obstacleGrid[0][0] == 1)
165+
return 0;
166+
vector<int> dp(obstacleGrid[0].size());
167+
for (int j = 0; j < dp.size(); ++j)
168+
if (obstacleGrid[0][j] == 1)
169+
dp[j] = 0;
170+
else if (j == 0)
171+
dp[j] = 1;
172+
else
173+
dp[j] = dp[j-1];
174+
175+
for (int i = 1; i < obstacleGrid.size(); ++i)
176+
for (int j = 0; j < dp.size(); ++j){
177+
if (obstacleGrid[i][j] == 1)
178+
dp[j] = 0;
179+
else if (j != 0)
180+
dp[j] = dp[j] + dp[j-1];
181+
}
182+
return dp.back();
183+
}
184+
};
185+
```
186+
187+
* 时间复杂度:$O(n × m)$,n、m 分别为obstacleGrid 长度和宽度
188+
* 空间复杂度:$O(m)$
189+
190+
158191
## 总结
159192

160193
本题是[62.不同路径](https://programmercarl.com/0062.不同路径.html)的障碍版,整体思路大体一致。

0 commit comments

Comments
 (0)