Skip to content

Commit

Permalink
Merge branch 'master' into patch-4
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 authored Mar 25, 2022
2 parents 94bf891 + 42c78d9 commit 4f0aa8b
Show file tree
Hide file tree
Showing 109 changed files with 1,664 additions and 772 deletions.
Binary file removed .DS_Store
Binary file not shown.
12 changes: 12 additions & 0 deletions problems/0001.两数之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ class Solution:
return [records[target - val], idx] # 如果存在就返回字典记录索引和当前索引
```

Python (v2):

```python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
rec = {}
for i in range(len(nums)):
rest = target - nums[i]
# Use get to get the index of the data, making use of one of the dictionary properties.
if rec.get(rest, None) is not None: return [rec[rest], i]
rec[nums[i]] = i
```

Go:

Expand Down
37 changes: 29 additions & 8 deletions problems/0005.最长回文子串.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,26 @@ public:

# 其他语言版本

## Java
Java:

```java
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
if(nums == null || nums.length == 0){
return res;
}
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
int temp = target - nums[i];
if(map.containsKey(temp)){
res[1] = i;
res[0] = map.get(temp);
}
map.put(nums[i], i);
}
return res;
}
```

```java
// 双指针 中心扩散法
Expand Down Expand Up @@ -291,7 +310,7 @@ class Solution {
}
```

## Python
Python

```python
class Solution:
Expand All @@ -312,7 +331,8 @@ class Solution:
return s[left:right + 1]

```
> 双指针法:
双指针:

```python
class Solution:
def longestPalindrome(self, s: str) -> str:
Expand Down Expand Up @@ -340,13 +360,13 @@ class Solution:
return s[start:end]

```
## Go
Go:

```go

```

## JavaScript
JavaScript

```js
//动态规划解法
Expand Down Expand Up @@ -462,8 +482,9 @@ var longestPalindrome = function(s) {
};
```

## C
动态规划:
C:

动态规划:
```c
//初始化dp数组,全部初始为false
bool **initDP(int strLen) {
Expand Down Expand Up @@ -513,7 +534,7 @@ char * longestPalindrome(char * s){
}
```
双指针:
双指针
```c
int left, maxLength;
void extend(char *str, int i, int j, int size) {
Expand Down
27 changes: 27 additions & 0 deletions problems/0015.三数之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,34 @@ class Solution:
right -= 1
return ans
```
Python (v2):

```python
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
if len(nums) < 3: return []
nums, res = sorted(nums), []
for i in range(len(nums) - 2):
cur, l, r = nums[i], i + 1, len(nums) - 1
if res != [] and res[-1][0] == cur: continue # Drop duplicates for the first time.

while l < r:
if cur + nums[l] + nums[r] == 0:
res.append([cur, nums[l], nums[r]])
# 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.
while l < r - 1 and nums[l] == nums[l + 1]:
l += 1
while r > l + 1 and nums[r] == nums[r - 1]:
r -= 1
if cur + nums[l] + nums[r] > 0:
r -= 1
else:
l += 1
return res
```

Go:

```Go
func threeSum(nums []int)[][]int{
sort.Ints(nums)
Expand Down
15 changes: 11 additions & 4 deletions problems/0018.四数之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ class Solution(object):

# good thing about using python is you can use set to drop duplicates.
ans = set()
# ans = [] # save results by list()
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
for k in range(j + 1, len(nums)):
Expand All @@ -224,10 +225,16 @@ class Solution(object):
# make sure no duplicates.
count = (nums[i] == val) + (nums[j] == val) + (nums[k] == val)
if hashmap[val] > count:
ans.add(tuple(sorted([nums[i], nums[j], nums[k], val])))
else:
continue
return ans
ans_tmp = tuple(sorted([nums[i], nums[j], nums[k], val]))
ans.add(ans_tmp)
# Avoiding duplication in list manner but it cause time complexity increases
# if ans_tmp not in ans:
# ans.append(ans_tmp)
else:
continue
return list(ans)
# if used list() to save results, just
# return ans

```

Expand Down
40 changes: 14 additions & 26 deletions problems/0024.两两交换链表中的节点.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,32 +254,20 @@ TypeScript:

```typescript
function swapPairs(head: ListNode | null): ListNode | null {
/**
* 初始状态:
* curNode -> node1 -> node2 -> tmepNode
* 转换过程:
* curNode -> node2
* curNode -> node2 -> node1
* curNode -> node2 -> node1 -> tempNode
* curNode = node1
*/
let retNode: ListNode | null = new ListNode(0, head),
curNode: ListNode | null = retNode,
node1: ListNode | null = null,
node2: ListNode | null = null,
tempNode: ListNode | null = null;

while (curNode && curNode.next && curNode.next.next) {
node1 = curNode.next;
node2 = curNode.next.next;
tempNode = node2.next;
curNode.next = node2;
node2.next = node1;
node1.next = tempNode;
curNode = node1;
}
return retNode.next;
};
const dummyHead: ListNode = new ListNode(0, head);
let cur: ListNode = dummyHead;
while(cur.next !== null && cur.next.next !== null) {
const tem: ListNode = cur.next;
const tem1: ListNode = cur.next.next.next;

cur.next = cur.next.next; // step 1
cur.next.next = tem; // step 2
cur.next.next.next = tem1; // step 3

cur = cur.next.next;
}
return dummyHead.next;
}
```

Kotlin:
Expand Down
10 changes: 5 additions & 5 deletions problems/0042.接雨水.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public:

## 其他语言版本

Java:
### Java:

双指针法
```java
Expand Down Expand Up @@ -468,7 +468,7 @@ class Solution {
}
```

Python:
### Python:

双指针法
```python3
Expand Down Expand Up @@ -575,7 +575,7 @@ class Solution:

```

Go:
### Go

```go
func trap(height []int) int {
Expand Down Expand Up @@ -642,7 +642,7 @@ func min(a,b int)int{



JavaScript:
### JavaScript:

```javascript
//双指针
Expand Down Expand Up @@ -744,7 +744,7 @@ var trap = function(height) {
};
```

C:
### C:

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

Expand Down
1 change: 1 addition & 0 deletions problems/0053.最大子序和(动态规划).md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ const maxSubArray = nums => {
// 数组长度,dp初始化
const len = nums.length;
let dp = new Array(len).fill(0);
dp[0] = nums[0];
// 最大值初始化为dp[0]
let max = dp[0];
for (let i = 1; i < len; i++) {
Expand Down
64 changes: 24 additions & 40 deletions problems/0059.螺旋矩阵II.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,47 +192,31 @@ python3:

```python
class Solution:

def generateMatrix(self, n: int) -> List[List[int]]:
# 初始化要填充的正方形
matrix = [[0] * n for _ in range(n)]

left, right, up, down = 0, n - 1, 0, n - 1
number = 1 # 要填充的数字

while left < right and up < down:

# 从左到右填充上边
for x in range(left, right):
matrix[up][x] = number
number += 1

# 从上到下填充右边
for y in range(up, down):
matrix[y][right] = number
number += 1

# 从右到左填充下边
for x in range(right, left, -1):
matrix[down][x] = number
number += 1

# 从下到上填充左边
for y in range(down, up, -1):
matrix[y][left] = number
number += 1

# 缩小要填充的范围
left += 1
right -= 1
up += 1
down -= 1

# 如果阶数为奇数,额外填充一次中心
if n % 2:
matrix[n // 2][n // 2] = number

return matrix
nums = [[0] * n for _ in range(n)]
startx, starty = 0, 0 # 起始点
loop, mid = n // 2, n // 2 # 迭代次数、n为奇数时,矩阵的中心点
count = 1 # 计数

for offset in range(1, loop + 1) : # 每循环一层偏移量加1,偏移量从1开始
for i in range(starty, n - offset) : # 从左至右,左闭右开
nums[startx][i] = count
count += 1
for i in range(startx, n - offset) : # 从上至下
nums[i][n - offset] = count
count += 1
for i in range(n - offset, starty, -1) : # 从右至左
nums[n - offset][i] = count
count += 1
for i in range(n - offset, startx, -1) : # 从下至上
nums[i][starty] = count
count += 1
startx += 1 # 更新起始点
starty += 1

if n % 2 != 0 : # n为奇数时,填充中心点
nums[mid][mid] = count
return nums
```

javaScript
Expand Down
33 changes: 33 additions & 0 deletions problems/0063.不同路径II.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,39 @@ public:
* 时间复杂度:$O(n × m)$,n、m 分别为obstacleGrid 长度和宽度
* 空间复杂度:$O(n × m)$
同样我们给出空间优化版本:
```CPP
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if (obstacleGrid[0][0] == 1)
return 0;
vector<int> dp(obstacleGrid[0].size());
for (int j = 0; j < dp.size(); ++j)
if (obstacleGrid[0][j] == 1)
dp[j] = 0;
else if (j == 0)
dp[j] = 1;
else
dp[j] = dp[j-1];
for (int i = 1; i < obstacleGrid.size(); ++i)
for (int j = 0; j < dp.size(); ++j){
if (obstacleGrid[i][j] == 1)
dp[j] = 0;
else if (j != 0)
dp[j] = dp[j] + dp[j-1];
}
return dp.back();
}
};
```

* 时间复杂度:$O(n × m)$,n、m 分别为obstacleGrid 长度和宽度
* 空间复杂度:$O(m)$


## 总结

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

0 comments on commit 4f0aa8b

Please sign in to comment.