Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 committed Jan 3, 2025
2 parents aa6d892 + 6973320 commit cf46334
Show file tree
Hide file tree
Showing 127 changed files with 2,095 additions and 394 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@

1. [字符串:344.反转字符串](./problems/0344.反转字符串.md)
2. [字符串:541.反转字符串II](./problems/0541.反转字符串II.md)
3. [字符串:替换数字](./problems/kama54.替换数字.md)
3. [字符串:替换数字](./problems/kamacoder/0054.替换数字.md)
4. [字符串:151.翻转字符串里的单词](./problems/0151.翻转字符串里的单词.md)
5. [字符串:右旋字符串](./problems/kama55.右旋字符串.md)
5. [字符串:右旋字符串](./problems/kamacoder/0055.右旋字符串.md)
6. [帮你把KMP算法学个通透](./problems/0028.实现strStr.md)
8. [字符串:459.重复的子字符串](./problems/0459.重复的子字符串.md)
9. [字符串:总结篇!](./problems/字符串总结.md)
Expand All @@ -154,7 +154,7 @@

1. [数组:27.移除元素](./problems/0027.移除元素.md)
2. [字符串:344.反转字符串](./problems/0344.反转字符串.md)
3. [字符串:替换数字](./problems/kama54.替换数字.md)
3. [字符串:替换数字](./problems/kamacoder/0054.替换数字.md)
4. [字符串:151.翻转字符串里的单词](./problems/0151.翻转字符串里的单词.md)
5. [链表:206.翻转链表](./problems/0206.翻转链表.md)
6. [链表:19.删除链表的倒数第 N 个结点](./problems/0019.删除链表的倒数第N个节点.md)
Expand Down
2 changes: 1 addition & 1 deletion problems/0001.两数之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ impl Solution {
}
```

### Javascript:
### JavaScript:

```javascript
var twoSum = function (nums, target) {
Expand Down
42 changes: 24 additions & 18 deletions problems/0015.三数之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

### 哈希解法

两层for循环就可以确定 a 和b 的数值了,可以使用哈希法来确定 0-(a+b) 是否在 数组里出现过,其实这个思路是正确的,但是我们有一个非常棘手的问题,就是题目中说的不可以包含重复的三元组。
两层for循环就可以确定 两个数值,可以使用哈希法来确定 第三个数 0-(a+b) 或者 0 - (a + c) 是否在 数组里出现过,其实这个思路是正确的,但是我们有一个非常棘手的问题,就是题目中说的不可以包含重复的三元组。

把符合条件的三元组放进vector中,然后再去重,这样是非常费时的,很容易超时,也是这道题目通过率如此之低的根源所在。

Expand All @@ -48,35 +48,41 @@
```CPP
class Solution {
public:
// 在一个数组中找到3个数形成的三元组,它们的和为0,不能重复使用(三数下标互不相同),且三元组不能重复。
// b(存储)== 0-(a+c)(检索)
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
sort(nums.begin(), nums.end());
// 找出a + b + c = 0
// a = nums[i], b = nums[j], c = -(a + b)

for (int i = 0; i < nums.size(); i++) {
// 排序之后如果第一个元素已经大于零,那么不可能凑成三元组
if (nums[i] > 0) {
// 如果a是正数,a<b<c,不可能形成和为0的三元组
if (nums[i] > 0)
break;
}
if (i > 0 && nums[i] == nums[i - 1]) { //三元组元素a去重

// [a, a, ...] 如果本轮a和上轮a相同,那么找到的b,c也是相同的,所以去重a
if (i > 0 && nums[i] == nums[i - 1])
continue;
}

// 这个set的作用是存储b
unordered_set<int> set;
for (int j = i + 1; j < nums.size(); j++) {
if (j > i + 2
&& nums[j] == nums[j-1]
&& nums[j-1] == nums[j-2]) { // 三元组元素b去重

for (int k = i + 1; k < nums.size(); k++) {
// 去重b=c时的b和c
if (k > i + 2 && nums[k] == nums[k - 1] && nums[k - 1] == nums[k - 2])
continue;

// a+b+c=0 <=> b=0-(a+c)
int target = 0 - (nums[i] + nums[k]);
if (set.find(target) != set.end()) {
result.push_back({nums[i], target, nums[k]}); // nums[k]成为c
set.erase(target);
}
int c = 0 - (nums[i] + nums[j]);
if (set.find(c) != set.end()) {
result.push_back({nums[i], nums[j], c});
set.erase(c);// 三元组元素c去重
} else {
set.insert(nums[j]);
else {
set.insert(nums[k]); // nums[k]成为b
}
}
}

return result;
}
};
Expand Down
2 changes: 1 addition & 1 deletion problems/0019.删除链表的倒数第N个节点.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

* fast和slow同时移动,直到fast指向末尾,如题:
<img src='https://code-thinking.cdn.bcebos.com/pics/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B92.png' width=600> </img></div>

//图片中有错别词:应该将“只到”改为“直到”
* 删除slow指向的下一个节点,如图:
<img src='https://code-thinking.cdn.bcebos.com/pics/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B93.png' width=600> </img></div>

Expand Down
2 changes: 1 addition & 1 deletion problems/0020.有效的括号.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def is_valid(strs)
end
```

### Javascript:
### JavaScript:

```javascript
var isValid = function (s) {
Expand Down
2 changes: 1 addition & 1 deletion problems/0024.两两交换链表中的节点.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func swapPairs(head *ListNode) *ListNode {
}
```

### Javascript:
### JavaScript:

```javascript
var swapPairs = function (head) {
Expand Down
19 changes: 18 additions & 1 deletion problems/0027.移除元素.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,24 @@ public:
## 其他语言版本
### Java:
```java
class Solution {
public int removeElement(int[] nums, int val) {
// 暴力法
int n = nums.length;
for (int i = 0; i < n; i++) {
if (nums[i] == val) {
for (int j = i + 1; j < n; j++) {
nums[j - 1] = nums[j];
}
i--;
n--;
}
}
return n;
}
}
```
```java
class Solution {
public int removeElement(int[] nums, int val) {
Expand Down
64 changes: 64 additions & 0 deletions problems/0028.实现strStr.md
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,70 @@ public int[] GetNext(string needle)
}
```

### C:

> 前缀表统一右移和减一
```c

int *build_next(char* needle, int len) {

int *next = (int *)malloc(len * sizeof(int));
assert(next); // 确保分配成功

// 初始化next数组
next[0] = -1; // next[0] 设置为 -1,表示没有有效前缀匹配
if (len <= 1) { // 如果模式串长度小于等于 1,直接返回
return next;
}
next[1] = 0; // next[1] 设置为 0,表示第一个字符没有公共前后缀

// 构建next数组, i 从模式串的第三个字符开始, j 指向当前匹配的最长前缀长度
int i = 2, j = 0;
while (i < len) {
if (needle[i - 1] == needle[j]) {
j++;
next[i] = j;
i++;
} else if (j > 0) {
// 如果不匹配且 j > 0, 回退到次长匹配前缀的长度
j = next[j];
} else {
next[i] = 0;
i++;
}
}
return next;
}

int strStr(char* haystack, char* needle) {

int needle_len = strlen(needle);
int haystack_len = strlen(haystack);

int *next = build_next(needle, needle_len);

int i = 0, j = 0; // i 指向主串的当前起始位置, j 指向模式串的当前匹配位置
while (i <= haystack_len - needle_len) {
if (haystack[i + j] == needle[j]) {
j++;
if (j == needle_len) {
free(next);
next = NULL
return i;
}
} else {
i += j - next[j]; // 调整主串的起始位置
j = j > 0 ? next[j] : 0;
}
}

free(next);
next = NULL;
return -1;
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
Expand Down
86 changes: 51 additions & 35 deletions problems/0037.解数独.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,40 +366,56 @@ class Solution:
"""
Do not return anything, modify board in-place instead.
"""
self.backtracking(board)

def backtracking(self, board: List[List[str]]) -> bool:
# 若有解,返回True;若无解,返回False
for i in range(len(board)): # 遍历行
for j in range(len(board[0])): # 遍历列
# 若空格内已有数字,跳过
if board[i][j] != '.': continue
for k in range(1, 10):
if self.is_valid(i, j, k, board):
board[i][j] = str(k)
if self.backtracking(board): return True
board[i][j] = '.'
# 若数字1-9都不能成功填入空格,返回False无解
return False
return True # 有解

def is_valid(self, row: int, col: int, val: int, board: List[List[str]]) -> bool:
# 判断同一行是否冲突
for i in range(9):
if board[row][i] == str(val):
return False
# 判断同一列是否冲突
for j in range(9):
if board[j][col] == str(val):
return False
# 判断同一九宫格是否有冲突
start_row = (row // 3) * 3
start_col = (col // 3) * 3
for i in range(start_row, start_row + 3):
for j in range(start_col, start_col + 3):
if board[i][j] == str(val):
return False
return True
row_used = [set() for _ in range(9)]
col_used = [set() for _ in range(9)]
box_used = [set() for _ in range(9)]
for row in range(9):
for col in range(9):
num = board[row][col]
if num == ".":
continue
row_used[row].add(num)
col_used[col].add(num)
box_used[(row // 3) * 3 + col // 3].add(num)
self.backtracking(0, 0, board, row_used, col_used, box_used)

def backtracking(
self,
row: int,
col: int,
board: List[List[str]],
row_used: List[List[int]],
col_used: List[List[int]],
box_used: List[List[int]],
) -> bool:
if row == 9:
return True

next_row, next_col = (row, col + 1) if col < 8 else (row + 1, 0)
if board[row][col] != ".":
return self.backtracking(
next_row, next_col, board, row_used, col_used, box_used
)

for num in map(str, range(1, 10)):
if (
num not in row_used[row]
and num not in col_used[col]
and num not in box_used[(row // 3) * 3 + col // 3]
):
board[row][col] = num
row_used[row].add(num)
col_used[col].add(num)
box_used[(row // 3) * 3 + col // 3].add(num)
if self.backtracking(
next_row, next_col, board, row_used, col_used, box_used
):
return True
board[row][col] = "."
row_used[row].remove(num)
col_used[col].remove(num)
box_used[(row // 3) * 3 + col // 3].remove(num)
return False
```

### Go
Expand Down Expand Up @@ -460,7 +476,7 @@ func isvalid(row, col int, k byte, board [][]byte) bool {



### Javascript
### JavaScript

```Javascript
var solveSudoku = function(board) {
Expand Down
2 changes: 1 addition & 1 deletion problems/0045.跳跃游戏II.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func max(a, b int) int {
}
```

### Javascript
### JavaScript

```Javascript
var jump = function(nums) {
Expand Down
2 changes: 1 addition & 1 deletion problems/0046.全排列.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func dfs(nums []int, cur int) {
}
```

### Javascript
### JavaScript

```js

Expand Down
2 changes: 1 addition & 1 deletion problems/0047.全排列II.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func dfs(nums []int, cur int) {
}
```

### Javascript
### JavaScript

```javascript
var permuteUnique = function (nums) {
Expand Down
2 changes: 1 addition & 1 deletion problems/0051.N皇后.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ func isValid(n, row, col int, chessboard [][]string) bool {
```


### Javascript
### JavaScript
```Javascript
/**
* @param {number} n
Expand Down
2 changes: 1 addition & 1 deletion problems/0053.最大子序和.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ pub fn max_sub_array(nums: Vec<i32>) -> i32 {
}
```

### Javascript:
### JavaScript:

```Javascript
var maxSubArray = function(nums) {
Expand Down
2 changes: 1 addition & 1 deletion problems/0054.螺旋矩阵.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class Solution {
}
```

### Javascript
### JavaScript
```
/**
* @param {number[][]} matrix
Expand Down
2 changes: 1 addition & 1 deletion problems/0055.跳跃游戏.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func max(a, b int ) int {
}
```

### Javascript
### JavaScript

```Javascript
var canJump = function(nums) {
Expand Down
2 changes: 1 addition & 1 deletion problems/0056.合并区间.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func max56(a, b int) int {
```


### Javascript
### JavaScript
```javascript
var merge = function (intervals) {
intervals.sort((a, b) => a[0] - b[0]);
Expand Down
Loading

0 comments on commit cf46334

Please sign in to comment.