Skip to content

Commit 6533e83

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents 94a4e81 + 14ff932 commit 6533e83

33 files changed

+634
-244
lines changed

problems/0001.两数之和.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ function twoSum(nums: number[], target: number): number[] {
349349
index = helperMap.get(target - nums[i]);
350350
if (index !== undefined) {
351351
resArr = [i, index];
352+
break;
352353
}
353354
helperMap.set(nums[i], i);
354355
}

problems/0017.电话号码的字母组合.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class Solution {
260260

261261
}
262262

263-
//每次迭代获取一个字符串,所以会设计大量的字符串拼接,所以这里选择更为高效的 StringBuilder
263+
//每次迭代获取一个字符串,所以会涉及大量的字符串拼接,所以这里选择更为高效的 StringBuilder
264264
StringBuilder temp = new StringBuilder();
265265

266266
//比如digits如果为"23",num 为0,则str表示2对应的 abc
@@ -274,7 +274,7 @@ class Solution {
274274
String str = numString[digits.charAt(num) - '0'];
275275
for (int i = 0; i < str.length(); i++) {
276276
temp.append(str.charAt(i));
277-
//c
277+
//递归,处理下一层
278278
backTracking(digits, numString, num + 1);
279279
//剔除末尾的继续尝试
280280
temp.deleteCharAt(temp.length() - 1);

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222

2323
输入:head = [1,2,3,4,5], n = 2
2424
输出:[1,2,3,5]
25+
2526
示例 2:
2627

2728
输入:head = [1], n = 1
2829
输出:[]
30+
2931
示例 3:
3032

3133
输入:head = [1,2], n = 1
@@ -111,7 +113,6 @@ class Solution {
111113
for (int i = 0; i <= n; i++) {
112114
fastIndex = fastIndex.next;
113115
}
114-
115116
while (fastIndex != null) {
116117
fastIndex = fastIndex.next;
117118
slowIndex = slowIndex.next;
@@ -193,16 +194,18 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode {
193194
* @param {number} n
194195
* @return {ListNode}
195196
*/
196-
var removeNthFromEnd = function(head, n) {
197-
let ret = new ListNode(0, head),
198-
slow = fast = ret;
199-
while(n--) fast = fast.next;
200-
while (fast.next !== null) {
201-
fast = fast.next;
202-
slow = slow.next
203-
};
204-
slow.next = slow.next.next;
205-
return ret.next;
197+
var removeNthFromEnd = function (head, n) {
198+
// 创建哨兵节点,简化解题逻辑
199+
let dummyHead = new ListNode(0, head);
200+
let fast = dummyHead;
201+
let slow = dummyHead;
202+
while (n--) fast = fast.next;
203+
while (fast.next !== null) {
204+
slow = slow.next;
205+
fast = fast.next;
206+
}
207+
slow.next = slow.next.next;
208+
return dummyHead.next;
206209
};
207210
```
208211
### TypeScript:

problems/0027.移除元素.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,32 @@ public class Solution {
476476
}
477477
```
478478

479+
###Dart:
480+
```dart
481+
int removeElement(List<int> nums, int val) {
482+
//相向双指针法
483+
var left = 0;
484+
var right = nums.length - 1;
485+
while (left <= right) {
486+
//寻找左侧的val,将其被右侧非val覆盖
487+
if (nums[left] == val) {
488+
while (nums[right] == val&&left<=right) {
489+
right--;
490+
if (right < 0) {
491+
return 0;
492+
}
493+
}
494+
nums[left] = nums[right--];
495+
} else {
496+
left++;
497+
}
498+
}
499+
//覆盖后可以将0至left部分视为所需部分
500+
return left;
501+
}
502+
503+
```
504+
479505
<p align="center">
480506
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
481507
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

problems/0035.搜索插入位置.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,18 @@ func searchInsert(nums []int, target int) int {
313313

314314
```rust
315315
impl Solution {
316-
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
317-
let mut left = 0;
318-
let mut right = nums.len();
319-
while left < right {
316+
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
317+
use std::cmp::Ordering::{Equal, Greater, Less};
318+
let (mut left, mut right) = (0, nums.len() as i32 - 1);
319+
while left <= right {
320320
let mid = (left + right) / 2;
321-
match nums[mid].cmp(&target) {
322-
Ordering::Less => left = mid + 1,
323-
Ordering::Equal => return ((left + right) / 2) as i32,
324-
Ordering::Greater => right = mid,
321+
match nums[mid as usize].cmp(&target) {
322+
Less => left = mid + 1,
323+
Equal => return mid,
324+
Greater => right = mid - 1,
325325
}
326326
}
327-
((left + right) / 2) as i32
327+
right + 1
328328
}
329329
}
330330
```

problems/0037.解数独.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public:
224224

225225

226226
### Java
227-
227+
解法一:
228228
```java
229229
class Solution {
230230
public void solveSudoku(char[][] board) {
@@ -291,7 +291,73 @@ class Solution {
291291
}
292292
}
293293
```
294+
解法二(bitmap标记)
295+
```
296+
class Solution{
297+
int[] rowBit = new int[9];
298+
int[] colBit = new int[9];
299+
int[] square9Bit = new int[9];
300+
301+
public void solveSudoku(char[][] board) {
302+
// 1 10 11
303+
for (int y = 0; y < board.length; y++) {
304+
for (int x = 0; x < board[y].length; x++) {
305+
int numBit = 1 << (board[y][x] - '1');
306+
rowBit[y] ^= numBit;
307+
colBit[x] ^= numBit;
308+
square9Bit[(y / 3) * 3 + x / 3] ^= numBit;
309+
}
310+
}
311+
backtrack(board, 0);
312+
}
313+
314+
public boolean backtrack(char[][] board, int n) {
315+
if (n >= 81) {
316+
return true;
317+
}
318+
319+
// 快速算出行列编号 n/9 n%9
320+
int row = n / 9;
321+
int col = n % 9;
322+
323+
if (board[row][col] != '.') {
324+
return backtrack(board, n + 1);
325+
}
326+
327+
for (char c = '1'; c <= '9'; c++) {
328+
int numBit = 1 << (c - '1');
329+
if (!isValid(numBit, row, col)) continue;
330+
{
331+
board[row][col] = c; // 当前的数字放入到数组之中,
332+
rowBit[row] ^= numBit; // 第一行rowBit[0],第一个元素eg: 1 , 0^1=1,第一个元素:4, 100^1=101,...
333+
colBit[col] ^= numBit;
334+
square9Bit[(row / 3) * 3 + col / 3] ^= numBit;
335+
}
336+
if (backtrack(board, n + 1)) return true;
337+
{
338+
board[row][col] = '.'; // 不满足条件,回退成'.'
339+
rowBit[row] &= ~numBit; // 第一行rowBit[0],第一个元素eg: 1 , 101&=~1==>101&111111110==>100
340+
colBit[col] &= ~numBit;
341+
square9Bit[(row / 3) * 3 + col / 3] &= ~numBit;
342+
}
343+
}
344+
return false;
345+
}
346+
294347
348+
boolean isValid(int numBit, int row, int col) {
349+
// 左右
350+
if ((rowBit[row] & numBit) > 0) return false;
351+
// 上下
352+
if ((colBit[col] & numBit) > 0) return false;
353+
// 9宫格: 快速算出第n个九宫格,编号[0,8] , 编号=(row / 3) * 3 + col / 3
354+
if ((square9Bit[(row / 3) * 3 + col / 3] & numBit) > 0) return false;
355+
return true;
356+
}
357+
358+
}
359+
360+
```
295361
### Python
296362

297363
```python

problems/0039.组合总和.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ class Solution:
311311

312312
for i in range(startIndex, len(candidates)):
313313
if total + candidates[i] > target:
314-
continue
314+
break
315315
total += candidates[i]
316316
path.append(candidates[i])
317317
self.backtracking(candidates, target, total, i, path, result)

problems/0063.不同路径II.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public:
145145
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
146146
int m = obstacleGrid.size();
147147
int n = obstacleGrid[0].size();
148-
if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) //如果在起点或终点出现了障碍,直接返回0
148+
if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) //如果在起点或终点出现了障碍,直接返回0
149149
return 0;
150150
vector<vector<int>> dp(m, vector<int>(n, 0));
151151
for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) dp[i][0] = 1;

problems/0070.爬楼梯完全背包版本.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,33 @@ func main() {
211211
```
212212

213213
### JavaScript:
214-
214+
```javaScript
215+
var climbStairs = function (n) {
216+
let dp = new Array(n + 1).fill(0);
217+
dp[0] = 1;
218+
// 排列题,注意循环顺序,背包在外物品在内
219+
for (let j = 1; j <= n; j++) {//遍历背包
220+
for (let i = 1; i <= 2; i++) {//遍历物品
221+
if (j - i >= 0) dp[j] = dp[j] + dp[j - i];
222+
}
223+
}
224+
return dp[n];
225+
}
226+
```
215227

216228
### TypeScript:
217-
229+
```typescript
230+
var climbStairs = function (n: number): number {
231+
let dp: number[] = new Array(n + 1).fill(0);
232+
dp[0] = 1;
233+
for (let j = 1; j <= n; j++) {//遍历背包
234+
for (let i = 1; i <= 2; i++) {//遍历物品
235+
if (j - i >= 0) dp[j] = dp[j] + dp[j - i];
236+
}
237+
}
238+
return dp[n];
239+
}
240+
```
218241

219242
### Rust:
220243

problems/0077.组合.md

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -469,28 +469,58 @@ func dfs(n int, k int, start int) {
469469
```
470470

471471
### Javascript
472+
未剪枝:
473+
474+
```js
475+
var combine = function (n, k) {
476+
// 回溯法
477+
let result = [],
478+
path = [];
479+
let backtracking = (_n, _k, startIndex) => {
480+
// 终止条件
481+
if (path.length === _k) {
482+
result.push(path.slice());
483+
return;
484+
}
485+
// 循环本层集合元素
486+
for (let i = startIndex; i <= _n; i++) {
487+
path.push(i);
488+
// 递归
489+
backtracking(_n, _k, i + 1);
490+
// 回溯操作
491+
path.pop();
492+
}
493+
};
494+
backtracking(n, k, 1);
495+
return result;
496+
};
497+
```
472498

473499
剪枝:
474500

475501
```javascript
476-
let result = []
477-
let path = []
478-
var combine = function(n, k) {
479-
result = []
480-
combineHelper(n, k, 1)
481-
return result
502+
var combine = function (n, k) {
503+
// 回溯法
504+
let result = [],
505+
path = [];
506+
let backtracking = (_n, _k, startIndex) => {
507+
// 终止条件
508+
if (path.length === _k) {
509+
result.push(path.slice());
510+
return;
511+
}
512+
// 循环本层集合元素
513+
for (let i = startIndex; i <= _n - (_k - path.length) + 1; i++) {
514+
path.push(i);
515+
// 递归
516+
backtracking(_n, _k, i + 1);
517+
// 回溯操作
518+
path.pop();
519+
}
520+
};
521+
backtracking(n, k, 1);
522+
return result;
482523
};
483-
const combineHelper = (n, k, startIndex) => {
484-
if (path.length === k) {
485-
result.push([...path])
486-
return
487-
}
488-
for (let i = startIndex; i <= n - (k - path.length) + 1; ++i) {
489-
path.push(i)
490-
combineHelper(n, k, i + 1)
491-
path.pop()
492-
}
493-
}
494524
```
495525

496526
### TypeScript

0 commit comments

Comments
 (0)