From ceaaa2fd2d3114c75794b89fbb08005dd991b81c Mon Sep 17 00:00:00 2001
From: Epoch <75031971+messenger1th@users.noreply.github.com>
Date: Mon, 21 Feb 2022 14:37:15 +0800
Subject: [PATCH 001/551] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200151.=E7=BF=BB?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95?=
=?UTF-8?q?=E8=AF=8D=E7=9A=84CPP=E6=9B=B4=E7=AE=80=E6=B4=81=E7=9A=84?=
=?UTF-8?q?=E7=89=88=E6=9C=AC2=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
用LC27的原理使得更为简洁。
---
...14\347\232\204\345\215\225\350\257\215.md" | 35 +++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
index ead5fa1237..677a8f649f 100644
--- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
+++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
@@ -222,7 +222,42 @@ public:
效率:
+```CPP
+//版本二:
+class Solution {
+public:
+ void reverseWord(string& s,int start,int end){ //这个函数,Carl哥的要更清晰。
+ for(int i=start;i<(end-start)/2+start;++i){
+ swap(s[i],s[end-1-i+start]);
+ }
+ }
+ void trim(string& s){//去除所有空格并在相邻单词之间添加空格
+ int slow = 0;
+ for(int i=0;i
Date: Mon, 14 Mar 2022 13:33:40 +0800
Subject: [PATCH 002/551] =?UTF-8?q?Update=200151.=E7=BF=BB=E8=BD=AC?=
=?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D?=
=?UTF-8?q?=20=E5=90=8C=E7=90=86CPP=20=E7=89=88=E6=9C=AC2=E7=AE=80?=
=?UTF-8?q?=E6=B4=81=E5=AE=9E=E7=8E=B0.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
规范代码,优化留白。
同时添加详细注解, 并给出同理题目练习链接。
---
...14\347\232\204\345\215\225\350\257\215.md" | 36 ++++++++++---------
1 file changed, 19 insertions(+), 17 deletions(-)
diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
index 677a8f649f..e7abd1d825 100644
--- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
+++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
@@ -224,34 +224,36 @@ public:
```CPP
//版本二:
+//原理同版本1,更简洁实现。
class Solution {
public:
- void reverseWord(string& s,int start,int end){ //这个函数,Carl哥的要更清晰。
- for(int i=start;i<(end-start)/2+start;++i){
- swap(s[i],s[end-1-i+start]);
+ void reverse(string& s, int start, int end){ //翻转,区间写法:闭区间 []
+ for (int i = start, j = end; i < j; i++, j--) {
+ swap(s[i], s[j]);
}
}
- void trim(string& s){//去除所有空格并在相邻单词之间添加空格
- int slow = 0;
- for(int i=0;i
Date: Sun, 20 Mar 2022 23:24:57 +0800
Subject: [PATCH 003/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880216.?=
=?UTF-8?q?=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8CIII.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...345\220\210\346\200\273\345\222\214III.md" | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
index 0bb4219265..32b1347e56 100644
--- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
+++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
@@ -396,6 +396,30 @@ var combinationSum3 = function(k, n) {
};
```
+## TypeScript
+
+```typescript
+function combinationSum3(k: number, n: number): number[][] {
+ const resArr: number[][] = [];
+ function backTracking(k: number, n: number, sum: number, startIndex: number, tempArr: number[]): void {
+ if (sum > n) return;
+ if (tempArr.length === k) {
+ if (sum === n) {
+ resArr.push(tempArr.slice());
+ }
+ return;
+ }
+ for (let i = startIndex; i <= 9 - (k - tempArr.length) + 1; i++) {
+ tempArr.push(i);
+ backTracking(k, n, sum + i, i + 1, tempArr);
+ tempArr.pop();
+ }
+ }
+ backTracking(k, n, 0, 1, []);
+ return resArr;
+};
+```
+
## C
```c
From 82feee15424aa110e5d30b333ea3c0a83245b3c2 Mon Sep 17 00:00:00 2001
From: dcj_hp <294487055@qq.com>
Date: Mon, 21 Mar 2022 12:04:13 +0800
Subject: [PATCH 004/551] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20647.=E5=9B=9E?=
=?UTF-8?q?=E6=96=87=E5=AD=90=E4=B8=B2=20=E6=96=87=E5=AD=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md"
index d9928b8f2f..739146f12a 100644
--- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md"
+++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md"
@@ -54,7 +54,7 @@
当s[i]与s[j]相等时,这就复杂一些了,有如下三种情况
* 情况一:下标i 与 j相同,同一个字符例如a,当然是回文子串
-* 情况二:下标i 与 j相差为1,例如aa,也是文子串
+* 情况二:下标i 与 j相差为1,例如aa,也是回文子串
* 情况三:下标:i 与 j相差大于1的时候,例如cabac,此时s[i]与s[j]已经相同了,我们看i到j区间是不是回文子串就看aba是不是回文就可以了,那么aba的区间就是 i+1 与 j-1区间,这个区间是不是回文就看dp[i + 1][j - 1]是否为true。
以上三种情况分析完了,那么递归公式如下:
From 74ac8c39c1248afd5d99604b7a20e27c67625679 Mon Sep 17 00:00:00 2001
From: Guang-Hou <87743934+Guang-Hou@users.noreply.github.com>
Date: Fri, 25 Mar 2022 15:49:48 -0400
Subject: [PATCH 005/551] =?UTF-8?q?Update=200134.=E5=8A=A0=E6=B2=B9?=
=?UTF-8?q?=E7=AB=99.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4.\345\212\240\346\262\271\347\253\231.md" | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md"
index ca95af6764..f73ab9f471 100644
--- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md"
+++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md"
@@ -239,6 +239,30 @@ class Solution {
### Python
```python
+# 解法1
+class Solution:
+ def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
+ n = len(gas)
+ cur_sum = 0
+ min_sum = float('inf')
+
+ for i in range(n):
+ cur_sum += gas[i] - cost[i]
+ min_sum = min(min_sum, cur_sum)
+
+ if cur_sum < 0: return -1
+ if min_sum >= 0: return 0
+
+ for j in range(n - 1, 0, -1):
+ min_sum += gas[j] - cost[j]
+ if min_sum >= 0:
+ return j
+
+ return -1
+```
+
+```python
+# 解法2
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
start = 0
From 9dbc51455d7f13b83fe63d30a477d89fd1e0c33a Mon Sep 17 00:00:00 2001
From: xuerbujia <83055661+xuerbujia@users.noreply.github.com>
Date: Sat, 26 Mar 2022 10:03:04 +0800
Subject: [PATCH 006/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880141.?=
=?UTF-8?q?=E7=8E=AF=E5=BD=A2=E9=93=BE=E8=A1=A8.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0go=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...16\257\345\275\242\351\223\276\350\241\250.md" | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md"
index 0712a2a2a5..ddd83c9422 100644
--- "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md"
+++ "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md"
@@ -106,6 +106,21 @@ class Solution:
## Go
```go
+func hasCycle(head *ListNode) bool {
+ if head==nil{
+ return false
+ } //空链表一定不会有环
+ fast:=head
+ slow:=head //快慢指针
+ for fast.Next!=nil&&fast.Next.Next!=nil{
+ fast=fast.Next.Next
+ slow=slow.Next
+ if fast==slow{
+ return true //快慢指针相遇则有环
+ }
+ }
+ return false
+}
```
### JavaScript
From 1fa83c2b3f6b1eded21f2f5313be38b1a72ff2e5 Mon Sep 17 00:00:00 2001
From: xuerbujia <83055661+xuerbujia@users.noreply.github.com>
Date: Sat, 26 Mar 2022 10:13:44 +0800
Subject: [PATCH 007/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880189.?=
=?UTF-8?q?=E8=BD=AE=E8=BD=AC=E6=95=B0=E7=BB=84.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0go=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\227\213\350\275\254\346\225\260\347\273\204.md" | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git "a/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" "b/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md"
index bbe152a267..8e39d253ee 100644
--- "a/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md"
+++ "b/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md"
@@ -124,6 +124,19 @@ class Solution:
## Go
```go
+func rotate(nums []int, k int) {
+ l:=len(nums)
+ index:=l-k%l
+ reverse(nums)
+ reverse(nums[:l-index])
+ reverse(nums[l-index:])
+}
+func reverse(nums []int){
+ l:=len(nums)
+ for i:=0;i
Date: Sat, 26 Mar 2022 21:47:35 -0500
Subject: [PATCH 008/551] =?UTF-8?q?=E5=A2=9E=E5=8A=A0python=E8=A7=A3?=
=?UTF-8?q?=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...25\350\257\215\346\216\245\351\276\231.md" | 24 ++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md"
index 407596c0d4..584bcb2a15 100644
--- "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md"
+++ "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md"
@@ -134,7 +134,29 @@ public int ladderLength(String beginWord, String endWord, List wordList)
```
## Python
-
+```
+class Solution:
+ def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
+ wordSet = set(wordList)
+ if len(wordSet)== 0 or endWord not in wordSet:
+ return 0
+ mapping = {beginWord:1}
+ queue = deque([beginWord])
+ while queue:
+ word = queue.popleft()
+ path = mapping[word]
+ for i in range(len(word)):
+ word_list = list(word)
+ for j in range(26):
+ word_list[i] = chr(ord('a')+j)
+ newWord = "".join(word_list)
+ if newWord == endWord:
+ return path+1
+ if newWord in wordSet and newWord not in mapping:
+ mapping[newWord] = path+1
+ queue.append(newWord)
+ return 0
+```
## Go
## JavaScript
From 0296ac0a0e4e0b7f0b63ded21004ccf24f755a60 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Mon, 28 Mar 2022 14:33:49 +0800
Subject: [PATCH 009/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880017.?=
=?UTF-8?q?=E7=94=B5=E8=AF=9D=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D?=
=?UTF-8?q?=E7=BB=84=E5=90=88.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesr?=
=?UTF-8?q?ipt=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...27\346\257\215\347\273\204\345\220\210.md" | 34 +++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md"
index 7040182f3b..9413656515 100644
--- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md"
+++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md"
@@ -420,6 +420,40 @@ var letterCombinations = function(digits) {
};
```
+## TypeScript
+
+```typescript
+function letterCombinations(digits: string): string[] {
+ if (digits === '') return [];
+ const strMap: { [index: string]: string[] } = {
+ 1: [],
+ 2: ['a', 'b', 'c'],
+ 3: ['d', 'e', 'f'],
+ 4: ['g', 'h', 'i'],
+ 5: ['j', 'k', 'l'],
+ 6: ['m', 'n', 'o'],
+ 7: ['p', 'q', 'r', 's'],
+ 8: ['t', 'u', 'v'],
+ 9: ['w', 'x', 'y', 'z'],
+ }
+ const resArr: string[] = [];
+ function backTracking(digits: string, curIndex: number, route: string[]): void {
+ if (curIndex === digits.length) {
+ resArr.push(route.join(''));
+ return;
+ }
+ let tempArr: string[] = strMap[digits[curIndex]];
+ for (let i = 0, length = tempArr.length; i < length; i++) {
+ route.push(tempArr[i]);
+ backTracking(digits, curIndex + 1, route);
+ route.pop();
+ }
+ }
+ backTracking(digits, 0, []);
+ return resArr;
+};
+```
+
## C
```c
From 4959bd4c8aa78d401f5a1f14c2d267a4d046baf5 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Mon, 28 Mar 2022 17:15:46 +0800
Subject: [PATCH 010/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880039.?=
=?UTF-8?q?=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8C.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\345\220\210\346\200\273\345\222\214.md" | 27 +++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
index 7a2084dd19..98b37b8467 100644
--- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
+++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
@@ -392,7 +392,34 @@ var combinationSum = function(candidates, target) {
};
```
+## TypeScript
+
+```typescript
+function combinationSum(candidates: number[], target: number): number[][] {
+ const resArr: number[][] = [];
+ function backTracking(
+ candidates: number[], target: number,
+ startIndex: number, route: number[], curSum: number
+ ): void {
+ if (curSum > target) return;
+ if (curSum === target) {
+ resArr.push(route.slice());
+ return
+ }
+ for (let i = startIndex, length = candidates.length; i < length; i++) {
+ let tempVal: number = candidates[i];
+ route.push(tempVal);
+ backTracking(candidates, target, i, route, curSum + tempVal);
+ route.pop();
+ }
+ }
+ backTracking(candidates, target, 0, [], 0);
+ return resArr;
+};
+```
+
## C
+
```c
int* path;
int pathTop;
From a021470215aa859a4d5b0e9fe48dd775e6c0ed51 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Tue, 29 Mar 2022 14:38:19 +0800
Subject: [PATCH 011/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880040.?=
=?UTF-8?q?=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8CII.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\345\220\210\346\200\273\345\222\214II.md" | 32 +++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md"
index 49acb8d63f..de13e031dd 100644
--- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md"
+++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md"
@@ -532,6 +532,7 @@ var combinationSum2 = function(candidates, target) {
};
```
**使用used去重**
+
```js
var combinationSum2 = function(candidates, target) {
let res = [];
@@ -562,6 +563,37 @@ var combinationSum2 = function(candidates, target) {
};
```
+## TypeScript
+
+```typescript
+function combinationSum2(candidates: number[], target: number): number[][] {
+ candidates.sort((a, b) => a - b);
+ const resArr: number[][] = [];
+ function backTracking(
+ candidates: number[], target: number,
+ curSum: number, startIndex: number, route: number[]
+ ) {
+ if (curSum > target) return;
+ if (curSum === target) {
+ resArr.push(route.slice());
+ return;
+ }
+ for (let i = startIndex, length = candidates.length; i < length; i++) {
+ if (i > startIndex && candidates[i] === candidates[i - 1]) {
+ continue;
+ }
+ let tempVal: number = candidates[i];
+ route.push(tempVal);
+ backTracking(candidates, target, curSum + tempVal, i + 1, route);
+ route.pop();
+
+ }
+ }
+ backTracking(candidates, target, 0, 0, []);
+ return resArr;
+};
+```
+
## C
```c
From c569dc505b59552f1435cecc1b584d88417a4cac Mon Sep 17 00:00:00 2001
From: zhujs
Date: Tue, 29 Mar 2022 23:33:34 +0800
Subject: [PATCH 012/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200332.=E9=87=8D?=
=?UTF-8?q?=E6=96=B0=E5=AE=89=E6=8E=92=E8=A1=8C=E7=A8=8B=20Go=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...11\346\216\222\350\241\214\347\250\213.md" | 72 +++++++++++++++++++
1 file changed, 72 insertions(+)
diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md"
index 01f81c4dc6..e65786c615 100644
--- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md"
+++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md"
@@ -568,5 +568,77 @@ for line in tickets {
}
```
+### Go
+```Go
+
+// 先排序,然后找到第一条路径即可返回
+func findItinerary(tickets [][]string) []string {
+ var path []string // 用来保存搜索的路径
+ data := make(map[string]ticketSlice) // 用来保存tickets排序后的结果
+
+ var search func(airport string) bool
+ search = func(airport string) bool {
+ if len(path) == len(tickets) {
+ path = append(path, airport)
+ return true
+ }
+ to := data[airport]
+ for _, item := range to {
+ if item.Count == 0 {
+ // 已用完
+ continue
+ }
+
+ path = append(path, airport)
+ item.Count--
+ if search(item.To) { return true }
+ item.Count++
+ path = path[:len(path) - 1]
+ }
+
+ return false
+ }
+
+ // 排序
+ // 感觉这段代码有点啰嗦,不知道能不能简化一下
+ tmp := make(map[string]map[string]int)
+ for _, ticket := range tickets {
+ if to, ok := tmp[ticket[0]]; ok {
+ if _, ok2 := to[ticket[1]]; ok2 {
+ to[ticket[1]]++
+ } else {
+ to[ticket[1]] = 1
+ }
+ } else {
+ tmp[ticket[0]] = map[string]int{
+ ticket[1]: 1,
+ }
+ }
+ }
+ for from, to := range tmp {
+ var tmp ticketSlice
+ for to, num := range to {
+ tmp = append(tmp, &ticketStat{To: to, Count: num})
+ }
+ sort.Sort(tmp)
+ data[from] = tmp
+ }
+
+ search("JFK")
+ return path
+}
+
+type ticketStat struct {
+ To string
+ Count int
+}
+type ticketSlice []*ticketStat
+
+func (p ticketSlice) Len() int { return len(p) }
+func (p ticketSlice) Less(i, j int) bool { return strings.Compare(p[i].To, p[j].To) == -1 }
+func (p ticketSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
+
+```
+
-----------------------
From f0b771af5c8d1fe5d5a298f00a2dbc05a1e23b42 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Wed, 30 Mar 2022 16:40:18 +0800
Subject: [PATCH 013/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880131.?=
=?UTF-8?q?=E5=88=86=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...62\345\233\236\346\226\207\344\270\262.md" | 32 +++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
index f50f1c1d11..10b747cb36 100644
--- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
+++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
@@ -450,6 +450,38 @@ var partition = function(s) {
};
```
+## TypeScript
+
+```typescript
+function partition(s: string): string[][] {
+ function isPalindromeStr(s: string, left: number, right: number): boolean {
+ while (left < right) {
+ if (s[left++] !== s[right--]) {
+ return false;
+ }
+ }
+ return true;
+ }
+ function backTracking(s: string, startIndex: number, route: string[]): void {
+ let length: number = s.length;
+ if (length === startIndex) {
+ resArr.push(route.slice());
+ return;
+ }
+ for (let i = startIndex; i < length; i++) {
+ if (isPalindromeStr(s, startIndex, i)) {
+ route.push(s.slice(startIndex, i + 1));
+ backTracking(s, i + 1, route);
+ route.pop();
+ }
+ }
+ }
+ const resArr: string[][] = [];
+ backTracking(s, 0, []);
+ return resArr;
+};
+```
+
## C
```c
From 7ee6bdbbfac43194c2ce3d5feb1f322824c3c7c7 Mon Sep 17 00:00:00 2001
From: h-yx-blog <2041290842@qq.com>
Date: Thu, 31 Mar 2022 08:59:58 +0800
Subject: [PATCH 014/551] =?UTF-8?q?416=E5=88=86=E5=89=B2=E7=AD=89=E5=92=8C?=
=?UTF-8?q?=E5=AD=90=E9=9B=86java=E7=89=88=E5=8F=A6=E4=B8=80=E7=A7=8D?=
=?UTF-8?q?=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...11\345\222\214\345\255\220\351\233\206.md" | 69 +++++++++++++++++++
1 file changed, 69 insertions(+)
diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
index c8d9bc0452..b24fb365f4 100644
--- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
+++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
@@ -208,6 +208,75 @@ class Solution {
}
```
+```java
+public class Solution {
+ public static void main(String[] args) {
+ int num[] = {1,5,11,5};
+ canPartition(num);
+
+ }
+ public static boolean canPartition(int[] nums) {
+ int len = nums.length;
+ // 题目已经说非空数组,可以不做非空判断
+ int sum = 0;
+ for (int num : nums) {
+ sum += num;
+ }
+ // 特判:如果是奇数,就不符合要求
+ if ((sum %2 ) != 0) {
+ return false;
+ }
+
+ int target = sum / 2; //目标背包容量
+ // 创建二维状态数组,行:物品索引,列:容量(包括 0)
+ /*
+ dp[i][j]表示从数组的 [0, i] 这个子区间内挑选一些正整数
+ 每个数只能用一次,使得这些数的和恰好等于 j。
+ */
+ boolean[][] dp = new boolean[len][target + 1];
+
+ // 先填表格第 0 行,第 1 个数只能让容积为它自己的背包恰好装满 (这里的dp[][]数组的含义就是“恰好”,所以就算容积比它大的也不要)
+ if (nums[0] <= target) {
+ dp[0][nums[0]] = true;
+ }
+ // 再填表格后面几行
+ //外层遍历物品
+ for (int i = 1; i < len; i++) {
+ //内层遍历背包
+ for (int j = 0; j <= target; j++) {
+ // 直接从上一行先把结果抄下来,然后再修正
+ dp[i][j] = dp[i - 1][j];
+
+ //如果某个物品单独的重量恰好就等于背包的重量,那么也是满足dp数组的定义的
+ if (nums[i] == j) {
+ dp[i][j] = true;
+ continue;
+ }
+ //如果某个物品的重量小于j,那就可以看该物品是否放入背包
+ //dp[i - 1][j]表示该物品不放入背包,如果在 [0, i - 1] 这个子区间内已经有一部分元素,使得它们的和为 j ,那么 dp[i][j] = true;
+ //dp[i - 1][j - nums[i]]表示该物品放入背包。如果在 [0, i - 1] 这个子区间内就得找到一部分元素,使得它们的和为 j - nums[i]。
+ if (nums[i] < j) {
+ dp[i][j] = dp[i - 1][j] || dp[i - 1][j - nums[i]];
+ }
+ }
+ }
+ for (int i = 0; i < len; i++) {
+ for (int j = 0; j <= target; j++) {
+ System.out.print(dp[i][j]+" ");
+ }
+ System.out.println();
+ }
+ return dp[len - 1][target];
+ }
+}
+//dp数组的打印结果
+false true false false false false false false false false false false
+false true false false false true true false false false false false
+false true false false false true true false false false false true
+false true false false false true true false false false true true
+```
+
+
二维数组版本(易于理解):
```Java
class Solution {
From 00dc57faaca1a77fd7c371f2975df5939a7edb0c Mon Sep 17 00:00:00 2001
From: MoonLight-Sherry <3397446353@qq.com>
Date: Thu, 31 Mar 2022 10:34:34 +0800
Subject: [PATCH 015/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=860509.?=
=?UTF-8?q?=E6=96=90=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0=E5=88=97Rust?=
=?UTF-8?q?=E4=B8=A4=E7=A7=8D=E8=A7=A3=E6=B3=95=E7=9A=84=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...42\351\202\243\345\245\221\346\225\260.md" | 25 ++++++++++++++++++-
1 file changed, 24 insertions(+), 1 deletion(-)
diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
index c6ce76c0cd..e60b8a9e5e 100644
--- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
+++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
@@ -277,7 +277,30 @@ int fib(int n){
return fib(n-1) + fib(n-2);
}
```
+### Rust
+动态规划:
+```Rust
+pub fn fib(n: i32) -> i32 {
+ let n = n as usize;
+ let mut dp = vec![0; 31];
+ dp[1] = 1;
+ for i in 2..=n {
+ dp[i] = dp[i - 1] + dp[i - 2];
+ }
+ dp[n]
+}
+```
-
+递归实现:
+```Rust
+pub fn fib(n: i32) -> i32 {
+ //若n小于等于1,返回n
+ f n <= 1 {
+ return n;
+ }
+ //否则返回fib(n-1) + fib(n-2)
+ return fib(n - 1) + fib(n - 2);
+}
+```
-----------------------
From b6688d7a8949694c2988d48c79f4ebd75d5229ed Mon Sep 17 00:00:00 2001
From: Qianzhengjun <55390356+Qianzhengjun@users.noreply.github.com>
Date: Thu, 31 Mar 2022 11:37:47 +0800
Subject: [PATCH 016/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=860844.=20?=
=?UTF-8?q?=E6=AF=94=E8=BE=83=E5=90=AB=E9=80=80=E6=A0=BC=E7=9A=84=E5=AD=97?=
=?UTF-8?q?=E7=AC=A6=E4=B8=B2=E5=8F=8C=E6=8C=87=E9=92=88=E6=96=B9=E6=B3=95?=
=?UTF-8?q?=E7=9A=84Java=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\345\255\227\347\254\246\344\270\262.md" | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md"
index 00d52e42e2..0d83d425de 100644
--- "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md"
@@ -185,6 +185,36 @@ class Solution {
}
```
+双指针:
+
+```java
+class Solution {
+public static boolean backspaceCompare(String s, String t) {
+ char[] sarray = s.toCharArray();
+ char[] tarray = t.toCharArray();
+ return generate(sarray).equals(generate(tarray));
+ }
+ public static String generate(char[] a){
+ int slow = -1;
+ int fast = 0;
+ if(a.length == 1){
+ return new String(a);
+ } else{
+ for(fast = 0; fast < a.length; fast++){
+ if(a[fast] != '#')
+ a[++slow] = a[fast];
+ else{
+ if(slow >= 0)
+ slow--;
+ }
+ }
+ return new String(a,0,slow + 1);
+ }
+ }
+}
+```
+
+
### python
From d1ea59aebd4659274165c5a352166696275faddc Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 31 Mar 2022 22:14:20 +0800
Subject: [PATCH 017/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880093.?=
=?UTF-8?q?=E5=A4=8D=E5=8E=9FIP=E5=9C=B0=E5=9D=80.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\345\216\237IP\345\234\260\345\235\200.md" | 39 +++++++++++++++++++
1 file changed, 39 insertions(+)
diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
index 714dcb4f89..7910fc50cc 100644
--- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
+++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
@@ -455,6 +455,45 @@ var restoreIpAddresses = function(s) {
};
```
+## TypeScript
+
+```typescript
+function isValidIpSegment(str: string): boolean {
+ let resBool: boolean = true;
+ let tempVal: number = Number(str);
+ if (
+ str.length === 0 || isNaN(tempVal) ||
+ tempVal > 255 || tempVal < 0 ||
+ (str.length > 1 && str[0] === '0')
+ ) {
+ resBool = false;
+ }
+ return resBool;
+}
+function restoreIpAddresses(s: string): string[] {
+ const resArr: string[] = [];
+ backTracking(s, 0, []);
+ return resArr;
+ function backTracking(s: string, startIndex: number, route: string[]): void {
+ let length: number = s.length;
+ if (route.length === 4 && startIndex >= length) {
+ resArr.push(route.join('.'));
+ return;
+ }
+ if (route.length === 4 || startIndex >= length) return;
+ let tempStr: string = '';
+ for (let i = startIndex + 1; i <= Math.min(length, startIndex + 3); i++) {
+ tempStr = s.slice(startIndex, i);
+ if (isValidIpSegment(tempStr)) {
+ route.push(s.slice(startIndex, i));
+ backTracking(s, i, route);
+ route.pop();
+ }
+ }
+ }
+};
+```
+
## Go
回溯(对于前导 0的IP(特别注意s[startIndex]=='0'的判断,不应该写成s[startIndex]==0,因为s截取出来不是数字))
From 48de0f8bd55e24b2b9233648e0f85e4fcb75c0c0 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 1 Apr 2022 11:02:47 +0800
Subject: [PATCH 018/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880078.?=
=?UTF-8?q?=E5=AD=90=E9=9B=86.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?=
=?UTF-8?q?ript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
"problems/0078.\345\255\220\351\233\206.md" | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md"
index cdb5f54891..e1c52b5b81 100644
--- "a/problems/0078.\345\255\220\351\233\206.md"
+++ "b/problems/0078.\345\255\220\351\233\206.md"
@@ -272,7 +272,28 @@ var subsets = function(nums) {
};
```
+## TypeScript
+
+```typescript
+function subsets(nums: number[]): number[][] {
+ const resArr: number[][] = [];
+ backTracking(nums, 0, []);
+ return resArr;
+ function backTracking(nums: number[], startIndex: number, route: number[]): void {
+ resArr.push(route.slice());
+ let length = nums.length;
+ if (startIndex === length) return;
+ for (let i = startIndex; i < length; i++) {
+ route.push(nums[i]);
+ backTracking(nums, i + 1, route);
+ route.pop();
+ }
+ }
+};
+```
+
## C
+
```c
int* path;
int pathTop;
From b85700890227fa69a7ca506384e7e8a821530675 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 1 Apr 2022 12:08:26 +0800
Subject: [PATCH 019/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880090.?=
=?UTF-8?q?=E5=AD=90=E9=9B=86II.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0type?=
=?UTF-8?q?script=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
"problems/0090.\345\255\220\351\233\206II.md" | 24 ++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md"
index 16bd1f2c19..6469f4ba54 100644
--- "a/problems/0090.\345\255\220\351\233\206II.md"
+++ "b/problems/0090.\345\255\220\351\233\206II.md"
@@ -319,6 +319,28 @@ var subsetsWithDup = function(nums) {
```
+### TypeScript
+
+```typescript
+function subsetsWithDup(nums: number[]): number[][] {
+ nums.sort((a, b) => a - b);
+ const resArr: number[][] = [];
+ backTraking(nums, 0, []);
+ return resArr;
+ function backTraking(nums: number[], startIndex: number, route: number[]): void {
+ resArr.push(route.slice());
+ let length: number = nums.length;
+ if (startIndex === length) return;
+ for (let i = startIndex; i < length; i++) {
+ if (i > startIndex && nums[i] === nums[i - 1]) continue;
+ route.push(nums[i]);
+ backTraking(nums, i + 1, route);
+ route.pop();
+ }
+ }
+};
+```
+
### C
```c
@@ -388,7 +410,7 @@ int** subsetsWithDup(int* nums, int numsSize, int* returnSize, int** returnColum
}
```
-## Swift
+### Swift
```swift
func subsetsWithDup(_ nums: [Int]) -> [[Int]] {
From c4a4a0323127f3b47e89fdeefd7bc8a57d404e1e Mon Sep 17 00:00:00 2001
From: Effy Wang
Date: Fri, 1 Apr 2022 17:26:47 +0800
Subject: [PATCH 020/551] =?UTF-8?q?Update=200704.=E4=BA=8C=E5=88=86?=
=?UTF-8?q?=E6=9F=A5=E6=89=BE.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
add detailed comments to the Javascript version of Binary Search
---
...72\214\345\210\206\346\237\245\346\211\276.md" | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
index 15e096a0b1..5562513089 100644
--- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
+++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
@@ -276,7 +276,7 @@ func search(nums []int, target int) int {
```
**JavaScript:**
-(版本一)左闭右闭区间
+(版本一)左闭右闭区间 [left, right]
```js
/**
@@ -285,10 +285,12 @@ func search(nums []int, target int) int {
* @return {number}
*/
var search = function(nums, target) {
+ // right是数组最后一个数的下标,num[right]在查找范围内,是左闭右闭区间
let left = 0, right = nums.length - 1;
- // 使用左闭右闭区间
+ // 当left=right时,由于nums[right]在查找范围内,所以要包括此情况
while (left <= right) {
let mid = left + Math.floor((right - left)/2);
+ // 如果中间数大于目标值,要把中间数排除查找范围,所以右边界更新为mid-1;如果右边界更新为mid,那中间数还在下次查找范围内
if (nums[mid] > target) {
right = mid - 1; // 去左面闭区间寻找
} else if (nums[mid] < target) {
@@ -300,7 +302,7 @@ var search = function(nums, target) {
return -1;
};
```
-(版本二)左闭右开区间
+(版本二)左闭右开区间 [left, right)
```js
/**
@@ -309,10 +311,13 @@ var search = function(nums, target) {
* @return {number}
*/
var search = function(nums, target) {
- let left = 0, right = nums.length;
- // 使用左闭右开区间 [left, right)
+ // right是数组最后一个数的下标+1,nums[right]不在查找范围内,是左闭右开区间
+ let left = 0, right = nums.length;
+ // 当left=right时,由于nums[right]不在查找范围,所以不必包括此情况
while (left < right) {
let mid = left + Math.floor((right - left)/2);
+ // 如果中间值大于目标值,中间值不应在下次查找的范围内,但中间值的前一个值应在;
+ // 由于right本来就不在查找范围内,所以将右边界更新为中间值,如果更新右边界为mid-1则将中间值的前一个值也踢出了下次寻找范围
if (nums[mid] > target) {
right = mid; // 去左区间寻找
} else if (nums[mid] < target) {
From 3f7dd67a8031663d2b673a41587e3a640e0a4b1f Mon Sep 17 00:00:00 2001
From: FrankLin
Date: Fri, 1 Apr 2022 10:57:25 -0400
Subject: [PATCH 021/551] Add C version for LeetCode349 using array
---
...04\347\232\204\344\272\244\351\233\206.md" | 32 +++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
index 82be1829d6..64d80a37c4 100644
--- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
+++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
@@ -281,6 +281,38 @@ impl Solution {
}
}
```
+
+C:
+```C
+int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
+
+ int nums1Cnt[1000] = {0};
+ int lessSize = nums1Size < nums2Size ? nums1Size : nums2Size;
+ int * result = (int *) calloc(lessSize, sizeof(int));
+ int resultIndex = 0;
+ int* tempNums;
+
+ int i;
+
+ //Calculate the number's counts for nums1 array
+ for(i = 0; i < nums1Size; i ++) {
+ nums1Cnt[nums1[i]]++;
+ }
+
+ //Check if the value existing in nums1 count array
+ for(i = 0; i < nums2Size; i ++) {
+ if(nums1Cnt[nums2[i]] > 0) {
+ result[resultIndex] = nums2[i];
+ resultIndex ++;
+ //Clear this count to avoid duplicated value
+ nums1Cnt[nums2[i]] = 0;
+ }
+ }
+ * returnSize = resultIndex;
+ return result;
+}
+```
+
## 相关题目
* 350.两个数组的交集 II
From abc08b6bb6f698135efe52ad0a7f20b333d32173 Mon Sep 17 00:00:00 2001
From: FrankLin
Date: Fri, 1 Apr 2022 11:01:00 -0400
Subject: [PATCH 022/551] correct comments of C version of Leetcode349
---
...\260\347\273\204\347\232\204\344\272\244\351\233\206.md" | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
index 64d80a37c4..45f19b6ef2 100644
--- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
+++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
@@ -294,17 +294,17 @@ int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* re
int i;
- //Calculate the number's counts for nums1 array
+ /* Calculate the number's counts for nums1 array */
for(i = 0; i < nums1Size; i ++) {
nums1Cnt[nums1[i]]++;
}
- //Check if the value existing in nums1 count array
+ /* Check if the value in nums2 is existing in nums1 count array */
for(i = 0; i < nums2Size; i ++) {
if(nums1Cnt[nums2[i]] > 0) {
result[resultIndex] = nums2[i];
resultIndex ++;
- //Clear this count to avoid duplicated value
+ /* Clear this count to avoid duplicated value */
nums1Cnt[nums2[i]] = 0;
}
}
From 297a22fd880f207bd9b621b7dbd5e8dfd84340a1 Mon Sep 17 00:00:00 2001
From: FrankLin
Date: Fri, 1 Apr 2022 15:11:00 -0400
Subject: [PATCH 023/551] Add C version for Leetcode202 passed Leetcode
submission
---
...2.\345\277\253\344\271\220\346\225\260.md" | 70 +++++++++++++++++++
1 file changed, 70 insertions(+)
diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md"
index f0a46a40d2..741a735a5d 100644
--- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md"
+++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md"
@@ -315,5 +315,75 @@ class Solution {
}
```
+C:
+```C
+typedef struct HashNodeTag {
+ int key; /* num */
+ struct HashNodeTag *next;
+}HashNode;
+
+/* Calcualte the hash key */
+static inline int hash(int key, int size) {
+ int index = key % size;
+ return (index > 0) ? (index) : (-index);
+}
+
+/* Calculate the sum of the squares of its digits*/
+static inline int calcSquareSum(int num) {
+ unsigned int sum = 0;
+ while(num > 0) {
+ sum += (num % 10) * (num % 10);
+ num = num/10;
+ }
+ return sum;
+}
+
+#define HASH_TABLE_SIZE (32)
+
+bool isHappy(int n){
+ int sum = n;
+ int index = 0;
+ bool bHappy = false;
+ bool bExit = false;
+ /* allocate the memory for hash table with chaining method*/
+ HashNode ** hashTable = (HashNode **)calloc(HASH_TABLE_SIZE, sizeof(HashNode));
+
+ while(bExit == false) {
+ /* check if n has been calculated */
+ index = hash(n, HASH_TABLE_SIZE);
+
+ HashNode ** p = hashTable + index;
+
+ while((*p) && (bExit == false)) {
+ /* Check if this num was calculated, if yes, this will be endless loop */
+ if((*p)->key == n) {
+ bHappy = false;
+ bExit = true;
+ }
+ /* move to next node of the same index */
+ p = &((*p)->next);
+ }
+
+ /* put n intot hash table */
+ HashNode * newNode = (HashNode *)malloc(sizeof(HashNode));
+ newNode->key = n;
+ newNode->next = NULL;
+
+ *p = newNode;
+
+ sum = calcSquareSum(n);
+ if(sum == 1) {
+ bHappy = true;
+ bExit = true;
+ }
+ else {
+ n = sum;
+
+ }
+ }
+
+ return bHappy;
+}
+```
-----------------------
From fb5571f16576d9d16274219d60674662970366ec Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 2 Apr 2022 11:24:06 +0800
Subject: [PATCH 024/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880491.?=
=?UTF-8?q?=E9=80=92=E5=A2=9E=E5=AD=90=E5=BA=8F=E5=88=97.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...36\345\255\220\345\272\217\345\210\227.md" | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md"
index 6103c3d0a1..3ea2382b1f 100644
--- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md"
@@ -396,7 +396,35 @@ var findSubsequences = function(nums) {
```
+## TypeScript
+
+```typescript
+function findSubsequences(nums: number[]): number[][] {
+ const resArr: number[][] = [];
+ backTracking(nums, 0, []);
+ return resArr;
+ function backTracking(nums: number[], startIndex: number, route: number[]): void {
+ let length: number = nums.length;
+ if (route.length >= 2) {
+ resArr.push(route.slice());
+ }
+ const usedSet: Set = new Set();
+ for (let i = startIndex; i < length; i++) {
+ if (
+ nums[i] < route[route.length - 1] ||
+ usedSet.has(nums[i])
+ ) continue;
+ usedSet.add(nums[i]);
+ route.push(nums[i]);
+ backTracking(nums, i + 1, route);
+ route.pop();
+ }
+ }
+};
+```
+
### C
+
```c
int* path;
int pathTop;
From 77f1e2c85da13846b26cd33159e0dee565ef1d9d Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 2 Apr 2022 19:24:03 +0800
Subject: [PATCH 025/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880046.?=
=?UTF-8?q?=E5=85=A8=E6=8E=92=E5=88=97.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typesript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...6.\345\205\250\346\216\222\345\210\227.md" | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md"
index c5369ddd1b..836c36462f 100644
--- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md"
+++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md"
@@ -331,6 +331,34 @@ var permute = function(nums) {
```
+## TypeScript
+
+```typescript
+function permute(nums: number[]): number[][] {
+ const resArr: number[][] = [];
+ const helperSet: Set = new Set();
+ backTracking(nums, []);
+ return resArr;
+ function backTracking(nums: number[], route: number[]): void {
+ if (route.length === nums.length) {
+ resArr.push(route.slice());
+ return;
+ }
+ let tempVal: number;
+ for (let i = 0, length = nums.length; i < length; i++) {
+ tempVal = nums[i];
+ if (!helperSet.has(tempVal)) {
+ route.push(tempVal);
+ helperSet.add(tempVal);
+ backTracking(nums, route);
+ route.pop();
+ helperSet.delete(tempVal);
+ }
+ }
+ }
+};
+```
+
### C
```c
From c9dfda1c955c0665108d8aa7fc78b33d5edded99 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 2 Apr 2022 23:34:25 +0800
Subject: [PATCH 026/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880047.?=
=?UTF-8?q?=E5=85=A8=E6=8E=92=E5=88=97II.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\345\205\250\346\216\222\345\210\227II.md" | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md"
index 0cecac5058..cce25cd9a1 100644
--- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md"
+++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md"
@@ -292,6 +292,34 @@ var permuteUnique = function (nums) {
```
+### TypeScript
+
+```typescript
+function permuteUnique(nums: number[]): number[][] {
+ nums.sort((a, b) => a - b);
+ const resArr: number[][] = [];
+ const usedArr: boolean[] = new Array(nums.length).fill(false);
+ backTracking(nums, []);
+ return resArr;
+ function backTracking(nums: number[], route: number[]): void {
+ if (route.length === nums.length) {
+ resArr.push(route.slice());
+ return;
+ }
+ for (let i = 0, length = nums.length; i < length; i++) {
+ if (i > 0 && nums[i] === nums[i - 1] && usedArr[i - 1] === false) continue;
+ if (usedArr[i] === false) {
+ route.push(nums[i]);
+ usedArr[i] = true;
+ backTracking(nums, route);
+ usedArr[i] = false;
+ route.pop();
+ }
+ }
+ }
+};
+```
+
### Swift
```swift
From 710e816012bb0198a5a42686e94d24b50aa68760 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 3 Apr 2022 11:26:52 +0800
Subject: [PATCH 027/551] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=8820201112?=
=?UTF-8?q?=E5=9B=9E=E6=BA=AF=E5=91=A8=E6=9C=AB=E6=80=BB=E7=BB=93.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E8=A1=A5=E5=85=85=E5=88=86=E6=9E=90=E6=8E=92?=
=?UTF-8?q?=E5=88=97=E9=97=AE=E9=A2=98=E6=97=B6=E9=97=B4=E5=A4=8D=E6=9D=82?=
=?UTF-8?q?=E5=BA=A6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md"
index c61de4bb4c..af08097b64 100644
--- "a/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md"
+++ "b/problems/\345\221\250\346\200\273\347\273\223/20201112\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md"
@@ -76,7 +76,7 @@
* 空间复杂度:$O(n)$,递归深度为n,所以系统栈所用空间为$O(n)$,每一层递归所用的空间都是常数级别,注意代码里的result和path都是全局变量,就算是放在参数里,传的也是引用,并不会新申请内存空间,最终空间复杂度为$O(n)$。
排列问题分析:
-* 时间复杂度:$O(n!)$,这个可以从排列的树形图中很明显发现,每一层节点为n,第二层每一个分支都延伸了n-1个分支,再往下又是n-2个分支,所以一直到叶子节点一共就是 n * n-1 * n-2 * ..... 1 = n!。
+* 时间复杂度:$O(n!)$,这个可以从排列的树形图中很明显发现,每一层节点为n,第二层每一个分支都延伸了n-1个分支,再往下又是n-2个分支,所以一直到叶子节点一共就是 n * n-1 * n-2 * ..... 1 = n!。每个叶子节点都会有一个构造全排列填进数组的操作(对应的代码:`result.push_back(path)`),该操作的复杂度为$O(n)$。所以,最终时间复杂度为:n * n!,简化为$O(n!)$。
* 空间复杂度:$O(n)$,和子集问题同理。
组合问题分析:
From e48336238d25eac775f1897ad85b9be551ba1e69 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=B4=81?= <3224935686@qq.com>
Date: Sun, 3 Apr 2022 14:03:31 +0800
Subject: [PATCH 028/551] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=B3=BB=E5=88=9719=20=E4=BB=8E=E4=B8=AD=E5=BA=8F?=
=?UTF-8?q?=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F=E5=88=97?=
=?UTF-8?q?=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91JS=E7=89=88?=
=?UTF-8?q?=E6=9C=AC=E5=8F=98=E9=87=8F=E5=90=8D=20preorder->inorder?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md"
index 496de431ed..7ecca773f5 100644
--- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md"
@@ -790,7 +790,7 @@ func findRootIndex(target int,inorder []int) int{
```javascript
var buildTree = function(inorder, postorder) {
- if (!preorder.length) return null;
+ if (!inorder.length) return null;
const rootVal = postorder.pop(); // 从后序遍历的数组中获取中间节点的值, 即数组最后一个值
let rootIndex = inorder.indexOf(rootVal); // 获取中间节点在中序遍历中的下标
const root = new TreeNode(rootVal); // 创建中间节点
From 1dcbf7f6f0ba7b50da6685532a42d2ceac9aeccc Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 3 Apr 2022 19:43:34 +0800
Subject: [PATCH 029/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E5=9B=9E?=
=?UTF-8?q?=E6=BA=AF=E7=AE=97=E6=B3=95=E5=8E=BB=E9=87=8D=E9=97=AE=E9=A2=98?=
=?UTF-8?q?=E7=9A=84=E5=8F=A6=E4=B8=80=E7=A7=8D=E5=86=99=E6=B3=95.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...00\347\247\215\345\206\231\346\263\225.md" | 81 +++++++++++++++++++
1 file changed, 81 insertions(+)
diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md"
index b4bdda0060..f48097e179 100644
--- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md"
+++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md"
@@ -365,6 +365,87 @@ class Solution:
return res
```
+TypeScript:
+
+**90.子集II**
+
+```typescript
+function subsetsWithDup(nums: number[]): number[][] {
+ nums.sort((a, b) => a - b);
+ const resArr: number[][] = [];
+ backTraking(nums, 0, []);
+ return resArr;
+ function backTraking(nums: number[], startIndex: number, route: number[]): void {
+ resArr.push(route.slice());
+ const helperSet: Set = new Set();
+ for (let i = startIndex, length = nums.length; i < length; i++) {
+ if (helperSet.has(nums[i])) continue;
+ helperSet.add(nums[i]);
+ route.push(nums[i]);
+ backTraking(nums, i + 1, route);
+ route.pop();
+ }
+ }
+};
+```
+
+**40. 组合总和 II**
+
+```typescript
+function combinationSum2(candidates: number[], target: number): number[][] {
+ candidates.sort((a, b) => a - b);
+ const resArr: number[][] = [];
+ backTracking(candidates, target, 0, 0, []);
+ return resArr;
+ function backTracking(
+ candidates: number[], target: number,
+ curSum: number, startIndex: number, route: number[]
+ ) {
+ if (curSum > target) return;
+ if (curSum === target) {
+ resArr.push(route.slice());
+ return;
+ }
+ const helperSet: Set = new Set();
+ for (let i = startIndex, length = candidates.length; i < length; i++) {
+ let tempVal: number = candidates[i];
+ if (helperSet.has(tempVal)) continue;
+ helperSet.add(tempVal);
+ route.push(tempVal);
+ backTracking(candidates, target, curSum + tempVal, i + 1, route);
+ route.pop();
+
+ }
+ }
+};
+```
+
+**47. 全排列 II**
+
+```typescript
+function permuteUnique(nums: number[]): number[][] {
+ const resArr: number[][] = [];
+ const usedArr: boolean[] = [];
+ backTracking(nums, []);
+ return resArr;
+ function backTracking(nums: number[], route: number[]): void {
+ if (nums.length === route.length) {
+ resArr.push(route.slice());
+ return;
+ }
+ const usedSet: Set = new Set();
+ for (let i = 0, length = nums.length; i < length; i++) {
+ if (usedArr[i] === true || usedSet.has(nums[i])) continue;
+ usedSet.add(nums[i]);
+ route.push(nums[i]);
+ usedArr[i] = true;
+ backTracking(nums, route);
+ usedArr[i] = false;
+ route.pop();
+ }
+ }
+};
+```
Go:
From 3635751759fcf689f9baae10ab5fe4a5d642b5f8 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 3 Apr 2022 23:15:06 +0800
Subject: [PATCH 030/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880332.?=
=?UTF-8?q?=E9=87=8D=E6=96=B0=E5=AE=89=E6=8E=92=E8=A1=8C=E7=A8=8B.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...11\346\216\222\350\241\214\347\250\213.md" | 44 +++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md"
index 01f81c4dc6..370db45bec 100644
--- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md"
+++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md"
@@ -448,6 +448,50 @@ var findItinerary = function(tickets) {
```
+### TypeScript
+
+```typescript
+function findItinerary(tickets: string[][]): string[] {
+ /**
+ TicketsMap 实例:
+ { NRT: Map(1) { 'JFK' => 1 }, JFK: Map(2) { 'KUL' => 1, 'NRT' => 1 } }
+ 这里选择Map数据结构的原因是:与Object类型的一个主要差异是,Map实例会维护键值对的插入顺序。
+ */
+ type TicketsMap = {
+ [index: string]: Map
+ };
+ tickets.sort((a, b) => {
+ return a[1] < b[1] ? -1 : 1;
+ });
+ const ticketMap: TicketsMap = {};
+ for (const [from, to] of tickets) {
+ if (ticketMap[from] === undefined) {
+ ticketMap[from] = new Map();
+ }
+ ticketMap[from].set(to, (ticketMap[from].get(to) || 0) + 1);
+ }
+ const resRoute = ['JFK'];
+ backTracking(tickets.length, ticketMap, resRoute);
+ return resRoute;
+ function backTracking(ticketNum: number, ticketMap: TicketsMap, route: string[]): boolean {
+ if (route.length === ticketNum + 1) return true;
+ const targetMap = ticketMap[route[route.length - 1]];
+ if (targetMap !== undefined) {
+ for (const [to, count] of targetMap.entries()) {
+ if (count > 0) {
+ route.push(to);
+ targetMap.set(to, count - 1);
+ if (backTracking(ticketNum, ticketMap, route) === true) return true;
+ targetMap.set(to, count);
+ route.pop();
+ }
+ }
+ }
+ return false;
+ }
+};
+```
+
### Swift
直接迭代tickets数组:
From d3a69ff358793c6c4f6f7636935b545437404f21 Mon Sep 17 00:00:00 2001
From: Camille0512
Date: Mon, 4 Apr 2022 12:55:12 +0800
Subject: [PATCH 031/551] Add one more python code
---
...11\346\220\234\347\264\242\346\240\221.md" | 23 +++++++++++-
...60\344\272\214\345\217\211\346\240\221.md" | 35 -------------------
2 files changed, 22 insertions(+), 36 deletions(-)
diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
index 4ed2961977..ff4335fca6 100644
--- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
+++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
@@ -408,7 +408,28 @@ class Solution:
return True
```
-## Go
+```python
+# 遵循Carl的写法,只添加了节点判断的部分
+class Solution:
+ def isValidBST(self, root: TreeNode) -> bool:
+ # method 2
+ que, pre = [], None
+ while root or que:
+ while root:
+ que.append(root)
+ root = root.left
+ root = que.pop()
+ # 对第一个节点只做记录,对后面的节点进行比较
+ if pre is None:
+ pre = root.val
+ else:
+ if pre >= root.val: return False
+ pre = root.val
+ root = root.right
+ return True
+```
+
+## Go
```Go
import "math"
diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md"
index 0007b4d44c..e4e232c884 100644
--- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md"
@@ -437,41 +437,6 @@ class Solution:
return True
```
-层序遍历
-
-```python
-class Solution:
- def isSymmetric(self, root: TreeNode) -> bool:
- if not root: return True
- que, cnt = [[root.left, root.right]], 1
- while que:
- nodes, tmp, sign = que.pop(), [], False
- for node in nodes:
- if not node:
- tmp.append(None)
- tmp.append(None)
- else:
- if node.left:
- tmp.append(node.left)
- sign = True
- else:
- tmp.append(None)
- if node.right:
- tmp.append(node.right)
- sign = True
- else:
- tmp.append(None)
- p1, p2 = 0, len(nodes) - 1
- while p1 < p2:
- if (not nodes[p1] and nodes[p2]) or (nodes[p1] and not nodes[p2]): return False
- elif nodes[p1] and nodes[p2] and nodes[p1].val != nodes[p2].val: return False
- p1 += 1
- p2 -= 1
- if sign: que.append(tmp)
- cnt += 1
- return True
-```
-
## Go
```go
From 2b80a6d36af3c00b260b55395361a8c960a02d0a Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Mon, 4 Apr 2022 16:56:36 +0800
Subject: [PATCH 032/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880051.N?=
=?UTF-8?q?=E7=9A=87=E5=90=8E.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?=
=?UTF-8?q?ript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
"problems/0051.N\347\232\207\345\220\216.md" | 52 ++++++++++++++++++++
1 file changed, 52 insertions(+)
diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md"
index 7eb0d7a0b1..85524e6f90 100644
--- "a/problems/0051.N\347\232\207\345\220\216.md"
+++ "b/problems/0051.N\347\232\207\345\220\216.md"
@@ -457,6 +457,58 @@ var solveNQueens = function(n) {
};
```
+## TypeScript
+
+```typescript
+function solveNQueens(n: number): string[][] {
+ const board: string[][] = new Array(n).fill(0).map(_ => new Array(n).fill('.'));
+ const resArr: string[][] = [];
+ backTracking(n, 0, board);
+ return resArr;
+ function backTracking(n: number, rowNum: number, board: string[][]): void {
+ if (rowNum === n) {
+ resArr.push(transformBoard(board));
+ return;
+ }
+ for (let i = 0; i < n; i++) {
+ if (isValid(i, rowNum, board) === true) {
+ board[rowNum][i] = 'Q';
+ backTracking(n, rowNum + 1, board);
+ board[rowNum][i] = '.';
+ }
+ }
+ }
+};
+function isValid(col: number, row: number, board: string[][]): boolean {
+ const n: number = board.length;
+ if (col < 0 || col >= n || row < 0 || row >= n) return false;
+ // 检查列
+ for (let row of board) {
+ if (row[col] === 'Q') return false;
+ }
+ // 检查45度方向
+ let x: number = col,
+ y: number = row;
+ while (y >= 0 && x < n) {
+ if (board[y--][x++] === 'Q') return false;
+ }
+ // 检查135度方向
+ x = col;
+ y = row;
+ while (x >= 0 && y >= 0) {
+ if (board[y--][x--] === 'Q') return false;
+ }
+ return true;
+}
+function transformBoard(board: string[][]): string[] {
+ const resArr = [];
+ for (let row of board) {
+ resArr.push(row.join(''));
+ }
+ return resArr;
+}
+```
+
### Swift
```swift
From 5374fb48a4ebacd714cb74ae979a5fd09a4c7f28 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Mon, 4 Apr 2022 21:41:56 +0800
Subject: [PATCH 033/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880037.?=
=?UTF-8?q?=E8=A7=A3=E6=95=B0=E7=8B=AC.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...7.\350\247\243\346\225\260\347\213\254.md" | 49 +++++++++++++++++++
1 file changed, 49 insertions(+)
diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md"
index 53b9bb67e0..c1ac15af89 100644
--- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md"
+++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md"
@@ -439,6 +439,55 @@ var solveSudoku = function(board) {
};
```
+### TypeScript
+
+```typescript
+/**
+ Do not return anything, modify board in-place instead.
+ */
+function isValid(col: number, row: number, val: string, board: string[][]): boolean {
+ let n: number = board.length;
+ // 列向检查
+ for (let rowIndex = 0; rowIndex < n; rowIndex++) {
+ if (board[rowIndex][col] === val) return false;
+ }
+ // 横向检查
+ for (let colIndex = 0; colIndex < n; colIndex++) {
+ if (board[row][colIndex] === val) return false;
+ }
+ // 九宫格检查
+ const startX = Math.floor(col / 3) * 3;
+ const startY = Math.floor(row / 3) * 3;
+ for (let rowIndex = startY; rowIndex < startY + 3; rowIndex++) {
+ for (let colIndex = startX; colIndex < startX + 3; colIndex++) {
+ if (board[rowIndex][colIndex] === val) return false;
+ }
+ }
+ return true;
+}
+function solveSudoku(board: string[][]): void {
+ let n: number = 9;
+ backTracking(n, board);
+ function backTracking(n: number, board: string[][]): boolean {
+ for (let row = 0; row < n; row++) {
+ for (let col = 0; col < n; col++) {
+ if (board[row][col] === '.') {
+ for (let i = 1; i <= n; i++) {
+ if (isValid(col, row, String(i), board)) {
+ board[row][col] = String(i);
+ if (backTracking(n, board) === true) return true;
+ board[row][col] = '.';
+ }
+ }
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+};
+```
+
### C
```C
From f9079ddc18206cd8a16eeb8085808b65ecab6d6e Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Tue, 5 Apr 2022 10:56:53 +0800
Subject: [PATCH 034/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880455.?=
=?UTF-8?q?=E5=88=86=E5=8F=91=E9=A5=BC=E5=B9=B2.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...06\345\217\221\351\245\274\345\271\262.md" | 43 +++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md"
index 210b492df6..f012df68b1 100644
--- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md"
+++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md"
@@ -209,7 +209,50 @@ var findContentChildren = function(g, s) {
```
+### TypeScript
+
+```typescript
+// 大饼干尽量喂胃口大的
+function findContentChildren(g: number[], s: number[]): number {
+ g.sort((a, b) => a - b);
+ s.sort((a, b) => a - b);
+ const childLength: number = g.length,
+ cookieLength: number = s.length;
+ let curChild: number = childLength - 1,
+ curCookie: number = cookieLength - 1;
+ let resCount: number = 0;
+ while (curChild >= 0 && curCookie >= 0) {
+ if (g[curChild] <= s[curCookie]) {
+ curCookie--;
+ resCount++;
+ }
+ curChild--;
+ }
+ return resCount;
+};
+```
+
+```typescript
+// 小饼干先喂饱小胃口的
+function findContentChildren(g: number[], s: number[]): number {
+ g.sort((a, b) => a - b);
+ s.sort((a, b) => a - b);
+ const childLength: number = g.length,
+ cookieLength: number = s.length;
+ let curChild: number = 0,
+ curCookie: number = 0;
+ while (curChild < childLength && curCookie < cookieLength) {
+ if (g[curChild] <= s[curCookie]) {
+ curChild++;
+ }
+ curCookie++;
+ }
+ return curChild;
+};
+```
+
### C
+
```c
int cmp(int* a, int* b) {
return *a - *b;
From c4992be374628782d18ffa8648fa1443825bc947 Mon Sep 17 00:00:00 2001
From: Lafish <34953046+LeUKi@users.noreply.github.com>
Date: Wed, 6 Apr 2022 08:52:45 +0800
Subject: [PATCH 035/551] =?UTF-8?q?=E6=9B=B4=E6=96=B00122.=E4=B9=B0?=
=?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?=
=?UTF-8?q?=E6=9C=BAII=EF=BC=9A=E9=94=99=E5=AD=97=E4=BF=AE=E6=AD=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...32\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
index 83b852c6c0..b31cbae3ec 100644
--- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
+++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
@@ -40,7 +40,7 @@
本题首先要清楚两点:
* 只有一只股票!
-* 当前只有买股票或者买股票的操作
+* 当前只有买股票或者卖股票的操作
想获得利润至少要两天为一个交易单元。
From fa00cd53b597d9ce837319f1967d0ae6478c430e Mon Sep 17 00:00:00 2001
From: lingyin
Date: Wed, 6 Apr 2022 10:28:57 +0800
Subject: [PATCH 036/551] =?UTF-8?q?=E7=AE=80=E5=8C=96Peek=E5=87=BD?=
=?UTF-8?q?=E6=95=B0=EF=BC=8C=E5=A4=8D=E7=94=A8Pop?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...345\256\236\347\216\260\351\230\237\345\210\227.md" | 10 +++-------
1 file changed, 3 insertions(+), 7 deletions(-)
diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md"
index b00ffd803f..1a56d9f386 100644
--- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md"
+++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md"
@@ -275,15 +275,11 @@ func (this *MyQueue) Pop() int {
/** Get the front element. */
func (this *MyQueue) Peek() int {
- for len(this.stack) != 0 {
- val := this.stack[len(this.stack)-1]
- this.stack = this.stack[:len(this.stack)-1]
- this.back = append(this.back, val)
- }
- if len(this.back) == 0 {
+ val := this.Pop()
+ if val == 0 {
return 0
}
- val := this.back[len(this.back)-1]
+ this.back = append(this.back, val)
return val
}
From ee93d4d62c36f74e5f878882cd28f7877c60d570 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Wed, 6 Apr 2022 15:40:01 +0800
Subject: [PATCH 037/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880376.?=
=?UTF-8?q?=E6=91=86=E5=8A=A8=E5=BA=8F=E5=88=97.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...06\345\212\250\345\272\217\345\210\227.md" | 50 +++++++++++++++++++
1 file changed, 50 insertions(+)
diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
index 5076c9adaa..acd8996e91 100644
--- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
+++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
@@ -298,5 +298,55 @@ var wiggleMaxLength = function(nums) {
};
```
+### TypeScript
+
+**贪心**
+
+```typescript
+function wiggleMaxLength(nums: number[]): number {
+ let length: number = nums.length;
+ if (length <= 1) return length;
+ let preDiff: number = 0;
+ let curDiff: number = 0;
+ let count: number = 1;
+ for (let i = 1; i < length; i++) {
+ curDiff = nums[i] - nums[i - 1];
+ if (
+ (preDiff <= 0 && curDiff > 0) ||
+ (preDiff >= 0 && curDiff < 0)
+ ) {
+ preDiff = curDiff;
+ count++;
+ }
+ }
+ return count;
+};
+```
+
+**动态规划**
+
+```typescript
+function wiggleMaxLength(nums: number[]): number {
+ const length: number = nums.length;
+ if (length <= 1) return length;
+ const dp: number[][] = new Array(length).fill(0).map(_ => []);
+ dp[0][0] = 1; // 第一个数作为波峰
+ dp[0][1] = 1; // 第一个数作为波谷
+ for (let i = 1; i < length; i++) {
+ dp[i][0] = 1;
+ dp[i][1] = 1;
+ for (let j = 0; j < i; j++) {
+ if (nums[j] < nums[i]) dp[i][0] = Math.max(dp[i][0], dp[j][1] + 1);
+ }
+ for (let j = 0; j < i; j++) {
+ if (nums[j] > nums[i]) dp[i][1] = Math.max(dp[i][1], dp[j][0] + 1);
+ }
+ }
+ return Math.max(dp[length - 1][0], dp[length - 1][1]);
+};
+```
+
+
+
-----------------------
From e4f34f3e47f6120c85e48d2032e6c00d1d13dad0 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Wed, 6 Apr 2022 16:23:34 +0800
Subject: [PATCH 038/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880053.?=
=?UTF-8?q?=E6=9C=80=E5=A4=A7=E5=AD=90=E5=BA=8F=E5=92=8C.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\345\255\220\345\272\217\345\222\214.md" | 37 ++++++++++++++++++-
1 file changed, 36 insertions(+), 1 deletion(-)
diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
index 3d11c91efb..b5fb7642c5 100644
--- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
+++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
@@ -211,7 +211,7 @@ func maxSubArray(nums []int) int {
return maxSum
}
```
-
+
### Javascript:
```Javascript
var maxSubArray = function(nums) {
@@ -230,6 +230,41 @@ var maxSubArray = function(nums) {
};
```
+### TypeScript
+
+**贪心**
+
+```typescript
+function maxSubArray(nums: number[]): number {
+ let curSum: number = 0;
+ let resMax: number = -Infinity;
+ for (let i = 0, length = nums.length; i < length; i++) {
+ curSum += nums[i];
+ resMax = Math.max(curSum, resMax);
+ if (curSum < 0) curSum = 0;
+ }
+ return resMax;
+};
+```
+
+**动态规划**
+
+```typescript
+// 动态规划
+function maxSubArray(nums: number[]): number {
+ const length = nums.length;
+ if (length === 0) return 0;
+ const dp: number[] = [];
+ dp[0] = nums[0];
+ let resMax: number = nums[0];
+ for (let i = 1; i < length; i++) {
+ dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
+ resMax = Math.max(resMax, dp[i]);
+ }
+ return resMax;
+};
+```
+
-----------------------
From 92c6b3609fb85036f68039fd20ac52c8c919fba0 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Wed, 6 Apr 2022 22:46:46 +0800
Subject: [PATCH 039/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880122.?=
=?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3?=
=?UTF-8?q?=E6=97=B6=E6=9C=BAII.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0type?=
=?UTF-8?q?script=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...234\200\344\275\263\346\227\266\346\234\272II.md" | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
index 83b852c6c0..1e4d017c36 100644
--- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
+++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
@@ -268,6 +268,18 @@ const maxProfit = (prices) => {
};
```
+TypeScript:
+
+```typescript
+function maxProfit(prices: number[]): number {
+ let resProfit: number = 0;
+ for (let i = 1, length = prices.length; i < length; i++) {
+ resProfit += Math.max(prices[i] - prices[i - 1], 0);
+ }
+ return resProfit;
+};
+```
+
C:
```c
From 1e6ec2d32a5cb21ba049646979b23ed948f46444 Mon Sep 17 00:00:00 2001
From: zhuye
Date: Wed, 6 Apr 2022 23:05:01 +0800
Subject: [PATCH 040/551] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=80=900707-?=
=?UTF-8?q?=E8=AE=BE=E8=AE=A1=E9=93=BE=E8=A1=A8.md=E3=80=91TypeScript?=
=?UTF-8?q?=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" | 3 +++
1 file changed, 3 insertions(+)
diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
index 86f3a683ed..c5a5557409 100644
--- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
+++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
@@ -973,6 +973,9 @@ class MyLinkedList {
// 处理头节点
if (index === 0) {
this.head = this.head!.next;
+ if (index === this.size - 1) {
+ this.tail = null
+ }
this.size--;
return;
}
From 6e09456aefe7981d272e355c28e4ab43597ac5b1 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Wed, 6 Apr 2022 23:23:22 +0800
Subject: [PATCH 041/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880055.?=
=?UTF-8?q?=E8=B7=B3=E8=B7=83=E6=B8=B8=E6=88=8F.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\263\350\267\203\346\270\270\346\210\217.md" | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md"
index c0890f75f3..9461424265 100644
--- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md"
+++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md"
@@ -154,6 +154,23 @@ var canJump = function(nums) {
};
```
+### TypeScript
+
+```typescript
+function canJump(nums: number[]): boolean {
+ let farthestIndex: number = 0;
+ let cur: number = 0;
+ while (cur <= farthestIndex) {
+ farthestIndex = Math.max(farthestIndex, cur + nums[cur]);
+ if (farthestIndex >= nums.length - 1) return true;
+ cur++;
+ }
+ return false;
+};
+```
+
+
+
-----------------------
From 31800730a2634f0a40c99075d277acc251c1634a Mon Sep 17 00:00:00 2001
From: zhuye
Date: Wed, 6 Apr 2022 23:30:18 +0800
Subject: [PATCH 042/551] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=80=900707-?=
=?UTF-8?q?=E8=AE=BE=E8=AE=A1=E9=93=BE=E8=A1=A8.md=E3=80=91TypeScript?=
=?UTF-8?q?=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" | 1 +
1 file changed, 1 insertion(+)
diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
index c5a5557409..37ce15adce 100644
--- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
+++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
@@ -973,6 +973,7 @@ class MyLinkedList {
// 处理头节点
if (index === 0) {
this.head = this.head!.next;
+ // 如果链表中只有一个元素,删除头节点后,需要处理尾节点
if (index === this.size - 1) {
this.tail = null
}
From f70acb963a499e3a627d2358a4119b24649d74d7 Mon Sep 17 00:00:00 2001
From: speed <771935730@qq.com>
Date: Thu, 7 Apr 2022 00:03:02 +0800
Subject: [PATCH 043/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200763.=E5=88=92?=
=?UTF-8?q?=E5=88=86=E5=AD=97=E6=AF=8D=E5=8C=BA=E9=97=B4.md=20=E6=96=B0?=
=?UTF-8?q?=E5=A2=9E=E7=B1=BB=E4=BC=BC=E5=BC=95=E7=88=86=E6=B0=94=E7=90=83?=
=?UTF-8?q?=E7=9A=84=E6=80=9D=E8=B7=AF=E4=BB=A5=E5=8F=8A=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...27\346\257\215\345\214\272\351\227\264.md" | 47 +++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
index 03d3a73b3b..d3958bd5e6 100644
--- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
+++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
@@ -77,6 +77,53 @@ public:
但这道题目的思路是很巧妙的,所以有必要介绍给大家做一做,感受一下。
+## 补充
+
+这里提供一种与[452.用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)、[435.无重叠区间](https://programmercarl.com/0435.无重叠区间.html)相同的思路。
+
+统计字符串中所有字符的起始和结束位置,记录这些区间(实际上也就是[435.无重叠区间](https://programmercarl.com/0435.无重叠区间.html)题目里的输入),**将区间按左边界从小到大排序,找到边界将区间划分成组,互不重叠。找到的边界就是答案。**
+
+```CPP
+class Solution {
+public:
+ static bool cmp(vector &a, vector &b) {
+ return a[0] < b[0];
+ }
+ // 记录每个字母出现的区间
+ void countLabels(string s, vector> &hash) {
+ for (int i = 0; i < s.size(); ++i) {
+ if (hash[s[i] - 'a'][0] == INT_MIN) {
+ hash[s[i] - 'a'][0] = i;
+ }
+ hash[s[i] - 'a'][1] = i;
+ }
+ }
+ vector partitionLabels(string s) {
+ vector res;
+ vector> hash(26, vector(2, INT_MIN));
+ countLabels(s, hash);
+ // 按照左边界从小到大排序
+ sort(hash.begin(), hash.end(), cmp);
+ // 记录最大右边界
+ int rightBoard = INT_MIN;
+ int leftBoard = 0;
+ for (int i = 0; i < hash.size(); ++i) {
+ // 过滤掉字符串中没有的字母
+ if (hash[i][0] == INT_MIN) {
+ continue;
+ }
+ // 一旦下一区间左边界大于当前右边界,即可认为出现分割点
+ if (rightBoard != INT_MIN && hash[i][0] > rightBoard) {
+ res.push_back(rightBoard - leftBoard + 1);
+ leftBoard = hash[i][0];
+ }
+ rightBoard = max(rightBoard, hash[i][1]);
+ }
+ res.push_back(rightBoard - leftBoard + 1);
+ return res;
+ }
+};
+```
## 其他语言版本
From 3048b00d72146838275c37ec549b76eeca8813a9 Mon Sep 17 00:00:00 2001
From: speed
Date: Thu, 7 Apr 2022 10:52:27 +0800
Subject: [PATCH 044/551] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=860763.?=
=?UTF-8?q?=E5=88=92=E5=88=86=E5=AD=97=E6=AF=8D=E5=8C=BA=E9=97=B4.md=20?=
=?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=B4=AA=E5=BF=83=E6=80=9D=E8=B7=AF=E7=9A=84?=
=?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...27\346\257\215\345\214\272\351\227\264.md" | 28 ++++++++++++-------
1 file changed, 18 insertions(+), 10 deletions(-)
diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
index d3958bd5e6..d350f2552a 100644
--- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
+++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
@@ -90,35 +90,43 @@ public:
return a[0] < b[0];
}
// 记录每个字母出现的区间
- void countLabels(string s, vector> &hash) {
+ vector> countLabels(string s) {
+ vector> hash(26, vector(2, INT_MIN));
+ vector> hash_filter;
for (int i = 0; i < s.size(); ++i) {
if (hash[s[i] - 'a'][0] == INT_MIN) {
hash[s[i] - 'a'][0] = i;
}
hash[s[i] - 'a'][1] = i;
}
+ // 去除字符串中未出现的字母所占用区间
+ for (int i = 0; i < hash.size(); ++i) {
+ if (hash[i][0] != INT_MIN) {
+ hash_filter.push_back(hash[i]);
+ }
+ }
+ return hash_filter;
}
vector partitionLabels(string s) {
vector res;
- vector> hash(26, vector(2, INT_MIN));
- countLabels(s, hash);
+ // 这一步得到的 hash 即为无重叠区间题意中的输入样例格式:区间列表
+ // 只不过现在我们要求的是区间分割点
+ vector> hash = countLabels(s);
// 按照左边界从小到大排序
sort(hash.begin(), hash.end(), cmp);
// 记录最大右边界
- int rightBoard = INT_MIN;
+ int rightBoard = hash[0][1];
int leftBoard = 0;
- for (int i = 0; i < hash.size(); ++i) {
- // 过滤掉字符串中没有的字母
- if (hash[i][0] == INT_MIN) {
- continue;
- }
+ for (int i = 1; i < hash.size(); ++i) {
+ // 由于字符串一定能分割,因此,
// 一旦下一区间左边界大于当前右边界,即可认为出现分割点
- if (rightBoard != INT_MIN && hash[i][0] > rightBoard) {
+ if (hash[i][0] > rightBoard) {
res.push_back(rightBoard - leftBoard + 1);
leftBoard = hash[i][0];
}
rightBoard = max(rightBoard, hash[i][1]);
}
+ // 最右端
res.push_back(rightBoard - leftBoard + 1);
return res;
}
From 3e856447424fd1a4cd37ed7b54331fea33fec588 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 7 Apr 2022 15:25:48 +0800
Subject: [PATCH 045/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880045.?=
=?UTF-8?q?=E8=B7=B3=E8=B7=83=E6=B8=B8=E6=88=8FII.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\350\267\203\346\270\270\346\210\217II.md" | 21 +++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
index 7a3f048c60..4caff04246 100644
--- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
+++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
@@ -250,6 +250,27 @@ var jump = function(nums) {
};
```
+### TypeScript
+
+```typescript
+function jump(nums: number[]): number {
+ const length: number = nums.length;
+ let curFarthestIndex: number = 0,
+ nextFarthestIndex: number = 0;
+ let curIndex: number = 0;
+ let stepNum: number = 0;
+ while (curIndex < length - 1) {
+ nextFarthestIndex = Math.max(nextFarthestIndex, curIndex + nums[curIndex]);
+ if (curIndex === curFarthestIndex) {
+ curFarthestIndex = nextFarthestIndex;
+ stepNum++;
+ }
+ curIndex++;
+ }
+ return stepNum;
+};
+```
+
From 38deed9e52058872eb522a8d629a265ae78df1e2 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 7 Apr 2022 15:58:32 +0800
Subject: [PATCH 046/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881005.K?=
=?UTF-8?q?=E6=AC=A1=E5=8F=96=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96?=
=?UTF-8?q?=E7=9A=84=E6=95=B0=E7=BB=84=E5=92=8C.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\346\225\260\347\273\204\345\222\214.md" | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md"
index 45f186e2b9..80c47147e5 100644
--- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md"
+++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md"
@@ -211,5 +211,29 @@ var largestSumAfterKNegations = function(nums, k) {
};
```
+### TypeScript
+
+```typescript
+function largestSumAfterKNegations(nums: number[], k: number): number {
+ nums.sort((a, b) => Math.abs(b) - Math.abs(a));
+ let curIndex: number = 0;
+ const length = nums.length;
+ while (curIndex < length && k > 0) {
+ if (nums[curIndex] < 0) {
+ nums[curIndex] *= -1;
+ k--;
+ }
+ curIndex++;
+ }
+ while (k > 0) {
+ nums[length - 1] *= -1;
+ k--;
+ }
+ return nums.reduce((pre, cur) => pre + cur, 0);
+};
+```
+
+
+
-----------------------
From 328b447cf44335883ce51109859f6f801e2515a0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E6=A2=A6=E6=B4=81?= <3224935686@qq.com>
Date: Thu, 7 Apr 2022 21:16:00 +0800
Subject: [PATCH 047/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0108.=E5=B0=86?=
=?UTF-8?q?=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA?=
=?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md):=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0JavaScript=E8=BF=AD=E4=BB=A3=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...11\346\220\234\347\264\242\346\240\221.md" | 40 ++++++++++++++++++-
1 file changed, 39 insertions(+), 1 deletion(-)
diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
index bd5915fd31..6ee3947bec 100644
--- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
+++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
@@ -355,6 +355,7 @@ func sortedArrayToBST(nums []int) *TreeNode {
```
## JavaScript
+递归
```javascript
var sortedArrayToBST = function (nums) {
@@ -372,7 +373,44 @@ var sortedArrayToBST = function (nums) {
return buildTree(nums, 0, nums.length - 1);
};
```
-
+迭代
+```JavaScript
+var sortedArrayToBST = function(nums) {
+ if(nums.length===0){
+ return null;
+ }
+ let root=new TreeNode(0); //初始根节点
+ let nodeQue=[root]; //放遍历的节点,并初始化
+ let leftQue=[0]; //放左区间的下标,初始化
+ let rightQue=[nums.length-1]; // 放右区间的下标
+
+ while(nodeQue.length){
+ let curNode=nodeQue.pop();
+ let left=leftQue.pop();
+ let right=rightQue.pop();
+ let mid=left+Math.floor((right-left)/2);
+
+ curNode.val=nums[mid]; //将下标为mid的元素给中间节点
+
+// 处理左区间
+ if(left<=mid-1){
+ curNode.left=new TreeNode(0);
+ nodeQue.push(curNode.left);
+ leftQue.push(left);
+ rightQue.push(mid-1);
+ }
+
+// 处理右区间
+ if(right>=mid+1){
+ curNode.right=new TreeNode(0);
+ nodeQue.push(curNode.right);
+ leftQue.push(mid+1);
+ rightQue.push(right);
+ }
+ }
+ return root;
+};
+```
## TypeScript
```typescript
From 82df90fd14fc8897559256a65d3ba53258daf262 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 7 Apr 2022 23:58:15 +0800
Subject: [PATCH 048/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880134.?=
=?UTF-8?q?=E5=8A=A0=E6=B2=B9=E7=AB=99.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4.\345\212\240\346\262\271\347\253\231.md" | 45 ++++++++++++++++++-
1 file changed, 44 insertions(+), 1 deletion(-)
diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md"
index 1062a91c2d..d3b3d4539b 100644
--- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md"
+++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md"
@@ -235,7 +235,7 @@ class Solution {
return index;
}
}
-```
+```
### Python
```python
@@ -340,7 +340,50 @@ var canCompleteCircuit = function(gas, cost) {
};
```
+### TypeScript
+
+**暴力法:**
+
+```typescript
+function canCompleteCircuit(gas: number[], cost: number[]): number {
+ for (let i = 0, length = gas.length; i < length; i++) {
+ let curSum: number = 0;
+ let index: number = i;
+ while (curSum >= 0 && index < i + length) {
+ let tempIndex: number = index % length;
+ curSum += gas[tempIndex] - cost[tempIndex];
+ index++;
+ }
+ if (index === i + length && curSum >= 0) return i;
+ }
+ return -1;
+};
+```
+
+**解法二:**
+
+```typescript
+function canCompleteCircuit(gas: number[], cost: number[]): number {
+ let total: number = 0;
+ let curGas: number = 0;
+ let tempDiff: number = 0;
+ let resIndex: number = 0;
+ for (let i = 0, length = gas.length; i < length; i++) {
+ tempDiff = gas[i] - cost[i];
+ total += tempDiff;
+ curGas += tempDiff;
+ if (curGas < 0) {
+ resIndex = i + 1;
+ curGas = 0;
+ }
+ }
+ if (total < 0) return -1;
+ return resIndex;
+};
+```
+
### C
+
```c
int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){
int curSum = 0;
From 1496b59646e00b189bd3586145e1474d593b65cb Mon Sep 17 00:00:00 2001
From: mxdneu
Date: Fri, 8 Apr 2022 00:17:47 +0800
Subject: [PATCH 049/551] =?UTF-8?q?fix=20js=E8=9E=BA=E6=97=8B=E6=95=B0?=
=?UTF-8?q?=E7=BB=84?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
index 5c679982ce..a7b19a34fe 100644
--- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
+++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
@@ -246,11 +246,11 @@ var generateMatrix = function(n) {
res[row][col] = count++;
}
// 下行从右到左(左闭右开)
- for (; col > startX; col--) {
+ for (; col > startY; col--) {
res[row][col] = count++;
}
// 左列做下到上(左闭右开)
- for (; row > startY; row--) {
+ for (; row > startX; row--) {
res[row][col] = count++;
}
From 0ecc3bd7cfdea391e9725030442cad617c277989 Mon Sep 17 00:00:00 2001
From: sanwulol
Date: Fri, 8 Apr 2022 17:00:59 +0800
Subject: [PATCH 050/551] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=80=900122-?=
=?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3?=
=?UTF-8?q?=E6=97=B6=E6=9C=BAII.md=E3=80=91javaScript=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...32\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
index 83b852c6c0..1e6dc7c0ae 100644
--- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
+++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
@@ -264,7 +264,7 @@ const maxProfit = (prices) => {
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i]);
}
- return dp[prices.length -1][0];
+ return dp[prices.length -1][1];
};
```
From ff135662de4d1f282ead7f76c17a35806d99259e Mon Sep 17 00:00:00 2001
From: sanwulol
Date: Fri, 8 Apr 2022 17:01:19 +0800
Subject: [PATCH 051/551] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=80=900122-?=
=?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3?=
=?UTF-8?q?=E6=97=B6=E6=9C=BAII.md(=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92)?=
=?UTF-8?q?=E3=80=91javaScript=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
index 615d79bbcd..5a165a1438 100644
--- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
+++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
@@ -276,7 +276,7 @@ const maxProfit = (prices) => {
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i]);
}
- return dp[prices.length -1][0];
+ return dp[prices.length -1][1];
};
// 方法二:动态规划(滚动数组)
From ccfa2c495fc5c7762517dc990a2fe58328a3292a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=AD=9F=E4=BB=A4=E4=BB=A4?=
Date: Fri, 8 Apr 2022 18:02:30 +0800
Subject: [PATCH 052/551] =?UTF-8?q?Update=200028.=E5=AE=9E=E7=8E=B0strStr.?=
=?UTF-8?q?md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 swift 前缀表两种实现方法
---
.../0028.\345\256\236\347\216\260strStr.md" | 107 ++++++++++++++++++
1 file changed, 107 insertions(+)
diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md"
index 634d8535a2..d67e5f70fc 100644
--- "a/problems/0028.\345\256\236\347\216\260strStr.md"
+++ "b/problems/0028.\345\256\236\347\216\260strStr.md"
@@ -1059,5 +1059,112 @@ func getNext(_ next: inout [Int], needle: [Character]) {
```
+> 前缀表右移
+
+```swift
+func strStr(_ haystack: String, _ needle: String) -> Int {
+
+ let s = Array(haystack), p = Array(needle)
+ guard p.count != 0 else { return 0 }
+
+ var j = 0
+ var next = [Int].init(repeating: 0, count: p.count)
+ getNext(&next, p)
+
+ for i in 0 ..< s.count {
+
+ while j > 0 && s[i] != p[j] {
+ j = next[j]
+ }
+
+ if s[i] == p[j] {
+ j += 1
+ }
+
+ if j == p.count {
+ return i - p.count + 1
+ }
+ }
+
+ return -1
+ }
+
+ // 前缀表后移一位,首位用 -1 填充
+ func getNext(_ next: inout [Int], _ needle: [Character]) {
+
+ guard needle.count > 1 else { return }
+
+ var j = 0
+ next[0] = j
+
+ for i in 1 ..< needle.count-1 {
+
+ while j > 0 && needle[i] != needle[j] {
+ j = next[j-1]
+ }
+
+ if needle[i] == needle[j] {
+ j += 1
+ }
+
+ next[i] = j
+ }
+ next.removeLast()
+ next.insert(-1, at: 0)
+ }
+```
+
+> 前缀表统一不减一
+```swift
+
+func strStr(_ haystack: String, _ needle: String) -> Int {
+
+ let s = Array(haystack), p = Array(needle)
+ guard p.count != 0 else { return 0 }
+
+ var j = 0
+ var next = [Int](repeating: 0, count: needle.count)
+ // KMP
+ getNext(&next, needle: p)
+
+ for i in 0 ..< s.count {
+ while j > 0 && s[i] != p[j] {
+ j = next[j-1]
+ }
+
+ if s[i] == p[j] {
+ j += 1
+ }
+
+ if j == p.count {
+ return i - p.count + 1
+ }
+ }
+ return -1
+ }
+
+ //前缀表
+ func getNext(_ next: inout [Int], needle: [Character]) {
+
+ var j = 0
+ next[0] = j
+
+ for i in 1 ..< needle.count {
+
+ while j>0 && needle[i] != needle[j] {
+ j = next[j-1]
+ }
+
+ if needle[i] == needle[j] {
+ j += 1
+ }
+
+ next[i] = j
+
+ }
+ }
+
+```
+
-----------------------
From 559f03c92869ca55ae929d66de872f5ddda70567 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=AD=9F=E4=BB=A4=E4=BB=A4?=
Date: Fri, 8 Apr 2022 18:11:05 +0800
Subject: [PATCH 053/551] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D?=
=?UTF-8?q?=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...20\345\255\227\347\254\246\344\270\262.md" | 43 +++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
index ccfb485cbb..6a9b4260ac 100644
--- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
@@ -421,5 +421,48 @@ function repeatedSubstringPattern(s: string): boolean {
};
```
+
+Swift:
+
+> 前缀表统一减一
+```swift
+ func repeatedSubstringPattern(_ s: String) -> Bool {
+
+ let sArr = Array(s)
+ let len = s.count
+ if len == 0 {
+ return false
+ }
+ var next = Array.init(repeating: -1, count: len)
+
+ getNext(&next,sArr)
+
+ if next.last != -1 && len % (len - (next[len-1] + 1)) == 0{
+ return true
+ }
+
+ return false
+ }
+
+ func getNext(_ next: inout [Int], _ str:[Character]) {
+
+ var j = -1
+ next[0] = j
+
+ for i in 1 ..< str.count {
+
+ while j >= 0 && str[j+1] != str[i] {
+ j = next[j]
+ }
+
+ if str[i] == str[j+1] {
+ j += 1
+ }
+
+ next[i] = j
+ }
+ }
+```
+
-----------------------
From 278bcb67b8250c0cea57b6a8f970e007d1259f23 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 8 Apr 2022 20:28:24 +0800
Subject: [PATCH 054/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880135.?=
=?UTF-8?q?=E5=88=86=E5=8F=91=E7=B3=96=E6=9E=9C.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...06\345\217\221\347\263\226\346\236\234.md" | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md"
index ccdabc16b4..b8bdae0ee1 100644
--- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md"
+++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md"
@@ -238,6 +238,32 @@ var candy = function(ratings) {
};
```
+### TypeScript
+
+```typescript
+function candy(ratings: number[]): number {
+ const candies: number[] = [];
+ candies[0] = 1;
+ // 保证右边高分孩子一定比左边低分孩子发更多的糖果
+ for (let i = 1, length = ratings.length; i < length; i++) {
+ if (ratings[i] > ratings[i - 1]) {
+ candies[i] = candies[i - 1] + 1;
+ } else {
+ candies[i] = 1;
+ }
+ }
+ // 保证左边高分孩子一定比右边低分孩子发更多的糖果
+ for (let i = ratings.length - 2; i >= 0; i--) {
+ if (ratings[i] > ratings[i + 1]) {
+ candies[i] = Math.max(candies[i], candies[i + 1] + 1);
+ }
+ }
+ return candies.reduce((pre, cur) => pre + cur);
+};
+```
+
+
+
-----------------------
From 2f5996de0d7bcb91c615dbe9ff8e07cb339a1e42 Mon Sep 17 00:00:00 2001
From: Austin <40263822+LookCos@users.noreply.github.com>
Date: Fri, 8 Apr 2022 21:01:24 +0800
Subject: [PATCH 055/551] =?UTF-8?q?Update=200416.=E5=88=86=E5=89=B2?=
=?UTF-8?q?=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
错别字。
---
...\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
index c8d9bc0452..70f0cb518e 100644
--- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
+++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
@@ -50,7 +50,7 @@
## 01背包问题
-背包问题,大家都知道,有N件物品和一个最多能被重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。
+背包问题,大家都知道,有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。
**背包问题有多种背包方式,常见的有:01背包、完全背包、多重背包、分组背包和混合背包等等。**
From 2f3f35c5727cb9469e3c186f26217d8cb053e242 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 8 Apr 2022 21:33:51 +0800
Subject: [PATCH 056/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880860.?=
=?UTF-8?q?=E6=9F=A0=E6=AA=AC=E6=B0=B4=E6=89=BE=E9=9B=B6.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...54\346\260\264\346\211\276\351\233\266.md" | 34 +++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md"
index f48ecf4d60..026c2e6384 100644
--- "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md"
+++ "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md"
@@ -254,5 +254,39 @@ var lemonadeChange = function(bills) {
```
+### TypeScript
+
+```typescript
+function lemonadeChange(bills: number[]): boolean {
+ let five: number = 0,
+ ten: number = 0;
+ for (let bill of bills) {
+ switch (bill) {
+ case 5:
+ five++;
+ break;
+ case 10:
+ if (five < 1) return false;
+ five--;
+ ten++
+ break;
+ case 20:
+ if (ten > 0 && five > 0) {
+ five--;
+ ten--;
+ } else if (five > 2) {
+ five -= 3;
+ } else {
+ return false;
+ }
+ break;
+ }
+ }
+ return true;
+};
+```
+
+
+
-----------------------
From 0e1cbda7153b3b5b2892110696f10afe7833a38f Mon Sep 17 00:00:00 2001
From: "Neil.Liu" <88214924@qq.com>
Date: Fri, 8 Apr 2022 23:56:49 +0800
Subject: [PATCH 057/551] =?UTF-8?q?Update=200121.=E5=8D=96=E8=82=A1?=
=?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA.md=20?=
=?UTF-8?q?=E8=B4=AA=E5=BF=83=E4=BB=A5=E5=8F=8A=E5=8A=A8=E6=80=81=E8=A7=84?=
=?UTF-8?q?=E5=88=92=20Go=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...00\344\275\263\346\227\266\346\234\272.md" | 52 +++++++++++++++++++
1 file changed, 52 insertions(+)
diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md"
index e7c0ac65ba..f0bc3b9764 100644
--- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md"
+++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md"
@@ -311,7 +311,36 @@ class Solution:
```
Go:
+> 贪心法:
+```Go
+func maxProfit(prices []int) int {
+ low := math.MaxInt32
+ rlt := 0
+ for i := range prices{
+ low = min(low, prices[i])
+ rlt = max(rlt, prices[i]-low)
+ }
+
+ return rlt
+}
+func min(a, b int) int {
+ if a < b{
+ return a
+ }
+
+ return b
+}
+
+func max(a, b int) int {
+ if a > b{
+ return a
+ }
+
+ return b
+}
+```
+> 动态规划:版本一
```Go
func maxProfit(prices []int) int {
length:=len(prices)
@@ -338,6 +367,29 @@ func max(a,b int)int {
}
```
+> 动态规划:版本二
+```Go
+func maxProfit(prices []int) int {
+ dp := [2][2]int{}
+ dp[0][0] = -prices[0]
+ dp[0][1] = 0
+ for i := 1; i < len(prices); i++{
+ dp[i%2][0] = max(dp[(i-1)%2][0], -prices[i])
+ dp[i%2][1] = max(dp[(i-1)%2][1], dp[(i-1)%2][0]+prices[i])
+ }
+
+ return dp[(len(prices)-1)%2][1]
+}
+
+func max(a, b int) int {
+ if a > b{
+ return a
+ }
+
+ return b
+}
+```
+
JavaScript:
> 动态规划
From 888bd8fe0b2320ffd0afe4cfe014e79116d23944 Mon Sep 17 00:00:00 2001
From: dmzlingyin
Date: Sat, 9 Apr 2022 00:12:46 +0800
Subject: [PATCH 058/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(ACM=E6=A8=A1?=
=?UTF-8?q?=E5=BC=8F=E5=A6=82=E4=BD=95=E6=9E=84=E5=BB=BA=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91.md):=20Go=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...72\344\272\214\345\217\211\346\240\221.md" | 66 +++++++++++++++++++
1 file changed, 66 insertions(+)
diff --git "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md"
index bd2e978046..dba87ca113 100644
--- "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md"
@@ -220,6 +220,72 @@ int main() {
## Go
```Go
+package main
+
+import "fmt"
+
+type TreeNode struct {
+ Val int
+ Left *TreeNode
+ Right *TreeNode
+}
+
+func constructBinaryTree(array []int) *TreeNode {
+ var root *TreeNode
+ nodes := make([]*TreeNode, len(array))
+
+ // 初始化二叉树节点
+ for i := 0; i < len(nodes); i++ {
+ var node *TreeNode
+ if array[i] != -1 {
+ node = &TreeNode{Val: array[i]}
+ }
+ nodes[i] = node
+ if i == 0 {
+ root = node
+ }
+ }
+ // 串联节点
+ for i := 0; i*2+2 < len(array); i++ {
+ if nodes[i] != nil {
+ nodes[i].Left = nodes[i*2+1]
+ nodes[i].Right = nodes[i*2+2]
+ }
+ }
+ return root
+}
+
+func printBinaryTree(root *TreeNode, n int) {
+ var queue []*TreeNode
+ if root != nil {
+ queue = append(queue, root)
+ }
+
+ result := []int{}
+ for len(queue) > 0 {
+ for j := 0; j < len(queue); j++ {
+ node := queue[j]
+ if node != nil {
+ result = append(result, node.Val)
+ queue = append(queue, node.Left)
+ queue = append(queue, node.Right)
+ } else {
+ result = append(result, -1)
+ }
+ }
+ // 清除队列中的本层节点, 进入下一层遍历
+ queue = queue[len(queue):]
+ }
+ // 参数n控制输出值数量, 否则二叉树最后一层叶子节点的孩子节点也会被打印(但是这些孩子节点是不存在的).
+ fmt.Println(result[:n])
+}
+
+func main() {
+ array := []int{4, 1, 6, 0, 2, 5, 7, -1, -1, -1, 3, -1, -1, -1, 8}
+ root := constructBinaryTree(array)
+ printBinaryTree(root, len(array))
+}
+
```
## JavaScript
From 7ad56a33e0cb2313fd0bdaaaf1bccb26f6b09b1f Mon Sep 17 00:00:00 2001
From: lingyin <32977462+dmzlingyin@users.noreply.github.com>
Date: Sat, 9 Apr 2022 00:19:52 +0800
Subject: [PATCH 059/551] =?UTF-8?q?Update=20ACM=E6=A8=A1=E5=BC=8F=E5=A6=82?=
=?UTF-8?q?=E4=BD=95=E6=9E=84=E5=BB=BA=E4=BA=8C=E5=8F=89=E6=A0=91.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...72\344\272\214\345\217\211\346\240\221.md" | 95 ++++++++++---------
1 file changed, 48 insertions(+), 47 deletions(-)
diff --git "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md"
index dba87ca113..444f00719e 100644
--- "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md"
@@ -225,65 +225,66 @@ package main
import "fmt"
type TreeNode struct {
- Val int
- Left *TreeNode
- Right *TreeNode
+ Val int
+ Left *TreeNode
+ Right *TreeNode
}
func constructBinaryTree(array []int) *TreeNode {
- var root *TreeNode
- nodes := make([]*TreeNode, len(array))
-
- // 初始化二叉树节点
- for i := 0; i < len(nodes); i++ {
- var node *TreeNode
- if array[i] != -1 {
- node = &TreeNode{Val: array[i]}
- }
- nodes[i] = node
- if i == 0 {
- root = node
- }
+ var root *TreeNode
+ nodes := make([]*TreeNode, len(array))
+
+ // 初始化二叉树节点
+ for i := 0; i < len(nodes); i++ {
+ var node *TreeNode
+ if array[i] != -1 {
+ node = &TreeNode{Val: array[i]}
}
- // 串联节点
- for i := 0; i*2+2 < len(array); i++ {
- if nodes[i] != nil {
- nodes[i].Left = nodes[i*2+1]
- nodes[i].Right = nodes[i*2+2]
- }
+ nodes[i] = node
+ if i == 0 {
+ root = node
}
- return root
+ }
+ // 串联节点
+ for i := 0; i*2+2 < len(array); i++ {
+ if nodes[i] != nil {
+ nodes[i].Left = nodes[i*2+1]
+ nodes[i].Right = nodes[i*2+2]
+ }
+ }
+ return root
}
func printBinaryTree(root *TreeNode, n int) {
- var queue []*TreeNode
- if root != nil {
- queue = append(queue, root)
- }
+ var queue []*TreeNode
+ if root != nil {
+ queue = append(queue, root)
+ }
- result := []int{}
- for len(queue) > 0 {
- for j := 0; j < len(queue); j++ {
- node := queue[j]
- if node != nil {
- result = append(result, node.Val)
- queue = append(queue, node.Left)
- queue = append(queue, node.Right)
- } else {
- result = append(result, -1)
- }
- }
- // 清除队列中的本层节点, 进入下一层遍历
- queue = queue[len(queue):]
- }
- // 参数n控制输出值数量, 否则二叉树最后一层叶子节点的孩子节点也会被打印(但是这些孩子节点是不存在的).
- fmt.Println(result[:n])
+ result := []int{}
+ for len(queue) > 0 {
+ for j := 0; j < len(queue); j++ {
+ node := queue[j]
+ if node != nil {
+ result = append(result, node.Val)
+ queue = append(queue, node.Left)
+ queue = append(queue, node.Right)
+ } else {
+ result = append(result, -1)
+ }
+ }
+ // 清除队列中的本层节点, 进入下一层遍历
+ queue = queue[len(queue):]
+ }
+
+ // 参数n控制输出值数量, 否则二叉树最后一层叶子节点的孩子节点也会被打印(但是这些孩子节点是不存在的).
+ fmt.Println(result[:n])
}
func main() {
- array := []int{4, 1, 6, 0, 2, 5, 7, -1, -1, -1, 3, -1, -1, -1, 8}
- root := constructBinaryTree(array)
- printBinaryTree(root, len(array))
+ array := []int{4, 1, 6, 0, 2, 5, 7, -1, -1, -1, 3, -1, -1, -1, 8}
+ root := constructBinaryTree(array)
+ printBinaryTree(root, len(array))
}
```
From 2e18054079ecfa1147bfc4335fd6c62609a44c37 Mon Sep 17 00:00:00 2001
From: jonathanx111 <57882619+jonathanx111@users.noreply.github.com>
Date: Sat, 9 Apr 2022 13:49:55 -0400
Subject: [PATCH 060/551] =?UTF-8?q?=E7=BA=A0=E6=AD=A30027=E7=9A=84Swift?=
=?UTF-8?q?=E7=AD=94=E6=A1=88?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Swift答案写错了。改成对的答案。
---
.../0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
index 8d6ca5022d..590cf0b924 100644
--- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
+++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
@@ -281,10 +281,8 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
for fastIndex in 0..
Date: Sun, 10 Apr 2022 10:17:10 +0800
Subject: [PATCH 061/551] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BA=86=E6=9D=A5?=
=?UTF-8?q?=E8=87=AAMac=E7=9A=84.DS=5FStore?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pics/.DS_Store | Bin 6148 -> 0 bytes
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 pics/.DS_Store
diff --git a/pics/.DS_Store b/pics/.DS_Store
deleted file mode 100644
index 5008ddfcf53c02e82d7eee2e57c38e5672ef89f6..0000000000000000000000000000000000000000
GIT binary patch
literal 0
HcmV?d00001
literal 6148
zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3
zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ
zLs35+`xjp>T0
Date: Sun, 10 Apr 2022 14:56:56 +0800
Subject: [PATCH 062/551] =?UTF-8?q?Update=200018.=E5=9B=9B=E6=95=B0?=
=?UTF-8?q?=E4=B9=8B=E5=92=8C.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
增加剪枝逻辑处理
---
...33\233\346\225\260\344\271\213\345\222\214.md" | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
index 7304254e54..ee70cb6909 100644
--- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
@@ -31,7 +31,7 @@
四数之和,和[15.三数之和](https://programmercarl.com/0015.三数之和.html)是一个思路,都是使用双指针法, 基本解法就是在[15.三数之和](https://programmercarl.com/0015.三数之和.html) 的基础上再套一层for循环。
-但是有一些细节需要注意,例如: 不要判断`nums[k] > target` 就返回了,三数之和 可以通过 `nums[i] > 0` 就返回了,因为 0 已经是确定的数了,四数之和这道题目 target是任意值。(大家亲自写代码就能感受出来)
+但是有一些细节需要注意,例如: 不要判断`nums[k] > target` 就返回了,三数之和 可以通过 `nums[i] > 0` 就返回了,因为 0 已经是确定的数了,四数之和这道题目 target是任意值。比如:数组是`[-4, -3, -2, -1]`,`target`是`-10`,不能因为`-4 > -10`而跳过。但是我们依旧可以去做剪枝,逻辑变成`nums[i] > target && (nums[i] >=0 || target >= 0)`就可以了。
[15.三数之和](https://programmercarl.com/0015.三数之和.html)的双指针解法是一层for循环num[i]为确定值,然后循环内有left和right下标作为双指针,找到nums[i] + nums[left] + nums[right] == 0。
@@ -72,15 +72,20 @@ public:
vector> result;
sort(nums.begin(), nums.end());
for (int k = 0; k < nums.size(); k++) {
- // 这种剪枝是错误的,这道题目target 是任意值
- // if (nums[k] > target) {
- // return result;
- // }
+ // 剪枝处理
+ if (nums[k] > target && (nums[k] >= 0 || target >= 0)) {
+ break; // 这里使用break,统一通过最后的return返回
+ }
// 去重
if (k > 0 && nums[k] == nums[k - 1]) {
continue;
}
for (int i = k + 1; i < nums.size(); i++) {
+ // 2级剪枝处理
+ if (nums[k] + nums[i] > target && (nums[k] + nums[i] >= 0 || target >= 0)) {
+ break;
+ }
+
// 正确去重方法
if (i > k + 1 && nums[i] == nums[i - 1]) {
continue;
From 175b71d21fc5e3bdddc21e5dafe8a73980d79016 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 10 Apr 2022 21:59:21 +0800
Subject: [PATCH 063/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880406.?=
=?UTF-8?q?=E6=A0=B9=E6=8D=AE=E8=BA=AB=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F?=
=?UTF-8?q?=E5=88=97.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...215\345\273\272\351\230\237\345\210\227.md" | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md"
index b2354d094c..ecb0530194 100644
--- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md"
+++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md"
@@ -290,6 +290,24 @@ var reconstructQueue = function(people) {
};
```
+### TypeScript
+
+```typescript
+function reconstructQueue(people: number[][]): number[][] {
+ people.sort((a, b) => {
+ if (a[0] === b[0]) return a[1] - b[1];
+ return b[0] - a[0];
+ });
+ const resArr: number[][] = [];
+ for (let i = 0, length = people.length; i < length; i++) {
+ resArr.splice(people[i][1], 0, people[i]);
+ }
+ return resArr;
+};
+```
+
+
+
-----------------------
From ef44e750cec20980095e63db3c090c8df48f620c Mon Sep 17 00:00:00 2001
From: Guanzhong Pan
Date: Mon, 11 Apr 2022 20:34:32 +0100
Subject: [PATCH 064/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200376.=E6=91=86?=
=?UTF-8?q?=E5=8A=A8=E5=BA=8F=E5=88=97.md=20C=E8=AF=AD=E8=A8=80?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...06\345\212\250\345\272\217\345\210\227.md" | 27 +++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
index 5076c9adaa..6bd2277ffe 100644
--- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
+++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
@@ -298,5 +298,32 @@ var wiggleMaxLength = function(nums) {
};
```
+### C
+**贪心**
+```c
+int wiggleMaxLength(int* nums, int numsSize){
+ if(numsSize <= 1)
+ return numsSize;
+
+ int length = 1;
+ int preDiff , curDiff;
+ preDiff = curDiff = 0;
+ for(int i = 0; i < numsSize - 1; ++i) {
+ // 计算当前i元素与i+1元素差值
+ curDiff = nums[i+1] - nums[i];
+
+ // 若preDiff与curDiff符号不符,则子序列长度+1。更新preDiff的符号
+ // 若preDiff与curDiff符号一致,当前i元素为连续升序/连续降序子序列的中间元素。不被记录入长度
+ // 注:当preDiff为0时,curDiff为正或为负都属于符号不同
+ if((curDiff > 0 && preDiff <= 0) || (preDiff >= 0 && curDiff < 0)) {
+ preDiff = curDiff;
+ length++;
+ }
+ }
+
+ return length;
+}
+```
+
-----------------------
From ae38a29068241636052e831d96b1e446dddd979e Mon Sep 17 00:00:00 2001
From: Jamcy123 <1219502823@qq.com>
Date: Tue, 12 Apr 2022 16:57:25 +0800
Subject: [PATCH 065/551] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200503.=E4=B8=8B?=
=?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II=20java?=
=?UTF-8?q?script=20=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\264\345\244\247\345\205\203\347\264\240II.md" | 15 +++++++--------
1 file changed, 7 insertions(+), 8 deletions(-)
diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md"
index 36e183e1b1..d6d3850fd5 100644
--- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md"
+++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md"
@@ -166,20 +166,19 @@ JavaScript:
* @return {number[]}
*/
var nextGreaterElements = function (nums) {
- // let map = new Map();
+ const len = nums.length;
let stack = [];
- let res = new Array(nums.length).fill(-1);
- for (let i = 0; i < nums.length * 2; i++) {
+ let res = Array(len).fill(-1);
+ for (let i = 0; i < len * 2; i++) {
while (
stack.length &&
- nums[i % nums.length] > nums[stack[stack.length - 1]]
+ nums[i % len] > nums[stack[stack.length - 1]]
) {
- let index = stack.pop();
- res[index] = nums[i % nums.length];
+ const index = stack.pop();
+ res[index] = nums[i % len];
}
- stack.push(i % nums.length);
+ stack.push(i % len);
}
-
return res;
};
```
From 82ab1706f88e390ff2f13478cd7771cc92e76754 Mon Sep 17 00:00:00 2001
From: Jamcy123 <1219502823@qq.com>
Date: Tue, 12 Apr 2022 17:09:20 +0800
Subject: [PATCH 066/551] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=89=88=E6=9C=AC?=
=?UTF-8?q?=E4=B8=80=E3=80=81=E5=A2=9E=E5=8A=A0=E7=89=88=E6=9C=AC=E4=BA=8C?=
=?UTF-8?q?=200739.=E6=AF=8F=E6=97=A5=E6=B8=A9=E5=BA=A6=20javascript=20?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...17\346\227\245\346\270\251\345\272\246.md" | 32 +++++++++++++------
1 file changed, 23 insertions(+), 9 deletions(-)
diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
index 710f5eb68f..5f53e4127d 100644
--- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
+++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
@@ -301,25 +301,22 @@ func dailyTemperatures(num []int) []int {
JavaScript:
```javascript
-/**
- * @param {number[]} temperatures
- * @return {number[]}
- */
+// 版本一
var dailyTemperatures = function(temperatures) {
- let n = temperatures.length;
- let res = new Array(n).fill(0);
- let stack = []; // 递减栈:用于存储元素右面第一个比他大的元素下标
+ const n = temperatures.length;
+ const res = Array(n).fill(0);
+ const stack = []; // 递增栈:用于存储元素右面第一个比他大的元素下标
stack.push(0);
for (let i = 1; i < n; i++) {
// 栈顶元素
- let top = stack[stack.length - 1];
+ const top = stack[stack.length - 1];
if (temperatures[i] < temperatures[top]) {
stack.push(i);
} else if (temperatures[i] === temperatures[top]) {
stack.push(i);
} else {
while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) {
- let top = stack.pop();
+ const top = stack.pop();
res[top] = i - top;
}
stack.push(i);
@@ -327,6 +324,23 @@ var dailyTemperatures = function(temperatures) {
}
return res;
};
+
+
+// 版本二
+var dailyTemperatures = function(temperatures) {
+ const n = temperatures.length;
+ const res = Array(n).fill(0);
+ const stack = []; // 递增栈:用于存储元素右面第一个比他大的元素下标
+ stack.push(0);
+ for (let i = 1; i < n; i++) {
+ while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) {
+ const top = stack.pop();
+ res[top] = i - top;
+ }
+ stack.push(i);
+ }
+ return res;
+};
```
From 54d0d9d1c1a579e0a8750f1be864ad78b8d13c45 Mon Sep 17 00:00:00 2001
From: Guanzhong Pan
Date: Tue, 12 Apr 2022 10:43:46 +0100
Subject: [PATCH 067/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200053.=E6=9C=80?=
=?UTF-8?q?=E5=A4=A7=E5=AD=90=E6=95=B0=E7=BB=84=E5=92=8C.md=20C=E8=AF=AD?=
=?UTF-8?q?=E8=A8=80=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...247\345\255\220\345\272\217\345\222\214.md" | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
index 3d11c91efb..9af02edf2c 100644
--- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
+++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
@@ -230,6 +230,24 @@ var maxSubArray = function(nums) {
};
```
+### C:
+```c
+int maxSubArray(int* nums, int numsSize){
+ int maxVal = INT_MIN;
+ int subArrSum = 0;
+
+ int i;
+ for(i = 0; i < numsSize; ++i) {
+ subArrSum += nums[i];
+ // 若当前局部和大于之前的最大结果,对结果进行更新
+ maxVal = subArrSum > maxVal ? subArrSum : maxVal;
+ // 若当前局部和为负,对结果无益。则从nums[i+1]开始应重新计算。
+ subArrSum = subArrSum < 0 ? 0 : subArrSum;
+ }
+
+ return maxVal;
+}
+```
-----------------------
From af724ef6d5ed95c4ba545a932f8257b487648aec Mon Sep 17 00:00:00 2001
From: xuerbujia <83055661+xuerbujia@users.noreply.github.com>
Date: Tue, 12 Apr 2022 17:45:50 +0800
Subject: [PATCH 068/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880143.?=
=?UTF-8?q?=E9=87=8D=E6=8E=92=E9=93=BE=E8=A1=A8.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0go=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...15\346\216\222\351\223\276\350\241\250.md" | 28 ++++++++++++++++++-
1 file changed, 27 insertions(+), 1 deletion(-)
diff --git "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md"
index 0062262328..790bcb482a 100644
--- "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md"
+++ "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md"
@@ -336,7 +336,33 @@ class Solution:
return pre
```
### Go
-
+```go
+# 方法三 分割链表
+func reorderList(head *ListNode) {
+ var slow=head
+ var fast=head
+ for fast!=nil&&fast.Next!=nil{
+ slow=slow.Next
+ fast=fast.Next.Next
+ } //双指针将链表分为左右两部分
+ var right =new(ListNode)
+ for slow!=nil{
+ temp:=slow.Next
+ slow.Next=right.Next
+ right.Next=slow
+ slow=temp
+ } //翻转链表右半部分
+ right=right.Next //right为反转后得右半部分
+ h:=head
+ for right.Next!=nil{
+ temp:=right.Next
+ right.Next=h.Next
+ h.Next=right
+ h=h.Next.Next
+ right=temp
+ } //将左右两部分重新组合
+}
+```
### JavaScript
```javascript
From dd63a865717b0a439cc2ce2b5abab0a55d18d7de Mon Sep 17 00:00:00 2001
From: Guanzhong Pan
Date: Tue, 12 Apr 2022 11:01:53 +0100
Subject: [PATCH 069/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200053.=E6=9C=80?=
=?UTF-8?q?=E5=A4=A7=E5=AD=90=E6=95=B0=E7=BB=84=E5=92=8C.md=20C=E8=AF=AD?=
=?UTF-8?q?=E8=A8=80=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\345\255\220\345\272\217\345\222\214.md" | 34 +++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
index 9af02edf2c..3d4459a377 100644
--- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
+++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
@@ -231,6 +231,7 @@ var maxSubArray = function(nums) {
```
### C:
+贪心:
```c
int maxSubArray(int* nums, int numsSize){
int maxVal = INT_MIN;
@@ -249,6 +250,39 @@ int maxSubArray(int* nums, int numsSize){
}
```
+动态规划:
+```c
+/**
+ * 解题思路:动态规划:
+ * 1. dp数组:dp[i]表示从0到i的子序列中最大序列和的值
+ * 2. 递推公式:dp[i] = max(dp[i-1] + nums[i], nums[i])
+ 若dp[i-1]<0,对最后结果无益。dp[i]则为nums[i]。
+ * 3. dp数组初始化:dp[0]的最大子数组和为nums[0]
+ * 4. 推导顺序:从前往后遍历
+ */
+
+#define max(a, b) (((a) > (b)) ? (a) : (b))
+
+int maxSubArray(int* nums, int numsSize){
+ int dp[numsSize];
+ // dp[0]最大子数组和为nums[0]
+ dp[0] = nums[0];
+ // 若numsSize为1,应直接返回nums[0]
+ int subArrSum = nums[0];
+
+ int i;
+ for(i = 1; i < numsSize; ++i) {
+ dp[i] = max(dp[i - 1] + nums[i], nums[i]);
+
+ // 若dp[i]大于之前记录的最大值,进行更新
+ if(dp[i] > subArrSum)
+ subArrSum = dp[i];
+ }
+
+ return subArrSum;
+}
+```
+
-----------------------
From 804d7e0171936ec370855ebb592fe123b731d8d8 Mon Sep 17 00:00:00 2001
From: programmercarl <826123027@qq.com>
Date: Tue, 12 Apr 2022 22:23:28 +0800
Subject: [PATCH 070/551] delete $
---
...\345\220\214\350\267\257\345\276\204II.md" | 4 +-
...344\275\263\346\227\266\346\234\272III.md" | 8 ++--
...25\350\257\215\346\213\206\345\210\206.md" | 2 +-
...14\347\232\204\345\215\225\350\257\215.md" | 2 +-
...64\346\225\260\346\213\206\345\210\206.md" | 4 +-
...55\347\232\204\350\212\202\347\202\271.md" | 2 +-
...\345\244\247\345\205\203\347\264\240II.md" | 2 +-
...22\345\242\236\345\272\217\345\210\227.md" | 10 ++--
...01\350\247\204\345\210\222\357\274\211.md" | 8 ++--
...04\345\255\227\347\254\246\344\270\262.md" | 2 +-
...\345\272\217\346\225\260\347\273\204II.md" | 2 +-
...57\345\244\232\345\244\247\357\274\237.md" | 2 +-
...50\350\277\231\351\207\214\357\274\201.md" | 6 +--
...50\350\277\231\351\207\214\357\274\201.md" | 46 +++++++++----------
...52\347\226\221\351\227\256\357\274\237.md" | 14 +++---
...71\346\257\233\346\261\202\347\226\265.md" | 2 +-
...02\345\272\246\345\210\206\346\236\220.md" | 26 +++++------
...15\346\235\202\345\272\246\357\274\201.md" | 24 +++++-----
...54\345\255\227\347\254\246\344\270\262.md" | 2 +-
...07\351\222\210\346\200\273\347\273\223.md" | 2 +-
...50\346\234\253\346\200\273\347\273\223.md" | 2 +-
...50\346\234\253\346\200\273\347\273\223.md" | 2 +-
...50\346\234\253\346\200\273\347\273\223.md" | 6 +--
...50\346\234\253\346\200\273\347\273\223.md" | 2 +-
...50\346\234\253\346\200\273\347\273\223.md" | 16 +++----
...06\350\256\272\345\237\272\347\241\200.md" | 14 +++---
...36\346\272\257\346\200\273\347\273\223.md" | 2 +-
...04\346\200\273\347\273\223\347\257\207.md" | 2 +-
...06\350\256\262\350\247\243\357\274\211.md" | 2 +-
...25\346\200\273\347\273\223\347\257\207.md" | 16 +------
30 files changed, 111 insertions(+), 123 deletions(-)
diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
index c71cf79649..a40ccedaa9 100644
--- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
+++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
@@ -184,8 +184,8 @@ public:
};
```
-* 时间复杂度:$O(n × m)$,n、m 分别为obstacleGrid 长度和宽度
-* 空间复杂度:$O(m)$
+* 时间复杂度:O(n × m),n、m 分别为obstacleGrid 长度和宽度
+* 空间复杂度:O(m)
## 总结
diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md"
index fc81c3e9c1..56ade34338 100644
--- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md"
+++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md"
@@ -148,8 +148,8 @@ public:
};
```
-* 时间复杂度:$O(n)$
-* 空间复杂度:$O(n × 5)$
+* 时间复杂度:O(n)
+* 空间复杂度:O(n × 5)
当然,大家可以看到力扣官方题解里的一种优化空间写法,我这里给出对应的C++版本:
@@ -173,8 +173,8 @@ public:
};
```
-* 时间复杂度:$O(n)$
-* 空间复杂度:$O(1)$
+* 时间复杂度:O(n)
+* 空间复杂度:O(1)
大家会发现dp[2]利用的是当天的dp[1]。 但结果也是对的。
diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md"
index 7f1e6f1737..ac834f047d 100644
--- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md"
+++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md"
@@ -207,7 +207,7 @@ public:
};
```
-* 时间复杂度:O(n^3),因为substr返回子串的副本是$O(n)$的复杂度(这里的n是substring的长度)
+* 时间复杂度:O(n^3),因为substr返回子串的副本是O(n)的复杂度(这里的n是substring的长度)
* 空间复杂度:O(n)
diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
index 8dfe9bbc98..4865821a53 100644
--- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
+++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
@@ -79,7 +79,7 @@ void removeExtraSpaces(string& s) {
逻辑很简单,从前向后遍历,遇到空格了就erase。
-如果不仔细琢磨一下erase的时间复杂读,还以为以上的代码是$O(n)$的时间复杂度呢。
+如果不仔细琢磨一下erase的时间复杂读,还以为以上的代码是O(n)的时间复杂度呢。
想一下真正的时间复杂度是多少,一个erase本来就是O(n)的操作,erase实现原理题目:[数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html),最优的算法来移除元素也要O(n)。
diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md"
index 777146ba78..4a7ba6ab0d 100644
--- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md"
+++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md"
@@ -119,8 +119,8 @@ public:
};
```
-* 时间复杂度:$O(n^2)$
-* 空间复杂度:$O(n)$
+* 时间复杂度:O(n^2)
+* 空间复杂度:O(n)
### 贪心
diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md"
index cc29bea4c6..e8f7e54c4b 100644
--- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md"
+++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md"
@@ -51,7 +51,7 @@ if (root == nullptr) return root;
* 确定单层递归的逻辑
-这里就把平衡二叉树中删除节点遇到的情况都搞清楚。
+这里就把二叉搜索树中删除节点遇到的情况都搞清楚。
有以下五种情况:
diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md"
index 36e183e1b1..ead31c8ba7 100644
--- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md"
+++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md"
@@ -68,7 +68,7 @@ public:
这种写法确实比较直观,但做了很多无用操作,例如修改了nums数组,而且最后还要把result数组resize回去。
-resize倒是不费时间,是$O(1)$的操作,但扩充nums数组相当于多了一个$O(n)$的操作。
+resize倒是不费时间,是O(1)的操作,但扩充nums数组相当于多了一个O(n)的操作。
其实也可以不扩充nums,而是在遍历的过程中模拟走了两边nums。
diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
index 51d04e92c7..e941d24263 100644
--- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
+++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
@@ -107,8 +107,8 @@ public:
};
```
-* 时间复杂度:$O(n)$
-* 空间复杂度:$O(n)$
+* 时间复杂度:O(n)
+* 空间复杂度:O(n)
### 贪心
@@ -135,12 +135,12 @@ public:
}
};
```
-* 时间复杂度:$O(n)$
-* 空间复杂度:$O(1)$
+* 时间复杂度:O(n)
+* 空间复杂度:O(1)
## 总结
-本题也是动规里子序列问题的经典题目,但也可以用贪心来做,大家也会发现贪心好像更简单一点,而且空间复杂度仅是$O(1)$。
+本题也是动规里子序列问题的经典题目,但也可以用贪心来做,大家也会发现贪心好像更简单一点,而且空间复杂度仅是O(1)。
在动规分析中,关键是要理解和[动态规划:300.最长递增子序列](https://programmercarl.com/0300.最长上升子序列.html)的区别。
diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
index b385d710d0..4ab63e7925 100644
--- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
+++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
@@ -38,8 +38,8 @@
使用贪心算法,的性能是:
-* 时间复杂度:$O(n)$
-* 空间复杂度:$O(1)$
+* 时间复杂度:O(n)
+* 空间复杂度:O(1)
那么我们再来看看是使用动规的方法如何解题。
@@ -87,8 +87,8 @@ public:
};
```
-* 时间复杂度:$O(n)$
-* 空间复杂度:$O(n)$
+* 时间复杂度:O(n)
+* 空间复杂度:O(n)
## 其他语言版本
diff --git "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md"
index 3bbfb73e20..41791114fa 100644
--- "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md"
@@ -71,7 +71,7 @@ public:
}
};
```
-* 时间复杂度:O(n + m),n为S的长度,m为T的长度 ,也可以理解是$O(n)$的时间复杂度
+* 时间复杂度:O(n + m),n为S的长度,m为T的长度 ,也可以理解是O(n)的时间复杂度
* 空间复杂度:O(n + m)
当然以上代码,大家可以发现有重复的逻辑处理S,处理T,可以把这块公共逻辑抽离出来,代码精简如下:
diff --git "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md"
index 4ff419d388..cb564fb683 100644
--- "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md"
+++ "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md"
@@ -112,7 +112,7 @@ public:
* 时间复杂度:O(n)
* 空间复杂度:O(1)
-这里时间复杂度并不是$O(n^2)$,因为偶数位和奇数位都只操作一次,不是n/2 * n/2的关系,而是n/2 + n/2的关系!
+这里时间复杂度并不是O(n^2),因为偶数位和奇数位都只操作一次,不是n/2 * n/2的关系,而是n/2 + n/2的关系!
## 其他语言版本
diff --git "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md"
index 75f441db11..24302b2dd9 100644
--- "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md"
+++ "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md"
@@ -148,7 +148,7 @@ O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符

-至于$O(\log n)$和$O(n^3)$ 等等这些时间复杂度在1s内可以处理的多大的数据规模,大家可以自己写一写代码去测一下了。
+至于O(log n)和O(n^3) 等等这些时间复杂度在1s内可以处理的多大的数据规模,大家可以自己写一写代码去测一下了。
# 完整测试代码
diff --git "a/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" "b/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md"
index 77fdacc4a7..a0fd6d924b 100644
--- "a/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md"
+++ "b/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md"
@@ -24,7 +24,7 @@
算法导论给出的解释:**大O用来表示上界的**,当用它作为算法的最坏情况运行时间的上界,就是对任意数据输入的运行时间的上界。
-同样算法导论给出了例子:拿插入排序来说,插入排序的时间复杂度我们都说是$O(n^2)$ 。
+同样算法导论给出了例子:拿插入排序来说,插入排序的时间复杂度我们都说是O(n^2) 。
输入数据的形式对程序运算时间是有很大影响的,在数据本来有序的情况下时间复杂度是O(n),但如果数据是逆序的话,插入排序的时间复杂度就是O(n^2),也就对于所有输入情况来说,最坏是O(n^2) 的时间复杂度,所以称插入排序的时间复杂度为O(n^2)。
@@ -44,7 +44,7 @@

-在决定使用哪些算法的时候,不是时间复杂越低的越好(因为简化后的时间复杂度忽略了常数项等等),要考虑数据规模,如果数据规模很小甚至可以用$O(n^2)$的算法比$O(n)$的更合适(在有常数项的时候)。
+在决定使用哪些算法的时候,不是时间复杂越低的越好(因为简化后的时间复杂度忽略了常数项等等),要考虑数据规模,如果数据规模很小甚至可以用O(n^2)的算法比O(n)的更合适(在有常数项的时候)。
就像上图中 O(5n^2) 和 O(100n) 在n为20之前 很明显 O(5n^2)是更优的,所花费的时间也是最少的。
@@ -125,7 +125,7 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项
如果是暴力枚举的话,时间复杂度是多少呢,是O(n^2)么?
-这里一些同学会忽略了字符串比较的时间消耗,这里并不像int 型数字做比较那么简单,除了n^2次的遍历次数外,字符串比较依然要消耗m次操作(m也就是字母串的长度),所以时间复杂度是$O(m × n × n)$。
+这里一些同学会忽略了字符串比较的时间消耗,这里并不像int 型数字做比较那么简单,除了n^2次的遍历次数外,字符串比较依然要消耗m次操作(m也就是字母串的长度),所以时间复杂度是O(m × n × n)。
接下来再想一下其他解题思路。
diff --git "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" "b/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md"
index b2acb7ddb8..f19984e66b 100644
--- "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md"
+++ "b/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md"
@@ -9,7 +9,7 @@
* 什么是大O
* 不同数据规模的差异
* 复杂表达式的化简
-* $O(\log n)$中的log是以什么为底?
+* O(log n)中的log是以什么为底?
* 举一个例子
@@ -23,21 +23,21 @@
那么该如何估计程序运行时间呢,通常会估算算法的操作单元数量来代表程序消耗的时间,这里默认CPU的每个单元运行消耗的时间都是相同的。
-假设算法的问题规模为n,那么操作单元数量便用函数f(n)来表示,随着数据规模n的增大,算法执行时间的增长率和f(n)的增长率相同,这称作为算法的渐近时间复杂度,简称时间复杂度,记为 $O(f(n)$)。
+假设算法的问题规模为n,那么操作单元数量便用函数f(n)来表示,随着数据规模n的增大,算法执行时间的增长率和f(n)的增长率相同,这称作为算法的渐近时间复杂度,简称时间复杂度,记为 O(f(n))。
## 什么是大O
-这里的大O是指什么呢,说到时间复杂度,**大家都知道$O(n)$,$O(n^2)$,却说不清什么是大O**。
+这里的大O是指什么呢,说到时间复杂度,**大家都知道O(n),O(n^2),却说不清什么是大O**。
算法导论给出的解释:**大O用来表示上界的**,当用它作为算法的最坏情况运行时间的上界,就是对任意数据输入的运行时间的上界。
-同样算法导论给出了例子:拿插入排序来说,插入排序的时间复杂度我们都说是$O(n^2)$ 。
+同样算法导论给出了例子:拿插入排序来说,插入排序的时间复杂度我们都说是O(n^2) 。
-输入数据的形式对程序运算时间是有很大影响的,在数据本来有序的情况下时间复杂度是$O(n)$,但如果数据是逆序的话,插入排序的时间复杂度就是$O(n^2)$,也就对于所有输入情况来说,最坏是$O(n^2)$ 的时间复杂度,所以称插入排序的时间复杂度为$O(n^2)$。
+输入数据的形式对程序运算时间是有很大影响的,在数据本来有序的情况下时间复杂度是O(n),但如果数据是逆序的话,插入排序的时间复杂度就是O(n^2),也就对于所有输入情况来说,最坏是O(n^2) 的时间复杂度,所以称插入排序的时间复杂度为O(n^2)。
-同样的同理再看一下快速排序,都知道快速排序是$O(n\log n)$,但是当数据已经有序情况下,快速排序的时间复杂度是$O(n^2)$ 的,**所以严格从大O的定义来讲,快速排序的时间复杂度应该是$O(n^2)$**。
+同样的同理再看一下快速排序,都知道快速排序是O(nlogn),但是当数据已经有序情况下,快速排序的时间复杂度是O(n^2) 的,**所以严格从大O的定义来讲,快速排序的时间复杂度应该是O(n^2)**。
-**但是我们依然说快速排序是$O(n\log n)$的时间复杂度,这个就是业内的一个默认规定,这里说的O代表的就是一般情况,而不是严格的上界**。如图所示:
+**但是我们依然说快速排序是O(nlogn)的时间复杂度,这个就是业内的一个默认规定,这里说的O代表的就是一般情况,而不是严格的上界**。如图所示:

我们主要关心的还是一般情况下的数据形式。
@@ -51,11 +51,11 @@

-在决定使用哪些算法的时候,不是时间复杂越低的越好(因为简化后的时间复杂度忽略了常数项等等),要考虑数据规模,如果数据规模很小甚至可以用$O(n^2)$的算法比$O(n)$的更合适(在有常数项的时候)。
+在决定使用哪些算法的时候,不是时间复杂越低的越好(因为简化后的时间复杂度忽略了常数项等等),要考虑数据规模,如果数据规模很小甚至可以用O(n^2)的算法比O(n)的更合适(在有常数项的时候)。
-就像上图中 $O(5n^2)$ 和 $O(100n)$ 在n为20之前 很明显 $O(5n^2)$是更优的,所花费的时间也是最少的。
+就像上图中 O(5n^2) 和 O(100n) 在n为20之前 很明显 O(5n^2)是更优的,所花费的时间也是最少的。
-那为什么在计算时间复杂度的时候要忽略常数项系数呢,也就说$O(100n)$ 就是$O(n)$的时间复杂度,$O(5n^2)$ 就是$O(n^2)$的时间复杂度,而且要默认$O(n)$ 优于$O(n^2)$ 呢 ?
+那为什么在计算时间复杂度的时候要忽略常数项系数呢,也就说O(100n) 就是O(n)的时间复杂度,O(5n^2) 就是O(n^2)的时间复杂度,而且要默认O(n) 优于O(n^2) 呢 ?
这里就又涉及到大O的定义,**因为大O就是数据量级突破一个点且数据量级非常大的情况下所表现出的时间复杂度,这个数据量也就是常数项系数已经不起决定性作用的数据量**。
@@ -63,13 +63,13 @@
**所以我们说的时间复杂度都是省略常数项系数的,是因为一般情况下都是默认数据规模足够的大,基于这样的事实,给出的算法时间复杂的的一个排行如下所示**:
-O(1)常数阶 < $O(\log n)$对数阶 < $O(n)$线性阶 < $O(n^2)$平方阶 < $O(n^3)$立方阶 < $O(2^n)$指数阶
+O(1)常数阶 < O(logn)对数阶 < O(n)线性阶 < O(n^2)平方阶 < O(n^3)立方阶 < O(2^n)指数阶
但是也要注意大常数,如果这个常数非常大,例如10^7 ,10^9 ,那么常数就是不得不考虑的因素了。
## 复杂表达式的化简
-有时候我们去计算时间复杂度的时候发现不是一个简单的$O(n)$ 或者$O(n^2)$, 而是一个复杂的表达式,例如:
+有时候我们去计算时间复杂度的时候发现不是一个简单的O(n) 或者O(n^2), 而是一个复杂的表达式,例如:
```
O(2*n^2 + 10*n + 1000)
@@ -95,19 +95,19 @@ O(n^2 + n)
O(n^2)
```
-如果这一步理解有困难,那也可以做提取n的操作,变成$O(n(n+1)$) ,省略加法常数项后也就别变成了:
+如果这一步理解有困难,那也可以做提取n的操作,变成O(n(n+1)) ,省略加法常数项后也就别变成了:
```
O(n^2)
```
-所以最后我们说:这个算法的算法时间复杂度是$O(n^2)$ 。
+所以最后我们说:这个算法的算法时间复杂度是O(n^2) 。
-也可以用另一种简化的思路,其实当n大于40的时候, 这个复杂度会恒小于$O(3 × n^2)$,
-$O(2 × n^2 + 10 × n + 1000) < O(3 × n^2)$,所以说最后省略掉常数项系数最终时间复杂度也是$O(n^2)$。
+也可以用另一种简化的思路,其实当n大于40的时候, 这个复杂度会恒小于O(3 × n^2),
+O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项系数最终时间复杂度也是O(n^2)。
-## $O(\log n)$中的log是以什么为底?
+## O(logn)中的log是以什么为底?
平时说这个算法的时间复杂度是logn的,那么一定是log 以2为底n的对数么?
@@ -130,21 +130,21 @@ $O(2 × n^2 + 10 × n + 1000) < O(3 × n^2)$,所以说最后省略掉常数项
通过这道面试题目,来分析一下时间复杂度。题目描述:找出n个字符串中相同的两个字符串(假设这里只有两个相同的字符串)。
-如果是暴力枚举的话,时间复杂度是多少呢,是$O(n^2)$么?
+如果是暴力枚举的话,时间复杂度是多少呢,是O(n^2)么?
-这里一些同学会忽略了字符串比较的时间消耗,这里并不像int 型数字做比较那么简单,除了n^2 次的遍历次数外,字符串比较依然要消耗m次操作(m也就是字母串的长度),所以时间复杂度是$O(m × n × n)$。
+这里一些同学会忽略了字符串比较的时间消耗,这里并不像int 型数字做比较那么简单,除了n^2 次的遍历次数外,字符串比较依然要消耗m次操作(m也就是字母串的长度),所以时间复杂度是O(m × n × n)。
接下来再想一下其他解题思路。
先排对n个字符串按字典序来排序,排序后n个字符串就是有序的,意味着两个相同的字符串就是挨在一起,然后在遍历一遍n个字符串,这样就找到两个相同的字符串了。
-那看看这种算法的时间复杂度,快速排序时间复杂度为$O(n\log n)$,依然要考虑字符串的长度是m,那么快速排序每次的比较都要有m次的字符比较的操作,就是$O(m × n × \log n)$ 。
+那看看这种算法的时间复杂度,快速排序时间复杂度为O(nlogn),依然要考虑字符串的长度是m,那么快速排序每次的比较都要有m次的字符比较的操作,就是O(m × n × log n) 。
-之后还要遍历一遍这n个字符串找出两个相同的字符串,别忘了遍历的时候依然要比较字符串,所以总共的时间复杂度是 $O(m × n × \log n + n × m)$。
+之后还要遍历一遍这n个字符串找出两个相同的字符串,别忘了遍历的时候依然要比较字符串,所以总共的时间复杂度是 O(m × n × logn + n × m)。
-我们对$O(m × n × \log n + n × m)$ 进行简化操作,把$m × n$提取出来变成 $O(m × n × (\log n + 1)$),再省略常数项最后的时间复杂度是 $O(m × n × \log n)$。
+我们对O(m × n × log n + n × m) 进行简化操作,把m × n提取出来变成 O(m × n × (logn + 1)),再省略常数项最后的时间复杂度是 O(m × n × log n)。
-最后很明显$O(m × n × \log n)$ 要优于$O(m × n × n)$!
+最后很明显O(m × n × logn) 要优于O(m × n × n)!
所以先把字符串集合排序再遍历一遍找到两个相同字符串的方法要比直接暴力枚举的方式更快。
diff --git "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\345\217\257\350\203\275\346\234\211\345\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md" "b/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\345\217\257\350\203\275\346\234\211\345\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md"
index 162dfe963e..19384fd980 100644
--- "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\345\217\257\350\203\275\346\234\211\345\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md"
+++ "b/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\345\217\257\350\203\275\346\234\211\345\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md"
@@ -3,14 +3,14 @@
# 空间复杂度分析
* [关于时间复杂度,你不知道的都在这里!](https://programmercarl.com/前序/关于时间复杂度,你不知道的都在这里!.html)
-* [$O(n)$的算法居然超时了,此时的n究竟是多大?](https://programmercarl.com/前序/On的算法居然超时了,此时的n究竟是多大?.html)
+* [O(n)的算法居然超时了,此时的n究竟是多大?](https://programmercarl.com/前序/On的算法居然超时了,此时的n究竟是多大?.html)
* [通过一道面试题目,讲一讲递归算法的时间复杂度!](https://programmercarl.com/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.html)
那么一直还没有讲空间复杂度,所以打算陆续来补上,内容不难,大家可以读一遍文章就有整体的了解了。
什么是空间复杂度呢?
-是对一个算法在运行过程中占用内存空间大小的量度,记做$S(n)=O(f(n)$。
+是对一个算法在运行过程中占用内存空间大小的量度,记做S(n)=O(f(n)。
空间复杂度(Space Complexity)记作S(n) 依然使用大O来表示。利用程序的空间复杂度,可以对程序运行中需要多少内存有个预先估计。
@@ -41,11 +41,11 @@ for (int i = 0; i < n; i++) {
}
```
-第一段代码可以看出,随着n的变化,所需开辟的内存空间并不会随着n的变化而变化。即此算法空间复杂度为一个常量,所以表示为大$O(1)$。
+第一段代码可以看出,随着n的变化,所需开辟的内存空间并不会随着n的变化而变化。即此算法空间复杂度为一个常量,所以表示为大O(1)。
-什么时候的空间复杂度是$O(n)$?
+什么时候的空间复杂度是O(n)?
-当消耗空间和输入参数n保持线性增长,这样的空间复杂度为$O(n)$,来看一下这段C++代码
+当消耗空间和输入参数n保持线性增长,这样的空间复杂度为O(n),来看一下这段C++代码
```CPP
int* a = new int(n);
for (int i = 0; i < n; i++) {
@@ -53,9 +53,9 @@ for (int i = 0; i < n; i++) {
}
```
-我们定义了一个数组出来,这个数组占用的大小为n,虽然有一个for循环,但没有再分配新的空间,因此,这段代码的空间复杂度主要看第一行即可,随着n的增大,开辟的内存大小呈线性增长,即 $O(n)$。
+我们定义了一个数组出来,这个数组占用的大小为n,虽然有一个for循环,但没有再分配新的空间,因此,这段代码的空间复杂度主要看第一行即可,随着n的增大,开辟的内存大小呈线性增长,即 O(n)。
-其他的 $O(n^2)$, $O(n^3)$ 我想大家应该都可以以此例举出来了,**那么思考一下 什么时候空间复杂度是 $O(\log n)$呢?**
+其他的 O(n^2), O(n^3) 我想大家应该都可以以此例举出来了,**那么思考一下 什么时候空间复杂度是 O(logn)呢?**
空间复杂度是logn的情况确实有些特殊,其实是在**递归的时候,会出现空间复杂度为logn的情况**。
diff --git "a/problems/\345\211\215\345\272\217/\347\274\226\347\250\213\347\264\240\345\205\273\351\203\250\345\210\206\347\232\204\345\220\271\346\257\233\346\261\202\347\226\265.md" "b/problems/\345\211\215\345\272\217/\347\274\226\347\250\213\347\264\240\345\205\273\351\203\250\345\210\206\347\232\204\345\220\271\346\257\233\346\261\202\347\226\265.md"
index 3f18f9d1d5..6099747a92 100644
--- "a/problems/\345\211\215\345\272\217/\347\274\226\347\250\213\347\264\240\345\205\273\351\203\250\345\210\206\347\232\204\345\220\271\346\257\233\346\261\202\347\226\265.md"
+++ "b/problems/\345\211\215\345\272\217/\347\274\226\347\250\213\347\264\240\345\205\273\351\203\250\345\210\206\347\232\204\345\220\271\346\257\233\346\261\202\347\226\265.md"
@@ -13,7 +13,7 @@
- 左孩子和右孩子的下标不太好理解。我给出证明过程:
- 如果父节点在第$k$层,第$m,m \in [0,2^k]$个节点,则其左孩子所在的位置必然为$k+1$层,第$2*(m-1)+1$个节点。
+ 如果父节点在第k层,第$m,m \in [0,2^k]$个节点,则其左孩子所在的位置必然为$k+1$层,第$2*(m-1)+1$个节点。
- 计算父节点在数组中的索引:
$$
diff --git "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md"
index 3c0d219ca6..142358da7b 100644
--- "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md"
+++ "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md"
@@ -26,7 +26,7 @@ int fibonacci(int i) {
在讲解递归时间复杂度的时候,我们提到了递归算法的时间复杂度本质上是要看: **递归的次数 * 每次递归的时间复杂度**。
-可以看出上面的代码每次递归都是$O(1)$的操作。再来看递归了多少次,这里将i为5作为输入的递归过程 抽象成一棵递归树,如图:
+可以看出上面的代码每次递归都是O(1)的操作。再来看递归了多少次,这里将i为5作为输入的递归过程 抽象成一棵递归树,如图:

@@ -36,7 +36,7 @@ int fibonacci(int i) {
我们之前也有说到,一棵深度(按根节点深度为1)为k的二叉树最多可以有 2^k - 1 个节点。
-所以该递归算法的时间复杂度为$O(2^n)$,这个复杂度是非常大的,随着n的增大,耗时是指数上升的。
+所以该递归算法的时间复杂度为O(2^n),这个复杂度是非常大的,随着n的增大,耗时是指数上升的。
来做一个实验,大家可以有一个直观的感受。
@@ -85,7 +85,7 @@ int main()
* n = 40,耗时:837 ms
* n = 50,耗时:110306 ms
-可以看出,$O(2^n)$这种指数级别的复杂度是非常大的。
+可以看出,O(2^n)这种指数级别的复杂度是非常大的。
所以这种求斐波那契数的算法看似简洁,其实时间复杂度非常高,一般不推荐这样来实现斐波那契。
@@ -119,14 +119,14 @@ int fibonacci(int first, int second, int n) {
这里相当于用first和second来记录当前相加的两个数值,此时就不用两次递归了。
-因为每次递归的时候n减1,即只是递归了n次,所以时间复杂度是 $O(n)$。
+因为每次递归的时候n减1,即只是递归了n次,所以时间复杂度是 O(n)。
-同理递归的深度依然是n,每次递归所需的空间也是常数,所以空间复杂度依然是$O(n)$。
+同理递归的深度依然是n,每次递归所需的空间也是常数,所以空间复杂度依然是O(n)。
代码(版本二)的复杂度如下:
-* 时间复杂度:$O(n)$
-* 空间复杂度:$O(n)$
+* 时间复杂度:O(n)
+* 空间复杂度:O(n)
此时再来测一下耗时情况验证一下:
@@ -198,7 +198,7 @@ int main()
递归第n个斐波那契数的话,递归调用栈的深度就是n。
-那么每次递归的空间复杂度是$O(1)$, 调用栈深度为n,所以这段递归代码的空间复杂度就是$O(n)$。
+那么每次递归的空间复杂度是O(1), 调用栈深度为n,所以这段递归代码的空间复杂度就是O(n)。
```CPP
int fibonacci(int i) {
@@ -233,24 +233,24 @@ int binary_search( int arr[], int l, int r, int x) {
}
```
-都知道二分查找的时间复杂度是$O(\log n)$,那么递归二分查找的空间复杂度是多少呢?
+都知道二分查找的时间复杂度是O(logn),那么递归二分查找的空间复杂度是多少呢?
我们依然看 **每次递归的空间复杂度和递归的深度**
每次递归的空间复杂度可以看出主要就是参数里传入的这个arr数组,但需要注意的是在C/C++中函数传递数组参数,不是整个数组拷贝一份传入函数而是传入的数组首元素地址。
-**也就是说每一层递归都是公用一块数组地址空间的**,所以 每次递归的空间复杂度是常数即:$O(1)$。
+**也就是说每一层递归都是公用一块数组地址空间的**,所以 每次递归的空间复杂度是常数即:O(1)。
-再来看递归的深度,二分查找的递归深度是logn ,递归深度就是调用栈的长度,那么这段代码的空间复杂度为 $1 * logn = O(logn)$。
+再来看递归的深度,二分查找的递归深度是logn ,递归深度就是调用栈的长度,那么这段代码的空间复杂度为 1 * logn = O(logn)。
-大家要注意自己所用的语言在传递函数参数的时,是拷贝整个数值还是拷贝地址,如果是拷贝整个数值那么该二分法的空间复杂度就是$O(n\log n)$。
+大家要注意自己所用的语言在传递函数参数的时,是拷贝整个数值还是拷贝地址,如果是拷贝整个数值那么该二分法的空间复杂度就是O(nlogn)。
## 总结
本章我们详细分析了递归实现的求斐波那契和二分法的空间复杂度,同时也对时间复杂度做了分析。
-特别是两种递归实现的求斐波那契数列,其时间复杂度截然不容,我们还做了实验,验证了时间复杂度为$O(2^n)$是非常耗时的。
+特别是两种递归实现的求斐波那契数列,其时间复杂度截然不容,我们还做了实验,验证了时间复杂度为O(2^n)是非常耗时的。
通过本篇大家应该对递归算法的时间复杂度和空间复杂度有更加深刻的理解了。
diff --git "a/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" "b/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md"
index 12af4dd8fe..b2db92f541 100644
--- "a/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md"
+++ "b/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md"
@@ -5,13 +5,13 @@
相信很多同学对递归算法的时间复杂度都很模糊,那么这篇来给大家通透的讲一讲。
-**同一道题目,同样使用递归算法,有的同学会写出了$O(n)$的代码,有的同学就写出了$O(\log n)$的代码**。
+**同一道题目,同样使用递归算法,有的同学会写出了O(n)的代码,有的同学就写出了O(logn)的代码**。
这是为什么呢?
如果对递归的时间复杂度理解的不够深入的话,就会这样!
-那么我通过一道简单的面试题,模拟面试的场景,来带大家逐步分析递归算法的时间复杂度,最后找出最优解,来看看同样是递归,怎么就写成了$O(n)$的代码。
+那么我通过一道简单的面试题,模拟面试的场景,来带大家逐步分析递归算法的时间复杂度,最后找出最优解,来看看同样是递归,怎么就写成了O(n)的代码。
面试题:求x的n次方
@@ -26,7 +26,7 @@ int function1(int x, int n) {
return result;
}
```
-时间复杂度为$O(n)$,此时面试官会说,有没有效率更好的算法呢。
+时间复杂度为O(n),此时面试官会说,有没有效率更好的算法呢。
**如果此时没有思路,不要说:我不会,我不知道了等等**。
@@ -44,11 +44,11 @@ int function2(int x, int n) {
```
面试官问:“那么这个代码的时间复杂度是多少?”。
-一些同学可能一看到递归就想到了$O(\log n)$,其实并不是这样,递归算法的时间复杂度本质上是要看: **递归的次数 * 每次递归中的操作次数**。
+一些同学可能一看到递归就想到了O(log n),其实并不是这样,递归算法的时间复杂度本质上是要看: **递归的次数 * 每次递归中的操作次数**。
那再来看代码,这里递归了几次呢?
-每次n-1,递归了n次时间复杂度是$O(n)$,每次进行了一个乘法操作,乘法操作的时间复杂度一个常数项$O(1)$,所以这份代码的时间复杂度是 $n × 1 = O(n)$。
+每次n-1,递归了n次时间复杂度是O(n),每次进行了一个乘法操作,乘法操作的时间复杂度一个常数项O(1),所以这份代码的时间复杂度是 n × 1 = O(n)。
这个时间复杂度就没有达到面试官的预期。于是又写出了如下的递归算法的代码:
@@ -81,11 +81,11 @@ int function3(int x, int n) {

-**时间复杂度忽略掉常数项`-1`之后,这个递归算法的时间复杂度依然是$O(n)$**。对,你没看错,依然是$O(n)$的时间复杂度!
+**时间复杂度忽略掉常数项`-1`之后,这个递归算法的时间复杂度依然是O(n)**。对,你没看错,依然是O(n)的时间复杂度!
-此时面试官就会说:“这个递归的算法依然还是$O(n)$啊”, 很明显没有达到面试官的预期。
+此时面试官就会说:“这个递归的算法依然还是O(n)啊”, 很明显没有达到面试官的预期。
-那么$O(\log n)$的递归算法应该怎么写呢?
+那么O(logn)的递归算法应该怎么写呢?
想一想刚刚给出的那份递归算法的代码,是不是有哪里比较冗余呢,其实有重复计算的部分。
@@ -108,7 +108,7 @@ int function4(int x, int n) {
依然还是看他递归了多少次,可以看到这里仅仅有一个递归调用,且每次都是n/2 ,所以这里我们一共调用了log以2为底n的对数次。
-**每次递归了做都是一次乘法操作,这也是一个常数项的操作,那么这个递归算法的时间复杂度才是真正的$O(\log n)$**。
+**每次递归了做都是一次乘法操作,这也是一个常数项的操作,那么这个递归算法的时间复杂度才是真正的O(logn)**。
此时大家最后写出了这样的代码并且将时间复杂度分析的非常清晰,相信面试官是比较满意的。
@@ -116,11 +116,11 @@ int function4(int x, int n) {
对于递归的时间复杂度,毕竟初学者有时候会迷糊,刷过很多题的老手依然迷糊。
-**本篇我用一道非常简单的面试题目:求x的n次方,来逐步分析递归算法的时间复杂度,注意不要一看到递归就想到了$O(\log n)$!**
+**本篇我用一道非常简单的面试题目:求x的n次方,来逐步分析递归算法的时间复杂度,注意不要一看到递归就想到了O(logn)!**
-同样使用递归,有的同学可以写出$O(\log n)$的代码,有的同学还可以写出$O(n)$的代码。
+同样使用递归,有的同学可以写出O(logn)的代码,有的同学还可以写出O(n)的代码。
-对于function3 这样的递归实现,很容易让人感觉这是$O(\log n)$的时间复杂度,其实这是$O(n)$的算法!
+对于function3 这样的递归实现,很容易让人感觉这是O(log n)的时间复杂度,其实这是O(n)的算法!
```CPP
int function3(int x, int n) {
diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md"
index 6139127423..fec83e1d5a 100644
--- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md"
@@ -86,7 +86,7 @@ public:
# 题外话
一些同学热衷于使用substr,来做这道题。
-其实使用substr 和 反转 时间复杂度是一样的 ,都是$O(n)$,但是使用substr申请了额外空间,所以空间复杂度是$O(n)$,而反转方法的空间复杂度是$O(1)$。
+其实使用substr 和 反转 时间复杂度是一样的 ,都是O(n),但是使用substr申请了额外空间,所以空间复杂度是O(n),而反转方法的空间复杂度是O(1)。
**如果想让这套题目有意义,就不要申请额外空间。**
diff --git "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md"
index 39096ff739..06752cacb0 100644
--- "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md"
+++ "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md"
@@ -89,7 +89,7 @@ for (int i = 0; i < array.size(); i++) {
# 总结
-本文中一共介绍了leetcode上九道使用双指针解决问题的经典题目,除了链表一些题目一定要使用双指针,其他题目都是使用双指针来提高效率,一般是将$O(n^2)$的时间复杂度,降为$O(n)$。
+本文中一共介绍了leetcode上九道使用双指针解决问题的经典题目,除了链表一些题目一定要使用双指针,其他题目都是使用双指针来提高效率,一般是将O(n^2)的时间复杂度,降为$O(n)$。
建议大家可以把文中涉及到的题目在好好做一做,琢磨琢磨,基本对双指针法就不在话下了。
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20200927\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20200927\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md"
index 60f0220541..ff8f67d47d 100644
--- "a/problems/\345\221\250\346\200\273\347\273\223/20200927\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md"
+++ "b/problems/\345\221\250\346\200\273\347\273\223/20200927\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md"
@@ -44,7 +44,7 @@ a->right = NULL;
在介绍前中后序遍历的时候,有递归和迭代(非递归),还有一种牛逼的遍历方式:morris遍历。
-morris遍历是二叉树遍历算法的超强进阶算法,morris遍历可以将非递归遍历中的空间复杂度降为$O(1)$,感兴趣大家就去查一查学习学习,比较小众,面试几乎不会考。我其实也没有研究过,就不做过多介绍了。
+morris遍历是二叉树遍历算法的超强进阶算法,morris遍历可以将非递归遍历中的空间复杂度降为O(1),感兴趣大家就去查一查学习学习,比较小众,面试几乎不会考。我其实也没有研究过,就不做过多介绍了。
## 周二
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md"
index 02fccc25e0..e310c0f8e0 100644
--- "a/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md"
+++ "b/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md"
@@ -41,7 +41,7 @@
一些录友不清楚[贪心算法:分发饼干](https://programmercarl.com/0455.分发饼干.html)中时间复杂度是怎么来的?
-就是快排$O(n\log n)$,遍历$O(n)$,加一起就是还是$O(n\log n)$。
+就是快排O(nlog n),遍历O(n),加一起就是还是O(nlogn)。
## 周三
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md"
index 1b404bf060..5e5f696d4c 100644
--- "a/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md"
+++ "b/problems/\345\221\250\346\200\273\347\273\223/20201210\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220\345\221\250\346\234\253\346\200\273\347\273\223.md"
@@ -70,9 +70,9 @@
# 周三
-在[$O(n)$的算法居然超时了,此时的n究竟是多大?](https://programmercarl.com/前序/On的算法居然超时了,此时的n究竟是多大?.html)中介绍了大家在leetcode上提交代码经常遇到的一个问题-超时!
+在[O(n)的算法居然超时了,此时的n究竟是多大?](https://programmercarl.com/前序/On的算法居然超时了,此时的n究竟是多大?.html)中介绍了大家在leetcode上提交代码经常遇到的一个问题-超时!
-估计很多录友知道算法超时了,但没有注意过 $O(n)$的算法,如果1s内出结果,这个n究竟是多大?
+估计很多录友知道算法超时了,但没有注意过 O(n)的算法,如果1s内出结果,这个n究竟是多大?
文中从计算机硬件出发,分析计算机的计算性能,然后亲自做实验,整理出数据如下:
@@ -95,7 +95,7 @@
文中给出了四个版本的代码实现,并逐一分析了其时间复杂度。
-此时大家就会发现,同一道题目,同样使用递归算法,有的同学会写出了$O(n)$的代码,有的同学就写出了$O(\log n)$的代码。
+此时大家就会发现,同一道题目,同样使用递归算法,有的同学会写出了O(n)的代码,有的同学就写出了$O(\log n)$的代码。
其本质是要对递归的时间复杂度有清晰的认识,才能运用递归来有效的解决问题!
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md"
index e9d22d6e22..4d12f92a88 100644
--- "a/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md"
+++ "b/problems/\345\221\250\346\200\273\347\273\223/20201217\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md"
@@ -8,7 +8,7 @@
在[贪心算法:加油站](https://programmercarl.com/0134.加油站.html)中给出每一个加油站的汽油和开到这个加油站的消耗,问汽车能不能开一圈。
-这道题目咋眼一看,感觉是一道模拟题,模拟一下汽车从每一个节点出发看看能不能开一圈,时间复杂度是$O(n^2)$。
+这道题目咋眼一看,感觉是一道模拟题,模拟一下汽车从每一个节点出发看看能不能开一圈,时间复杂度是O(n^2)。
即使用模拟这种情况,也挺考察代码技巧的。
diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md"
index 0bf9dbdb6f..21cc53ad09 100644
--- "a/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md"
+++ "b/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md"
@@ -211,8 +211,8 @@ public:
};
```
-* 时间复杂度:$O(n^2)$
-* 空间复杂度:$O(1)$
+* 时间复杂度:O(n^2)
+* 空间复杂度:O(1)
贪心解法代码如下:
@@ -233,8 +233,8 @@ public:
};
```
-* 时间复杂度:$O(n)$
-* 空间复杂度:$O(1)$
+* 时间复杂度:O(n)
+* 空间复杂度:O(1)
动规解法,版本一,代码如下:
@@ -256,8 +256,8 @@ public:
};
```
-* 时间复杂度:$O(n)$
-* 空间复杂度:$O(n)$
+* 时间复杂度:O(n)
+* 空间复杂度:O(n)
从递推公式可以看出,dp[i]只是依赖于dp[i - 1]的状态。
@@ -282,8 +282,8 @@ public:
};
```
-* 时间复杂度:$O(n)$
-* 空间复杂度:$O(1)$
+* 时间复杂度:O(n)
+* 空间复杂度:O(1)
建议先写出版本一,然后在版本一的基础上优化成版本二,而不是直接就写出版本二。
diff --git "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md"
index 40a8d0ca9e..3b6c5ce549 100644
--- "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md"
+++ "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md"
@@ -22,7 +22,7 @@
例如要查询一个名字是否在这所学校里。
-要枚举的话时间复杂度是$O(n)$,但如果使用哈希表的话, 只需要$O(1)$就可以做到。
+要枚举的话时间复杂度是O(n),但如果使用哈希表的话, 只需要O(1)就可以做到。
我们只需要初始化把这所学校里学生的名字都存在哈希表里,在查询的时候通过索引直接就可以知道这位同学在不在这所学校里了。
@@ -88,17 +88,17 @@
|集合 |底层实现 | 是否有序 |数值是否可以重复 | 能否更改数值|查询效率 |增删效率|
|---|---| --- |---| --- | --- | ---|
-|std::set |红黑树 |有序 |否 |否 | $O(\log n)$|$O(\log n)$ |
-|std::multiset | 红黑树|有序 |是 | 否| $O(\log n)$ |$O(\log n)$ |
-|std::unordered_set |哈希表 |无序 |否 |否 |$O(1)$ | $O(1)$|
+|std::set |红黑树 |有序 |否 |否 | O(log n)|O(log n) |
+|std::multiset | 红黑树|有序 |是 | 否| O(logn) |O(logn) |
+|std::unordered_set |哈希表 |无序 |否 |否 |O(1) | O(1)|
std::unordered_set底层实现为哈希表,std::set 和std::multiset 的底层实现是红黑树,红黑树是一种平衡二叉搜索树,所以key值是有序的,但key不可以修改,改动key值会导致整棵树的错乱,所以只能删除和增加。
|映射 |底层实现 | 是否有序 |数值是否可以重复 | 能否更改数值|查询效率 |增删效率|
|---|---| --- |---| --- | --- | ---|
-|std::map |红黑树 |key有序 |key不可重复 |key不可修改 | $O(\log n)$|$O(\log n)$ |
-|std::multimap | 红黑树|key有序 | key可重复 | key不可修改|$O(\log n)$ |$O(\log n)$ |
-|std::unordered_map |哈希表 | key无序 |key不可重复 |key不可修改 |$O(1)$ | $O(1)$|
+|std::map |红黑树 |key有序 |key不可重复 |key不可修改 | O(logn)|O(logn) |
+|std::multimap | 红黑树|key有序 | key可重复 | key不可修改|O(log n) |O(log n) |
+|std::unordered_map |哈希表 | key无序 |key不可重复 |key不可修改 |O(1) | O(1)|
std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底层实现是红黑树。同理,std::map 和std::multimap 的key也是有序的(这个问题也经常作为面试题,考察对语言容器底层的理解)。
diff --git "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md"
index 5b8e227603..54ac485b3d 100644
--- "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md"
+++ "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md"
@@ -302,7 +302,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加
**而使用used数组在时间复杂度上几乎没有额外负担!**
-**使用set去重,不仅时间复杂度高了,空间复杂度也高了**,在[本周小结!(回溯算法系列三)](https://programmercarl.com/周总结/20201112回溯周末总结.html)中分析过,组合,子集,排列问题的空间复杂度都是O(n),但如果使用set去重,空间复杂度就变成了$O(n^2)$,因为每一层递归都有一个set集合,系统栈空间是n,每一个空间都有set集合。
+**使用set去重,不仅时间复杂度高了,空间复杂度也高了**,在[本周小结!(回溯算法系列三)](https://programmercarl.com/周总结/20201112回溯周末总结.html)中分析过,组合,子集,排列问题的空间复杂度都是O(n),但如果使用set去重,空间复杂度就变成了O(n^2),因为每一层递归都有一个set集合,系统栈空间是n,每一个空间都有set集合。
那有同学可能疑惑 用used数组也是占用O(n)的空间啊?
diff --git "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md"
index 242c149878..d256298b9a 100644
--- "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md"
+++ "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md"
@@ -102,7 +102,7 @@
本题中,主要要理解滑动窗口如何移动 窗口起始位置,达到动态更新窗口大小的,从而得出长度最小的符合条件的长度。
-**滑动窗口的精妙之处在于根据当前子序列和大小的情况,不断调节子序列的起始位置。从而将$O(n^2)$的暴力解法降为$O(n)$。**
+**滑动窗口的精妙之处在于根据当前子序列和大小的情况,不断调节子序列的起始位置。从而将O(n^2)的暴力解法降为O(n)。**
如果没有接触过这一类的方法,很难想到类似的解题思路,滑动窗口方法还是很巧妙的。
diff --git "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md"
index 6c972b41b4..e229f39726 100644
--- "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md"
+++ "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md"
@@ -151,7 +151,7 @@ public:
大家应该发现了,编程语言中一个普通容器的insert,delete的使用,都可能对写出来的算法的有很大影响!
-如果抛开语言谈算法,除非从来不用代码写算法纯分析,**否则的话,语言功底不到位O(n)的算法可以写出$O(n^2)$的性能**,哈哈。
+如果抛开语言谈算法,除非从来不用代码写算法纯分析,**否则的话,语言功底不到位O(n)的算法可以写出O(n^2)的性能**,哈哈。
相信在这里学习算法的录友们,都是想在软件行业长远发展的,都是要从事编程的工作,那么一定要深耕好一门编程语言,这个非常重要!
diff --git "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md"
index 1db9b4dc10..6539501f6b 100644
--- "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md"
+++ "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md"
@@ -127,9 +127,9 @@ Carl个人认为:如果找出局部最优并可以推出全局最优,就是
贪心专题汇聚为一张图:
-
+
-这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[青](https://wx.zsxq.com/dweb2/index/footprint/185251215558842)所画,总结的非常好,分享给大家。
+这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412)所画,总结的非常好,分享给大家。
很多没有接触过贪心的同学都会感觉贪心有啥可学的,但只要跟着「代码随想录」坚持下来之后,就会发现,贪心是一种很重要的算法思维而且并不简单,贪心往往妙的出其不意,触不及防!
@@ -145,18 +145,6 @@ Carl个人认为:如果找出局部最优并可以推出全局最优,就是
**一个系列的结束,又是一个新系列的开始,我们将在明年第一个工作日正式开始动态规划,来不及解释了,录友们上车别掉队,我们又要开始新的征程!**
-## 其他语言版本
-
-
-Java:
-
-
-Python:
-
-
-Go:
-
-
-----------------------
From 1b935259a6c83beab9f730059da4bec509bb1f61 Mon Sep 17 00:00:00 2001
From: Guanzhong Pan
Date: Tue, 12 Apr 2022 21:01:23 +0100
Subject: [PATCH 071/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200122.=E4=B9=B0?=
=?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?=
=?UTF-8?q?II.md=20C=E8=AF=AD=E8=A8=80=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?=
=?UTF-8?q?=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\344\275\263\346\227\266\346\234\272II.md" | 24 ++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
index 83b852c6c0..1b84f728f1 100644
--- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
+++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
@@ -269,7 +269,7 @@ const maxProfit = (prices) => {
```
C:
-
+贪心:
```c
int maxProfit(int* prices, int pricesSize){
int result = 0;
@@ -284,5 +284,27 @@ int maxProfit(int* prices, int pricesSize){
}
```
+动态规划:
+```c
+#define max(a, b) (((a) > (b)) ? (a) : (b))
+
+int maxProfit(int* prices, int pricesSize){
+ int dp[pricesSize][2];
+ dp[0][0] = 0 - prices[0];
+ dp[0][1] = 0;
+
+ int i;
+ for(i = 1; i < pricesSize; ++i) {
+ // dp[i][0]为i-1天持股的钱数/在第i天用i-1天的钱买入的最大值。
+ // 若i-1天持股,且第i天买入股票比i-1天持股时更亏,说明应在i-1天时持股
+ dp[i][0] = max(dp[i-1][0], dp[i-1][1] - prices[i]);
+ //dp[i][1]为i-1天不持股钱数/在第i天卖出所持股票dp[i-1][0] + prices[i]的最大值
+ dp[i][1] = max(dp[i-1][1], dp[i-1][0] + prices[i]);
+ }
+ // 返回在最后一天不持股时的钱数(将股票卖出后钱最大化)
+ return dp[pricesSize - 1][1];
+}
+```
+
-----------------------
From 0fc1cb334d4f8612e95eabfc9e97fe8d273dc25e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=AD=9F=E4=BB=A4=E4=BB=A4?=
Date: Wed, 13 Apr 2022 15:51:42 +0800
Subject: [PATCH 072/551] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D?=
=?UTF-8?q?=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md=20=E6=B7=BB?=
=?UTF-8?q?=E5=8A=A0swift=E6=96=B9=E6=B3=95(=E5=89=8D=E7=BC=80=E8=A1=A8?=
=?UTF-8?q?=E7=BB=9F=E4=B8=80=E4=B8=8D=E5=87=8F=E4=B8=80)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...20\345\255\227\347\254\246\344\270\262.md" | 40 +++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
index 6a9b4260ac..f2fe728dd4 100644
--- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
@@ -463,6 +463,46 @@ Swift:
}
}
```
+> 前缀表统一不减一
+```swift
+ func repeatedSubstringPattern(_ s: String) -> Bool {
+
+ let sArr = Array(s)
+ let len = sArr.count
+ if len == 0 {
+ return false
+ }
+
+ var next = Array.init(repeating: 0, count: len)
+ getNext(&next, sArr)
+
+ if next[len-1] != 0 && len % (len - next[len-1]) == 0 {
+ return true
+ }
+
+ return false
+ }
+
+ // 前缀表不减一
+ func getNext(_ next: inout [Int], _ sArr:[Character]) {
+
+ var j = 0
+ next[0] = 0
+
+ for i in 1 ..< sArr.count {
+ while j > 0 && sArr[i] != sArr[j] {
+ j = next[j-1]
+ }
+
+ if sArr[i] == sArr[j] {
+ j += 1
+ }
+
+ next[i] = j
+ }
+ }
+
+```
-----------------------
From cfb8ab8b936418f6b446bad655be491f9159be14 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=AD=9F=E4=BB=A4=E4=BB=A4?=
Date: Wed, 13 Apr 2022 16:01:26 +0800
Subject: [PATCH 073/551] Revert "Merge branch 'youngyangyang04:master' into
master"
This reverts commit 33be177e63bc622c4e1e67d9e268bc13d4d5ba54, reversing
changes made to 559f03c92869ca55ae929d66de872f5ddda70567.
---
...11\346\216\222\350\241\214\347\250\213.md" | 58 -------------------
1 file changed, 58 deletions(-)
diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md"
index 041a7f031e..01f81c4dc6 100644
--- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md"
+++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md"
@@ -342,64 +342,6 @@ class Solution:
return path
```
-### Go
-```go
-type pair struct {
- target string
- visited bool
-}
-type pairs []*pair
-
-func (p pairs) Len() int {
- return len(p)
-}
-func (p pairs) Swap(i, j int) {
- p[i], p[j] = p[j], p[i]
-}
-func (p pairs) Less(i, j int) bool {
- return p[i].target < p[j].target
-}
-
-func findItinerary(tickets [][]string) []string {
- result := []string{}
- // map[出发机场] pair{目的地,是否被访问过}
- targets := make(map[string]pairs)
- for _, ticket := range tickets {
- if targets[ticket[0]] == nil {
- targets[ticket[0]] = make(pairs, 0)
- }
- targets[ticket[0]] = append(targets[ticket[0]], &pair{target: ticket[1], visited: false})
- }
- for k, _ := range targets {
- sort.Sort(targets[k])
- }
- result = append(result, "JFK")
- var backtracking func() bool
- backtracking = func() bool {
- if len(tickets)+1 == len(result) {
- return true
- }
- // 取出起飞航班对应的目的地
- for _, pair := range targets[result[len(result)-1]] {
- if pair.visited == false {
- result = append(result, pair.target)
- pair.visited = true
- if backtracking() {
- return true
- }
- result = result[:len(result)-1]
- pair.visited = false
- }
- }
- return false
- }
-
- backtracking()
-
- return result
-}
-```
-
### C语言
```C
From efe987be6b6a9cdc908d836e86954aab22227da7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=AD=9F=E4=BB=A4=E4=BB=A4?=
Date: Wed, 13 Apr 2022 16:07:23 +0800
Subject: [PATCH 074/551] =?UTF-8?q?Revert=20"Update=200459.=E9=87=8D?=
=?UTF-8?q?=E5=A4=8D=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md=20?=
=?UTF-8?q?=E6=B7=BB=E5=8A=A0swift=E6=96=B9=E6=B3=95(=E5=89=8D=E7=BC=80?=
=?UTF-8?q?=E8=A1=A8=E7=BB=9F=E4=B8=80=E4=B8=8D=E5=87=8F=E4=B8=80)"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This reverts commit 0fc1cb334d4f8612e95eabfc9e97fe8d273dc25e.
---
...20\345\255\227\347\254\246\344\270\262.md" | 40 -------------------
1 file changed, 40 deletions(-)
diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
index f2fe728dd4..6a9b4260ac 100644
--- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
@@ -463,46 +463,6 @@ Swift:
}
}
```
-> 前缀表统一不减一
-```swift
- func repeatedSubstringPattern(_ s: String) -> Bool {
-
- let sArr = Array(s)
- let len = sArr.count
- if len == 0 {
- return false
- }
-
- var next = Array.init(repeating: 0, count: len)
- getNext(&next, sArr)
-
- if next[len-1] != 0 && len % (len - next[len-1]) == 0 {
- return true
- }
-
- return false
- }
-
- // 前缀表不减一
- func getNext(_ next: inout [Int], _ sArr:[Character]) {
-
- var j = 0
- next[0] = 0
-
- for i in 1 ..< sArr.count {
- while j > 0 && sArr[i] != sArr[j] {
- j = next[j-1]
- }
-
- if sArr[i] == sArr[j] {
- j += 1
- }
-
- next[i] = j
- }
- }
-
-```
-----------------------
From a391c2c949c393141e7aed3d1e8cdbf6b6a0b956 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=AD=9F=E4=BB=A4=E4=BB=A4?=
Date: Wed, 13 Apr 2022 16:10:50 +0800
Subject: [PATCH 075/551] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D?=
=?UTF-8?q?=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md=20=E6=B7=BB?=
=?UTF-8?q?=E5=8A=A0swift=E6=96=B9=E6=B3=95(=E5=89=8D=E7=BC=80=E8=A1=A8?=
=?UTF-8?q?=E7=BB=9F=E4=B8=80=E4=B8=8D=E5=87=8F=E4=B8=80)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...20\345\255\227\347\254\246\344\270\262.md" | 41 +++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
index 6a9b4260ac..a51c68eea7 100644
--- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
@@ -464,5 +464,46 @@ Swift:
}
```
+> 前缀表统一不减一
+```swift
+ func repeatedSubstringPattern(_ s: String) -> Bool {
+
+ let sArr = Array(s)
+ let len = sArr.count
+ if len == 0 {
+ return false
+ }
+
+ var next = Array.init(repeating: 0, count: len)
+ getNext(&next, sArr)
+
+ if next[len-1] != 0 && len % (len - next[len-1]) == 0 {
+ return true
+ }
+
+ return false
+ }
+
+ // 前缀表不减一
+ func getNext(_ next: inout [Int], _ sArr:[Character]) {
+
+ var j = 0
+ next[0] = 0
+
+ for i in 1 ..< sArr.count {
+
+ while j > 0 && sArr[i] != sArr[j] {
+ j = next[j-1]
+ }
+
+ if sArr[i] == sArr[j] {
+ j += 1
+ }
+
+ next[i] = j
+ }
+ }
+```
+
-----------------------
From e1abedc13ad4d5f744ecc27fa9931aa24508364b Mon Sep 17 00:00:00 2001
From: Guanzhong Pan
Date: Wed, 13 Apr 2022 09:41:50 +0100
Subject: [PATCH 076/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200055.=E8=B7=B3?=
=?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8F.md=20C=E8=AF=AD=E8=A8=80=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...63\350\267\203\346\270\270\346\210\217.md" | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md"
index c0890f75f3..4ba0c03bd3 100644
--- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md"
+++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md"
@@ -154,6 +154,28 @@ var canJump = function(nums) {
};
```
+### C
+```c
+#define max(a, b) (((a) > (b)) ? (a) : (b))
+
+bool canJump(int* nums, int numsSize){
+ int cover = 0;
+
+ int i;
+ // 只可能获取cover范围中的步数,所以i<=cover
+ for(i = 0; i <= cover; ++i) {
+ // 更新cover为从i出发能到达的最大值/cover的值中较大值
+ cover = max(i + nums[i], cover);
+
+ // 若更新后cover可以到达最后的元素,返回true
+ if(cover >= numsSize - 1)
+ return true;
+ }
+
+ return false;
+}
+```
+
-----------------------
From b73a7da7585b03b42ab51515f5463259ac2e0631 Mon Sep 17 00:00:00 2001
From: Guanzhong Pan
Date: Fri, 15 Apr 2022 18:25:41 +0100
Subject: [PATCH 077/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201005.K=E6=AC=A1?=
=?UTF-8?q?=E5=8F=96=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84?=
=?UTF-8?q?=E6=95=B0=E7=BB=84=E5=92=8C.md=20C=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\346\225\260\347\273\204\345\222\214.md" | 39 +++++++++++++++++++
1 file changed, 39 insertions(+)
diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md"
index 45f186e2b9..0d122d917b 100644
--- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md"
+++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md"
@@ -211,5 +211,44 @@ var largestSumAfterKNegations = function(nums, k) {
};
```
+### C
+```c
+#define abs(a) (((a) > 0) ? (a) : (-(a)))
+
+// 对数组求和
+int sum(int *nums, int numsSize) {
+ int sum = 0;
+
+ int i;
+ for(i = 0; i < numsSize; ++i) {
+ sum += nums[i];
+ }
+ return sum;
+}
+
+int cmp(const void* v1, const void* v2) {
+ return abs(*(int*)v2) - abs(*(int*)v1);
+}
+
+int largestSumAfterKNegations(int* nums, int numsSize, int k){
+ qsort(nums, numsSize, sizeof(int), cmp);
+
+ int i;
+ for(i = 0; i < numsSize; ++i) {
+ // 遍历数组,若当前元素<0则将当前元素转变,k--
+ if(nums[i] < 0 && k > 0) {
+ nums[i] *= -1;
+ --k;
+ }
+ }
+
+ // 若遍历完数组后k还有剩余(此时所有元素应均为正),则将绝对值最小的元素nums[numsSize - 1]变为负
+ if(k % 2 == 1)
+ nums[numsSize - 1] *= -1;
+
+ return sum(nums, numsSize);
+}
+```
+
-----------------------
From 7c26aba0e1d9ba38a631eb5dbae8577890deb2d2 Mon Sep 17 00:00:00 2001
From: Falldio <1031249495@qq.com>
Date: Sun, 17 Apr 2022 11:59:30 +0800
Subject: [PATCH 078/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880209.?=
=?UTF-8?q?=E9=95=BF=E5=BA=A6=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0?=
=?UTF-8?q?=E7=BB=84.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0Kotlin=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\345\255\220\346\225\260\347\273\204.md" | 31 ++++++++++++++++---
1 file changed, 26 insertions(+), 5 deletions(-)
diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
index 82a1138183..fd72cf1b3a 100644
--- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
+++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
@@ -198,7 +198,7 @@ JavaScript:
var minSubArrayLen = function(target, nums) {
// 长度计算一次
const len = nums.length;
- let l = r = sum = 0,
+ let l = r = sum = 0,
res = len + 1; // 子数组最大不会超过自身
while(r < len) {
sum += nums[r++];
@@ -260,12 +260,12 @@ Rust:
```rust
impl Solution {
- pub fn min_sub_array_len(target: i32, nums: Vec) -> i32 {
+ pub fn min_sub_array_len(target: i32, nums: Vec) -> i32 {
let (mut result, mut subLength): (i32, i32) = (i32::MAX, 0);
let (mut sum, mut i) = (0, 0);
-
+
for (pos, val) in nums.iter().enumerate() {
- sum += val;
+ sum += val;
while sum >= target {
subLength = (pos - i + 1) as i32;
if result > subLength {
@@ -364,7 +364,7 @@ int minSubArrayLen(int target, int* nums, int numsSize){
int minLength = INT_MAX;
int sum = 0;
- int left = 0, right = 0;
+ int left = 0, right = 0;
//右边界向右扩展
for(; right < numsSize; ++right) {
sum += nums[right];
@@ -380,5 +380,26 @@ int minSubArrayLen(int target, int* nums, int numsSize){
}
```
+Kotlin:
+```kotlin
+class Solution {
+ fun minSubArrayLen(target: Int, nums: IntArray): Int {
+ var start = 0
+ var end = 0
+ var ret = Int.MAX_VALUE
+ var count = 0
+ while (end < nums.size) {
+ count += nums[end]
+ while (count >= target) {
+ ret = if (ret > (end - start + 1)) end - start + 1 else ret
+ count -= nums[start++]
+ }
+ end++
+ }
+ return if (ret == Int.MAX_VALUE) 0 else ret
+ }
+}
+```
+
-----------------------
From 0375a5b147c40a449c04ef7f988dfcb2d93b9e01 Mon Sep 17 00:00:00 2001
From: Guacker <92098421+Guacker@users.noreply.github.com>
Date: Sun, 17 Apr 2022 17:19:00 +0800
Subject: [PATCH 079/551] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?=
=?UTF-8?q?=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
修改了c语言实现的先序遍历函数名 和内部调用时用的函数名
及在preorderTraversal中的调用先序遍历操作的函数名
---
...04\351\200\222\345\275\222\351\201\215\345\216\206.md" | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md"
index 35d19d7b4a..612f2394f9 100644
--- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md"
+++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md"
@@ -371,18 +371,18 @@ C:
```c
//前序遍历:
-void preOrderTraversal(struct TreeNode* root, int* ret, int* returnSize) {
+void preOrder(struct TreeNode* root, int* ret, int* returnSize) {
if(root == NULL)
return;
ret[(*returnSize)++] = root->val;
- preOrderTraverse(root->left, ret, returnSize);
- preOrderTraverse(root->right, ret, returnSize);
+ preOrder(root->left, ret, returnSize);
+ preOrder(root->right, ret, returnSize);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
int* ret = (int*)malloc(sizeof(int) * 100);
*returnSize = 0;
- preOrderTraversal(root, ret, returnSize);
+ preOrder(root, ret, returnSize);
return ret;
}
From db84840986aab5ce791230eba963084bf1d5ba4b Mon Sep 17 00:00:00 2001
From: GitHubQAQ <31883473+GitHubQAQ@users.noreply.github.com>
Date: Tue, 19 Apr 2022 14:23:08 +0800
Subject: [PATCH 080/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A2=84=E5=88=A4?=
=?UTF-8?q?=E6=96=AD=E9=95=BF=E5=BA=A6=E7=9A=84=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
在magazine中字符存入数组前,预先判断ransomNote和magazine的相对长度,进行处理。
---
"problems/0383.\350\265\216\351\207\221\344\277\241.md" | 4 ++++
1 file changed, 4 insertions(+)
diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md"
index 0070734779..5d9e8295c6 100644
--- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md"
+++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md"
@@ -84,6 +84,10 @@ class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int record[26] = {0};
+ //add
+ if (ransomNote.size() > magazine.size()) {
+ return false;
+ }
for (int i = 0; i < magazine.length(); i++) {
// 通过recode数据记录 magazine里各个字符出现次数
record[magazine[i]-'a'] ++;
From 68e47dc4b30ffe38d1a26bb5104614f2f1cc067a Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Tue, 19 Apr 2022 17:10:20 +0800
Subject: [PATCH 081/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880452.?=
=?UTF-8?q?=E7=94=A8=E6=9C=80=E5=B0=91=E6=95=B0=E9=87=8F=E7=9A=84=E7=AE=AD?=
=?UTF-8?q?=E5=BC=95=E7=88=86=E6=B0=94=E7=90=83.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...25\347\210\206\346\260\224\347\220\203.md" | 26 ++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)
diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md"
index 33bbad5538..2ab14b612b 100644
--- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md"
+++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md"
@@ -193,7 +193,7 @@ func min(a,b int) int{
}
return a
}
-```
+```
### Javascript
```Javascript
@@ -214,7 +214,31 @@ var findMinArrowShots = function(points) {
};
```
+### TypeScript
+
+```typescript
+function findMinArrowShots(points: number[][]): number {
+ const length: number = points.length;
+ if (length === 0) return 0;
+ points.sort((a, b) => a[0] - b[0]);
+ let resCount: number = 1;
+ let right: number = points[0][1]; // 右边界
+ let tempPoint: number[];
+ for (let i = 1; i < length; i++) {
+ tempPoint = points[i];
+ if (tempPoint[0] > right) {
+ resCount++;
+ right = tempPoint[1];
+ } else {
+ right = Math.min(right, tempPoint[1]);
+ }
+ }
+ return resCount;
+};
+```
+
### C
+
```c
int cmp(const void *a,const void *b)
{
From 84f41f6685e744b138890cf7143ed3069a02a78e Mon Sep 17 00:00:00 2001
From: Guanzhong Pan
Date: Tue, 19 Apr 2022 17:00:17 +0100
Subject: [PATCH 082/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200135.=E5=88=86?=
=?UTF-8?q?=E5=8F=91=E7=B3=96=E6=9E=9C.md=20C=E8=AF=AD=E8=A8=80=E8=A7=A3?=
=?UTF-8?q?=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...06\345\217\221\347\263\226\346\236\234.md" | 41 +++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md"
index ccdabc16b4..27071aee20 100644
--- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md"
+++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md"
@@ -238,6 +238,47 @@ var candy = function(ratings) {
};
```
+### C
+```c
+#define max(a, b) (((a) > (b)) ? (a) : (b))
+
+int *initCandyArr(int size) {
+ int *candyArr = (int*)malloc(sizeof(int) * size);
+
+ int i;
+ for(i = 0; i < size; ++i)
+ candyArr[i] = 1;
+
+ return candyArr;
+}
+
+int candy(int* ratings, int ratingsSize){
+ // 初始化数组,每个小孩开始至少有一颗糖
+ int *candyArr = initCandyArr(ratingsSize);
+
+ int i;
+ // 先判断右边是否比左边评分高。若是,右边孩子的糖果为左边孩子+1(candyArr[i] = candyArr[i - 1] + 1)
+ for(i = 1; i < ratingsSize; ++i) {
+ if(ratings[i] > ratings[i - 1])
+ candyArr[i] = candyArr[i - 1] + 1;
+ }
+
+ // 再判断左边评分是否比右边高。
+ // 若是,左边孩子糖果为右边孩子糖果+1/自己所持糖果最大值。(若糖果已经比右孩子+1多,则不需要更多糖果)
+ // 举例:ratings为[1, 2, 3, 1]。此时评分为3的孩子在判断右边比左边大后为3,虽然它比最末尾的1(ratings[3])大,但是candyArr[3]为1。所以不必更新candyArr[2]
+ for(i = ratingsSize - 2; i >= 0; --i) {
+ if(ratings[i] > ratings[i + 1])
+ candyArr[i] = max(candyArr[i], candyArr[i + 1] + 1);
+ }
+
+ // 求出糖果之和
+ int result = 0;
+ for(i = 0; i < ratingsSize; ++i) {
+ result += candyArr[i];
+ }
+ return result;
+}
+```
-----------------------
From f9146a3b98fd46ca548c8852bf3b7df0625cfe72 Mon Sep 17 00:00:00 2001
From: zhenghao <1650937065@qq.com>
Date: Wed, 20 Apr 2022 10:23:18 +0800
Subject: [PATCH 083/551] =?UTF-8?q?=E6=9B=B4=E6=96=B00435=20=E6=97=A0?=
=?UTF-8?q?=E9=87=8D=E5=8F=A0=E5=8C=BA=E9=97=B4=20java=E7=89=88=E6=9C=AC?=
=?UTF-8?q?=E5=8F=B3=E8=BE=B9=E7=95=8C=E8=A7=A3=E6=B3=95=E4=B8=AD=E6=8E=92?=
=?UTF-8?q?=E5=BA=8F=E7=9A=84=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...7\240\351\207\215\345\217\240\345\214\272\351\227\264.md" | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md"
index b24ca0246d..cf58b9e2a9 100644
--- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md"
+++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md"
@@ -184,13 +184,14 @@ public:
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> {
- if (a[0] == a[0]) return a[1] - b[1];
- return a[0] - b[0];
+ // 按照区间右边界升序排序
+ return a[1] - b[1];
});
int count = 0;
int edge = Integer.MIN_VALUE;
for (int i = 0; i < intervals.length; i++) {
+ // 若上一个区间的右边界小于当前区间的左边界,说明无交集
if (edge <= intervals[i][0]) {
edge = intervals[i][1];
} else {
From b5f11af3edc66af674d486ace76cde8a99637170 Mon Sep 17 00:00:00 2001
From: zhenyu <48195906+szluyu99@users.noreply.github.com>
Date: Wed, 20 Apr 2022 23:56:20 +0800
Subject: [PATCH 084/551] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E9=97=AE?=
=?UTF-8?q?=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=AE=8C=E5=85=A8?=
=?UTF-8?q?=E8=83=8C=E5=8C=85.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md"
index faa1dc46e2..3ec399f16e 100644
--- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md"
+++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md"
@@ -37,7 +37,7 @@
* [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)
首先在回顾一下01背包的核心代码
-```
+```cpp
for(int i = 0; i < weight.size(); i++) { // 遍历物品
for(int j = bagWeight; j >= weight[i]; j--) { // 遍历背包容量
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
From 4154d3db2eadecdddaba45d09f09bfe16c0cc594 Mon Sep 17 00:00:00 2001
From: 243wresfdxvc
Date: Thu, 21 Apr 2022 09:55:19 +0000
Subject: [PATCH 085/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200134.=20=E5=8A=A0?=
=?UTF-8?q?=E6=B2=B9=E7=AB=99.md=20C=E8=AF=AD=E8=A8=80=E8=B4=AA=E5=BF=83?=
=?UTF-8?q?=E8=A7=A3=E6=B3=95=E6=96=B9=E6=B3=95=E4=BA=8C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4.\345\212\240\346\262\271\347\253\231.md" | 32 +++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md"
index ca95af6764..45e05fedc9 100644
--- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md"
+++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md"
@@ -341,6 +341,7 @@ var canCompleteCircuit = function(gas, cost) {
```
### C
+贪心算法:方法一
```c
int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){
int curSum = 0;
@@ -370,5 +371,36 @@ int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){
}
```
+贪心算法:方法二
+```c
+int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){
+ int curSum = 0;
+ int totalSum = 0;
+ int start = 0;
+
+ int i;
+ for(i = 0; i < gasSize; ++i) {
+ // 当前i站中加油量与耗油量的差
+ int diff = gas[i] - cost[i];
+
+ curSum += diff;
+ totalSum += diff;
+
+ // 若0到i的加油量都为负,则开始位置应为i+1
+ if(curSum < 0) {
+ curSum = 0;
+ // 当i + 1 == gasSize时,totalSum < 0(此时i为gasSize - 1),油车不可能返回原点
+ start = i + 1;
+ }
+ }
+
+ // 若总和小于0,加油车无论如何都无法返回原点。返回-1
+ if(totalSum < 0)
+ return -1;
+
+ return start;
+}
+```
+
-----------------------
From be07599474b87bf7dba3e97107b19435a304e5e7 Mon Sep 17 00:00:00 2001
From: berserk-112 <40333359+berserk-112@users.noreply.github.com>
Date: Fri, 22 Apr 2022 09:16:34 +0800
Subject: [PATCH 086/551] =?UTF-8?q?Update=200309.=E6=9C=80=E4=BD=B3?=
=?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E6=97=B6=E6=9C=BA=E5=90=AB?=
=?UTF-8?q?=E5=86=B7=E5=86=BB=E6=9C=9F.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...34\272\345\220\253\345\206\267\345\206\273\346\234\237.md" | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md"
index 53caa46e7a..8bd2fcf84c 100644
--- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md"
+++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md"
@@ -214,8 +214,8 @@ class Solution {
for (int i = 2; i <= prices.length; i++) {
/*
- dp[i][0] 第i天未持有股票收益;
- dp[i][1] 第i天持有股票收益;
+ dp[i][0] 第i天持有股票收益;
+ dp[i][1] 第i天不持有股票收益;
情况一:第i天是冷静期,不能以dp[i-1][1]购买股票,所以以dp[i - 2][1]买股票,没问题
情况二:第i天不是冷静期,理论上应该以dp[i-1][1]购买股票,但是第i天不是冷静期说明,第i-1天没有卖出股票,
则dp[i-1][1]=dp[i-2][1],所以可以用dp[i-2][1]买股票,没问题
From c3f746a25d81c299ca8a3efa4fc1f9e8d45509d3 Mon Sep 17 00:00:00 2001
From: qyg <1600314850@qq.com>
Date: Fri, 22 Apr 2022 10:55:01 +0800
Subject: [PATCH 087/551] =?UTF-8?q?0209.=E9=95=BF=E5=BA=A6=E6=9C=80?=
=?UTF-8?q?=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84=EF=BC=9A=E8=B0=83?=
=?UTF-8?q?=E6=95=B4=E7=AC=94=E8=AF=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
index 82a1138183..23b27edd13 100644
--- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
+++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
@@ -112,7 +112,7 @@ public:
**一些录友会疑惑为什么时间复杂度是O(n)**。
-不要以为for里放一个while就以为是O(n^2)啊, 主要是看每一个元素被操作的次数,每个元素在滑动窗后进来操作一次,出去操作一次,每个元素都是被被操作两次,所以时间复杂度是 2 × n 也就是O(n)。
+不要以为for里放一个while就以为是O(n^2)啊, 主要是看每一个元素被操作的次数,每个元素在滑动窗后进来操作一次,出去操作一次,每个元素都是被操作两次,所以时间复杂度是 2 × n 也就是O(n)。
## 相关题目推荐
From 4d668b0efc89cd15f50dcb93da01e1ace633b07b Mon Sep 17 00:00:00 2001
From: qyg <1600314850@qq.com>
Date: Fri, 22 Apr 2022 11:02:11 +0800
Subject: [PATCH 088/551] =?UTF-8?q?0059.=E8=9E=BA=E6=97=8B=E7=9F=A9?=
=?UTF-8?q?=E9=98=B5II=EF=BC=9A=E8=B0=83=E6=95=B4=E7=AC=94=E8=AF=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...9.\350\236\272\346\227\213\347\237\251\351\230\265II.md" | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
index 5c679982ce..1162c7ebb6 100644
--- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
+++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
@@ -30,7 +30,7 @@
相信很多同学刚开始做这种题目的时候,上来就是一波判断猛如虎。
-结果运行的时候各种问题,然后开始各种修修补补,最后发现改了这里哪里有问题,改了那里这里又跑不起来了。
+结果运行的时候各种问题,然后开始各种修修补补,最后发现改了这里那里有问题,改了那里这里又跑不起来了。
大家还记得我们在这篇文章[数组:每次遇到二分法,都是一看就会,一写就废](https://programmercarl.com/0704.二分查找.html)中讲解了二分法,提到如果要写出正确的二分法一定要坚持**循环不变量原则**。
@@ -47,7 +47,7 @@
可以发现这里的边界条件非常多,在一个循环中,如此多的边界条件,如果不按照固定规则来遍历,那就是**一进循环深似海,从此offer是路人**。
-这里一圈下来,我们要画每四条边,这四条边怎么画,每画一条边都要坚持一致的左闭右开,或者左开又闭的原则,这样这一圈才能按照统一的规则画下来。
+这里一圈下来,我们要画每四条边,这四条边怎么画,每画一条边都要坚持一致的左闭右开,或者左开右闭的原则,这样这一圈才能按照统一的规则画下来。
那么我按照左闭右开的原则,来画一圈,大家看一下:
@@ -59,7 +59,7 @@
一些同学做这道题目之所以一直写不好,代码越写越乱。
-就是因为在画每一条边的时候,一会左开又闭,一会左闭右闭,一会又来左闭右开,岂能不乱。
+就是因为在画每一条边的时候,一会左开右闭,一会左闭右闭,一会又来左闭右开,岂能不乱。
代码如下,已经详细注释了每一步的目的,可以看出while循环里判断的情况是很多的,代码里处理的原则也是统一的左闭右开。
From 664746fe155a137f01c5d1478b0325c2dd8e0ffe Mon Sep 17 00:00:00 2001
From: qyg <1600314850@qq.com>
Date: Fri, 22 Apr 2022 11:21:18 +0800
Subject: [PATCH 089/551] =?UTF-8?q?=E6=95=B0=E7=BB=84=E6=80=BB=E7=BB=93?=
=?UTF-8?q?=E7=AF=87=EF=BC=9A=E8=B0=83=E6=95=B4=E7=AC=94=E8=AF=AF=EF=BC=8C?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=8C=E7=BB=B4=E6=95=B0=E7=BB=84=E7=9A=84?=
=?UTF-8?q?=E5=86=85=E5=AE=B9=E6=98=AF=E9=92=88=E5=AF=B9Java=E8=AF=AD?=
=?UTF-8?q?=E8=A8=80=E7=9A=84=E9=99=90=E5=AE=9A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...60\347\273\204\346\200\273\347\273\223\347\257\207.md" | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md"
index d256298b9a..39fa17a61d 100644
--- "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md"
+++ "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md"
@@ -43,19 +43,19 @@
**那么二维数组在内存的空间地址是连续的么?**
-我们来举一个例子,例如: `int[][] rating = new int[3][4];` , 这个二维数据在内存空间可不是一个 `3*4` 的连续地址空间
+我们来举一个Java的例子,例如: `int[][] rating = new int[3][4];` , 这个二维数组在内存空间可不是一个 `3*4` 的连续地址空间
看了下图,就应该明白了:
-所以**二维数据在内存中不是 `3*4` 的连续地址空间,而是四条连续的地址空间组成!**
+所以**Java的二维数组在内存中不是 `3*4` 的连续地址空间,而是四条连续的地址空间组成!**
# 数组的经典题目
在面试中,数组是必考的基础数据结构。
-其实数据的题目在思想上一般比较简单的,但是如果想高效,并不容易。
+其实数组的题目在思想上一般比较简单的,但是如果想高效,并不容易。
我们之前一共讲解了四道经典数组题目,每一道题目都代表一个类型,一种思想。
@@ -115,7 +115,7 @@
在这道题目中,我们再一次介绍到了**循环不变量原则**,其实这也是写程序中的重要原则。
-相信大家又遇到过这种情况: 感觉题目的边界调节超多,一波接着一波的判断,找边界,踩了东墙补西墙,好不容易运行通过了,代码写的十分冗余,毫无章法,其实**真正解决题目的代码都是简洁的,或者有原则性的**,大家可以在这道题目中体会到这一点。
+相信大家有遇到过这种情况: 感觉题目的边界调节超多,一波接着一波的判断,找边界,拆了东墙补西墙,好不容易运行通过了,代码写的十分冗余,毫无章法,其实**真正解决题目的代码都是简洁的,或者有原则性的**,大家可以在这道题目中体会到这一点。
# 总结
From 074937378aad536271afd42fdb5cc27a1f193f7f Mon Sep 17 00:00:00 2001
From: qyg <1600314850@qq.com>
Date: Fri, 22 Apr 2022 11:42:50 +0800
Subject: [PATCH 090/551] =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=90=86=E8=AE=BA?=
=?UTF-8?q?=E5=9F=BA=E7=A1=80=EF=BC=9A=E8=B0=83=E6=95=B4=E7=AC=94=E8=AF=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...41\250\347\220\206\350\256\272\345\237\272\347\241\200.md" | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md"
index 095282f54f..2fe9f14c7a 100644
--- "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md"
+++ "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md"
@@ -24,7 +24,7 @@
## 双链表
-单链表中的节点只能指向节点的下一个节点。
+单链表中的指针域只能指向节点的下一个节点。
双链表:每一个节点有两个指针域,一个指向下一个节点,一个指向上一个节点。
@@ -56,7 +56,7 @@

-这个链表起始节点为2, 终止节点为7, 各个节点分布在内存个不同地址空间上,通过指针串联在一起。
+这个链表起始节点为2, 终止节点为7, 各个节点分布在内存的不同地址空间上,通过指针串联在一起。
# 链表的定义
From be4051893888427b41b172135b21add3fa096579 Mon Sep 17 00:00:00 2001
From: Jamcy123 <1219502823@qq.com>
Date: Fri, 22 Apr 2022 15:34:06 +0800
Subject: [PATCH 091/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201047.=20=E5=88=A0?=
=?UTF-8?q?=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80?=
=?UTF-8?q?=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9=20?=
=?UTF-8?q?=E5=8F=8C=E6=8C=87=E9=92=88=E8=A7=A3=E6=B3=95=20javascript?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...73\351\207\215\345\244\215\351\241\271.md" | 25 ++++++++++++++++---
1 file changed, 21 insertions(+), 4 deletions(-)
diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md"
index 54455f6273..638c8f4ed2 100644
--- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md"
+++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md"
@@ -250,11 +250,9 @@ func removeDuplicates(s string) string {
javaScript:
+法一:使用栈
+
```js
-/**
- * @param {string} s
- * @return {string}
- */
var removeDuplicates = function(s) {
const stack = [];
for(const x of s) {
@@ -267,6 +265,25 @@ var removeDuplicates = function(s) {
};
```
+法二:双指针(模拟栈)
+
+```js
+// 原地解法(双指针模拟栈)
+var removeDuplicates = function(s) {
+ s = [...s];
+ let top = -1; // 指向栈顶元素的下标
+ for(let i = 0; i < s.length; i++) {
+ if(top === -1 || s[top] !== s[i]) { // top === -1 即空栈
+ s[++top] = s[i]; // 入栈
+ } else {
+ top--; // 推出栈
+ }
+ }
+ s.length = top + 1; // 栈顶元素下标 + 1 为栈的长度
+ return s.join('');
+};
+```
+
TypeScript:
```typescript
From 5a7e247e5f5a70c1e33dfce4cc42b498cc031f61 Mon Sep 17 00:00:00 2001
From: ChubbyPan
Date: Fri, 22 Apr 2022 07:59:32 +0000
Subject: [PATCH 092/551] update build binary tree in ACM pattern with python
---
...72\344\272\214\345\217\211\346\240\221.md" | 56 ++++++++++++++++++-
1 file changed, 55 insertions(+), 1 deletion(-)
diff --git "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md"
index bd2e978046..f6ec2dd3f9 100644
--- "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md"
@@ -213,7 +213,61 @@ int main() {
## Python
-```Python
+```Python3
+class TreeNode:
+ def __init__(self, val = 0, left = None, right = None):
+ self.val = val
+ self.left = left
+ self.right = right
+
+
+# 根据数组构建二叉树
+
+def construct_binary_tree(nums: []) -> TreeNode:
+ if not nums:
+ return None
+ # 用于存放构建好的节点
+ root = TreeNode(-1)
+ Tree = []
+ # 将数组元素全部转化为树节点
+ for i in range(len(nums)):
+ if nums[i]!= -1:
+ node = TreeNode(nums[i])
+ else:
+ node = None
+ Tree.append(node)
+ if i == 0:
+ root = node
+ for i in range(len(Tree)):
+ node = Tree[i]
+ if node and (2 * i + 2) < len(Tree):
+ node.left = Tree[i * 2 + 1]
+ node.right = Tree[i * 2 + 2]
+ return root
+
+
+
+# 算法:中序遍历二叉树
+
+class Solution:
+ def __init__(self):
+ self.T = []
+ def inorder(self, root: TreeNode) -> []:
+ if not root:
+ return
+ self.inorder(root.left)
+ self.T.append(root.val)
+ self.inorder(root.right)
+ return self.T
+
+
+
+# 验证创建二叉树的有效性,二叉排序树的中序遍历应为顺序排列
+
+test_tree = [3, 1, 5, -1, 2, 4 ,6]
+root = construct_binary_tree(test_tree)
+A = Solution()
+print(A.inorder(root))
```
From a0c16d48cb851b3c118cf3da0900797be767f9d0 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 23 Apr 2022 18:19:36 +0800
Subject: [PATCH 093/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880435.?=
=?UTF-8?q?=E6=97=A0=E9=87=8D=E5=8F=A0=E5=8C=BA=E9=97=B4.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...15\345\217\240\345\214\272\351\227\264.md" | 51 ++++++++++++++++++-
1 file changed, 50 insertions(+), 1 deletion(-)
diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md"
index b24ca0246d..c7f755bf33 100644
--- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md"
+++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md"
@@ -263,7 +263,7 @@ func min(a,b int)int{
}
return a
}
-```
+```
### Javascript:
- 按右边界排序
@@ -306,6 +306,55 @@ var eraseOverlapIntervals = function(intervals) {
}
```
+### TypeScript
+
+> 按右边界排序,从左往右遍历
+
+```typescript
+function eraseOverlapIntervals(intervals: number[][]): number {
+ const length = intervals.length;
+ if (length === 0) return 0;
+ intervals.sort((a, b) => a[1] - b[1]);
+ let right: number = intervals[0][1];
+ let count: number = 1;
+ for (let i = 1; i < length; i++) {
+ if (intervals[i][0] >= right) {
+ count++;
+ right = intervals[i][1];
+ }
+ }
+ return length - count;
+};
+```
+
+> 按左边界排序,从左往右遍历
+
+```typescript
+function eraseOverlapIntervals(intervals: number[][]): number {
+ if (intervals.length === 0) return 0;
+ intervals.sort((a, b) => a[0] - b[0]);
+ let right: number = intervals[0][1];
+ let tempInterval: number[];
+ let resCount: number = 0;
+ for (let i = 1, length = intervals.length; i < length; i++) {
+ tempInterval = intervals[i];
+ if (tempInterval[0] >= right) {
+ // 未重叠
+ right = tempInterval[1];
+ } else {
+ // 有重叠,移除当前interval和前一个interval中右边界更大的那个
+ right = Math.min(right, tempInterval[1]);
+ resCount++;
+ }
+ }
+ return resCount;
+};
+```
+
+
+
+
+
-----------------------
From 7c752afaf472f8e3056d9bfefd9628548cbec5d8 Mon Sep 17 00:00:00 2001
From: h4 <20080114+tan-i-ham@users.noreply.github.com>
Date: Sat, 23 Apr 2022 22:19:09 +0900
Subject: [PATCH 094/551] chore: Sync 150 markdown render format
---
...41\250\350\276\276\345\274\217\346\261\202\345\200\274.md" | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
index f4dad8231a..fd3d69aa33 100644
--- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
+++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
@@ -109,7 +109,7 @@ public:
};
```
-# 题外话
+## 题外话
我们习惯看到的表达式都是中缀表达式,因为符合我们的习惯,但是中缀表达式对于计算机来说就不是很友好了。
@@ -128,7 +128,7 @@ public:
-# 其他语言版本
+## 其他语言版本
java:
From 531c1b0a3bfac9867a8403baaf95a6cd284f6a41 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 23 Apr 2022 21:19:23 +0800
Subject: [PATCH 095/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880763.?=
=?UTF-8?q?=E5=88=92=E5=88=86=E5=AD=97=E6=AF=8D=E5=8C=BA=E9=97=B4.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...27\346\257\215\345\214\272\351\227\264.md" | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
index 03d3a73b3b..901dccb4b8 100644
--- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
+++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
@@ -174,6 +174,31 @@ var partitionLabels = function(s) {
};
```
+### TypeScript
+
+```typescript
+function partitionLabels(s: string): number[] {
+ const length: number = s.length;
+ const resArr: number[] = [];
+ const helperMap: Map = new Map();
+ for (let i = 0; i < length; i++) {
+ helperMap.set(s[i], i);
+ }
+ let left: number = 0;
+ let right: number = 0;
+ for (let i = 0; i < length; i++) {
+ right = Math.max(helperMap.get(s[i])!, right);
+ if (i === right) {
+ resArr.push(i - left + 1);
+ left = i + 1;
+ }
+ }
+ return resArr;
+};
+```
+
+
+
-----------------------
From 8966752197c174e46052ab549495a045ce1a5f14 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 23 Apr 2022 21:44:11 +0800
Subject: [PATCH 096/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880056.?=
=?UTF-8?q?=E5=90=88=E5=B9=B6=E5=8C=BA=E9=97=B4.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...10\345\271\266\345\214\272\351\227\264.md" | 20 +++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
index a9caeaf057..b44d602c5c 100644
--- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
+++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
@@ -266,6 +266,26 @@ var merge = function(intervals) {
};
```
+### TypeScript
+
+```typescript
+function merge(intervals: number[][]): number[][] {
+ const resArr: number[][] = [];
+ intervals.sort((a, b) => a[0] - b[0]);
+ resArr[0] = [...intervals[0]]; // 避免修改原intervals
+ for (let i = 1, length = intervals.length; i < length; i++) {
+ let interval: number[] = intervals[i];
+ let last: number[] = resArr[resArr.length - 1];
+ if (interval[0] <= last[1]) {
+ last[1] = Math.max(interval[1], last[1]);
+ } else {
+ resArr.push([...intervals[i]]);
+ }
+ }
+ return resArr;
+};
+```
+
-----------------------
From e2807eb59ad5b9e45311544278c905fd942e85b3 Mon Sep 17 00:00:00 2001
From: 243wresfdxvc
Date: Sat, 23 Apr 2022 16:15:53 +0000
Subject: [PATCH 097/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200203.=E7=A7=BB?=
=?UTF-8?q?=E5=87=BA=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0.md=20C=E8=AF=AD?=
=?UTF-8?q?=E8=A8=80=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...76\350\241\250\345\205\203\347\264\240.md" | 32 +++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
index c34831b768..751553e26f 100644
--- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
+++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
@@ -145,6 +145,38 @@ public:
## 其他语言版本
C:
+用原来的链表操作:
+```c
+struct ListNode* removeElements(struct ListNode* head, int val){
+ struct ListNode* temp;
+ // 当头结点存在并且头结点的值等于val时
+ while(head && head->val == val) {
+ temp = head;
+ // 将新的头结点设置为head->next并删除原来的头结点
+ head = head->next;
+ free(temp);
+ }
+
+ struct ListNode *cur = head;
+ // 当cur存在并且cur->next存在时
+ // 此解法需要判断cur存在因为cur指向head。若head本身为NULL或者原链表中元素都为val的话,cur也会为NULL
+ while(cur && (temp = cur->next)) {
+ // 若cur->next的值等于val
+ if(temp->val == val) {
+ // 将cur->next设置为cur->next->next并删除cur->next
+ cur->next = temp->next;
+ free(temp);
+ }
+ // 若cur->next不等于val,则将cur后移一位
+ else
+ cur = cur->next;
+ }
+
+ // 返回头结点
+ return head;
+}
+```
+设置一个虚拟头结点:
```c
/**
* Definition for singly-linked list.
From 7cf68bd1f2eaa6ad83d69b969f29b1b146397dfa Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 24 Apr 2022 17:40:23 +0800
Subject: [PATCH 098/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880738.?=
=?UTF-8?q?=E5=8D=95=E8=B0=83=E9=80=92=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97?=
=?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...36\347\232\204\346\225\260\345\255\227.md" | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md"
index c8ce8a2b9a..4e4079a76e 100644
--- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md"
+++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md"
@@ -225,6 +225,28 @@ var monotoneIncreasingDigits = function(n) {
};
```
+### TypeScript
+
+```typescript
+function monotoneIncreasingDigits(n: number): number {
+ let strArr: number[] = String(n).split('').map(i => parseInt(i));
+ const length = strArr.length;
+ let flag: number = length;
+ for (let i = length - 2; i >= 0; i--) {
+ if (strArr[i] > strArr[i + 1]) {
+ strArr[i] -= 1;
+ flag = i + 1;
+ }
+ }
+ for (let i = flag; i < length; i++) {
+ strArr[i] = 9;
+ }
+ return parseInt(strArr.join(''));
+};
+```
+
+
+
-----------------------
From d5f21d534198069fee0dd4a90397d1322d435179 Mon Sep 17 00:00:00 2001
From: eat to 160 pounds <2915390277@qq.com>
Date: Sun, 24 Apr 2022 21:05:31 +0800
Subject: [PATCH 099/551] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E4=B8=89?=
=?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8Cjavascript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...11\346\225\260\344\271\213\345\222\214.md" | 70 +++++++------------
1 file changed, 26 insertions(+), 44 deletions(-)
diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
index bfde6b3538..cc184c879e 100644
--- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
@@ -313,54 +313,36 @@ func threeSum(nums []int)[][]int{
javaScript:
```js
-/**
- * @param {number[]} nums
- * @return {number[][]}
- */
-
-// 循环内不考虑去重
-var threeSum = function(nums) {
- const len = nums.length;
- if(len < 3) return [];
- nums.sort((a, b) => a - b);
- const resSet = new Set();
- for(let i = 0; i < len - 2; i++) {
- if(nums[i] > 0) break;
- let l = i + 1, r = len - 1;
- while(l < r) {
- const sum = nums[i] + nums[l] + nums[r];
- if(sum < 0) { l++; continue };
- if(sum > 0) { r--; continue };
- resSet.add(`${nums[i]},${nums[l]},${nums[r]}`);
- l++;
- r--;
- }
- }
- return Array.from(resSet).map(i => i.split(","));
-};
-
-// 去重优化
var threeSum = function(nums) {
- const len = nums.length;
- if(len < 3) return [];
- nums.sort((a, b) => a - b);
- const res = [];
- for(let i = 0; i < len - 2; i++) {
- if(nums[i] > 0) break;
- // a去重
- if(i > 0 && nums[i] === nums[i - 1]) continue;
- let l = i + 1, r = len - 1;
+ const res = [], len = nums.length
+ // 将数组排序
+ nums.sort((a, b) => a - b)
+ for (let i = 0; i < len; i++) {
+ let l = i + 1, r = len - 1, iNum = nums[i]
+ // 数组排过序,如果第一个数大于0直接返回res
+ if (iNum > 0) return res
+ // 去重
+ if (iNum == nums[i - 1]) continue
while(l < r) {
- const sum = nums[i] + nums[l] + nums[r];
- if(sum < 0) { l++; continue };
- if(sum > 0) { r--; continue };
- res.push([nums[i], nums[l], nums[r]])
- // b c 去重
- while(l < r && nums[l] === nums[++l]);
- while(l < r && nums[r] === nums[--r]);
+ let lNum = nums[l], rNum = nums[r], threeSum = iNum + lNum + rNum
+ // 三数之和小于0,则左指针向右移动
+ if (threeSum < 0) l++
+ else if (threeSum > 0) r--
+ else {
+ res.push([iNum, lNum, rNum])
+ // 去重
+ while(l < r && nums[l] == nums[l + 1]){
+ l++
+ }
+ while(l < r && nums[r] == nums[r - 1]) {
+ r--
+ }
+ l++
+ r--
+ }
}
}
- return res;
+ return res
};
```
TypeScript:
From 84750aca45f96f15e4dc271410c32cde78667e41 Mon Sep 17 00:00:00 2001
From: 243wresfdxvc
Date: Sun, 24 Apr 2022 18:32:09 +0000
Subject: [PATCH 100/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200860.=E6=9F=A0?=
=?UTF-8?q?=E6=AA=AC=E6=B0=B4=E6=89=BE=E9=9B=B6.md=20C=E8=AF=AD=E8=A8=80?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...54\346\260\264\346\211\276\351\233\266.md" | 43 +++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md"
index ffd5490d6d..2738f57497 100644
--- "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md"
+++ "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md"
@@ -250,6 +250,49 @@ var lemonadeChange = function(bills) {
return true
};
+```
+### C
+```c
+bool lemonadeChange(int* bills, int billsSize){
+ // 分别记录五元、十元的数量(二十元不用记录,因为不会用到20元找零)
+ int fiveCount = 0; int tenCount = 0;
+
+ int i;
+ for(i = 0; i < billsSize; ++i) {
+ // 分情况讨论每位顾客的付款
+ switch(bills[i]) {
+ // 情况一:直接收款五元
+ case 5:
+ fiveCount++;
+ break;
+ // 情况二:收款十元
+ case 10:
+ // 若没有五元找零,返回false
+ if(fiveCount == 0)
+ return false;
+ // 收款十元并找零五元
+ fiveCount--;
+ tenCount++;
+ break;
+ // 情况三:收款二十元
+ case 20:
+ // 若可以,优先用十元和五元找零(因为十元只能找零20,所以需要尽量用掉。而5元能找零十元和二十元)
+ if(fiveCount > 0 && tenCount > 0) {
+ fiveCount--;
+ tenCount--;
+ }
+ // 若没有十元,但是有三张五元。用三张五元找零
+ else if(fiveCount >= 3)
+ fiveCount-=3;
+ // 无法找开,返回false
+ else
+ return false;
+ break;
+ }
+ }
+ // 全部可以找开,返回true
+ return true;
+}
```
-----------------------
From 8291e5e1c6d1b9c913a35015848124ad9cfd1808 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Mon, 25 Apr 2022 14:05:53 +0800
Subject: [PATCH 101/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880714.?=
=?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3?=
=?UTF-8?q?=E6=97=B6=E6=9C=BA=E5=90=AB=E6=89=8B=E7=BB=AD=E8=B4=B9.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...53\346\211\213\347\273\255\350\264\271.md" | 44 +++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md"
index 2f27d6eaf5..b27631c68c 100644
--- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md"
+++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md"
@@ -293,6 +293,50 @@ var maxProfit = function(prices, fee) {
};
```
+TypeScript:
+
+> 贪心
+
+```typescript
+function maxProfit(prices: number[], fee: number): number {
+ if (prices.length === 0) return 0;
+ let minPrice: number = prices[0];
+ let profit: number = 0;
+ for (let i = 1, length = prices.length; i < length; i++) {
+ if (minPrice > prices[i]) {
+ minPrice = prices[i];
+ }
+ if (minPrice + fee < prices[i]) {
+ profit += prices[i] - minPrice - fee;
+ minPrice = prices[i] - fee;
+ }
+ }
+ return profit;
+};
+```
+
+> 动态规划
+
+```typescript
+function maxProfit(prices: number[], fee: number): number {
+ /**
+ dp[i][1]: 第i天不持有股票的最大所剩现金
+ dp[i][0]: 第i天持有股票的最大所剩现金
+ */
+ const length: number = prices.length;
+ const dp: number[][] = new Array(length).fill(0).map(_ => []);
+ dp[0][1] = 0;
+ dp[0][0] = -prices[0];
+ for (let i = 1, length = prices.length; i < length; i++) {
+ dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee);
+ dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
+ }
+ return Math.max(dp[length - 1][0], dp[length - 1][1]);
+};
+```
+
+
+
-----------------------
From 7b45a3209014190bbee02de8df566638701db734 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=BC=A0=E4=BD=B3=E4=B9=90?= <1791781644@qq.com>
Date: Mon, 25 Apr 2022 22:10:18 +0800
Subject: [PATCH 102/551] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20=E7=BB=99=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E9=81=8D=E5=8E=86=E9=A2=98=E7=9B=AE=E5=8A=A0?=
=?UTF-8?q?=E4=B8=8A=E5=8A=9B=E6=89=A3=E9=93=BE=E6=8E=A5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\204\350\277\255\344\273\243\351\201\215\345\216\206.md" | 6 +++---
...\204\351\200\222\345\275\222\351\201\215\345\216\206.md" | 6 +++---
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md"
index 8164724b31..13ba5f1e71 100644
--- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md"
+++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md"
@@ -11,9 +11,9 @@
看完本篇大家可以使用迭代法,再重新解决如下三道leetcode上的题目:
-* 144.二叉树的前序遍历
-* 94.二叉树的中序遍历
-* 145.二叉树的后序遍历
+* [144.二叉树的前序遍历](https://leetcode-cn.com/problems/binary-tree-preorder-traversal/)
+* [94.二叉树的中序遍历](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/)
+* [145.二叉树的后序遍历](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/)
为什么可以用迭代法(非递归的方式)来实现二叉树的前后中序遍历呢?
diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md"
index 35d19d7b4a..13e704ea94 100644
--- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md"
+++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md"
@@ -99,9 +99,9 @@ void traversal(TreeNode* cur, vector& vec) {
此时大家可以做一做leetcode上三道题目,分别是:
-* 144.二叉树的前序遍历
-* 145.二叉树的后序遍历
-* 94.二叉树的中序遍历
+* [144.二叉树的前序遍历](https://leetcode-cn.com/problems/binary-tree-preorder-traversal/)
+* [145.二叉树的后序遍历](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/)
+* [94.二叉树的中序遍历](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/)
可能有同学感觉前后中序遍历的递归太简单了,要打迭代法(非递归),别急,我们明天打迭代法,打个通透!
From 9da09584cd39f56c937b5c68b7375ab0d602957b Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Tue, 26 Apr 2022 00:03:54 +0800
Subject: [PATCH 103/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880968.?=
=?UTF-8?q?=E7=9B=91=E6=8E=A7=E4=BA=8C=E5=8F=89=E6=A0=91.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\344\272\214\345\217\211\346\240\221.md" | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md"
index 35c3ccdc2b..9a510a1b79 100644
--- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md"
@@ -476,7 +476,35 @@ var minCameraCover = function(root) {
};
```
+### TypeScript
+
+```typescript
+function minCameraCover(root: TreeNode | null): number {
+ /** 0-无覆盖, 1-有摄像头, 2-有覆盖 */
+ type statusCode = 0 | 1 | 2;
+ let resCount: number = 0;
+ if (recur(root) === 0) resCount++;
+ return resCount;
+ function recur(node: TreeNode | null): statusCode {
+ if (node === null) return 2;
+ const left: statusCode = recur(node.left),
+ right: statusCode = recur(node.right);
+ let resStatus: statusCode = 0;
+ if (left === 0 || right === 0) {
+ resStatus = 1;
+ resCount++;
+ } else if (left === 1 || right === 1) {
+ resStatus = 2;
+ } else {
+ resStatus = 0;
+ }
+ return resStatus;
+ }
+};
+```
+
### C
+
```c
/*
**函数后序遍历二叉树。判断一个结点状态时,根据其左右孩子结点的状态进行判断
From 3ec08f41dd08cdd830215bdf2b602bbdcfc92512 Mon Sep 17 00:00:00 2001
From: 243wresfdxvc
Date: Mon, 25 Apr 2022 18:18:50 +0000
Subject: [PATCH 104/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200406.=E6=A0=B9?=
=?UTF-8?q?=E6=8D=AE=E8=BA=AB=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97?=
=?UTF-8?q?.md=20C=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...15\345\273\272\351\230\237\345\210\227.md" | 47 +++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md"
index b2354d094c..9cefa11cda 100644
--- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md"
+++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md"
@@ -290,6 +290,53 @@ var reconstructQueue = function(people) {
};
```
+### C
+```c
+int cmp(const void *p1, const void *p2) {
+ int *pp1 = *(int**)p1;
+ int *pp2 = *(int**)p2;
+ // 若身高相同,则按照k从小到大排列
+ // 若身高不同,按身高从大到小排列
+ return pp1[0] == pp2[0] ? pp1[1] - pp2[1] : pp2[0] - pp1[0];
+}
+
+// 将start与end中间的元素都后移一位
+// start为将要新插入元素的位置
+void moveBack(int **people, int peopleSize, int start, int end) {
+ int i;
+ for(i = end; i > start; i--) {
+ people[i] = people[i-1];
+ }
+}
+
+int** reconstructQueue(int** people, int peopleSize, int* peopleColSize, int* returnSize, int** returnColumnSizes){
+ int i;
+ // 将people按身高从大到小排列(若身高相同,按k从小到大排列)
+ qsort(people, peopleSize, sizeof(int*), cmp);
+
+ for(i = 0; i < peopleSize; ++i) {
+ // people[i]要插入的位置
+ int position = people[i][1];
+ int *temp = people[i];
+ // 将position到i中间的元素后移一位
+ // 注:因为已经排好序,position不会比i大。(举例:排序后people最后一位元素最小,其可能的k最大值为peopleSize-2,小于此时的i)
+ moveBack(people, peopleSize, position, i);
+ // 将temp放置到position处
+ people[position] = temp;
+
+ }
+
+
+ // 设置返回二维数组的大小以及里面每个一维数组的长度
+ *returnSize = peopleSize;
+ *returnColumnSizes = (int*)malloc(sizeof(int) * peopleSize);
+ for(i = 0; i < peopleSize; ++i) {
+ (*returnColumnSizes)[i] = 2;
+ }
+ return people;
+}
+```
+
-----------------------
From fa2e1b097b555f900dce38f01e158634df074817 Mon Sep 17 00:00:00 2001
From: programmercarl <826123027@qq.com>
Date: Tue, 26 Apr 2022 10:59:56 +0800
Subject: [PATCH 105/551] Update
---
...3\350\241\241\344\272\214\345\217\211\346\240\221.md" | 2 +-
...0\347\273\204\346\200\273\347\273\223\347\257\207.md" | 9 ++++-----
...6\350\241\250\346\200\273\347\273\223\347\257\207.md" | 4 ++++
3 files changed, 9 insertions(+), 6 deletions(-)
diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md"
index 411434d411..d98ff8a90c 100644
--- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md"
@@ -264,7 +264,7 @@ int getDepth(TreeNode* cur) {
}
```
-然后再用栈来模拟前序遍历,遍历每一个节点的时候,再去判断左右孩子的高度是否符合,代码如下:
+然后再用栈来模拟后序遍历,遍历每一个节点的时候,再去判断左右孩子的高度是否符合,代码如下:
```CPP
bool isBalanced(TreeNode* root) {
diff --git "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md"
index d256298b9a..ef7a487651 100644
--- "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md"
+++ "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md"
@@ -120,14 +120,13 @@
# 总结
-从二分法到双指针,从滑动窗口到螺旋矩阵,相信如果大家真的认真做了「代码随想录」每日推荐的题目,定会有所收获。
-
-推荐的题目即使大家之前做过了,再读一遍文章,也会帮助你提炼出解题的精髓所在。
+
-如果感觉有所收获,希望大家多多支持,打卡转发,点赞在看 都是对我最大的鼓励!
+这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412),所画,总结的非常好,分享给大家。
-最后,大家周末愉快!
+从二分法到双指针,从滑动窗口到螺旋矩阵,相信如果大家真的认真做了「代码随想录」每日推荐的题目,定会有所收获。
+推荐的题目即使大家之前做过了,再读一遍文章,也会帮助你提炼出解题的精髓所在。
-----------------------
diff --git "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md"
index abbb288e3b..e3fe47401b 100644
--- "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md"
+++ "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md"
@@ -76,6 +76,10 @@
## 总结
+
+
+这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412),所画,总结的非常好,分享给大家。
+
考察链表的操作其实就是考察指针的操作,是面试中的常见类型。
链表篇中开头介绍[链表理论知识](https://programmercarl.com/0203.移除链表元素.html),然后分别通过经典题目介绍了如下知识点:
From e15492b0b69ca06d8d90057093237de8767bf784 Mon Sep 17 00:00:00 2001
From: dbcai
Date: Tue, 26 Apr 2022 22:08:25 +0800
Subject: [PATCH 106/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=89=8D=E5=BA=8F?=
=?UTF-8?q?.ACM=E6=A8=A1=E5=BC=8F=E5=A6=82=E4=BD=95=E6=9E=84=E5=BB=BA?=
=?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Java=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...72\344\272\214\345\217\211\346\240\221.md" | 53 +++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md"
index 28c4b6f7ae..07674b312c 100644
--- "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md"
@@ -217,6 +217,59 @@ int main() {
## Java
```Java
+public class Solution {
+ // 节点类
+ static class TreeNode {
+ // 节点值
+ int val;
+
+ // 左节点
+ TreeNode left;
+
+ // 右节点
+ TreeNode right;
+
+ // 节点的构造函数(默认左右节点都为null)
+ public TreeNode(int x) {
+ this.val = x;
+ this.left = null;
+ this.right = null;
+ }
+ }
+
+ /**
+ * 根据数组构建二叉树
+ * @param arr 树的数组表示
+ * @return 构建成功后树的根节点
+ */
+ public TreeNode constructBinaryTree(final int[] arr) {
+ // 构建和原数组相同的树节点列表
+ List treeNodeList = arr.length > 0 ? new ArrayList<>(arr.length) : null;
+ TreeNode root = null;
+ // 把输入数值数组,先转化为二叉树节点列表
+ for (int i = 0; i < arr.length; i++) {
+ TreeNode node = null;
+ if (arr[i] != -1) { // 用 -1 表示null
+ node = new TreeNode(arr[i]);
+ }
+ treeNodeList.add(node);
+ if (i == 0) {
+ root = node;
+ }
+ }
+ // 遍历一遍,根据规则左右孩子赋值就可以了
+ // 注意这里 结束规则是 i * 2 + 2 < arr.length,避免空指针
+ for (int i = 0; i * 2 + 2 < arr.length; i++) {
+ TreeNode node = treeNodeList.get(i);
+ if (node != null) {
+ // 线性存储转连式存储关键逻辑
+ node.left = treeNodeList.get(2 * i + 1);
+ node.right = treeNodeList.get(2 * i + 2);
+ }
+ }
+ return root;
+ }
+}
```
From ce8e2019db658788e1231c5d56d2e50c3cc417a0 Mon Sep 17 00:00:00 2001
From: programmercarl <826123027@qq.com>
Date: Wed, 27 Apr 2022 00:00:37 +0800
Subject: [PATCH 107/551] Update
---
README.md | 42 +---
"problems/0051.N\347\232\207\345\220\216.md" | 2 -
...50\345\272\223\345\207\275\346\225\260.md" | 8 +-
...76\351\235\236\347\247\221\347\217\255.md" | 47 -----
...50\346\204\217\344\272\213\351\241\271.md" | 89 --------
...4-\345\206\263\350\265\233\345\234\210.md" | 87 --------
...25\345\216\273\344\275\225\344\273\216.md" | 88 --------
...er\347\232\204\351\200\211\346\213\251.md" | 137 -------------
...67\347\232\204\344\270\203\345\244\225.md" | 73 -------
...01\346\200\216\344\271\210\345\212\236.md" | 45 ----
...76\345\274\203\347\247\213\346\213\233.md" | 81 --------
...45\350\277\231\344\271\210\345\206\231.md" | 69 -------
...33\350\267\237\344\270\215\344\270\212.md" | 55 -----
...66\347\232\204\347\226\221\351\227\256.md" | 87 --------
...00\344\272\233\345\273\272\350\256\256.md" | 72 -------
...00\344\272\233\351\227\256\351\242\230.md" | 100 ---------
...\347\240\224VS\345\267\245\344\275\234.md" | 49 -----
...01\345\210\267\344\270\244\351\201\215.md" | 67 ------
...14\350\256\241\347\256\227\346\234\272.md" | 53 -----
...57\346\234\200\345\245\275\347\232\204.md" | 37 ----
...22\345\261\212\347\247\213\346\213\233.md" | 67 ------
...71\345\205\273\344\275\223\347\263\273.md" | 81 --------
...46\344\270\200\350\210\254\351\273\221.md" | 71 -------
...13\346\213\233\345\244\215\344\271\240.md" | 47 -----
.../\345\256\242\344\270\211\346\266\210.md" | 95 ---------
...00\346\234\257\346\226\271\345\220\221.md" | 32 ---
...17\345\274\200\345\247\213\344\272\206.md" | 48 -----
...4\276\235\347\204\266\346\262\241offer.md" | 99 ---------
...13\346\213\233\345\274\200\345\245\226.md" | 65 ------
...3\346\213\233\346\200\273\347\273\2231.md" | 194 ------------------
...3\346\213\233\346\200\273\347\273\2232.md" | 48 -----
...3\346\213\233\346\200\273\347\273\2233.md" | 83 --------
...04\344\270\212\345\215\212\345\234\272.md" | 55 -----
...53\344\270\216\347\204\246\350\231\221.md" | 55 -----
...15\344\270\215\351\207\215\350\246\201.md" | 58 ------
...15\350\246\201\350\200\203\347\240\224.md" | 45 ----
.../\351\200\274\347\255\276.md" | 51 -----
...13\346\213\233\346\200\273\347\273\223.md" | 117 -----------
...55\347\232\204\345\233\260\346\211\260.md" | 54 -----
...43\346\200\247\351\227\256\351\242\230.md" | 45 ----
40 files changed, 3 insertions(+), 2695 deletions(-)
rename "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md" => "problems/\345\211\215\345\272\217/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md" (82%)
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/HR\347\211\271\346\204\217\345\210\201\351\232\276\351\235\236\347\247\221\347\217\255.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/HR\351\235\242\346\263\250\346\204\217\344\272\213\351\241\271.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/offer\345\257\271\346\257\224-\345\206\263\350\265\233\345\234\210.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/offer\346\200\273\345\206\263\350\265\233\357\274\214\344\275\225\345\216\273\344\275\225\344\273\216.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/offer\347\232\204\351\200\211\346\213\251.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\215\344\270\200\346\240\267\347\232\204\344\270\203\345\244\225.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\215\345\226\234\346\254\242\345\206\231\344\273\243\347\240\201\346\200\216\344\271\210\345\212\236.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\215\345\260\221\345\275\225\345\217\213\346\203\263\346\224\276\345\274\203\347\247\213\346\213\233.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\223\344\270\232\346\212\200\350\203\275\345\217\257\344\273\245\350\277\231\344\271\210\345\206\231.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\205\245\350\201\214\345\220\216\346\213\205\345\277\203\344\273\243\347\240\201\350\203\275\345\212\233\350\267\237\344\270\215\344\270\212.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\205\263\344\272\216\345\256\236\344\271\240\345\244\247\345\256\266\347\232\204\347\226\221\351\227\256.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\205\263\344\272\216\346\217\220\345\211\215\346\211\271\347\232\204\344\270\200\344\272\233\345\273\272\350\256\256.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\206\231\347\256\200\345\216\206\347\232\204\344\270\200\344\272\233\351\227\256\351\242\230.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\210\235\345\205\245\345\244\247\344\270\211\351\200\211\346\213\251\350\200\203\347\240\224VS\345\267\245\344\275\234.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\210\267\351\242\230\346\224\273\347\225\245\350\246\201\345\210\267\344\270\244\351\201\215.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\215\232\345\243\253\350\275\254\350\241\214\350\256\241\347\256\227\346\234\272.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\220\210\351\200\202\350\207\252\345\267\261\347\232\204\345\260\261\346\230\257\346\234\200\345\245\275\347\232\204.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\244\207\346\210\2302022\345\261\212\347\247\213\346\213\233.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\244\247\345\216\202\346\226\260\344\272\272\345\237\271\345\205\273\344\275\223\347\263\273.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\244\251\344\270\213\344\271\214\351\270\246\344\270\200\350\210\254\351\273\221.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\246\202\344\275\225\346\235\203\350\241\241\345\256\236\344\271\240\344\270\216\347\247\213\346\213\233\345\244\215\344\271\240.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\256\242\344\270\211\346\266\210.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\346\212\200\346\234\257\344\270\215\345\245\275\345\246\202\344\275\225\351\200\211\346\213\251\346\212\200\346\234\257\346\226\271\345\220\221.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\346\217\220\345\211\215\346\211\271\345\267\262\347\273\217\345\274\200\345\247\213\344\272\206.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\344\270\213\345\215\212\345\234\272\344\276\235\347\204\266\346\262\241offer.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\345\274\200\345\245\226.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\346\200\273\347\273\2231.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\346\200\273\347\273\2232.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\346\200\273\347\273\2233.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\347\232\204\344\270\212\345\215\212\345\234\272.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\350\277\233\350\241\214\344\270\255\347\232\204\350\277\267\350\214\253\344\270\216\347\204\246\350\231\221.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\350\213\261\350\257\255\345\210\260\345\272\225\351\207\215\344\270\215\351\207\215\350\246\201.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\350\246\201\344\270\215\350\246\201\350\200\203\347\240\224.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\200\274\347\255\276.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\235\236\347\247\221\347\217\2552021\347\247\213\346\213\233\346\200\273\347\273\223.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\235\236\347\247\221\347\217\255\347\232\204\345\233\260\346\211\260.md"
delete mode 100644 "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\235\242\350\257\225\344\270\255\345\217\221\346\225\243\346\200\247\351\227\256\351\242\230.md"
diff --git a/README.md b/README.md
index 148f49d885..1d7f219d2c 100644
--- a/README.md
+++ b/README.md
@@ -102,6 +102,7 @@
* [看了这么多代码,谈一谈代码风格!](./problems/前序/代码风格.md)
* [力扣上的代码想在本地编译运行?](./problems/前序/力扣上的代码想在本地编译运行?.md)
* [什么是核心代码模式,什么又是ACM模式?](./problems/前序/什么是核心代码模式,什么又是ACM模式?.md)
+ * [刷题要不要用库函数](./problems/前序/刷力扣用不用库函数.md)
* [ACM模式如何构造二叉树](./problems/前序/ACM模式如何构建二叉树.md)
* [解密互联网大厂研发流程](./problems/前序/互联网大厂研发流程.md)
@@ -129,45 +130,6 @@
* [递归算法的时间与空间复杂度分析!](./problems/前序/递归算法的时间与空间复杂度分析.md)
* [刷了这么多题,你了解自己代码的内存消耗么?](./problems/前序/刷了这么多题,你了解自己代码的内存消耗么?.md)
-## 知识星球精选
-
-* [秋招面试,心态很重要!](./problems/知识星球精选/秋招总结3.md)
-* [秋招倒霉透顶,触底反弹!](./problems/知识星球精选/秋招总结2.md)
-* [无竞赛,无实习,如何秋招?](./problems/知识星球精选/秋招总结1.md)
-* [offer总决赛,何去何从!](./problems/知识星球精选/offer总决赛,何去何从.md)
-* [入职后担心代码能力跟不上!](./problems/知识星球精选/入职后担心代码能力跟不上.md)
-* [秋招进入offer决赛圈!](./problems/知识星球精选/offer对比-决赛圈.md)
-* [非科班的困扰](./problems/知识星球精选/非科班的困扰.md)
-* [offer的选择-开奖](./problems/知识星球精选/秋招开奖.md)
-* [看到代码就抵触!怎么办?](./problems/知识星球精选/不喜欢写代码怎么办.md)
-* [遭遇逼签,怎么办?](./problems/知识星球精选/逼签.md)
-* [HR特意刁难非科班!](./problems/知识星球精选/HR特意刁难非科班.md)
-* [offer的选择](./problems/知识星球精选/offer的选择.md)
-* [天下乌鸦一般黑,哪家没有PUA?](./problems/知识星球精选/天下乌鸦一般黑.md)
-* [初入大三,考研VS工作](./problems/知识星球精选/初入大三选择考研VS工作.md)
-* [非科班2021秋招总结](./problems/知识星球精选/非科班2021秋招总结.md)
-* [秋招下半场依然没offer,怎么办?](./problems/知识星球精选/秋招下半场依然没offer.md)
-* [合适自己的就是最好的](./problems/知识星球精选/合适自己的就是最好的.md)
-* [为什么都说客户端会消失](./problems/知识星球精选/客三消.md)
-* [博士转计算机如何找工作](./problems/知识星球精选/博士转行计算机.md)
-* [不一样的七夕](./problems/知识星球精选/不一样的七夕.md)
-* [HR面注意事项](./problems/知识星球精选/HR面注意事项.md)
-* [刷题攻略要刷两遍!](./problems/知识星球精选/刷题攻略要刷两遍.md)
-* [秋招进行中的迷茫与焦虑......](./problems/知识星球精选/秋招进行中的迷茫与焦虑.md)
-* [大厂新人培养体系应该是什么样的?](./problems/知识星球精选/大厂新人培养体系.md)
-* [你的简历里「专业技能」写的够专业么?](./problems/知识星球精选/专业技能可以这么写.md)
-* [Carl看了上百份简历,总结了这些!](./problems/知识星球精选/写简历的一些问题.md)
-* [备战2022届秋招](./problems/知识星球精选/备战2022届秋招.md)
-* [技术不太好,如果选择方向](./problems/知识星球精选/技术不好如何选择技术方向.md)
-* [刷题要不要使用库函数](./problems/知识星球精选/刷力扣用不用库函数.md)
-* [关于实习的几点问题](./problems/知识星球精选/关于实习大家的疑问.md)
-* [面试中遇到了发散性问题,怎么办?](./problems/知识星球精选/面试中发散性问题.md)
-* [英语到底重不重要!](./problems/知识星球精选/英语到底重不重要.md)
-* [计算机专业要不要读研!](./problems/知识星球精选/要不要考研.md)
-* [关于提前批的一些建议](./problems/知识星球精选/关于提前批的一些建议.md)
-* [已经在实习的录友要如何准备秋招](./problems/知识星球精选/如何权衡实习与秋招复习.md)
-* [华为提前批已经开始了](./problems/知识星球精选/提前批已经开始了.md)
-
## 杂谈
* [「代码随想录」刷题网站上线](https://mp.weixin.qq.com/s/-6rd_g7LrVD1fuKBYk2tXQ)。
@@ -569,7 +531,7 @@
如果是已工作,备注:姓名-城市-岗位-组队刷题。如果学生,备注:姓名-学校-年级-组队刷题。**备注没有自我介绍不通过哦**
-
+
diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md"
index 85524e6f90..c03e48c2e4 100644
--- "a/problems/0051.N\347\232\207\345\220\216.md"
+++ "b/problems/0051.N\347\232\207\345\220\216.md"
@@ -129,7 +129,6 @@ for (int col = 0; col < n; col++) {
```CPP
bool isValid(int row, int col, vector& chessboard, int n) {
- int count = 0;
// 检查列
for (int i = 0; i < row; i++) { // 这是一个剪枝
if (chessboard[i][col] == 'Q') {
@@ -178,7 +177,6 @@ void backtracking(int n, int row, vector& chessboard) {
}
}
bool isValid(int row, int col, vector& chessboard, int n) {
- int count = 0;
// 检查列
for (int i = 0; i < row; i++) { // 这是一个剪枝
if (chessboard[i][col] == 'Q') {
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md" "b/problems/\345\211\215\345\272\217/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md"
similarity index 82%
rename from "problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md"
rename to "problems/\345\211\215\345\272\217/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md"
index c8e2f5c618..ae0940bfdc 100644
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md"
+++ "b/problems/\345\211\215\345\272\217/\345\210\267\345\212\233\346\211\243\347\224\250\344\270\215\347\224\250\345\272\223\345\207\275\346\225\260.md"
@@ -1,8 +1,3 @@
-
-
-
-
-
# 究竟什么时候用库函数,什么时候要自己实现
在[知识星球](https://programmercarl.com/other/kstar.html)里有录友问我,刷题究竟要不要用库函数? 刷题的时候总是禁不住库函数的诱惑,如果都不用库函数一些题目做起来还很麻烦。
@@ -11,7 +6,7 @@
一些同学可能比较喜欢看力扣上直接调用库函数的评论和题解,**其实我感觉娱乐一下还是可以的,但千万别当真,别沉迷!**
-例如:[字符串:151. 翻转字符串里的单词](https://mp.weixin.qq.com/s/X3qpi2v5RSp08mO-W5Vicw)这道题目本身是综合考察同学们对字符串的处理能力,如果 split + reverse的话,那就失去了题目的意义了。
+例如:[字符串:151. 翻转字符串里的单词](https://programmercarl.com/0151.%E7%BF%BB%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%87%8C%E7%9A%84%E5%8D%95%E8%AF%8D.html)这道题目本身是综合考察同学们对字符串的处理能力,如果 split + reverse的话,那就失去了题目的意义了。
有的同学可能不屑于实现这么简单的功能,直接调库函数完事,把字符串分成一个个单词,一想就是那么一回事,多简单。
@@ -31,4 +26,3 @@
在刷题的时候本着我说的标准来使用库函数,详细对大家回有所帮助!
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/HR\347\211\271\346\204\217\345\210\201\351\232\276\351\235\236\347\247\221\347\217\255.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/HR\347\211\271\346\204\217\345\210\201\351\232\276\351\235\236\347\247\221\347\217\255.md"
deleted file mode 100644
index d5d39fe35e..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/HR\347\211\271\346\204\217\345\210\201\351\232\276\351\235\236\347\247\221\347\217\255.md"
+++ /dev/null
@@ -1,47 +0,0 @@
-
-
-
-
-
-
-# HR特意刁难非科班!
-
-不少录友都是非科班转程序员,或者进军互联网的,但有一些HR在HR面的时候特意刁难大家。
-
-正如[知识星球](https://programmercarl.com/other/kstar.html)里,这位录友所遭受的情景。
-
-
-
-1. 你的研究方向并不是这个方面的,你为什么要转行?
-2. 你和非科班的同学相比肯定有一些劣势,你打算怎么赶上他们?或者是你如何应对你作为非科班生在工作中的挑战?
-
-以下是我的回答:
-
-对于问题一,你这么说没问题,可以再润色一下,说一说自己看过哪些技术大牛的传记,例如《黑客与画家》,对自己影响很大,然后对编程就非常感兴趣,想从事这个行业 等等。 或者说 你感觉 新能源汽车是以后非常明确的方向,而自动驾驶是新能源汽车的标配,所以想投身这个行业。
-
-问题二: 首先要自信起来,说:在技术方面和对编程的热情方面,我不比科班同学差,因为大学里教的内容和企业要求的基本脱钩的,大家准备面试进大厂都是靠自学,**反而因为我是非科班,我更加的努力,也特别珍惜来之不易的机会**。
-
-如果要说科班同学有什么优势的话,我感觉他们的学习编程的氛围会好好一些,也就是遇到问题大家能一起交流,因为我车辆工程专业,所以我会经常去蹭计算机的课,也会去认识很多计算机专业的同学和他们一起讨论问题。
-
-总之在HR面的时候,不要说自己哪里的缺点,也不说自己哪里技术掌握的不好,**HR不懂技术,你说自己哪里不懂,他就真认为你不懂了**。
-
-缺点就说一些不痛不痒的,甚至化缺点为自己的优势。
-
-例如问你的缺点是什么?
-
-**可以说自己有时候 对技术细节过于执着,以至于影响整体的进度**。
-
-这种缺点 无形之中 就体现出自己 对技术的热爱和专研 (起到装逼于无形的效果),而且 这种缺点 是分分钟就可以改的。
-
-如果问你 :作为非科班生在工作中的挑战?
-
-你也这么说:其实大家都是靠自学,如果说非科班在工作中遇到的挑战,我相信 科班同学在工作中也是遇到一样的挑战,工作之后自学能力更加重要,互联网变化是飞快的,只有自学能力强的同学才能跟上步伐。
-
-然后随便举例一下,说明自己自学能力如何如何的强,就可以了。
-
-**总之不能示弱,不能说自己哪里不好,哪里不行!**
-
-HR也不懂技术,主要就是看你的态度。
-
-就酱,希望对录友们有所启发,加油💪
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/HR\351\235\242\346\263\250\346\204\217\344\272\213\351\241\271.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/HR\351\235\242\346\263\250\346\204\217\344\272\213\351\241\271.md"
deleted file mode 100644
index 5dba672c5d..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/HR\351\235\242\346\263\250\346\204\217\344\272\213\351\241\271.md"
+++ /dev/null
@@ -1,89 +0,0 @@
-
-
-
-
-
-
-# HR面注意事项
-
-[知识星球](https://programmercarl.com/other/kstar.html)里已经有一些录友开始准备HR面。
-
-
-
-甚至星球里已经有录友拿到百度提前批的offer
-
-
-
-看到一些录友面试这么顺利还是非常开心的,同时我也在知识星球里分享了HR面最容易遇到的 问题,已经应该如何回答。
-
-相信公众号里不少录友也会遇到同样的问题,所以就给大家再分享一下。
-
-HR面的话,如果不犯重大问题,一般不会刷人。
-
-但有些同学也会犯重大问题,这样丢了offer,可就太可惜了。
-
-**HR的职责是选择符合公司文化价值观的员工**,那么说到文化价值观,大家可能感觉 这虚无缥缈的,怎么能证明自己符合文化价值观呢。
-
-其实HR主要从如下几个问题来考察,大家只要把这几个问题想清楚,就差不多了。
-
-
-## 为什么选择我们公司?
-
-这个大家一定要有所准备,不能被问到了之后一脸茫然,然后说 “就是想找个工作”,那基本就没戏了
-
-要从**技术氛围,职业发展,公司潜力**等等方面来说自己为什么选择这家公司。
-
-要表现自己如何如何看好这家公司,期待和这家公司一起成长。
-
-## 有没有职业规划?
-
-一般应届生都没有明确的职业规划,不过当HR问起来的时候,不要说 自己想工作几年想做项目经理,工作几年想做产品经理,甚至想当leader带团队,这样会被HR认为 职业规划不清晰,尽量从技术的角度规划自己。
-
-这个策略同样适用于社招。
-
-虽然大部分程序员的终极目标都想做leader,或者做管理,(极少数想要写一辈子代码的大牛除外,不过国内环境对这种大牛并不友好)
-
-大家都想做战略做规划,那比写代码有意思,有成就感多了。
-
-但不要说出来,一定要围绕技术这块来规划,根据你的岗位,**一年 技术达到什么程度,三年在某个技术领域有深入研究,五年成为技术专家之类的**等等。
-
-这块其实我和HR朋友还真的讨论过,我说:就大厂,百分之九十五以上的程序员都不想写代码,以后指定是想转产品或者升leader做项目管理, 但你为什么还要问这么 无聊的问题呢。
-
-HR朋友的回答是:你不说真相,我会认为你可能对技术有追求,但如果你说出真相,那么明确你对技术没有追求。
-
-所以,即使你有其他想法,在职业规划HR面的时候,**也要仅仅围绕技术,树立自己要深耕技术的形象**。
-
-## 是否接受加班?
-
-虽然大家都不喜欢加班,但是这个问题,我还是建议如果手头没有offer的话,大家尽量选择接受了吧
-
-就说:自己可以介绍 XX程度的加班。
-
-如果确实身体不适,那就直接拒绝,毕竟健康是第一位。
-
-## 坚持最长的一件事情是什么?
-
-这个问题,大家最好之前就想好,有一些同学可能印象里自己没有坚持很长的事情,也没有好好想过这个问题,在HR面的时候被问到的时候,一脸茫然,不知道该说啥。
-
-憋了半天说出一个不痛不痒的事情。这就是一个减分项了!
-
-问这个问题主要是考察大家的韧性,会不会做一个件事遇到困难就中途放弃了。
-
-星球里的录友可以说自己坚持每日打卡总结,这也是可以的,毕竟这种需要自己克制才能做到的事情。
-
-## 如果校招,直接会问:期望薪资XXX是否接受?
-
-这里大家如果感觉自己表现的很好 给面试官留下的很好的印象,**可以在这里争取 special offer**
-
-这都是可以的,但是要真的对自己信心十足。
-
-## 如果社招,则会了解前一家目前公司薪水多少 ?
-
-**这里大家切记不要虚报工资,因为入职前是要查流水的,也就是要把上一件公司的银行流水截图报上来,这个是比较严肃的问题。**
-
-
-好了,说了这么多,希望对大家有所帮助。
-
----------------
-
-加入「代码随想录」知识星球,[点击这里](https://programmercarl.com/other/kstar.html)
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/offer\345\257\271\346\257\224-\345\206\263\350\265\233\345\234\210.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/offer\345\257\271\346\257\224-\345\206\263\350\265\233\345\234\210.md"
deleted file mode 100644
index 081ae5ece2..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/offer\345\257\271\346\257\224-\345\206\263\350\265\233\345\234\210.md"
+++ /dev/null
@@ -1,87 +0,0 @@
-
-
-
-
-
-# offer选择进入决赛圈.md
-
-秋招已经结束了,该开奖的差不多都陆续开奖了,很多录友的也进入了offer决赛圈。
-
-我每天都在[知识星球](https://programmercarl.com/other/kstar.html)里,回答十几个offer对比的问题,我也是结合自己过来人的经验给大家做做分析,我也选几个案例,在公众号上也给大家分享一下,希望对大家有所启发。
-
-以下是知识星球里的部分问答:
-
-
-
-C++后端开发 总包40w,这个薪资和岗位很不错了,至于干的活杂不杂 都不是你能打听出来的,要进去,看具体工作,以及领导的具体安排,只有亲自去感受了,才知道是不是打杂。
-
-深信服 云计算 大多数是做toB的业务,做私有云,几乎是和硬件设备一起卖,和阿里云,腾讯云,这些公有云厂商不是一个概念的。
-
-深信服也不错,但不用奔着云计算这个壳子就去冲 深信服,进去干的活未必和云计算有多关系,而且去深信服 你就能保证不打杂了么? 对吧
-
-所以 具体工作内容是 我们控制不了的,知道个大概方向就可以了。
-
-去百度吧,挺好的。
-
--------------
-
-
-
-普渡科技 是是一家C轮创业公司,一般创业公司 双休的可能性不太大。而且 看两家给你的薪资,如果算上公积金的话,到手的钱没有百度开的高。
-
-应届生去创业公司,有一点吃亏,就是你刚进去,因为你还不能干活,有明显的产出,或者能力有限,并不会给你 期权甚至股份,可能股份兑现不了,但表示的是对候选人的重视程度。
-
-创业公司研发流程还不够规范,所谓的野蛮成长 就是没有流程。 **关键是能成长起来 万事大吉,就怕长不起来**。
-
-百度虽然是测开的岗位,但薪资开得挺高了,可以看出对你的重视,飞桨深度学习平台部,也很不错,是百度重点打造的深度学习框架。
-
-我倾向于选百度,虽然是测开,但进去依然可以学很多东西。 这样稳一些。
-
-当然如果你想赌一把,可以去普渡科技,谁也说不好,万一后面起飞了呢。
-
-----------
-
-
-
-双非本,还是电子信息类专业,能拿到这些offer,真的很不容易了👍
-
-如果喜欢搞硬件,嵌入式应用方向其实也挺宽的,需求面也很广,华为 中兴,还有一堆手机厂商 新能源车企,智能家居,涉及到物联网的行业,都需要嵌入式开发。
-
-中新赛克 也是上市公司,其实和 海康威视 大华股份 都差不多,这几家给薪资都差不多,一样的薪资,你在南京可比在杭州舒服多了啊,而且你家也在南京。
-
-如果要真的是去大厂学技术,或者工资特别高,背井离乡也是可以的,但 海康威视 大华股份 估计也没有达到这个程度,薪资也没高出来,甚至可能不如 中新赛克 ,你还有 中新赛克给你的签字费呢。
-
-综上,我倾向于 去 中新赛克,在老家,这个薪资不挺舒服的么,南京也很不错。 好好干吧💪
-
--------------
-
-
-
-腾讯 这个部门有点边缘,技术栈比较浅。企业IT部就是做 企业内部应用的,最多也就是 几万人使用吧,大概率也没有机会磨练技术。系统很成熟,当然不加班。
-
-百度推荐架构部,还是挺核心的,技术能力 还是比较强的。 **在上海应届生可以落户了,比深圳户口香太多太多了**。
-
-至于晋升,百度晋升 一点也不慢,顺利的话,T3 到 T6 可以三年,一般情况是4年, T6跳 腾讯可以对应的是3-1 或者 3-2级别了。
-
-至于光环,对个人来说,百度工程师在业内是很受认可的,一点不比腾讯差, 很多人说:拼多多啊,京东啊 市值都超过百度了,百度不是第一梯队了,等等。
-
-说实话,**那公司的市值和我们这些码农有关系么**,对吧,**我们对关心的是 自己技术的成长,自己值多少钱,而不是公司值多少钱**。
-
-至于薪资,相对于岗位,一年差6w,不算多,倾向于选百度。
-
--------------
-
-
-
-去字节吧, 云存储方向 这个方向值得深耕, 是一个技术驱动的方向,而且各大厂商 都搞分布式存储,就业机会挺多的,再说 字节给的薪资也不错。
-
-如果以后想离家近,你可以跳槽 腾讯云 继续做分布式存储,薪资还能涨一波。
-
-如果对技术有追求的话,整个技术生涯都是可以认准云存储方向。存储是刚需!
-
-你可别在准备春招了,这大厂offer 都不要,准备啥 春招啊,哈哈哈,别卷了,休息吧。
-
-----------
-
-以上就是我在知识星球,针对录友们offer决赛圈的解答,希望对大家也有所帮助。
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/offer\346\200\273\345\206\263\350\265\233\357\274\214\344\275\225\345\216\273\344\275\225\344\273\216.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/offer\346\200\273\345\206\263\350\265\233\357\274\214\344\275\225\345\216\273\344\275\225\344\273\216.md"
deleted file mode 100644
index e22c5d4a3a..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/offer\346\200\273\345\206\263\350\265\233\357\274\214\344\275\225\345\216\273\344\275\225\344\273\216.md"
+++ /dev/null
@@ -1,88 +0,0 @@
-
-
-
-
-
-
-# offer总决赛,何去何从!
-
-最近在[知识星球](https://programmercarl.com/other/kstar.html)上,给至少300位录友做了offer选择,准对大家的情况,结合我的经验做一做分析。
-
-希望可以给大家带来不一样的分析视角,帮大家少走弯路。
-
-以下是我在知识星球里分析的部分案例,公众号上再给大家分享一波。
-
------------
-
-
-
-1. C++ 后端,客户端,测开,指定选 C++后端啊,这是送分题。
-2. 转型的问题不好回答,各有各的出路,**就算大厂里,95%以上的程序员都不想写代码,就想指点江山**,但为什么大家都在写代码呢,因为出路没有固定的公式,没有固定的方法,很多人完美转型看的是运气,看时机,也看努力,**但最重要的是运气和时机**。 最不缺的就是努力的人,其实大家都挺努力的。
-3. 不会的,大厂里也没有一家语言独大,这种担心没必要。
-4. 不同公司处理情况不一样,甚至每年都会变,大部分都不会黑名单,你这几家都不会。
-5. 你的担心有点过头了,既然你拿到的offer就要对自己有信心,你也不是走后门拿到的offer,对自己能力这么没信心么,进去之后好好干就好了。剩下的交给缘分。 计划的在周密都没有变化快。
-6. 你问的太全面了[捂脸],我都没想到 拿到大厂offer,能但担心出来 这么多事情。已经很周密了👍
-
-最后 倾向于你去阿里云吧,这么好的的机会 有啥犹豫的。
-
-
------------------
-
-
-
-
-我感觉你不用不甘心大白菜,其实挺好的,客观来看 非科班转码 拿腾信后端开发的 offer 挺不错的。
-
-虾皮在公司影响力上 和 腾讯不是一个数量级的。
-
-跳槽虽然看base,但也看公司的,腾讯光环加持 比 虾皮可有力多了。
-
-腾讯的总包是有点虚(花样有点多),但第一年到 43w了 挺不错的,腾讯好好干,升到高级工程师,在外年很值钱的。 现在这点钱也不算啥。**而且现在应届生薪资真的挺高的,不用在去追高,容易摔到的**。
-
-我建议你直接去腾讯就好, 这个选择题 其实挺好选的。
-
--------------
-
-
-
-广州没有百度吧,可以活水到深圳百度,工位很宽敞,比北京工位舒服多了。
-
-我倾向于选百度吧,而且百度的岗位也不错,薪资也比 虎牙高(虎牙的股票价值不好说),**虎牙你听说是 965 但未必是 965,可能你已入职项目就忙起来了,瞬间996,这个工作强度都不是永恒的,都是跟着项目走**。
-
-广州可选择的互联网公司不错的,你去广州虎牙,一旦离职 其实不太好选下一家。
-
-百度 试用期不过,这个感觉有点谣言,哪家大厂都有试用期不过的,不过这个看项目组,整体来说 基本试用期都能过,问题不大。
-
-虾皮还在抽盲盒,就不考虑了。
-
-去百度吧,好好锻炼几年,然后再找机会 回南方。或者transfer 深圳 也不错。
-
-------------
-
-
-
-1. 公积金可以提出来 80%,包括自己交的和公司交的,还是挺方便的,现在支付宝可以一键提取。剩下的等你买房之后 可以全部提取。
-
-2. 如果先去腾讯的话,你去 字节 做客户端更合适一些,而且你还是做 游戏直播的客户端,和腾讯互娱还是很匹配的。
-
-至于研究生做客户端亏了 的问题,其实 95% 的研究生 基本都是做研发,后端或者客户端。
-
-确实很多人感觉 读个研究生不做算法,不搞AI,和本科生没啥区别,**但事实是 搞算法 搞AI 可能都找不到工作了,那么就没有必要有这个执念**。
-
-你现在能拿到 字节offer,也有你是研究生的加持 ,虽然 学历不等于能力,但 人多而卷,用学历晒人是最高效的方式。
-
-------------
-
-
-
-
-1. 一般来说管培计划,都是培养企业未来高管的,是很不错的,不同原员工干到高管多难,**但如果参加管培计划,那就不一样的,资源 和 机会 要不正常员工 多很多**, 从 去哪给你的薪资和签字费就能看出来。 不过去哪网的管培其实我不了解,我只是说一说普遍管培的情况。
-
-2. 小米有很多东西可以学习? 这个其实 你现在是应届生,去哪家公司 都有很多东西可以学习,这个看具体内容了。
-3. 我感觉不用了吧,这offer也不错啊,当然如果还有激情,再战也可以的。毕竟没啥损失,不过感觉可以休息了,躺平吧。
-
-建议再去详细了解一下 去哪网管培计划 的具体内容,因为不同公司 管培计划不太一样,你是 技术岗管培 还是其他方向管培。
-
-
-以上就是我在知识星球,针对录友们offer决赛圈的解答,希望对大家也有所帮助。
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/offer\347\232\204\351\200\211\346\213\251.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/offer\347\232\204\351\200\211\346\213\251.md"
deleted file mode 100644
index 106bc5f889..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/offer\347\232\204\351\200\211\346\213\251.md"
+++ /dev/null
@@ -1,137 +0,0 @@
-
-
-
-
-
-
-# offer的选择
-
-秋招基本要结束了,一些录友也拿到了一些offer,如果是拿到大厂和小厂的offer,那当然就不用纠结了,直接选大厂。
-
-不过大部分同学应该拿到的是 两个大厂offer,或者说拿到两个小厂offer,还要考虑岗位,业务,公司前景,那么就要纠结如何选择了。
-
-在[知识星球](https://programmercarl.com/other/kstar.html)里,我已经给很多录友提供了选择offer的建议,这里也分享出来,希望对大家在选择offer上有所启发。
-
-## 保研与工作
-
-
-
-1. 建议你直接工作,你都已经拿到了大厂offer,没有必要读研究生了,如果你是保研到 985高校,倒是可以考虑考虑。
-2. 这是送分题,去百度吧,贴吧不算边缘,而且百度对新人的培养体系是很到位了,及时是边缘,对你技术成长也很有帮助,而且还有大厂光环。
-
-3. 星球里 前端同学也很多啊,只不过你没注意到而已,我经常看到。而且前端和后端都是一样的,不能说没地位,不过不同的部门不太一样而已。(大家都是打工的,不用搞出鄙视链哈哈)
-4. 躺平吧,可以歇息了,给还没拿offer的录友一条出路
-
-
-## 阿里云与微众
-
-
-
-中间件一点都不吭啊。
-
-有云的地方,有分布式的地方,就有中间件,应用面很广泛,各大厂哪个能离开云计算,哪个能离开分布式,你就知道中间件有多重要了。当然中间件也是很大的一个领域了,是基础架构范畴。
-
-关于业务部门和基础架构部门的选择,**我也倾向于 应届生选择基础架构部, 业务部门很忙没有时间沉底技术**。
-
-其实你还要考虑,以后跳槽出来如何,在微众做基础架构,以后 跳槽可能更容易一些。注意跳槽不一定是你主动的,可能是被动跳槽。
-
-假如阿里给你来个3.25 你就要找下家了,做 供应链管理 技术上没有太精进的话,找下家不太容易。
-
-关于两个公司,我感觉差不多,微众也很不错,一般给出的薪资都比较高。
-
-至于买房,杭州现在的房价涨的很猛,虽然说整体没有深圳高,但也差不多了,码农聚集地,就别想着房价多便宜。
-
-互联网金融 这块后面发展一定是大势所趋的,不会差。
-
-两个offer都不错,选哪个都可以!
-
-对技术有追求,我倾向于微众,如果对BAT有执念,就去阿里吧。 都差不多。
-
-## 深信服、小米和海康
-
-
-音视频开发现在很火,是一个很好的方向,音视频里也是一个很大的方向了,里面有 音视频信号分析与处理、音视频编解码格式压缩、音视频文件打包封装、流媒体推流协议处理等等。
-
-现在 腾讯会议,阿里钉钉,zoom都是靠音视频技术起家。 而且发展势头很不错。
-
-但如果只是基于现有音视频做 SDK开发,那就没啥意思了。所以也是看具体的工作内容了。
-
-股票这个 不用报太大期望,大概率 你等不到那个时候。
-
-其实 深信服的技术栈 也挺封闭的,毕竟是算是比较偏硬的厂商,深信服做云计算,也是私有云,最终也是卖硬件。
-
-不过小米其实你也说不好最终入职具体是干啥。
-
-选一个的话,倾向于选深信服吧,毕竟深信服总部就在深圳,同事多,大家有个交流,相对来说发展稳定一些。小米 好像最近 深圳才有部门吧,估计应该没多少人,甚至入职之后 可能你单兵作战,和同事没有交流的话,无论是工作还是成长都比较难。
-
-海康更硬一些,是做安防的,所以C++服务器开发基本是服务安防设备。 相对来说,选深信服更好一些,毕竟深信服是做网络安全和云计算的。
-
-## 奇安信、顺网科技和东方财富
-
-
-
-这两家公司我也不太了解。 我就从业务上 简单分析一下
-
-现在很多公司做私有云都有用OpenStack,你针对OpenStack 做二次开发,估计不太用深入理解OpenStack,但如何你能沉下心来学下去,以后跳槽的话 出路还是比较多的。 **就要看你对技术有没有钻劲了**。
-
-东方财富 毕竟不是互联网公司,**主要业务也不是技术驱动,可能技术部门话语权还是挺低的**,大概率 可能是日常后台增删改查处理一些信息,(注意这也是我猜的,真实情况也要亲自体验了才知道)
-
-综上,我感觉 如果对技术热情一般,可以考虑东方财富(毕竟给的钱多), 如果想以后技术立身,考虑奇安信。
-
-
-## 字节客户端、度小满后端
-
-
-你这拿到这么多大厂offer,还说自己菜,凡尔赛石锤了,哈哈哈
-
-客户端确实在劝退,但 客户端不会消失,现在客户端都在往大前端方向去转,**如果你很喜欢字节,喜欢 抖音的话,可以考虑去抖音做IOS**。但如果你一直做IOS的话,指定是发展不容乐观,入职之后就要考虑自己的下一步方向。
-
-度小满后端支付业务 这个其实也不错,支付业务是核心部门,以后跳 微信支付,跳蚂蚁 都是可以的,每个互联网巨头都要做自己的支付。
-
-这两个offer都可以。
-
-如果非要选一个的话,我倾向于 选度小满后端支付业务吧。 以后 跳槽 选择更多一些。可以 通过社招 再去BAT,这时候自己的方向也比较稳定。缺点:不是大厂
-
-如果对字节有情节,去也可以的,大厂福利待遇都不错,缺点:要考虑自己以后的方向。
-
-
-## 百度和华为
-
-
-
-针对于你的问题,我挨个说一下哈:
-
-1. 大华是做安防的,和主流互联网偏了一些。
-2. 成都有没有招人的机会,这个我也确定不了,但可以确定的是,第一份工作确实很重要!对以后的发展还是有很大影响的。 不过和 女朋友异地 如何权衡还是要看你自己。
-3. 百度大数据 也有做toB,就是给企业提供服务,做toB是比较辛苦,而且赚的不多(这里指的是部门营收),不过相对于你的其他几个offer,我倾向于你选百度,百度对应届生的培养还是很到位的。对你以后的技术发展有帮助。 而且大数据做tob的不止百度一家,阿里云,腾讯云,等等很多都做大数据toB,以后跳槽也容易。
-4. 这种情况有没有救,我也不清楚了,大概率是不太行了,不用过于纠结,能抓住目前的机会就很好了。
-
-
-## 大华和小米
-
-
-
-倾向于选小米吧,大华是做安防的,**在安防公司里做客户端,可以说是偏上加偏了**。
-
-关于 小米南京工资 我也不了解,不过一般来说 南京工资都会低一些。
-
-小米手机部门,以后你的选择 也会多一些,毕竟国内这么多手机厂商,而且小米的发展势头还是可以的,最近小米在发力小米汽车,如果能内部转岗到小米汽车,就很不错了,这是未来十年的一个重大机会,可以跟着雷总起飞。
-
-在看以后定居,南京的房价可比杭州亲民多了,南京也就2w一平左右,以后你在南京定居基本压力不大,如果算上房价,杭州大华多出来的那点工资 简直不值一提了。
-
-# 总结
-
-最后我也只是针对大家给我的情况,我来做一个基本的分析,给出我的判断。
-
-毕竟最了解你的,还是你自己,而且入职之后 工作具体内容,部门发展,其实我们都无法预测,只能结合我们能确定的内容来做分析。
-
-拿我自己来举例,我当初毕业拿到是腾讯互娱XX工作室的后端开发offer 和 华为2012 数据库部门的offer,当然还有其他offer就不提了,那么当时问身边朋友前辈,一定是选 腾讯了,我也倾向于腾讯,但这么多年过后 反过来看,我感觉当初如果去华为可能更好一些。
-
-具体原因我也会在知识星球里做分享。
-
-所以 **在选择offer上,是有很多是未知的,再好的部门也有坑,再差的部门 遇到好领导也会很舒服**。
-
-**我们只能把握住 目前能把握的,至于后面怎么样,只有经历了才知道**。
-
-录友们在选择offer上,也多和问一问身边的同学,前辈们,多方面接受建议,在结合自己的情况做出判断,也希望录友们都有一个好的发展,加油💪
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\215\344\270\200\346\240\267\347\232\204\344\270\203\345\244\225.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\215\344\270\200\346\240\267\347\232\204\344\270\203\345\244\225.md"
deleted file mode 100644
index 40d15ecdc6..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\215\344\270\200\346\240\267\347\232\204\344\270\203\345\244\225.md"
+++ /dev/null
@@ -1,73 +0,0 @@
-
-
-
-
-
-# 特殊的七夕
-
-昨天在[知识星球](https://programmercarl.com/other/kstar.html)发了一个状态:
-
-
-
-我还以为 过节嘛,录友们应该不会打卡了,但还依旧阻挡不辽录友打卡学习的步伐,来瞅瞅:
-
-
-
-
-
-
-
-
-
-
-
-当然了,我截图了一小部分,星球里每天的信息量都非常大。
-
-如果说一个人坚持每天总结记笔记,**其实是很容易放弃的,今天不记,明天不急,后天不整理感觉也无所谓**。
-
-这样时间就一点一点的被浪费掉了。
-
-**因为我们学过的东西都会忘,不及时整理,时间就不能沉淀下来**,这就造成了一边学,一边忘,最后感觉自己好像也没学啥的感觉!
-
-所以每天记笔记,及时整理,是非常重要的。
-
-这个习惯其实靠自己约束很容易放弃,但一群人一起坚持,就会不一样,大家相互监督,每天不总结记录点什么就会感觉少了点啥。
-
-而且,大家每天的总结,我都会看,有问题 我都及时指出来,这样也防止自己学着学着学跑偏了。
-
-昨天我也在[知识星球](https://mp.weixin.qq.com/s/X1XCH-KevURi3LnakJsCkA)回答了几位录友的问题,其中有两个问题 还是比较典型的,估计公众号里的录友也会遇到这样的疑惑。
-
-所以也给大家说一说:
-
-## 准备的太晚了,想放弃秋招,直接准备春招
-
-
-
-很多准备今年秋招的录友感觉自己还没准备好,想先找实习,或者 自己在学习学习,然后直接春招。
-
-其实不到万不得已,我还是建议要冲刺秋招。
-
-如果说,因为没准备好,提前批放弃还是可以的,但秋招不能也直接放弃了!
-
-秋招没找到好工作,一般11月份左右,一些大厂还会有补招,腾讯就经常补招,实在不行再准备春招,春招可能国企单位会多一些。
-
-**而且面试也很看缘分,永远没有真正准备好的时候,知识一直都学不完**,所以 把秋招当做最后的机会,就算秋招没找到,也可以在冲春招,而不是 直接放弃秋招。
-
-还有就心态方面来说,直接放弃秋招,等 今年 10月份,身边同学都找到工作了,天天吃喝玩乐,见面打招呼就问:你去哪了,你签哪了。那时候 自己心里压力会非常大,甚至会影响 春招找工作。
-
-
-## HR/面试官问你手上有没有其他offer,如何回答
-
-
-
-这个问题,无论是校招还是社招,大家都会遇到。
-
-如果大家手上有其他更好的offer,或者说同等水平公司的offer,可以说一说,这样凸显出自己的优势,即:你们不要我,有更好的公司要我, 这样给面试官或者HR点压力,可以争取到更高的薪酬。
-
-如果没有更好的offer,可以说没有,然后解释:只认准贵公司,从技术氛围,职业发展,公司前景,来说贵司多么多么的好,我多么渴望和贵司一起成长之类的。**总之,就是捧起来,显得自己很专一**。
-
-都是套路,哈哈哈哈。
-
-**好了,说了这么多,希望对大家有所帮助**。
-
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\215\345\226\234\346\254\242\345\206\231\344\273\243\347\240\201\346\200\216\344\271\210\345\212\236.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\215\345\226\234\346\254\242\345\206\231\344\273\243\347\240\201\346\200\216\344\271\210\345\212\236.md"
deleted file mode 100644
index 84d372f080..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\215\345\226\234\346\254\242\345\206\231\344\273\243\347\240\201\346\200\216\344\271\210\345\212\236.md"
+++ /dev/null
@@ -1,45 +0,0 @@
-
-# 看到代码就抵触!怎么办?
-
-最近在[知识星球](https://programmercarl.com/other/kstar.html)里,看到了不少录友,其实是不喜欢写代码,看到 哪些八股文都是很抵触的。
-
-其实是一个普遍现象,我在星球里分享了一下,我对这一情况的一些想法。
-
-发表在星球里,很快就有了60多个赞,也确实说道大家的心里去了。
-
-
-
-我在这里再给公众号的录友们也分享一下:
-
-很多同学不喜欢计算机,也不喜欢写代码,看到各种大堆的面经 八股文,心里是十分抗拒的。
-
-所以总是拿一些其他的事情来拖延,最后发现秋招或者春招 完全拖过去了,或者说等求职的时候发现自己完全没有达到应聘的水平,再陷入 深深的焦虑之中。
-
-这里有一个很重要的问题,就是**当你发现你不喜欢计算机的时候,那问问自己喜欢什么呢?**
-
-如果喜欢看电影,逛街,打游戏,甚至格调再高一点 喜欢画画,喜欢音乐,唱歌之类的,这些爱好如果没有达到专业的程度,那么仅仅是日常消遣而已。(甚至是一种逃避)
-
-**真实情况是大部分人都说不出来自己真正喜欢什么的**。
-
-这是中国教育制度的问题,大家从小学、初中、高中、到大学再到工作 就没有时间或者机会去思考自己真正想干什么。
-
-别说在校学生了,就互联网大厂的程序员,我敢说 百分之九十以上 都不喜欢写代码,都感觉写代码是最苦逼的,天天对着屏幕,写写一堆正常人看不懂的英文单词,有啥意思,对吧。
-
-但为什么大家都依旧写代码呢。
-
-**要生活啊,要赚钱啊!**
-
-那种可以不考虑经济问题,一言不合就寻找人生意义的选手,都是家里有矿的,那种咱们不讨论。
-
-还一些同学迷茫的时候,就放空自己。 还有已经工作的,可能会去一趟西藏,“寻找一下生命的意义”。
-
-其实我也是不建议的,**放空自己之后,并不会找到 自己真正的方向,只会更迷茫!**
-
-所以,**当你不知道自己真正喜欢什么的时候,就先把眼前的事情做好吧!**
-
-如果你知道自己不喜欢计算机,不喜欢写代码,但也不知道自己究竟喜欢什么的时候,那么就先把自己现在该做的事情做好,制定计划,算法题刷题起来,八股文看起来。
-
-**这才是是最重要的**。
-
-希望对录友们有所启发,加油💪
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\215\345\260\221\345\275\225\345\217\213\346\203\263\346\224\276\345\274\203\347\247\213\346\213\233.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\215\345\260\221\345\275\225\345\217\213\346\203\263\346\224\276\345\274\203\347\247\213\346\213\233.md"
deleted file mode 100644
index 81c9950350..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\215\345\260\221\345\275\225\345\217\213\346\203\263\346\224\276\345\274\203\347\247\213\346\213\233.md"
+++ /dev/null
@@ -1,81 +0,0 @@
-
-
-
-
-
-# 不少录友想放弃秋招了.....
-
-马上就要九月份了,互联网大厂的秋招的序幕早已拉开。
-
-发现[知识星球](https://programmercarl.com/other/kstar.html)里有一部分录友想放弃秋招,直接准备明年的春招,估计关注公众号的录友也有不少有这种想法的。
-
-
-
-一般有这种想法的录友都是 **之前没有准备好,或者是总感觉时间赶趟赶趟,然后突然间 发现时间不赶趟了。。。**
-
-也有一些感觉自己没有实习经历,简历上也没什么好写,想下半年去找一找实习,不去秋招,然后直接准备春招。
-
-**对于这种情况,我的建议依然要冲刺秋招!**
-
-# 把秋招当做最后的机会
-
-**等到春招的时候,可以选岗位已经很少了,各个大厂几乎都招满了**。
-
-而且就算秋招没找到好工作,一般 11月份左右,一些大厂还会有补招,腾讯就经常补招。
-
-补招的情况是就是腾讯发出了 offer,有的候选人 选择违约,不来了,那么腾讯就需要补招,把人数凑齐。
-
-可能有录友想,谁居然连腾讯的offer都拒绝呢?
-
-其实挺多的,例如:有其他大厂的核心部门offer,父母给安排了 国企、央企 的核心岗位,或者有的选择 读博了之类的,导师毕业能给安排留校 或者去其他高校任教。
-
-所以几乎每年,腾讯都要补招,其他大厂也会有补招,一般是11月份,所以就算秋招没拿到大厂offer,依然有机会!
-
-话再说回来,面试其实也很看缘分,**永远没有真正准备好的时候,知识一直都学不完**。
-
-所以 **把秋招当做最后的机会,就算秋招没找到,也可以在冲春招,而不是 直接放弃秋招**。
-
-
-# 放弃秋招,对心态的影响
-
-如果直接放弃秋招,等 今年 10月份,身边同学都找到工作了,那时候的场面就是歌舞升平,大家天天吃喝玩乐。
-
-见面打会招呼就问:你去哪了,你签哪了?
-
-那时候如果自己还没有面试,还在准备面试,此时自己心里阴影面积有多大,甚至会影响春招找工作。
-
-# 面试要趁早准备
-
-每年这时候,都会有同学后悔,我怎么就没早点准备,就感觉时间不够用。
-
-所以也给明年找工作的录友们(2023届)提一个醒,现在就要系统性的准备起来了,因为明年春季实习招聘 是一个很好的进大厂的机会,剩下的时间也不是很多了。
-
-来看看[知识星球](https://programmercarl.com/other/kstar.html)里,一位准大三的录友准备的情况
-
-
-
-再来看看一位准大二的录友准备情况
-
-
-
-**我已经预感到 这两位 等到秋招的时候就是稳稳的offer收割机**。
-
-[知识星球](https://programmercarl.com/other/kstar.html)还有很多已经开始提前准备,或者看了 星球发文状态就开始着手准备的录友了。
-
-
-所以 **所谓的大牛,都是 很早就规划自己要学的东西,很早就开始向过来人请教应该如何找工作,很早就知道自己应该学哪些技术,看哪些书, 这样等到找工作的时候,才是剑锋出鞘的时候**。
-
-我们远远还没有到拼智商的程度。
-
-这里 也是给公众号里的录友们提一个醒,估计还有不少录友依然在感觉时间还赶趟,但 未来的卷王已经在路上了 哈哈哈。
-
-**不过话说回来,现在互联网求职确实卷!**
-
-但这是社会问题,我们改变不了。
-
-**卷的核心是,好的东西少,但要想的人多!**
-
-**如果你也想要,就要提前准备,提前规划,提前努力!**
-
-也希望录友们都能找到一个自己心仪的工作,加油💪。
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\223\344\270\232\346\212\200\350\203\275\345\217\257\344\273\245\350\277\231\344\271\210\345\206\231.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\223\344\270\232\346\212\200\350\203\275\345\217\257\344\273\245\350\277\231\344\271\210\345\206\231.md"
deleted file mode 100644
index a1b7ba3abb..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\344\270\223\344\270\232\346\212\200\350\203\275\345\217\257\344\273\245\350\277\231\344\271\210\345\206\231.md"
+++ /dev/null
@@ -1,69 +0,0 @@
-
-
-
-
-
-
-
-# 你简历里的「专业技能」写的够专业么?
-
-
-其实我几乎每天都要看一些简历,有一些写的不错的,我都会在[知识星球](https://programmercarl.com/other/kstar.html)里分享一下。
-
-
-这次呢,我再专门说一说简历中的【专业技能】这一栏应该怎么写。
-
-很多同学【专业技能】这块写的很少,其实不是掌握的少,而是没有表达出来。
-
-例如有的同学这么写:
-
-
-
-
----------------------
-
-
-
---------------------
-
-
-
--------------------
-
-
-
-
-这些【专业技能】都写的很少,其实是可以在丰富一些的。
-
-我来给大家拓展一下、
-
-
-* 熟练C++,(列举C++的若干知识点),了解 Java,python,go (适当补充对这些语言的理解)
-* 熟悉常见设计模式(例句一些设计模式)
-* 熟悉linux操作系统vim开发环境,(列举网络编程相关知识,例如epoll,socket等等)
-* 熟悉网络,(列举网络协议相关考点,tcp/ip,http, https, 三次,四次握手,流量控制等等)
-* 数量掌握数据结构与算法(列举常用算法,最好搞透一个算法,说对该算法有独到见解)
-* 数量使用Git,等版本控制
-* 以上为公共写法,下面可以在补充自己的其他领域的内容
-
-
-针对以上这个模板, 再来补充相关内容:
-
-1. 熟悉C/C++,熟练使用C的指针应用及内存管理,C++的封装继承多态,STL常用容器,C++11常用特性(智能指针等) ,了解 Python,Gtest等。
-2. 熟悉常用设计模式(单例模式,工厂模式等)
-3. 熟悉Linux下vim开发环境,了解网络编程,IO多路复用,epoll等等。
-4. 熟悉OSI五层网络模型,熟悉TCP/IP,UDP,HTTP/HTTPS,DNS等网络协议,熟悉TCP三次握手,四次挥手,流量控制,拥塞控制等手段。
-5. 熟悉常用的数据结构(链表、栈、队列、二叉树等),熟练使用排序,贪心,动态规划等算法。
-6. 熟悉使用Git,vscode工具使用。
-
-但需要注意的是,这里写的点,自己一定要熟练掌握,因为简历上写的,面试官一定会问。
-
-这样有一个好处,就是 **缩小面试官的问题范围**, 只要简历上写的,你都准备好了,那么简历上的知识点面试官一定会问,这样你就掌握了主动权。
-
-举一个例子,如果简历上直写:熟悉C++。其他都没介绍,那么面试官指定围绕C++漫天遍野的问起来了,你也猜不透面试官想问啥。
-
-如果简历写熟悉C/C++,熟练使用C的指针应用及内存管理,C++的封装继承多态,STL常用容器,C++11常用特性(智能指针等)。那么面试官基本上只会问,内存管理,多态,STL和C++11的一些特性, **这样你就把面试官的问题都圈在可控范围内**,从而掌握主动权!
-
-这一点很重要,希望大家要有这个思路,去写自己的简历。
-
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\205\245\350\201\214\345\220\216\346\213\205\345\277\203\344\273\243\347\240\201\350\203\275\345\212\233\350\267\237\344\270\215\344\270\212.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\205\245\350\201\214\345\220\216\346\213\205\345\277\203\344\273\243\347\240\201\350\203\275\345\212\233\350\267\237\344\270\215\344\270\212.md"
deleted file mode 100644
index 58b8c32ced..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\205\245\350\201\214\345\220\216\346\213\205\345\277\203\344\273\243\347\240\201\350\203\275\345\212\233\350\267\237\344\270\215\344\270\212.md"
+++ /dev/null
@@ -1,55 +0,0 @@
-
-
-
-
-
-
-# 入职后担心代码能力跟不上
-
-在[知识星球](https://programmercarl.com/other/kstar.html)上,很多录友已经担心自己去了公司工作以后,代码能力跟不上,会压力很大。
-
-
-
-其实 星球里 也有很多 已经确定offer的录友,想在入职之前提升代码能力,或者说 如何可以更快的融入项目组。
-
-
-
-这里我把应届生刚刚工作需要培养的能力都说一说,其实也适用于毕业一两年的录友。
-
-## 代码能力
-
-练习代码能力最直接的方式就是多写,最简单的是用你的语言写一个管理系统,甚至你每天刷刷题,也是可以的。
-
-再直白一遍,不是有26种设计模式,你全都实现一遍,即锻炼 代码能力,还学习了设计模式,等入职之后多看项目代码,看看大家在设计上使用了那种设计模式,或者说哪里还可以优化。
-
-设计模式可以说 面试中最实用的知识的,大部分面试内容,工作中其实都用不上,所以大家会发现面试造大炮,工作拧螺丝。
-
-## 心态
-
-工作之后 从心态上来说,要不怕困难,不怕bug,不怕未知技术。
-
-很多同学之所以代码能力弱,**就是太怕 难点**,遇到点 困难 就想:恨不得谁告诉我应该怎么怎么办得了,而不是自己去解决问题。
-
-要善于使用搜索引擎,如何所谓问题是一个技术活,有的问题你怎么搜都搜不出来答案,但有的同学一搜 就搜出来了。
-
-为什么呢?
-
-这是遇到问题,提取关键词的能力,很多同学遇到问题,不知道用什么关键词去搜。
-
-**还有要有一种 越挫越勇 的精神**,这样你后面 的困难就会越来越少,也会培养出一种自信。
-
-写代码也需要自信,工作之后 经常 有一些比较难任务,有的 同学就不敢接,怕做不出来,有的同学就敢接,并不说这位同学技术上一定强,而是他有自信。
-
-**这种自信都在是以往 解决各种问题,排查各种bug,练就出来的**。一旦拥有自信,那能量就是 摧枯拉朽,真正的逢山开路遇水搭桥。领导也喜欢这样的组员
-
-## 快速成长
-
-刚入职的时候,不要怕问问题,不要怕出错,不要怕不好意思。
-
-因为你是新人,就算问简单的问题,大家不会嫌弃你,但 这个新人窗口期是很短的,如果 过了几个月 还问一些非常基础的问题,那么 可能职场上就不太好了。
-
-所以要在新人窗口期,快速成长,我看过太多的应届生,入职的时候 都是畏头畏尾,然后 过了新人期,能力还没有提高,还问很基础的问题,这样 项目组也不会分给他 有难度的任务,他也就得不到锻炼,越得不到锻炼,就能力越没提升,就越没自信,就越接不到有难度的活,然后陷入死循环!
-
-
-综上,算是从学校到职场的上的一个转变。 希望对大家有帮助。
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\205\263\344\272\216\345\256\236\344\271\240\345\244\247\345\256\266\347\232\204\347\226\221\351\227\256.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\205\263\344\272\216\345\256\236\344\271\240\345\244\247\345\256\266\347\232\204\347\226\221\351\227\256.md"
deleted file mode 100644
index 88de443634..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\205\263\344\272\216\345\256\236\344\271\240\345\244\247\345\256\266\347\232\204\347\226\221\351\227\256.md"
+++ /dev/null
@@ -1,87 +0,0 @@
-
-
-
-
-
-# 关于实习,大家可能有点迷茫!
-
-我在[知识星球](https://programmercarl.com/other/kstar.html)里回答了很多关于实习相关的问题,其实很多录友可能都有这么样的疑问,主要关于实习的问题有如下四点:
-
-* 秋招什么时候开始准备
-* 要不要准备实习
-* 实习是不是重要?
-* 什么时候找实习最有帮助
-* 如何选择实习的offer
-
-下面都分别来说一说:
-
-## 秋招什么时候开始准备
-
-
-
-**准备工作指定是越早越好的**。
-
-准备的越早,在8,9月份就越淡定,每年校招很多同学都会对于准备找工作总感觉赶趟赶趟,结果到了8月份开始慌得一笔了。
-
-正常校招8月份就开始提前批(各大企业提前抢优秀毕业生)了,有的企业甚至7月份就开始。
-
-基本到了7月份可能就没有整块的时间静下心来准备找工作,那时候已经铺天盖地的各种招聘信息,甚至一些同学已经拿到了offer了。
-
-所以准备找工作的内容以7月为终结点比较稳妥,七月份之后以复习为主,有个整体框架,定时复习补充补充,多和同学交流面试经验。
-
-## 要不要准备实习
-
-有的同学是3,4月份准备面实习,然后7、8月份就去企业实习了,**实习有利有弊**。
-
-如果可以去一线互联网公司实习,而且岗位也合适,那当然要去,如果去不了也别难过,因为实习生大部分都是打杂,干的活甚至都写不到简历上。
-
-也有一小部分的实习生能够真正做到项目。
-
-如果没有去实习,就把基础好好补充一下,**基础打好,毕竟对于应届生基础最为重要**, 编程语言、数据结构算法、计算机网络、操作系统、数据库这些都是基础,规划好时间把这些内容学好。
-
-**对于应届生来说,项目经历是锦上添花,不是决定性的**。
-
-有实习经历(前提是实习工作内容是真正的做项目,不是打杂),那么面试的时候面试官可能对项目经历感兴趣,问基础的内容就比较少, 如果没有实习经历,就把基础内容巩固一下,校招是一样的。
-
-## 实习是不是非常重要?
-
-
-
-**大厂的实习经历对秋招还是很有帮助的**。
-
-
-但也不绝对,实习的话会耽误三个月左右,如果没有转正,而且一直在打杂的话,再去找秋招工作,**那时候一些基础的内容就都忘光了,反而秋招很被动**。
-
-现在当然也可以按照准备找实习的状态来要求自己,给自己点压力,毕竟找实习准备的知识和秋招准备的知识差不多。
-
-也可以提前经历一下面试,培养一下面试感觉,数据库方面知识你比较短缺,可以通过大量看这方面的面经迅速补充一下(秋招之前还可以系统看一看)。
-
-如果拿到了大厂的实习offer,就去吧,实习的时候心里要有个秤,如果工作是打杂,就要尽快自己抽时间看基础准备秋招。
-
-**另外需要注意的是,有些公司投递过简历面试没通过是有记录的,所以投递要当心,不要感觉投简历没有成本**,我知道的例如阿里,你每次投简历都有记录,如果实习面试都挂了,秋招的时候面试官也会看当时实习面试的记录(会考虑当时实习面试的结果)。
-
-## 什么时候找实习最有帮助
-
-
-
-6月份那时候基本不招实习生了,找的话也是日常实习(没有转正,实习时间是比较长的,要六个月),如果不是暑期实习就直接准备秋招吧。
-
-**只有应届的暑期实习才有转正的机会,因为企业这样安排也是为了提前发现优秀毕业生!**
-
-例如:今年暑期实习,只招明年毕业的应届生。
-
-
-## 如何选择实习的offer
-
-
-
-如果目标应该是C++后端开发,那客户端实习offer可以选择别去了。 或者 选一个实习时间最短的offer先去着,例如两个月之类的,这样既能体现一下工作流程,也别耽误太多时间(毕竟客户端开发不是你的目标)。
-
-**实习也不是必要的,一要看实习的岗位,是不是你想要的工作,二是实习的内容是不是打杂**,一些实习岗位其实是在浪费时间,如果转正不了的话,秋招就特别被动了,耽误了复习基础的时间。
-
-还有就是**秋招的时候,一定要找小公司先练手,然后在面大公司**。
-
-
-以上基本覆盖了大家对实习的各种疑惑,不过现在已经到了5月份,实习面试基本结束了,如果没有拿到实习offer,大家安心准备秋招吧,依然可以冲刺大厂!
-
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\205\263\344\272\216\346\217\220\345\211\215\346\211\271\347\232\204\344\270\200\344\272\233\345\273\272\350\256\256.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\205\263\344\272\216\346\217\220\345\211\215\346\211\271\347\232\204\344\270\200\344\272\233\345\273\272\350\256\256.md"
deleted file mode 100644
index 6a316bd234..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\205\263\344\272\216\346\217\220\345\211\215\346\211\271\347\232\204\344\270\200\344\272\233\345\273\272\350\256\256.md"
+++ /dev/null
@@ -1,72 +0,0 @@
-
-
-
-
-
-# 秋招和提前批都越来越提前了....
-
-正在准备秋招的录友,可能都感受到了,现在的秋招越来越提前了....
-
-以前提前批,都是 8月份,8月份中序左右,而不少大厂现在就已经提前批了。
-
-不少录友在 公众号留言,和[知识星球](https://programmercarl.com/other/kstar.html)里,表示提前批来的还是有点快。
-
-
-
-还没有把刷题攻略刷完的录友,要尽快刷完,至少先刷一遍,了解相关题型。
-
-星球里,也有一些录友感受到了提前批的突如其来。
-
-
-
-目前已经开始的提前批有 vivo, tp-link,京东,百度.....
-
-
-
-
-有的录友已经明显紧张起来了。
-
-
-
-其实这才刚刚开始,等 7月份 以后,又是铺天盖地的信息,大家更没心情静下心来看书了。
-
-
-# 关于提前批的一点小建议
-
-**首先我们要知道为什么有提前批?**
-
-提前批不是大厂们呆着没事闲着多给我们一次面试机会,**而是提前抢优秀毕业生**,这一点大家一定要明确。
-
-
-了解了为什么有提前批,这样我们就有正确的心态面对它了。
-
-如果没有准备好,或者自己定位因为不是 “优秀毕业生”,先不要碰提前提。
-
-当然可以先面一些自己不想去的公司的提前批,用来练手。
-
-至于对于自己心仪的公司,如果盲目参加提前批,首先会打乱自己的复习计划,和心态,然后就是提前批挂了后台都是有记录的。
-
-只要是大厂的内部信息化做的比较完善,提前批挂了,是一定会记录的。
-
-**那么提前批有没有影响呢?**
-
-很多招聘宣传的时候说,提前批挂了对秋招没影响,确实在一定程度上没影响,因为提前批挂了,依然可以投递秋招。
-
-然后秋招面试的时候,面试官在不在意你的提前批成绩,就是另一回事了。
-
-我之前内推了一些录友面试腾讯微信支付的部门,面试官和我很熟悉,我最近还和他吃了饭,聊一聊我内推的同学,他说看后台记录有些同学都投过好几次了,他看了之前面试结果的评价之后,就直接pass了。
-
-所以大家可能要想好一个回答,就是面试官可能问:你的提前批为什么挂了?
-
-而且提前批挂了,都是有当时面试官评语的,如果7月份提前批面试,面试官评价:这位候选人基础不行。
-
-秋招的时候,面试官也不会相信,一两个月能把基础补上来了。 即使你的基础其实没问题,只不过恰巧面试中的几个问题没答好而已。
-
-
-对于技术能力确实强的同学,我建议全力以赴准备提前批面试,因为提前批要求就比较高,很容易谈sp offer。而且现在就拿到了大厂offer,比找实习还香。
-
-如果没准备好的同学,建议不要让提前批打乱阵脚,有计划的巩固基础,准备秋招。或者先拿自己不想去的公司的提前批练手。
-
-
-好了,说了这么多,希望对录友们有所帮助,加油💪
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\206\231\347\256\200\345\216\206\347\232\204\344\270\200\344\272\233\351\227\256\351\242\230.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\206\231\347\256\200\345\216\206\347\232\204\344\270\200\344\272\233\351\227\256\351\242\230.md"
deleted file mode 100644
index 426eb2a653..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\206\231\347\256\200\345\216\206\347\232\204\344\270\200\344\272\233\351\227\256\351\242\230.md"
+++ /dev/null
@@ -1,100 +0,0 @@
-
-
-
-
-
-# 程序员应该这么写简历!
-
-自运营[知识星球](https://programmercarl.com/other/kstar.html)以来,我已经给星球里的录友们看了 一百多份简历,并准对大家简历上的问题都给出了对应的详细建议。
-
-社招,校招,实习的都有,其实大家的简历看多了,发现有很多共性的问题,这里就和大家分享一下。
-
-我的简历模板也分享出来了,大家在「代码随想录」后台回复:简历模板,就可以获取!
-
-# 简历布局
-
-不少录友的简历布局就很不合理, 有的把专业技能放在最下面了,有的甚至把教育经历放下面了,建议简历布局的顺序是这样:
-
-* 教育工作经历
-* 专业技能
-* 项目经验
-* 荣誉奖项
-* 个人简述
-
-# 教育工作经历
-
-一些录友可能本科学历不是很好,然后 简历上直接不写自己的本科学校。
-
-其实教育经历是最基本的,你不写 面试官也一定会问,问出来 那么感觉更不好,所以关于教育经历,大家是一定要写的。
-
-写本科以后教育经历的就行了,一些录友可能是 高中就读了一些特别牛逼的高中,然后把高中也写出来了,哈哈哈,高中经历真的就不用写了。
-
-还有一些社招的录友,研究生和本科之间空了几年,这几年 一定要说清楚做了些什么,甚至是“编一下”,因为这个面试官也会问的。
-
-# 专业技能
-
-一些录友简历上没有「专业技能」这一栏,或者写的很短。
-
-可能是不知道该写啥,甚至就不写了。
-
-通常「专业技能」是在 「教育工作经历」之后的,我这里给出一个模板,大家按照这个格式来写「专业技能」就可以。
-
-1. 熟练使用 C++,掌握Go,了解 Java、Python、PHP 等编程语言
-2. 熟练使用 linux 下 vim、Git 开发环境
-3. 了解 Linux 下网络编程、TCP/IP 协议
-4. 掌握基础数据结构和算法的基本原理
-5. 英语六级:XXX
-
-
-一些录友会列举自己主修的课程,列了一堆,其实凑篇幅 我是理解的,就是感觉简历太单薄的,列课程来凑。
-
-但大家凑篇幅 尽力在「专业技能」和「项目经验」上凑篇幅,如果把 自己主修可能都列出来,会让面试官感觉没有什么干货。(有的同学甚至靠留白才凑篇幅,这就更不要了)
-
-当然应届生如果有一些课程自己成绩确实很好,可以和「教育经历」写在一起,简单并行列举一下就可以了。
-
-# 项目经验
-
-很多录友写项目经验就是流水账,这是什么项目,自己完成了功能1,2,3,4。堆了很多字。
-
-要知道面试官是不了解你的项目的,面试也只有 一个小时左右的时间,如果堆了很多文字 面试官也懒得去读。
-
-面试官最在意的是什么呢?
-
-**项目中有哪些技术难点,以及 你是如何克服的**。
-
-这是面试官最关心的,也是最能体现出候选人技术深度的问题。
-
-所以大家在描述项目经验的时候,一定要时刻想着,这个项目的难点究竟是什么,要反复问自己这个问题。
-
-可能有的同学说了,我这项目本来就没有难点啊,就是1,2,3,4功能,然后 遇到不会的,百度搜一下,差不多就这样了。
-
-**项目没有难点,也要自己“造难点”**。 因为这个问题是面试官必问的!
-
-所以一定要准备好。
-
-还有不少录友的项目经历都写了 web server,使用线程池 + 非阻塞 socket + epoll(ET 和 LT) + 事件处理 (Reactor 和模拟 Proactor) 等等。
-
-这个项目可能是很多准备后台开发的同学 首选的 项目。
-
-这种自己搞的小项目,**最好把你的代码上传的github上,然后在简历中贴出github地址**,面试官一定会看的。
-
-如果看你的代码写的确实不错,那指定是加分项。比简历上写的天花乱坠都强!
-
-还有的同学项目经历特别多,写了5,6个项目,每个项目都是概述了一下自己做了XXX。
-
-其实面试官,基本就会和你深入聊 2个的项目左右,列举这么多项目没有用的,关键这些项目一看也是技术含量不大。
-
-**所以不用单纯堆项目个数。项目经历 两个足够,把两个项目搞深搞透**
-
-
-# 校园经历
-
-一些录友会把自己学校工作列出一大堆,例如各种学生会啊,创新部门啊之类的。甚至有的会把自己的减肥经历也列举出来。
-
-如果面技术岗位,这一块其实不是面试官关心的,可以在 最后一栏「个人简述」,简单一两句概括一下自己的学生会经历就好,表明自己沟通能力没问题。
-
-关于标明自己有毅力,有恒心,不怕吃苦等等,都简单一句概括。
-
-
-好了,关于简历的问题,我就先分享这些,估计应该击中了不少录友的痛点了。
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\210\235\345\205\245\345\244\247\344\270\211\351\200\211\346\213\251\350\200\203\347\240\224VS\345\267\245\344\275\234.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\210\235\345\205\245\345\244\247\344\270\211\351\200\211\346\213\251\350\200\203\347\240\224VS\345\267\245\344\275\234.md"
deleted file mode 100644
index f602e9e918..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\210\235\345\205\245\345\244\247\344\270\211\351\200\211\346\213\251\350\200\203\347\240\224VS\345\267\245\344\275\234.md"
+++ /dev/null
@@ -1,49 +0,0 @@
-
-
-
-
-
-
-# 初入大三,考研VS工作
-
-9月份开学季,已过,一些录友也升入大三了,升入大三摆在自己面前最大的问题就是,考研还是找工作?
-
-在[知识星球](https://programmercarl.com/other/kstar.html)里就有录友问我这样一个问题, 其实每个人情况不一样,做出的选择也不一样,这里给大家分享一下,相信对你也会有启发。
-
-
-
-以下是我的回答:
-
-首先我是考过研的,比较幸运一次就上岸了,这里说一下我的心得。
-
-你才刚刚大三 就已经做了这么充分的准备了,那我倾向于 选择直接工作。
-
-因为现在你准备的这些,都是找工作需要的,也都是实用的技术。
-
-如果你明年初就开始准备考研了,那么你现在学的这些东西,就是半途而废了,考研一年 能让你的 编程技能水平 回到解放前(考过研的同学应该都懂的)。
-
-不能说考研的内容一点用都没有,如果从技术学习的角度来说,其投入产出性价比极其极其极其的低。
-
-举一个不太恰当的例子,考研就是大家一起学 “一个不太实用的知识”,看谁学的好。
-
-所以考研其实更多的是学历上的提升,如果想通过考研,或者读研学习到什么? **还是不要有这个打算,大概率会让你失望的**。
-
-正如你所说的,你有信心成为年级里比较优秀的(就业方面),也正是 准备的早,所以给了自己信心。
-
-而且你们学校还有很多学长本科毕业就找到了好的工作,完全可以追随他们的足迹。
-
-去考研的话,有信心考上更好的学校,当然可以,关键是 考研也是千军万马过独木桥,特别是计算机考研,特别是985名校,非常的卷。
-
-如果没考上研究生,再去找工作就很被动了。
-
-这也是为什么,很多一战失利都会选择二战,因为如果失败,损失很大,所以这条路还要继续走下去,一定要上岸。
-
-再结合自己的情况,假如能考上,但考上了一所一般学校,其实对自己来说都是损失。 毕业之后 未必 有现在直接找工作找的好,年轻就是优势,特别是做研发,读研出来也是做研发,本科也是做研发,其实没太大区别的。
-
-所以 如果本科毕业的学长学姐 就业也不错,可以追随他们的脚步,毕竟你已经开始准备了。
-
-**如果有信心要冲 名校计算机研究生,或者说对某一所大学有情节,添补高考遗憾,那么可以冲,考上了是值得的**。
-
-
-当然也可以多和身边的 师兄 师姐交流,看看他们的说法,综合评估一下。
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\210\267\351\242\230\346\224\273\347\225\245\350\246\201\345\210\267\344\270\244\351\201\215.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\210\267\351\242\230\346\224\273\347\225\245\350\246\201\345\210\267\344\270\244\351\201\215.md"
deleted file mode 100644
index 285a5728dd..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\210\267\351\242\230\346\224\273\347\225\245\350\246\201\345\210\267\344\270\244\351\201\215.md"
+++ /dev/null
@@ -1,67 +0,0 @@
-
-
-
-
-
-# 代码随想录上的题目最好刷两遍以上
-
-今天秋招可能要提前很多,往年9月份开始秋招,今天可能9月份就已经结束了,所以 正在准备秋招的录友,还是要抓紧时间了。。
-
-星球里已经有录友的给出了关于秋招提前的信息
-
-
-
-那么在正式秋招之前,大家在准备算法,代码随想录上的题目 应该刷几篇呢?
-
-**至少刷两遍,只刷一遍是不够的**。
-
-只刷一遍基本就会忘,而且关键方法论理解的也不到位,对递归三部曲,回溯三部曲,动规五部曲,只掌握了简单招式,而没有理解真正精髓。
-
-拿到最简单的反转链表来说,只做一遍,下次面试出现基本还是做不出来。
-
-这也是星球里 录友们的 多么痛的领悟!
-
-
-
-**等大家刷第二遍的时候,才能找到触类旁通的感觉!**
-
-第三遍基本就得心应手了。
-
-在[「代码随想录」知识星球](https://programmercarl.com/other/kstar.html)中,我都是强调大家要至少刷两遍,有时间的话刷三遍,
-
-可以看看星球里录友们的打卡:
-
-
-
-
-
-
-
-有的录友已经开始三刷:
-
-
-
-
-我为什么鼓励大家有时间的话,多刷几遍呢,首先这里的题目都是经典题目,而且在面试汇总也是频频出现,
-
-下面也是星球里的录友总结的面经:
-
-
-
-
-
-
-## 那么已有的题目刷完了,可以刷些什么呢?
-
-我在Github上也做了一些题目的补充,在[上榜之后,都有哪些变化?](https://mp.weixin.qq.com/s/VJBV0qSBthjnbbmW-lctLA)说到了。
-
-对于面试来说除了「代码随想录」上的题目,再了解一下:排序系列,简单图论(深搜,广搜,最小生成树,最短路径等),高级数据结构:并查集,字典树(了解一下),之后就差不多了。随便在leetcode找一些题目保持手感,题量至少300+,会稳一点。
-
-关于深搜和广搜,其实深度优先搜索,我在二叉树的专题中讲解递归遍历,和回溯算法中 都讲了。
-
-广度优先搜索,在二叉树树的层序遍历也讲了。
-
-而图论中主要是在邻接表上进行的深搜和广搜。
-
-面试中还是很少会考察图论,因为图论的代码量往往比较大,不适合在面试中考察,**面试中出现题目概率最大的是二叉树,回溯算法和动态规划!**
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\215\232\345\243\253\350\275\254\350\241\214\350\256\241\347\256\227\346\234\272.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\215\232\345\243\253\350\275\254\350\241\214\350\256\241\347\256\227\346\234\272.md"
deleted file mode 100644
index 4da79e9743..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\215\232\345\243\253\350\275\254\350\241\214\350\256\241\347\256\227\346\234\272.md"
+++ /dev/null
@@ -1,53 +0,0 @@
-
-
-
-
-
-# 本硕非计算机博士,如果找计算机相关工作
-
-在[知识星球](https://programmercarl.com/other/kstar.html)里,有一位博士录友,本硕都不是计算机,博士转的计算机,问了这样一个问题
-
-
-
-一下是我的回答,在这里分享给大家。
-
-我的一些研究生同学,都在读博,有的毕业有的还没毕业,平时和他们聊,对博士就业也是有一定的了解,这里说一说我的建议。
-
-对于博士,强烈建议能去高校就去高校,这样最大程度发挥出了博士的好处,赚国家科研经费的钱还是香的。
-
-虽然现在对青年研究者并不友好,基本经济大头都被实验室boss拿走了。
-
-但高校壁垒高,外界再优秀的人才,也进不去和你竞争,所以可以小范围的卷。出来的话,就是和整个社会AI领域甚至和研发的同学一起卷。
-
-**在企业 是谁有能力谁就上, 在高校,至少你得有博士学位才能上! 这就是很高的门槛了**。
-
-
-而且能给博士提供岗位的企业少之又少,所以博士的就业面反而窄了。
-
-可能有同学想,薪酬要的低一点还不行么,其实博士毕业对薪资还是有要求的,如果薪资和本科,硕士应届生一样的话,自己也接受不了。
-
-所以高校能给博士的机会更多一些,不过现在高校也是 青年科研人员都是 五年合同制,如果没有产出,也要走人了,压力也很大。
-
-及时这样,还是建议能去高校去高校,当然这需要有心善、能力强、有人脉的博导,那是最好的了,(**注意这里选择博导,心善是最重要的一个选项**)
-
-实在去不了高校,接下来我们在看企业。
-
-博士找工作不建议走正式招聘流程,例如走官网投递之类的,面试都没戏。
-
-**AI岗现在对coding能力,工程能力要求都很高,企业现在特别喜欢有科研能力和工程能力的博士**,但这种人才还是稀缺的,大多数博士可能不会做那么多的工程项目,更别说还是本硕是非计算机专业的博士。
-
-所以博士找工作要靠门派,最好你的导师,实验室大boss 有哪些徒弟在外面企业,BAT华为之类的干的很好,能联系上,就是同一门派的师兄弟。
-
-联系上,做一个内推,他们看一下你的博士论文和研究成果,如果行的话,面试基本就是走个流程,这样就很舒服了。
-
-如果上来一波算法题,然后 操作系统,网络 数据库,这样考察下来,基本计算机专业的博士也招架不住,毕竟大多数博士是科研型的,一般来说工程能力比较弱,计算机基础哪些基本也没时间看。
-
-
-再说一说非科班博士如果有机会去面试,**一定要重点突出知识的迁移能力,和对学术的研究能力,这个是硕士本科生所不能具备的**。
-
-企业还是比较喜欢有快速学习能力和知识迁移能力的人,因为技术是不断在变化了,只有学习能力足够强再能跟上。
-
-所以**不能和本科硕士去硬拼专业技能的储备量**,特别是最新最热的技术(因为本来就是非科班嘛), 要体现出博士对技术对知识的理解,这是你的优势。
-
-以上是我的回答,希望多大家有所启发。录友们周末愉快🌹
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\220\210\351\200\202\350\207\252\345\267\261\347\232\204\345\260\261\346\230\257\346\234\200\345\245\275\347\232\204.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\220\210\351\200\202\350\207\252\345\267\261\347\232\204\345\260\261\346\230\257\346\234\200\345\245\275\347\232\204.md"
deleted file mode 100644
index dc31f5a79d..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\220\210\351\200\202\350\207\252\345\267\261\347\232\204\345\260\261\346\230\257\346\234\200\345\245\275\347\232\204.md"
+++ /dev/null
@@ -1,37 +0,0 @@
-
-
-
-
-
-# 合适自己的,才是最好的!
-
-秋招已经进入下半场了,不少同学也拿到了offer,但不是说非要进大厂,每个人情况都不一样,**合适自己的,就是最好的!**。
-
-[知识星球](https://programmercarl.com/other/kstar.html)里有一位录友,就终于拿到了合适自己的offer,并不是大厂,是南京的一家公司,**但很合适自己,其实就非常值得开心**。
-
-
-
-
-
-其实我算是一路见证了这位录友披荆斩棘,**从一开始基础并不好,还是非科班,到 实验室各种不顺利,再到最后面试次次受打击,最后终于拿到自己满意的offer**。
-
-这一路下来确实不容易!
-
-这位录友是从几年五月份加入星球。
-
-
-
-然后就开始每天坚持打卡。 可以看看她每天的打卡内容。
-
-
-
-后面因为天天面试,不能坚持打卡了,也是和大部分同学一样,焦虑并努力着。
-
-
-
-星球里完整的记录了 这位录友 从五月份以来每天的学习内容和学习状态,能感觉出来 **虽然苦难重重,但依然元气满满!**
-
-我在发文的时候 看了一遍她这几个月完整的打卡过程,还是深有感触的。
-
-[知识星球](https://programmercarl.com/other/kstar.html)里还有很多很多这样的录友在每日奋斗着,**我相信 等大家拿到offer之后,在回头看一下当初星球里曾经每日打卡的点点滴滴,不仅会感动自己 也会感动每一位见证者**。
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\244\207\346\210\2302022\345\261\212\347\247\213\346\213\233.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\244\207\346\210\2302022\345\261\212\347\247\213\346\213\233.md"
deleted file mode 100644
index 55c2a3bf6c..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\244\207\346\210\2302022\345\261\212\347\247\213\346\213\233.md"
+++ /dev/null
@@ -1,67 +0,0 @@
-
-
-
-
-
-# 要开始准备2022届的秋招了
-
-在[知识星球](https://programmercarl.com/other/kstar.html)里准备秋招的录友还真不少,也会回答过不少关于秋招的问题。
-
-
-
-能感觉出来慌,不止这一位提问题的录友,很多同学都是这样,就是感觉一天天过的很快,也没干什么事情,然后心里就一直恐慌。
-
-其实**慌主要是因为没有计划**,每天没有目的,然后一天一天的过,秋招越来越近,自然慌的很。
-
-我在这篇里系统性的解答了实习相关的问题,[关于实习,大家可能有点迷茫!](https://mp.weixin.qq.com/s/xcxzi7c78kQGjvZ8hh7taA),也提到了 一般秋招8月份就要正式开始了,那时候各种提前批,面试小道消息,甚至身边一些同学已经拿到了offer,恐怕已经没有时间静下心好好学习了。
-
-所以从现在看是满打满算,还有三个月的准备时间,如果利用好还是挺充足的,不要每天在把时间浪费在各种无聊的活动上。
-
-这里我来列举一下,此时大家应该明确哪些事情:
-
-## 确定自己要面试的岗位
-
-说道选择哪些岗位,一般的回答都是:选择自己感兴趣的呗,兴趣是最好的老师之类的balabala。
-
-但我能亲身体会到,目前咱们的教育环境 也不可能说培养出什么 自己真正的兴趣,在大街上随便问一个人你的兴趣是什么? 基本回答都是:吃喝玩睡呗,还能有啥兴趣。
-
-所以务实的建议是:现在去各大公司校招招聘官网 把所有的岗位都看一看,看看都有哪些要求,结合目前自己的经历和已经掌握的内容,看看哪些岗位可能最接近,然后再问问师兄师姐 这个岗位或者公司如何,最后后就把自己的目标岗位定下来了。
-
-也有很多录友在星球里问我关于一些公司,岗位前景之类的问题,我都会给出相应的建议,这也是我工作过程中接触过了解过的。后面我也把部分内容整理一下,在公众号分享一下。
-
-这样接下来的时间就是全身心去准备这个岗位所需要的技能。
-
-**不少同学在秋招的时候,试试算法岗感觉也行,投投前端感觉也行,尝试后端也不是不可以,甚至再面面产品经理**。
-
-**这样在秋招的时候就很被动了**,哪个岗位都没准备好,哪个岗位还都想试试,大概率是最后都没面上的。
-
-所以现在就要把自己的目标岗位锁定了。 不同岗位之间 要求还是不一样的。 大家去看看招聘官网的要求就可以了。
-
-## 制定学习计划
-
-知道自己的目标岗位了,也知道岗位的要求了,剩下的就是制定详细的计划。
-
-* 编程语言
-* 计算机基础(操作系统,网络,数据库、设计模式)
-* linux相关(客户端岗位应该不需要)
-* 项目
-* 准备自己的博客,github
-
-这几块按照岗位要求,8月份应该学会哪些内容,需要看哪些书等等。
-
-然后以八月份以deadline开始倒推,每个月应该学哪些,每周应该学哪些,每天应该看那些。
-
-把这些都明确了,心里就不慌了,也不会每天浪费大量宝贵的时间。
-
-星球里的录友每天都在坚持打卡,总结自己今天学习的内容,这样就很好,把自己每天学习进度量化。
-
-这样每天过的就很扎实。而且每天的打卡星球里录友和我都可以看到,我也会及时评论评论,大家也相互监督。
-
-给大家看一位录友在星球里的总结:
-
-
-
-大家平时的话,也要养成这种总结的习惯,只有及时总结把自己学过的内容积累下来,才能把时间沉淀下来,要不然就是一边学一边忘的节奏了。
-
-**其实这种习惯,无论是秋招,还是准备跳槽,还是平时工作积累,都非常总要!**
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\244\247\345\216\202\346\226\260\344\272\272\345\237\271\345\205\273\344\275\223\347\263\273.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\244\247\345\216\202\346\226\260\344\272\272\345\237\271\345\205\273\344\275\223\347\263\273.md"
deleted file mode 100644
index 0e905e42f2..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\244\247\345\216\202\346\226\260\344\272\272\345\237\271\345\205\273\344\275\223\347\263\273.md"
+++ /dev/null
@@ -1,81 +0,0 @@
-
-
-
-
-
-# 大厂的新人培养体系是什么样的
-
-之前我一直在[知识星球](https://programmercarl.com/other/kstar.html)和大家讲,能进大厂一定要进大厂,大厂有比较好的培养体系。
-
-也有录友在星球里问我,究竟培养体系应该是什么样的呢? 大厂都会这么培养新人么?
-
-
-
-其实大厂部门也是非常多,不同的领导对待新人的态度也是不一样的。
-
-就拿腾讯来说,腾讯里面 上千个部门,基本就是上千个小公司,只不过外面披一个腾讯的壳子,每个小公司的leader风格截然不同。
-
-赶上什么样的领导了,这就看命运了。
-
-**只能说进大厂,大概率会有一个比较好的培养体系**。
-
-那么好的培养体系是什么呢?
-
-要从两个方面来说:
-
-* 给你详细的学习路线(自我技术提升)
-* 给你有产出的活(用来晋升)
-
-## 详细的学习路线
-
-关于详细的学习路线,一般大厂入职之后配有导师的,导师给你安排的每一个功能,应该带你熟悉整个研发的流程。
-
-一个功能的开发,需要经历如下几步:
-
-1. 看需求文档,确定需求
-2. 这个需求包含了哪些功能
-3. 有哪些难点
-4. 后台架构是什么样的(要有架构图)
-5. 定协议(前后台一起商量),服务与服务之间的,后台与客户端之间的
-6. 设计数据结构+算法=程序
-7. 预估一下容量(各种资源例如带宽,存储,CPU等等)
-8. 考虑一下部署(安全性,容灾,可伸缩性。。。。)
-9. 设计评审
-(上面过程都是在分析)
-10. 编码
-11. 自测
-12. 联调
-13. 交给测试
-14. 代码review
-15. 合入
-16. 发布
-
-可以看出来,写代码仅仅是 其中的一小步。导师应该带你走一遍完整的开发流程,然后告诉一些注意事项,**这样为自己程序员生涯打好基础**。
-
-可能有的同学会感觉:我就开发一个小功能,哪用得着这么多步骤,一把梭哈,直接代码写完。
-
-哈哈,这么想的同学一般是没有参与过大型且流程规范的项目开发。互联网千万级用户的项目,几十上百人一起开发是需要规范的,**所以上面我说的每一步都很重要!**
-
-## 有产出的活
-
-初入职场的同学,可能都非常在意能不能学到东西,也就是自我技术提升,往往忽视了你干的活,是否有产出,能不能用来晋升。
-
-这里就是很多所谓的“套路”,老司机一般挑的活符合如下几点:
-
-* 非常规整(周边没有烂糟的额外工作,例如还要开发各种小工具之类的)
-* 独立模块(不需要和其他人扯皮,省事)
-* 对项目组很重要(既有技术难点,又没有大量的业务增删改查)
-* 风险系数比较低(上线出问题,锅甩不到他这里)
-
-这种活就是好活,用来晋升的利器,而且干着也舒服。
-
-但一个项目,一定会有大量需要打杂的活,写各种脚本,各种处理数据,然后看各种问题,整理文章,汇报,开发边缘工具等等。
-
-新人一般进来都是先打杂的,**但如果领导确实是细心培养你,还是额外给你一个小模块,让你做好,这个小模块就是让你用来晋升的或者转正的**。
-
-这个建议不仅适用于实习生,对于初入职场的同学也很用帮助,这个部门是不是有在培养你,老司机一眼就能看出来,只不过新人可能自己很难发现。
-
-所以需要过来人点拨一下,大家就知道自己现在的处境了。
-
-希望对大家求职和工作有所帮助!
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\244\251\344\270\213\344\271\214\351\270\246\344\270\200\350\210\254\351\273\221.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\244\251\344\270\213\344\271\214\351\270\246\344\270\200\350\210\254\351\273\221.md"
deleted file mode 100644
index ccd1c32679..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\244\251\344\270\213\344\271\214\351\270\246\344\270\200\350\210\254\351\273\221.md"
+++ /dev/null
@@ -1,71 +0,0 @@
-
-
-
-
-
-
-# 天下乌鸦一般黑,哪家没有PUA?
-
-相信大家应该经常在 各大论坛啊之类的 看到对各个互联网公司的评价,有风评好的,也有风评不好的。
-
-在[知识星球](https://programmercarl.com/other/kstar.html)里有录友问我这样一个问题:
-
-
-
-这位录友,拿了阿里云,腾讯,百度,shopee的意向书,可以确是收割机了,但如果还没有进入职场,还容易被网上这些风评误导。
-
-卡哥来客观的分析一下,如何看到这些网上对公司的评论。
-
-脉脉,知乎上喷阿里,百度的 都比较多。喷腾讯的确实少一些。
-
-**只能说腾讯的舆论控制相对比较好**,在加上职能体系的人文关怀比较到位(公仔,各种活动,逢年过节小礼品之类的)。
-
-但**天下乌鸦一般黑**,腾讯的职场PUA,一点都不比阿里的少,各种套路 ,阿里有的,腾讯都有,还有什么各种花边吃瓜新闻,在腾讯内网特别多。
-
-但这些都在公司内网传播,很少传出去。 这就是腾讯厉害所在了。
-
-其实我们在选择公司的时候,**主要是看岗位,看业务,看发展**,至于 有没有PUA之类的,只能说**有人的地方就有江湖**,腾讯人 就比 阿里人 就更正直么?
-
-相信大家都参加过面试。招聘的时候,几个小时的面试能看出人品么?对吧。
-
-各种新人背锅,末尾淘汰,PUA,阿里有的,腾讯都有。 所以大家求职的时候不用在乎这些风评。
-
-至于这种锅和套路 能不能落到自己的头上,就要看碰到了什么样的直属领导了。
-
-例如两位同学去了同一家公司,同一个事业群,同一个部门,同一个项目组,只是在两个不同的领导下干活,其区别都可以一个天上,一个地上。
-
-有的录友 可能对职场套路不太了解,或者 初入职场比较顺利,没有感受过什么套路。
-
-这里卡哥给大家随便说一个,例如,一个项目组,有前端组和后端组,分别是两个老大,有一个需求,要开发一个功能,这个功能本来前端就可以独立完成的,但上线可能会有风险,保护自己手下的前端领导,就会让前端同学和后端的同学一起实现这个功能,也就是前端实现一部分,后端也要做一部分数据处理,前端展示就可以了。
-
-为什么这么安排呢?
-
-因为一旦上线出问题了,就是前端和后端一起背锅,这样前端同学压力就小很多了。
-
-而整个需求安排,前端同学其实并不知道 自己的风险,其实是领导保护了他。
-
-那么 不保守下手的领导,当然就啥也不说了,让你一个人把这个功能做了,上线没出问题 那还算万事大吉,一旦出问题,那年底考核是不是就要背一个指标了。
-
-所以不要感觉程序员的工作环境很单纯,其实套路多得很,还是那句话:**有人的地方就有江湖**,不区分公司的。
-
-只能说 业务发展越好的部门,套路相对来说少一点,毕竟高速发展可以掩盖很多问题。
-
-**所以遇到什么样的直属领导非常非常的重要**,但这又是我们决定不了的。 所以这都看缘分(运气)了。
-
-有的同学毕业在大厂顺风顺水,除了自己努力外(而大家谁又不努力呢?),更重要的是遇到了好领导。
-
-但有的同学同样进大厂,发展就很差,而且没人给他指引一些部门潜在的规则,那就难免会撞坑。
-
-未必是他不够努力,不够聪明,不会沟通,可能恰巧 部门效益不好,部门考核就差,领导一般不会让老人背锅,毕竟系统的bug都是老人写的,老人都走了,谁来修bug呢(人间真实)。
-
-那领导就拿他这个新人开刀了呗。
-
-所以,**同样是进大厂,发展好的同学 不用过于优越感,感觉是自己能力多强,其实大概率是赶上了 好部门好领导,发展不好的同学也不要 自责自己能力不行,甚至开始自卑,大概率是运气不太好而已**。
-
-那么是 发展好坏全看运气了么,当然不是!
-
-重要是 遇到挫折(背锅,绩效不好,甚至被开除),不要自卑,不要放弃,相信自己,只要把时间拉长,5-10年的时间,**真正努力的人,发展都不错!**
-
-卡哥希望录友们都有好的发展,加油💪
-
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\246\202\344\275\225\346\235\203\350\241\241\345\256\236\344\271\240\344\270\216\347\247\213\346\213\233\345\244\215\344\271\240.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\246\202\344\275\225\346\235\203\350\241\241\345\256\236\344\271\240\344\270\216\347\247\213\346\213\233\345\244\215\344\271\240.md"
deleted file mode 100644
index 07e2bba6a5..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\246\202\344\275\225\346\235\203\350\241\241\345\256\236\344\271\240\344\270\216\347\247\213\346\213\233\345\244\215\344\271\240.md"
+++ /dev/null
@@ -1,47 +0,0 @@
-
-
-
-
-
-# 已经在实习的录友如何准备秋招?
-
-最近在[知识星球](https://programmercarl.com/other/kstar.html)一位录友问了实习生如何权衡工作和准备秋招的问题。
-
-
-
-
-其实这个问题挺有代表性,我之前一直讲的都是没有去实习的录友应该怎么怎么办,其实已经在大厂实习的录友也有很多烦恼。
-
-这里我说一说已经有大厂实习经历,准备秋招的重点。
-
-一般来说有大厂的实习经历,**面试官的兴趣都在大厂实习的项目经验上**。
-
-所以关于权衡实习和准备基础这两个方面,**可以把主要精力放在项目包装上,其次是看基础**。
-
-要在包装实习项目上多花点心思,实习生做的项目偏上业务很正常,不可能让实习生搞太复杂的,一旦出了问题还得导师背锅。
-
-自己干的活,或者项目很简单 不要紧,可以好好包装一下,如果没有难点,**可以结合业务自己“造”难点**,大厂内部研发文档都特别多而且特别全。
-
-例如整个项目的立项,各个模块的开发,以及研发中遇到的困难,技术选型,上线事故,等等这些都是有完整的文档记录的。(当然大厂也有一些部门研发流程很原始,没有文档,全靠口述)
-
-从这些文档中也可以找出 难点糅合到自己的项目中。
-
-假如线上出了事故,虽然自己不用去排查但可以跟着同事们一起看问题,一起分析,甚至帮他捞捞日志,打打下手。
-
-这次事故的表现,起因,定位等等,排查问题的同事都会记录的清清楚楚,放在项目文档下。
-
-可以把这些文档都多看看,然后就可以转变成自己排查线上事故的经历了。
-
-**这种经历在面试官来看就是很大的加分项了**。
-
-所以在大厂实习,想包装自己的项目方法有很多,只不过一些同学初入职场,对自己有帮助的资料或者内容不太敏感,不善于利用已有的资源。
-
-**需要过来人点一下,基本就上道了,哈哈哈**。
-
-当然不是说只要在大厂实习,基础完全就不用看了,抽空也要看,只不过 项目经验(项目的包装)更重要!
-
-关于提前批,一般来说本厂实习生是不能参加提前批的。
-
-你可以参加 其他大厂的提前批,只不过参加提前批是有代价的,面试不通过都是有记录的,得看自己项目准备的如何,能不能拿出手,而且一边实习一边面试可能也不太方便,如果想试一试,就找自己不想去的企业的提前批试试水!
-
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\256\242\344\270\211\346\266\210.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\256\242\344\270\211\346\266\210.md"
deleted file mode 100644
index 6b81ab2c6f..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\345\256\242\344\270\211\346\266\210.md"
+++ /dev/null
@@ -1,95 +0,0 @@
-
-
-
-
-
-# 客三消!
-
-给大家科普一下:什么是客三消。
-
-翻译过来就是客户端三年消失。
-
-**听起来是不是有点吓人**!这种说法略夸张,但只要能传开,就说明客户端一定有一些困局,并不是空穴来风。
-
-昨天卡哥在朋友圈里分享了一个段子的截图
-
-
-
-然后朋友圈就炸了,上百条的留言,问我这是为啥。
-
-其实这个问题在[知识星球](https://programmercarl.com/other/kstar.html)里也有录友问过我。
-
-
-
-当时已经详细的回答了一波,估计很多录友都有这方面的困惑,所以在公众号上再来给大家讲一讲。
-
-**关于星球提问中SRE和后端,在这里就不介绍了,卡哥重点说一说,客户端**。
-
-客户端目前比较突出的问题,主要是 这四点:
-
-1. 客户端岗位需求相对较小,而且有越来越小的趋势
-
-2. 技术做深相对较难
-
-3. 客户端晋升相对困难
-
-4. 中年危机 (其实程序员有,不过客户端可能更明显一些)
-
-
-## 岗位需求相对较小
-
-客户端需求的减少,主要是体现在中小厂,或者说创业公司,因为大家都养不起原生客户端,基本都会采用跨端的技术方案,也就是大前端(即一套代码可以编译出各个端的版本,包括安卓,IOS等各种终端)。
-
-这样就节省了很大的人力,不过目前在功能上一定没有 原生客户端体验好。
-
-**但大前端取代客户端是技术趋势!**
-
-如果选择客户端,那么就多少要掌握一些跨端技术方案。
-
-互联网软件的思维,就是轻前端,重后端,为什么PC软件搬到了浏览器上,移动APP搬到小程序上,都是这个道理,一般重头戏在后端。
-
-所以后端的需求才会比较大。
-
-## 技术做深相对较难
-
-这里就不止客户端,其实前端都有这个问题。
-
-关于前端和客户端的区别,其实没有那么严格的定义,大家可以理解 前端包含了客户端。一切可视化皆为前端。
-
-前端框架、渲染引擎 变化相对快,可能你刚熟悉一个框架,然后就换了,最关键是可能还拿不准哪一种框架日后会成为主流,一不小心就跑偏了。
-
-而后端框架变化相对就慢得多,而且 更容易(或者说更有机会)把技术做深,因为 高并发,高可用,低延迟 这些基本都是后端的工作。
-
-正是前端 技术栈更新太快,所以要持续高强度学习 (这种学习可能不是往深去学习,而是 适应一个又一个框架的学习)。
-
-而且前端 很容易陷入需求的反复变化之中,因为一个功能或者界面的修改,都是前端同学的工作量。
-
-后端可能 什么都不用改,接口都是一样的,然后就可以空出时间研究技术。
-
-## 晋升
-
-目前在大厂,客户端职业天花板相对较低,一般情况下,可能到组长就到头了。
-
-搞技术一路升上去,甚至到CTO的,基本都是后端,这也是因为前面讲过的:大部分的互联网产品,重头戏在后端,所有后端更有机会把技术做深,更直白说,后端更有机会在晋升做ppt的时候 “吹牛逼”。
-
-
-## 中年危机
-
-这个就更范范一些了,程序员都有这个危机,不过客户端可能更突出一些。
-
-原生客户端的岗位需求确实会越来越少,如果继续干下去,没有晋升到管理层,然后退居二线公司,发现二线公司都没有原生客户端的岗位,那么就非常被动了。
-
-所以可以往大前端的方向去转。
-
-大前端现在也有很多技术方案,ReactNative和weex(阿里,脸书的方案),Flutter(Google的方案),微信小程序(腾讯的方案)
-
-不过最终哪一个方案一统天下,这还是未知数,所以就需要持续学习咯。
-
-# 总结
-
-以上就是我在[知识星球](https://programmercarl.com/other/kstar.html)里的详细回答。
-
-注意我这里说的一般情况,当然各个岗位都有佼佼者,或者说大牛,客户端也有大牛,也很香,不过这是极少数,就不在讨论范围内了。
-
-希望对大家理解目前客户端的趋势有所帮助。
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\346\212\200\346\234\257\344\270\215\345\245\275\345\246\202\344\275\225\351\200\211\346\213\251\346\212\200\346\234\257\346\226\271\345\220\221.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\346\212\200\346\234\257\344\270\215\345\245\275\345\246\202\344\275\225\351\200\211\346\213\251\346\212\200\346\234\257\346\226\271\345\220\221.md"
deleted file mode 100644
index 4ad4659bc4..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\346\212\200\346\234\257\344\270\215\345\245\275\345\246\202\344\275\225\351\200\211\346\213\251\346\212\200\346\234\257\346\226\271\345\220\221.md"
+++ /dev/null
@@ -1,32 +0,0 @@
-
-
-
-
-
-# 技术不太好,也不知道对技术有没有兴趣,我该怎么选?
-
-最近在[知识星球](https://programmercarl.com/other/kstar.html)里解答了不少录友们的疑惑,其实发现一个挺普遍的问题:
-
-* 我技术很一般
-* 对技术也没有什么追求
-* 要是对什么岗位感兴趣我也不知道
-* 以后自己喜欢干什么也不太清楚
-
-**相信说到了不少录友心里去了, 其实目前应试教育下 确实很难找到自己感兴趣的事情**。
-
-但我想说的是,并不是技术不好就什么都做不了,依然有很多选择,只不过录友们没有接触过,所以就不知道自己接下来要往哪个方向走。
-
-这里我给出一些路线,大家可以参考:
-
-方向一:走纯研发路线,去大厂,如果真的对技术提不上兴趣可能会走的很辛苦。
-
-方向二:如果技术还凑合,不做纯研发,退一步也可以考虑去大厂做测试相关的工作,对技术要求没有那么高,但也需要技术能力,而且技术能力越强越吃香。
-
-方向三:去银行,证券,国企类的企业去做研发岗位,这种国有企业的技术面试相对简单不少,比较轻松,还很稳定,收入虽说不高,但生活足够滋润。
-
-方向四:做toC(面向普通用户)的产品经理,toC产品经理这个岗位就特别卷,因为这个岗位门槛太低了,任何专业的同学都可以去做产品经理。 这样自己所学的技术就基本没有用了,也凸显不出技术上的优势,但如果自己真的对APP之类的各种应用得心应手,优点缺点,用户爽点、日活、次留等等手到擒来,倒可以试一试。
-
-方向五:做toB的产品经理,包括云计算,大数据这些产品都是需要产品经理的,例如百度云,腾讯云,阿里云等等,这种产品本身就是技术产品,所以需要懂技术的产品经理来做设计,即需要产品的抓住需求的能力,也需要懂点技术,既可以发挥自己的技术能力,还可以做产品规划,基本也不用写代码。
-
-对技术要求不高的岗位也挺多的,发展也很好,只要大家多去了解,总会找打符合自己的岗位。
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\346\217\220\345\211\215\346\211\271\345\267\262\347\273\217\345\274\200\345\247\213\344\272\206.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\346\217\220\345\211\215\346\211\271\345\267\262\347\273\217\345\274\200\345\247\213\344\272\206.md"
deleted file mode 100644
index 3e25574669..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\346\217\220\345\211\215\346\211\271\345\267\262\347\273\217\345\274\200\345\247\213\344\272\206.md"
+++ /dev/null
@@ -1,48 +0,0 @@
-
-
-
-
-
-# 不知不觉华为提前提已经开始了
-
-最近华为提前批已经开始了,不少同学已经陆续参加了提前批的面试。
-
-在[知识星球](https://programmercarl.com/other/kstar.html)上就有录友问我这么个问题:
-
-
-
-华为是比较看重学历的,如果学校比较好(985以上),华为的面试会随意一些,几乎就是走一个流程。
-
-我记得当初校招的时候,华为来我们学校就是 几个大巴把整个计算机学院的同学拉到酒店,批量面试,面试过程差不多就是走个形式,大家基本都拿到offer了。
-
-不是说 非985/211的同学 技术就不好,而是当企业每年要招上万人的时候,通过学历还筛选相对来说是 效率较高且成本最低的一种方式。
-
-不过现在竞争越来越激烈了,华为也很少这种粗暴方式来召人。再说华为给出的薪酬相对互联网大厂也 很有竞争力,发展前景也很不错。
-
-那么在说一说面试的内容。
-
-可能有的同学感觉,我为了面试,准备了这么多,结果面试都没问,就问问项目问问编程语言就完事了。
-
-这其实很正常!
-
-不同的公司,同一个公司不同部门,同一个部门不同面试官 面试风格都不太一样。
-
-可能部门就比较缺人,面试官看一下 简历 学校可以,技术看上去还凑合,项目也还行,那么面试可能就放水一点,然后就过了。
-
-毕竟面试官也很忙,在大厂谁都不想当面试官,都是工作之余加班的工作量,**所以面试官也想快点结束**。
-
-还有另一种可能,就是部门已经招满了,但依然安排了面试,那么面试官就随便问问,然后也不会招人了。
-
-还有一种,就是有的面试官就习惯考察算法题,问完算法了,其他的他也就不会问了。
-
-因为操作系统,网络,数据库这些面试官在面试之前也要突击准备的,工作那么多年,都忘的差不多了,**所以要复习一波,这也是工作(都是加班啊!)**。
-
-甚至可能面试官前天刚要和女朋友过生日,然后就没准备计算机基础方面的内容,索性面试的时候也就不问了.....
-
-这都是有可能的,很多同学可能没做过面试官,所以对一些面试过程就容易想不通,其实面试官也是普普通通的打工仔,他也不想加班,也想只要人品端正,积极肯干,随便召个技术差不多的就算了,哈哈哈。
-
-所以说,面试有的时候也很看缘分的,大家辛辛苦苦造火箭,结果面试都没问是很正常的。
-
-大家也放平心态,把该做的做好,剩下的交个天意了。
-
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\344\270\213\345\215\212\345\234\272\344\276\235\347\204\266\346\262\241offer.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\344\270\213\345\215\212\345\234\272\344\276\235\347\204\266\346\262\241offer.md"
deleted file mode 100644
index 5862dd329f..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\344\270\213\345\215\212\345\234\272\344\276\235\347\204\266\346\262\241offer.md"
+++ /dev/null
@@ -1,99 +0,0 @@
-
-
-
-
-
-# 秋招下半场依然没offer,怎么办?
-
-[知识星球](https://programmercarl.com/other/kstar.html)里一些录友拿到了满意的offer,也有一些录友,依然没有offer,每天的状态已经不能用焦虑来形容了。
-
-在星球里就有录友向我提问了这样一个问题:
-
-
-
-估计还有公众号上还有很多录友也是这种情况,马上面试,但总感觉哪里都还没准备好,然后还必须要面试,每次面试结果还不理想。
-
-能感受到此时大家非常迫切要知道还有没有什么切实可行的方案 ,只要执行 ,就能拿到offer。
-
-恨不得提前知道面试官的问题,然后把问题都背下来得了。。。。
-
-其实我是非常理解大家的心情的,这个时候怪自己准备的太晚也没有用。
-
-说实话,已经是秋招下半场(接近末尾了),**已经没有针对面试的复习方案了。什么学习路线,突击计划 在这个时候 都没啥作用了**。
-
-现在什么最重要呢?
-
-是**心态**。
-
-心态要稳住,**放低预期,但别放低努力的程度**。
-
-估计参加过面试的同学,都会有这种感觉,面试前一天复习,突击的内容,**第二天面试都不会考!是的,一道都不会考!**
-
-那么为什么还学呢?
-
-就是这股劲不能泄,只要憋住劲,每天面试,复盘,学习,面试再复盘,再学习,最终大家其实都能拿到offer的,只不过是offer的满意程度罢了。
-
-**如果泄了劲,那就真没戏了**。
-
-**可能自暴自弃两天,然后就发现自己啥也学不进去了**。
-
-所以这个时候了,算法题还要一直刷,八股文也要背起来。
-
-讲真,现在刷的题,看的八股文,面试可能也不一定会考,但为什么还要看呢,**就是稳定心态**。
-
-**剩下的就看缘分了!**
-
-面试挺看缘分的, 可能一个面试官对你的评价就是基础不牢,下一家公司面试官对你的评价就是 基础不错,但项目经验不足。
-
-所以此时你自己都蒙了,究竟自己是 基础不牢,还是项目经验不足呢?
-
-其实面试的本质,面试官主观性还是比较强的,可能就是问的几个问题 你都背过,而且背的很深入,那评价就是基础牢了呗。
-
-
-## 在学点技术,冲春招?
-
-[知识星球](https://programmercarl.com/other/kstar.html)里还有一位录友,也是类似的情况,秋招感觉很艰难,要不要在学一学微服务分布式之类的,再冲春招。
-
-
-
-其实这个时候,大家也不要再心思学点什么新技术,增加自己的筹码了。
-
-还是那句话,**现在学啥,面试也未必会考!**
-
-就算 现在不参加秋招了,去学 微服务了,分布式。面试的时候 ,面试官未必会考不说,而且 你也未必学的透彻。
-
-再说,春招的岗位 很少,而且优质岗位更少。
-
-所以大家这个时候,就不要等了。
-
-**直接海投,面试,复盘总结,再面试**。
-
-
-## 给参加明年秋招录友的劝告
-
-其实我在[知识星球](https://programmercarl.com/other/kstar.html)里,**看到了太多太多 参加今年秋招的录友 埋怨自己 准备的太晚了,没想到要看的东西这么多,没想到竞争这么激烈**。
-
-所以明年参加秋招的录友,要提前就开始准备,明确自己的岗位,知道岗位的要求,制定自己的计划,然后按计划执行。
-
-**其实多早开始准备,都不算早!**
-
-很多在[知识星球](https://programmercarl.com/other/kstar.html)里的准大三,研一的录友,都能在星球里感受到 秋招的竞争与激烈。
-
-所以他们也就早早的开始准备了。
-
-来看看星球里 这位录友的提问,**他也才刚刚准大三,就已经为明年的秋招开始精心准备了**。
-
-
-
-估计大多数准大三或者准研一的同学都还没有这种意识。
-
-**但在[知识星球](https://programmercarl.com/other/kstar.html)里,通过每天录友们的打卡,每天都能感受到这种紧迫感**。
-
-正如一位星球里的录友这么说:
-
-
-
-很多录友加入星球之后,**刷贴吧刷剧已经不香了,只有刷星球!**
-
-感觉每天自己被push上去,其实有时候 **大家需要的就是一个氛围,自己一个人很难有提前准备的意识,也很难坚持下来**。
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\345\274\200\345\245\226.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\345\274\200\345\245\226.md"
deleted file mode 100644
index 826867854e..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\345\274\200\345\245\226.md"
+++ /dev/null
@@ -1,65 +0,0 @@
-
-
-
-
-
-
-# 开奖
-
-最近秋招的录友已经陆续开奖了,同时开奖多少,也是offer选择的一个重要因素,毕竟谁能和钱过意不去呢。
-
-[知识星球](https://programmercarl.com/other/kstar.html)里这位录友拿到的百度offer薪资确实很高
-
-
-
-以下是我的回答:
-
---------------
-
-百度给你的这个薪资很不错了,百度一向是大厂里最扣的那个,能给这个价位,说明 面试官对你很满意。
-
-语言方面你不用过于纠结,其实学什么语言最终都是为了赚钱是吧,钱给到位了,写啥不都一样么。
-
-而且如果后面php 要转go的话,你就还很多锻炼的机会,对你晋升十分有好处,php转go,就是把已有的后端代码全部重写,相当于是重构,就是很多工作可以做。
-
-**程序员最喜欢的就是重构**,重构既能提升技术,又能向上邀功(**可以理解就是容易晋升**)。
-
-如果是稳定的业务,进去大概率是打杂的,因为没有那么多代码可写,进去就改改bug,写写脚本分析分析日志,之类的,总之不会接触到核心代码,没有机会,也没有必要。
-
-但如果稳定的业务 代码要重构,就有机会可以读一读核心代码,分析哪里设计的不好,重写一遍,那就可以大展宏图。
-
-这就是为什么程序员都喜欢重构。
-
-字节的话 后端-系统架构,这个岗位是可以的,但具体什么部门,具体做什么就不太清楚了,至于你说的边缘部门,如果这个部门就叫做系统架构部的话,不可能是边缘部门。系统架构部门还是挺重要的。
-
-但如果 字节和百度选的话,我还是倾向于百度。 百度对新人培养还很到位的,比字节强多了。
-
-滴滴就不考虑了, 华为 2012实验室中央软件院,其实是不错的,适合好好搞技术,但关于 多媒体开发 这块 要看具体工作了。
-2012 一般薪资开的不高,毕竟不是华为的业务部门。
-
-如果百度过几天逼签的话,就签了吧,挺香的,总包都40w+了,百度一向是很抠的,**这个价位,百度是下血本了**。
-
-如果还能拖,就等 字节 和 华为开奖,如果能开到 50w+,那就考虑考虑。
-
-如果薪资差不多,就百度吧。
-
-
--------------
-
-这位录友最后也决定签百度了。
-
-
-
-大家也注意到,这位录友是把《代码随想录》刷了两篇,这里我也是推荐大家至少要把 《代码随想录》上的题目刷两遍,才能真正的掌握我讲解的内容,第一遍大家可能就是力扣提交通过了,然后这道题目就过了,没有深入去思考,也没有深入去看我的题解。
-
-但第二遍的时候,就可以深入思考 上一道题目与本题之间的联系,动态规划或者二叉树的题目套路 等等了。
-
-
-还有这位录友在星球里提到,用了我推荐的项目,其实这就是这个:[基于跳表的轻量级KV存储引擎](https://mp.weixin.qq.com/s/i3vJd0nPqQFyLRH9Px84YA)
-
-
-
-这个项目是我18年的时候,业余时间写的,大家如果按照我的代码来,从代码质量到结构设计,绝对是可以拿得出手的。
-
-但我没有写这个项目的详细教程,很多基础不太好的录友可能看着有点费劲,后面我会在B站上出一期详细的讲解视频,公众号也会同步文字版本。
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\346\200\273\347\273\2231.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\346\200\273\347\273\2231.md"
deleted file mode 100644
index 2aec24dc9e..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\346\200\273\347\273\2231.md"
+++ /dev/null
@@ -1,194 +0,0 @@
-
-
-
-
-
-# 无竞赛,无实习,如何秋招?
-
-今年秋招已经结束了,今天给大家介绍一位知识星球里的录友,我也是见证了他一步一步准备,从绝望到看到希望再绝望到最后拿到offer的全部过程。
-
-我记得给他改简历的时候,就说过他冲客户端的话,可能更稳一些。
-
-时间总是过得很快,但曾经焦虑的小伙,现在也拿到几个offer了,不一定人人都要冲大厂,卷算法,卷后端,合适自己就好,要不然会把自己搞的很累。
-
-以下是他的秋招总结,**写的很用心,说了很多面试中使用的方法,发在[知识星球](https://programmercarl.com/other/kstar.html)里,立刻就引来星球小伙伴们的围观**,算是给星球里明年要秋招的录友做了一个参考。
-
-
-
-我也征求了他本人的同意,将其分享在公众号上,希望对大家有所启发。
-
-以下原文:
-
-
-## 个人背景
-
-* 学校:末流985
-* 学历:本科
-* 所学专业:网络空间安全
-* 意向岗位:后端开发
-* 校内学习情况:**无竞赛,无实习经历,自己平时也没有捣鼓过任何项目,只是跟着学校的课程学习**😅。
-
-## 秋招时间线
-
-(**加粗内容为时间节点**,中间还穿插着各种笔试,这里就不列举了)
-
-* 二月初-二月中旬:开始刷leetcode
-* 三月初-四月初:背八股文,刷题,同时投递实习岗,但由于个人实力不足以及投递时间太晚,一面均未通过
-* 四月-七月:完成学校内的课程,准备专业课考试,同时背八股文,不断地重复刷算法题
-* **七月二十六日**:收到字节跳动**客户端软件安全工程师**岗位的面试邀请
-* 八月初:面试**腾讯**后台开发岗,一面挂
-* **八月十五日**:通过字节的三轮专业面试和一轮hr面拿到**意向书**
-* 八月下旬:连续面试美团,蚂蚁;美团一面挂,蚂蚁一面通过
-* **九月三日**:蚂蚁二面通过
-* **九月十三日**:成为字节跳动实习生,提前感受工作内容
-* 九月下旬:**通过五轮面试**:蚂蚁三面和hr面;华为两轮技术面和主管面
-* **十月二十九日**:收到字节正式offer
-* **十一月五日**:收到华为意向书
-
-小结:
-
-- 面试的企业:华为,字节,蚂蚁,腾讯,美团
-- 通过的企业:华为,字节(蚂蚁泡池子)
-- 字节客户端安全-深圳
-- 华为软开-深圳(东莞):14a
-- offer选择:大概率华为,中概率字节,小概率春招再战
-
-
-
-## 技能准备
-
-### 算法和数据结构
-
-掌握程度:
-
-- 代码随想录 上的题目:programmercarl.com, 除了单调栈以外的所有题目都做过,且大部分题目**3-7**刷。
-- leetcode总共题数:219题。
-- (https://www.nowcoder.com/ta/huawei )做了68道。
-- 二叉树,红黑树,B+树,数组,链表,堆栈等基础知识均掌握。
-
-取得的效果:
-- 95%的面试题都能做出来,能讲出时间,空间复杂度和实现思路,但没见过的题目不一定能想出最优解。
-- 华为笔试:200分 (100分通过) ,蚂蚁笔试通过率:70%,网易笔试通过率:80%。
-
-### 学习建议
-
-按照卡哥**出题的顺序**刷,且要**重复刷**,**不能只是背代码**。还记得春招的时候,面试官问我**两数之和**,我能把代码写出来,但当问到实现思路,为什么这么实现,我答不上来;相类似的还有**二叉树迭代法的统一写法**,当面试官问我为什么这么写的时候,我说不知道:sob:
-
-把卡哥的题刷得差不多的时候,建议再刷两个专题:
-
-* 二分法 :https://leetcode-cn.com/problems/search-insert-position/solution/te-bie-hao-yong-de-er-fen-cha-fa-fa-mo-ban-python-/)
-* 滑动窗口:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/hua-dong-chuang-kou-by-powcai 。
-
-
-笔试题刷题要点:
-
-- **多刷**,**多刷**,**多刷**,**熟悉ACM格式**
-- 面试题刷题要点:
- - **时间,空间复杂度**
- - **优化方案**
- - **解题思路**
-
-以下是本人对各个知识块的掌握情况:
-
-### 计算机网络
-
-掌握程度:应用层到数据链路层常考题都背得很熟悉,但细节没记到位,美团曾经问过我**tcp报文的可选字段**,没答上来。
-
-### 操作系统
-
-掌握程度:熟悉常见面试题,但当问到特别具体的问题时难以回答,比如:给一个具体的虚拟地址,请求出它映射到的物理地址。
-
-### 数据库
-
-掌握程度:这部分掌握得最少,仅限于面经里MySQL的面试题:cold_sweat:。
-
-### C++
-
-掌握程度:基础面试题,stl,面向对象等面试题滚瓜烂熟,但面试的时候语言考察得很少,我遇到过的只有:**malloc**底层实现,**stl**相关底层原理。
-
-### 项目
-
-提前准备好以下问题的答案:
-- 为什么要做这个项目?
-- 是**为了解决什么问题**?
-- 项目的性质是什么?
-- 多少个人完成?
-- 你负责了什么工作?
-- 有什么亮点?难点?
-- 运用了哪些技术?
-- 有什么收获?
-
-## 经验技巧
-
-### 模拟问答
-
-把**一切**在面试中可能被问到问题的**答案背下来**,假设你的对面有一个面试官,把问题的答案说给他听,这样有几个好处:
-
-1. **锻炼表达能力**。有过面试经验的同学应该可以感受到,明明自己对一些问题很了解,但面试时讲得磕磕巴巴,逻辑不清晰。
-
-提前模拟面试场景,可以帮助你提前发现**语言表述上的问题**,以达到在面试过程中**清晰表述内容**的效果。
-
-2. **查漏补缺**。在回答问题时,自己很有可能产生一些**疑问**,而这些疑问也是面试官**特别喜欢**考察的地方。
-
-举例:
-
-- 面试官问:进程和线程的区别?
-- 回答:
-- 进程切换比线程切换开销小
-- .......
-- 面试官追问:进程切换开销**小在哪**?你能详细描述出进程切换的具体过程吗?
-
-如果你能在面试前自己发现这个问题,查漏补缺,面试的时候也会更游刃有余。
-
-### 转移话题
-
-转移话题,即改变话题方向。
-
-举例:
-
-面试官问:为什么使用DH对称加密算法而不使用其他其他对称加密算法?
-
-两种回答:
-
-* 这个我没想过:sob:
-* 我在完善项目时**不侧重于**将其与其他算法比较,而是把**精力更多地放在**DH算法的改进和优化上。
-
-针对第二个回答,面试官会追问,那有哪些优化呢?而这个恰恰是我背得滚瓜烂熟的地方😍。
-
-### 关于实习
-
-实习重要吗?很重要,我去提前实习后,更清楚了企业看中实习经历的原因:**节省培养成本**,更快上手业务。
-
-没有实习经历可以吗?可以,如果时间紧迫,秋招/春招前没有实习机会,那就花时间**把实习期需要学习的事情去做一做**,例如:学习GIT,搭建博客,学习开源项目,学习linux等(当然这些我实习前都没做,是实习的时候才知道的:joy:),这些放在简历上也是加分项。
-
-
-## 与代码随想录的故事
-
-### **算法**
-
-今年二月初,刷题的时,看到carl哥的题解,觉得讲得很不错,关注了微信公众号:代码随想录。于是我就按照刷题攻略的顺序进行刷题,题解讲得很详细。在秋招前我几乎把当时所有题都刷完,而且刷了好几遍。面试时的出题命中率**真的很高(字节,蚂蚁的出的所有算法题都被包含在内)**,即使不是原题,实现的算法也是类似的。
-
-
-
-### **公众号文章**
-
-公众号里的文章我也非常喜欢,比如各城市互联网公司的总结,之前一直没太注意这个事情,觉得头部不就是那几个大厂吗?冲就完了。秋招过后觉得这篇文章还是挺有用的,因为眼光**不能只局限于互联网大厂**,并不是任何时候都应该向大厂冲,适合自己才是最重要的。
-
-
-
-### **知识星球**
-
-后来卡哥开了知识星球,我马上就申请进来了,这段时间一直在打卡,督促自己学习。星球上主要分为两大块内容:打卡系列和问答系列。
-
-- 打卡系列,记录着各位同学的每日学习情况,其中有不少文章都总结得用心。
-
-- **问答系列(对我帮助很大)**,主要包括**offer选择**,**学习路线**,**学习建议**;每天都会有同学提出自己的问题(当然我也问了不少),carl哥的回答很真诚,**也很有参考价值。**
-
-
-
-## 小结
-
-秋招已告一段落, 这段时间真的经历了很多事情,虽然现在对结果没有很满意,但也积累了一些经验,走一步看一步吧。
-
-大家可以结合着我的经历思考这么一个问题:**知识掌握到什么程度可以拿到一个什么水平的offer**?最后,祝大家都能拿到自己满意的offer~
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\346\200\273\347\273\2232.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\346\200\273\347\273\2232.md"
deleted file mode 100644
index 897a7ec3ec..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\346\200\273\347\273\2232.md"
+++ /dev/null
@@ -1,48 +0,0 @@
-
-
-
-
-# 倒霉透顶,触底反弹!
-
-星球里不少录友秋招已经陆续结束了,很多录友都在[知识星球](https://programmercarl.com/other/kstar.html)里写下了自己的秋招总结,但今天这位录友很特殊,甚至我给她修改简历的时候我都“有点愁”。
-
-他的秋招过程也是极其坎坷,**逼签、被养鱼最后收到感谢信、校招流程收到实习offer,还有数不清的简历挂……**,可能是太倒霉了,最后触底反弹,接到了百度的offer,虽然是白菜价,但真的很不错了。
-
-这篇总结并没有说学习路线,而是说一说她自己的感想,算是另一个维度,感觉会对大家很有帮助,所以我也分享出来。
-
-
-
-以下星球原文:
-
--------------
-
-秋招终于结束了,收到了百度C++开发、小米C++音视频开发、深信服C++开发、知乎go后端、神策java后端的offer,不是啥大佬,全是烂白菜,但是作为一个双非本的孩子,我觉得已经很幸运了,最终无脑选择了百度,春招还会再冲一波。
-
-**星球里的学习气氛很好,向卡尔哥问过好几个问题,每个都挺长,卡尔哥也给我耐心解答过,还帮我改过简历,感谢和大家相遇!** 关于学习路线大佬们说的很详细了,我觉得参考他们的已经足够有价值。**那我就为星球里还在奋斗的兄弟姐妹、以及学弟学妹们写一下我的一些其他的感想**。
-
-1、**准备一定要趁早**。我是今年1月份准备的实习,那个时候我连C++智能指针都不知道,就一直陆续学着,6月份上岸滴滴实习,8月下旬跑路,可以说9月份才开始大规模投递。我是很菜的人,还不算勤奋,秋招不至于空手而归就是找实习的时候还有些积淀。 所以最大的感想就是,一定要越早越好,我认为理想的时间节点:3月份去实习,7月份投提前批,所以什么时候去准备可以根据这个时间去准备,当然越早越好。越早企业越缺人,越好进。
-
-2、**多面试,哪怕实习不了也要多面试;多投简历,投了就有机会**。我没啥自主学习的意识,只有在要面试的前一个礼拜才会多少看点,还记得当时收到第二天就要腾讯面试的消息,前一天晚上我几乎通宵了,虽然最后还是挂了。但是我当时真的是要面试的恐惧支配着我学习 。
-
-3、**八股文、看书和刷题**。实习面试的话看八股文还是能应付过去的,但是我看了大多数八股文感觉千篇一律,想真正化为自己的东西还是要看书,我一般喜欢看书,然后把书上的东西化为自己的理解记在笔记上。 刷题也是,我一般喜欢根据自己的理解把题归类,自己写题解。
-
-4、**实习。我觉得实习比较好的时间节点是3月份(或者更早)**,暑假去实习如果不能保证转正,感觉还是有点耽误提前批,提前批是最最最好进大厂的一次。像我的话,白天工作一天到晚上九十点,回家就只想躺着,再遇上比较push的工作环境,做到实习和复习兼顾是有点难的。我觉得核心竞争力应该是基础知识的掌握程度,身边确实没有实习、但是基础和项目足够牛批的人也能进大厂,所以还需要做一个平衡吧。当然也不是劝退,实在平衡不了的话就要要根据自身情况吧。
-
-但是实习确实让我简历好过了一些,这个就是实习最大的好处之一,但是我感觉像美团腾讯百度的话,如果笔试面试还可以的话,应该不太卡简历的。字节后端确实有点卡实习,还有一些没那么大但是也很强的厂是卡学历或者实习的。
-
-5、还有秋招不要all in,不要all in ,不要all in!!!
-
-以上就是我的一些感想,可能仁者见仁智者见智,希望大家多多指正。
-
-我的秋招真的不是很顺利。逼签、被养鱼最后收到感谢信、校招流程收到实习offer,还有数不清的简历挂……,但是最后也有好的结果了,其实想的比较开,大不了就春招再进大厂呗。本来都要打算春招了,书都买了几本,但是没想到能收到百度意向,已经开始流程推进啦。
-
-可能这就是倒霉透顶了就触底反弹吧。**一直觉得自己倒霉透了,奇葩的时间都被我遇上了,但是最后还是被好运眷顾了一下**。我的学校还是双非本,很一般的学校,所以任何时候都有机会,拿到offer不光是实力因素,运气成分也很大,大家一定要多投多面,万一哪次就上岸大厂了。
-
-**还是想参与一下春招,但是现在发现躺着太舒服啦**。还是得卷起来,争取以后能每天在星球里打卡哈哈
-
-祝愿各位都有理想的offer!所愿皆所得!
-
--------------
-
-以上就是星球里的原文,所以这位录友现在又可以愉快的在星球里,每日打卡了 哈哈哈。
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\346\200\273\347\273\2233.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\346\200\273\347\273\2233.md"
deleted file mode 100644
index 05a677ed81..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\346\200\273\347\273\2233.md"
+++ /dev/null
@@ -1,83 +0,0 @@
-
-
-
-
-# 秋招面试,心态很重要!
-
-其实无论社招,还是校招,心态都很重要,例如,别人那个一堆offer,自己陷入深深的焦虑。 面试分明感觉自己表现的不错,结果就是挂了。面试中遇到了面试官的否定,然后就开始自我怀疑,等等等。
-
-如果你也遇到这些问题,可以认真读完[知识星球](https://programmercarl.com/other/kstar.html)里一位录友的总结,他是非科班,机械转码,今年5月份加入的星球,坚持打卡几个月,如果也获得自己心仪的offer,他的心路历程对大家会很有启发。
-
-
-
-以下原文:
-
-----------------------
-
-写在最前:作为最一般的秋招选手,希望能对和我有相似经历的录友提供一些帮助。
-
-1、个人情况:本硕211机械转码,无竞赛,无实习。转码原因:机械的工资真的不高,也不是很喜欢天天画图的生活;导师教本科C/C++语言,研一当助教重新学了一下C++。
-
-2、加入星球:
-
-3月份开学决定转码,3月到5月看C++primer和刷题,之前也是按题目顺序刷,效果一般。我是在LeetCode刷题的时候看到Carl哥的题解,然后关注随想录的GitHub,刷完链表在5.27决定加入的星球(那时还没有优惠,哈哈)现在也快半年了,那段时间是我最慌的时候,毕业课题没做完,秋招准备的也不行。刚看完C++ Primer,刷了几十题的状态。就是在星球打卡的这段时间学习比较认真,让我走过了比较难的一段路,感谢Carl哥的指导和各位录友的输出。(用Forest防止玩手机并记录了一下,6~9月平均学习15000分钟)
-
-3、秋招历程:
-
-6月到7月,C++基础就每天看一条《effective C++》,操作系统和计算机网络看书来不及就准备找网上大佬的精华过一遍然后直接面经。我找的是小林大佬的图解网络+图解操作系统,阿秀大佬的面经。(每日一问的主要来源)。
-
-刷题大概每日一题+3道随想录+2剑指offer,7月完成一刷,8月底完成二刷。一刷的时候白天在LeetCode提交之后,晚上会在本子上手写总结一遍,理一理思路。二刷的时候就在Typora上快速记录了一下,节约时间。开始面试的时候题量:二刷完随想录+剑指offer,一共350题左右。在刷完以后可以上CodeTop刷各公司部门岗位的高频题。
-
-7月份有的公司就陆陆续续开始提前批了,虽然知道提前批不需要笔试多一次机会,但是因为自己简历项目一块还比较单薄,一直没有进行投递。7月底买了《高性能服务器编程》一周多看完,然后做了一个基础的Web服务器,有不懂的就上网搜类似的解析。自己画了一下流程图之类的梳理了一下大概思路。第二个项目就是Carl哥的跳表项目,不是很长,主要是理解跳表这个数据结构,自己画一画就清楚了。
-
-8月底开始投简历,对自己定位比较明确,主要以中厂为主,大厂随缘。8月底开始基本每天都有笔试,9月开始面试,一直到10月下旬结束秋招。面试的时候主要是录音,结束后进行面试总结,包括面试问题,手撕代码,面试表现(回答问题思路是否严谨,吐词是否清晰)。
-
-其余关于实习,竞赛,面试的经验可以参考星球里其他大佬,哈哈哈。我感触比较深的是心态问题,这里写一写我自己遇到的心态问题和感想。
-
-
-4、心态很重要:
-
-心态问题1:同学或室友提前批就拿到了30大几w甚至更高的的offer,自己才开始笔试,说不羡慕是不可能的,有几天晚上都有一点焦虑没有睡好。这可能是因为自己并没有全力以赴,害怕因为自己的不努力而失去机会,后来每天都在按计划学习,每天都比较充实,抱着努力不后悔的心态。当你一心学习的时候就不太会一直考虑让自己焦虑的问题,总之一句话,冲就完事了。
-
-心态问题2:总感觉自己没有准备好而不敢投简历。项目没准备或者面经记得不熟。其实投了简历以后还会有一段时间,包括HR筛简历,测评笔试才到面试,所以能投尽早投,在面试之前有压力的时候复习效率比较高的。
-
-心态问题3:面试时的心态:心里默念一定要思考一下,然后慢慢说。我有一次面试,前面几个问题不是很顺利,然后问了我一个我会的,我就把我想到的一股脑说出来,我想着终于遇到一个我会的了,自我感觉良好,结果面试结束复盘的时候才发现,那个问题说的不是很全面而且说的比较快,面试官感觉可能很一般。
-
-心态问题4:有时可能会紧张的不行,导致本来自己知道或者有思路而因为紧张语无伦次没有表达出来,从而影响面试表现(可以事先准备好自我介绍,流畅的自我介绍在一定程度上可以建立信心,减缓紧张。参加一些模拟面试,找同学模拟或者在网上模拟,还可以录一下视频,看一下自己的表现怎样,有什么地方需要注意;找一些中小厂练手实战也不错)
-
-心态问题5:面试后的心态,可能有时候觉得自己表现还不错但是面试挂了而愤愤不平,或者和同学一起面一家公司,同学过了,自己挂了。这个时候需要把心态放平,及时总结自己的面试表现,可能没有你想象的那么好。而且面试主观因素挺多的,结果和面试官有很大关系,所以做好自己,尽人事听天命。
-
-心态问题6:面试过程中可能会遇到比较不好的面试官,导致面试体验比较差而影响自己的心态,这个时候也是需要及时发泄,不要自我否定或赌气,想着后面还有更多的机会,把更多精力投入到下一次面试或笔试中。我遇到的面试官都比较nice的,希望大家也一样!
-
-5、写在最后:
-
-最后对主要面试结果做一个总结:腾讯(笔试挂),百度(二面挂),最后拿到的:华为通用软开,深信服C++开发,科大讯飞C++,中电28所,30所。最后选择离家近的成都华为,总的来说虽然没去成大厂,但是自己已经很满意了。
-
-最后的最后:说的有点多,比较啰嗦,感谢大家能看完。希望大家一定不要放弃,相信自己,能拿到自己想要的offer!!
-
----------------
-
-可以看一下这篇总结在星球下的评论:
-
-
-
-**他在星球里知名度还是很高的,为啥知名度这么高呢?**
-
-因为他坚持几个月的打卡,而且打卡质量非常高,所以星球里的录友们都认识他。
-
-来看看他的打卡内容:
-
-
-
-
-
-
-
-我只是随便选了几条,他有将近200条的打卡记录,如果大家也在准备明年秋招,可以进星球里看看他的每日的打卡记录,相信会对规划自己的学习计划,任务安排 都很有帮助。
-
-而且星球里的学习氛围会让你惊讶,原来有这么多大佬,还这么努力。
-
-
-
-
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\347\232\204\344\270\212\345\215\212\345\234\272.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\347\232\204\344\270\212\345\215\212\345\234\272.md"
deleted file mode 100644
index 6c81757745..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\347\232\204\344\270\212\345\215\212\345\234\272.md"
+++ /dev/null
@@ -1,55 +0,0 @@
-
-
-
-
-
-# 秋招上半场的总结
-
-八月份已经接近尾声,不少录友已经在[知识星球](https://programmercarl.com/other/kstar.html) 已经总结了秋招的上半场。
-
-
-
-可以看出 这位录友也是身经百战,目前也拿到了几个offer。
-
-星球里还有不少录友已经拿到了字节,阿里,百度提前批的offer。
-
-
-
-不过绝大部分录友还在经受着考验,处于焦虑的状态,秋招上半场也是几多欢喜几多愁。
-
-找工作其实是一个很虐心的过程,没有offer、没准备好、面试没发挥好、差一点就能把这题过了 等等,每一个场景都给大家增添一份焦虑。
-
-星球里有一些录友就找到了一个方向,或者一个准备同一家公司的伙伴,就会好一些。
-
-
-
-**有时候,有压力,自己憋着,没人交流,只会压力越来越大**。
-
-对于还没有offer的录友,我对大家的建议也是,**心态要稳住,要适当放低预期,不是所有人都要大厂,但不能放低自己对学习的要求!**
-
-有些同学,经过几次面试的打击之后,直接就自暴自弃,狂玩几天想释放压力,这么做的结果,只会压力越来越大。
-
-所以,**秋招进行时,大家不要过于放松,无论什么情况,只要没有拿到心仪offer,就不能放松,一旦放松之后,换来的就是更看不到希望**。
-
-有的同学可能学校不好,有的同学是转行计算机,一路下来确实艰难。
-
-我在星球里,看到的不仅是大家准备秋招过程的每日学习总结、打卡,也看到了一些录友的不容易。
-
-
-
-说实话,看着评论区,还是很感动的,估计这位 打卡的录友,也得到了很大的鼓励。
-
-这可能是 成千上万 目前正在冲刺秋招应届生 的一个缩影。
-
-面试不仅仅是只看技术,也挺看缘分的,有的同学可能感觉莫名其妙的就挂了,有的同学可能感觉莫名其妙的就拿到了offer。
-
-我就简单列举几个原因。
-
-* 可能部门缺人,或满了
-* 可能是当天面试的同学都不太行,就矬子里拔大个
-* 可能之前有几个优秀的毕业生,但按照之前的标准都没过,然后面试官发现这么下去招不到人了,一下子就把标准降低了,然后轮到了你,你感觉你发挥的并不好,但也给你offer了。
-
-所以面试也有很多很多其他因素,也很看缘分。
-
-大家放平心态就好。
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\350\277\233\350\241\214\344\270\255\347\232\204\350\277\267\350\214\253\344\270\216\347\204\246\350\231\221.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\350\277\233\350\241\214\344\270\255\347\232\204\350\277\267\350\214\253\344\270\216\347\204\246\350\231\221.md"
deleted file mode 100644
index 24e7760cb9..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\347\247\213\346\213\233\350\277\233\350\241\214\344\270\255\347\232\204\350\277\267\350\214\253\344\270\216\347\204\246\350\231\221.md"
+++ /dev/null
@@ -1,55 +0,0 @@
-
-
-
-
-
-# 秋招进行时,其实大家都很焦虑
-
-大家应该都发现了,提前批和秋招都越来越提前的,大部分的录友此时的心态还是挺焦虑的。
-
-特别是大三的同学吧,同时面临这找工作和考研两个方向的诱惑。
-
-一位录友就在[知识星球](https://programmercarl.com/other/kstar.html)问了我这个问题:
-
-
-
-其实 互联网35岁中年危机,应该吓退了不少小鲜肉,互联网35岁中年危机是真的,网上哪些关于裁员的消息 大多数也都是真的。
-
-但我想说:特别是正在找工作的同学,大家应该都是 95后,00后,可以先不用想那么长远,能把未来五年规划好,就不错不错的了,现在行业的变化,都是两三年一变天,等大家35岁的时候,都不一定啥样了。
-
-而且卡哥还替大家先趟趟路,哈哈哈
-
-现在的大三,最忙的就是两伙人,**考研的和找工作的,这两条路都挺难的**。
-
-另外还有不少录友应该在考研与找工作之间摇摆。可能有的学校大多数都在找工作,有的学校大多数都在考研,不过应该考研占绝大多数。
-
-关于考研我的观点是,如果 本科毕业能拿到大厂或者中厂offer,可以不考研,看看自己比较优秀的学长学姐,毕业都去哪了,是否去了大厂,如果比较优秀的学长学姐 就业也一般,我还是推荐读研的,因为顺便也提升一下学历。
-
-但有的同学是从大一入学就规划了自己 以后直接工作的,这种无论学校如何,我都是比较支持的!
-
-**因为从大一就明确自己的方向,按找工作的要求来学习,一般来说 最后找工作都不会差**。
-
-最危险的是,大一大二没计划,到了大三开始摇摆,考研还是找工作。这种是最危险的,如果在纠结一阵纠结到 现在的话,那基本哪条路都走不好了。
-
-对于现在找工作的录友,可能发现身边的同学都在考研,而且在准备找工作中,可能还没有明确的学习计划,东打一发西扯一下,就会很焦虑,主要也是身边一起工作的同学可能不多,交流的少。
-
-找工作是一个累心的过程,非常累,恨不得哪家公司赶紧给我个offer,让我干啥都行,甚至怀疑自己是不是要再去考研。
-
-**其实这时候,不是自己方法不对,也不是自己选择的路错了,而是需要一个过来人,给你打打气**。
-
-静下心来,把最近开始面试的公司排一下,把自己还要学习的内容做好计划,都列出来,按着一步一步去执行,心里会踏实的多。
-
-再说考研,考研也一点都不轻松,进大厂卷,**现在计算机考研比进大厂还卷(特别是名校计算机)**,如果考研没考上,找工作还挺难的,毕竟考研那套知识,对于找工作来说几乎没啥用。
-
-所以不论是找工作,还是考研,大家都是一样的焦虑,每条路都不好走,但自己一旦选择了,就坚持下来,走好自己的路。
-
-话再说回来,**现在不论身在什么阶段,都离不开“卷”,就算最后进了大厂工作,依然也是卷**。
-
-大家都感觉自己准备面试好辛苦,好心累。其实给你面试的面试官,可能自己手上的模块线上又出问题了,还要担心年底是不是要背锅了,是不是年终没了,晋升不了了,是不是要准备跳槽了,甚至应届生的工资比自己的还高 等等。
-
-**所以面试官也许比你还心累!**
-
-是不是想到这里,心里就舒服点了,哈哈哈哈,其实是有时候自己很迷茫但没有人沟通,就会陷入一个死循环,只要和过来人聊一聊,没啥大不了的。
-
-大家其实这这样。
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\350\213\261\350\257\255\345\210\260\345\272\225\351\207\215\344\270\215\351\207\215\350\246\201.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\350\213\261\350\257\255\345\210\260\345\272\225\351\207\215\344\270\215\351\207\215\350\246\201.md"
deleted file mode 100644
index 5ee7fc2dc0..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\350\213\261\350\257\255\345\210\260\345\272\225\351\207\215\344\270\215\351\207\215\350\246\201.md"
+++ /dev/null
@@ -1,58 +0,0 @@
-
-
-
-
-
-# 对程序员来说,英语到底重不重要
-
-在[知识星球](https://programmercarl.com/other/kstar.html)有一位录友问了我这么一个问题。
-
-
-
-这个问题我曾经在上学的时候也思考过。
-
-这次正好来好好说一说。
-
-当时我搞ACM的时候都是英文题目的,哪会有中文题目,现在力扣全面汉化也是最近几年的事情。
-
-如今又工作了这么多年后重新看待这个问题,又有更全面的看法了。
-
-其实我想说,**对英语对程序员即重要,也不重要!** 这是要看环境,看背景的。
-
-如果你现在在准备秋招,或者是跳槽,目标是冲刺国内大厂,那么暂时不用花费精力学英语,就算四级都没过,大厂面试官也不会问你过没过四六级的。
-
-貌似华为对英语四六级是有要求的,如果面试BAT,英语不是关键性问题。
-
-但工作之后,英语其实就很重要了,也要注意程序员英语和普通英语是不一样的。
-
-一手的技术资料,和优秀的问答 基本都是英文的,国内的资料都是别人嚼过的,质量参差不齐。
-
-而且国内的问答社区其实环境并不好(懂的人都懂),真正解决问题,还得靠Stack Overflow。
-
-**所以技术文档(英文),Stack Overflow , Quora才是程序员的利器**。
-
-工作以后如果你把程序员英语(注意不是普通英语)练好,其实对技能和视野的提升是很有帮助的。
-
-这里为什么强调是程序员英语呢, 因为有太多专业词是CS特有的,而不是日常英语。
-
-**继承,多态,变量,英文怎么说? 估计可以难住不少人了**。
-
-所以当我们查问题的时候,第一反应 一定是用 中文关键词去搜索,因为我们不知道对应的英文关键词(也懒的去查一下)。
-
-所以英语好,这是一种技术壁垒,可以任意遨游在中文和英文的世界里,有两极的思考!
-
-**那么对程序员来说,英语口语重要么?**
-
-如果你直接想去外企的话,练一练吧,也是挺重要的,如果在国内的话,用处不太大。
-
-那一定有人说了:练好口语 一定是有利的。
-
-这个我也 赞同,练啥其实都有有利的,但我们要看**投入产出比**
-
-我在学校的时候英语口语还是挺不错的,当时学校的留学生我基本都认识,和他们扯扯皮没问题,可工作这些年之后,全!都!还!回!去!了!
-
-所以如果练习口语,一定要有目标,要么就是雅思托付考试要出国,要么就一定要去外企找机会transfer美帝,这样有一个环境可以一直保持下去。
-
-否则,花费大量时间练习,其实仅仅是感动自己,过不了多久,就都还回去。(例如说我,哈哈哈哈)
-
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\350\246\201\344\270\215\350\246\201\350\200\203\347\240\224.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\350\246\201\344\270\215\350\246\201\350\200\203\347\240\224.md"
deleted file mode 100644
index 180e5d13cb..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\350\246\201\344\270\215\350\246\201\350\200\203\347\240\224.md"
+++ /dev/null
@@ -1,45 +0,0 @@
-
-
-
-
-
-# 到底要不要读研
-
-在[知识星球](https://programmercarl.com/other/kstar.html)里讨论了一下关于要不要读研的问题。
-
-
-
-其实不少同学纠结于计算机专业要不要考研。
-
-我的观点是 **如果认为自己本科毕业 可以拿到大厂的研发岗offer,那么就不用读研**(除非保研到一个特别好的学校了)。
-
-那么怎么能发现自己本科毕业能不能拿到大厂offer呢。
-
-看看自己学生学哥学姐,大部分人的就业去向,如果很多都可以进入大厂,那么就可以追寻他们的足迹。
-
-如果自己学校本科毕业,就业比较一般,那么就去读研吧。
-
-当然这里还有一个情况,就是算法岗,算法岗一般要求研究生学历以上。但算法岗现在很卷,985高校研究生,找算法岗的工作都很难,既要顶会,也要coding的能力。
-
-目前的现况是很多搞AI的985研究生都在转研发岗,**但如果你依然立志从事人工智能(说明是真的热爱),那么就去读研吧**。
-
-研究生毕业去做研发岗和本科毕业做的研发,其工作其实没区别。那么为什么读研呢。
-
-现在环境就比较卷,两个候选人,实力差不多,一个本科,一个研究生,价钱也差不多,那么大厂就要个研究生呗,在招生简章里看着也好看,说明公司都是高学历人才。
-
-当然一般来说 研究生毕竟又多读了 两三年,普遍会比本科强一些。
-
-**PS:大厂研发岗校招本科和研究生薪资几乎没啥差别**。
-
-
-那么话在说回来,**如果打算考研,那么就争取更好学校的研究生,提升一下学历,把考研所付出的努力最大化**。
-
-最后关于本科生要不要读研:
-
-* 本科如果有实力去大厂做研发,那么就去!
-
-* 如果本科去不了大厂,可以考虑考研,考研是一次提升学历的机会,一旦选择考研这条路,就给自己点压力!
-
-* 如果知道AI岗位目前就业情况,依然要立志从事AI,那么就去读研
-
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\200\274\347\255\276.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\200\274\347\255\276.md"
deleted file mode 100644
index f75e364218..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\200\274\347\255\276.md"
+++ /dev/null
@@ -1,51 +0,0 @@
-
-# 遭遇逼签,怎么办?
-
-最近各大公司陆续开奖了,也有不少公司开始逼签了。
-
-为什么会有逼签呢,无疑就是 这家公司你不太想去,但还想要一个保底(备胎),同时这家公司也知道自己竞争力可能不是很大,也不想当大家的备胎,所以就要三天内必须签三方。
-
-如果是心仪的公司要求三天内签三方,我相信大家就没有被逼签的感觉了,哈哈哈
-
-[知识星球](https://programmercarl.com/other/kstar.html)里很多录友都问我,XX公司又要逼签了,怎么办。 我在公众号也分享一下,希望对大家有所帮助。
-
-
-
-以下是我的回答:
-
-TPLINK 的嵌入式,确实有点偏,但钱给的还真多 (估计是发现不好找人了)。
-
-从发展来看,当然百度 虾皮 是首选,百度一般薪资不高,但我估计 虾皮薪资应该可以。
-
-虾皮如果 10月底 才下offer的那,一般有更好的offer的同学 指定早就签了,我估计虾皮是能接受 毁约三方 也就是等你毁约三方,再签虾皮的 (注意,这是我猜的),要不 10月底 在下offer,优秀的毕业生遭抢没了。 所以如果能联系上HR的话,可以问一问,是否一定要三方。
-
-你去签 TPLINK 三方的时候 也问问,毁约的流程,TPLINK 什么时候会退给你三方,如果是 TPLINK HR很坚决 不接受毁约的话,就等百度 和虾皮吧,到时候谈薪资的时候 拿tplink这个作为依据,高点要薪资。
-
-如果 TPLINK HR 能接受毁约,给出 毁约 流程的话,那就签了。 (但这里不能排除 HR 口是心非,说是能毁约,最后不给你退三方的情况,所以多问问 有没有师兄毁约 TPLINK成功的)
-
-我当初校招的时候签的就是华为,也给三方了,毁约去的腾讯。我腾讯都入职了,华为才把三方退回来,我在补上三方,所以腾讯是不要求三方的,只要人去了就行。这个也看公司的。
-
--------------
-
-
-
-以下是我的回答:
-
-7月开始自学c++,秋招就能拿到这些offer已经很棒了👍
-
-先看逼签的这两家,其实我都不太了解,我的朋友们主要都在互联网圈,所以 没有认识的朋友在这两家公司。
-
-不过 thoughtworks 网评不太好,说是外包公司, 中孚信息 至少是土生土长的企业,做网络安全,也是上市公司,至少会靠谱一些。
-
-华为成都 光产品线的测开,如果你想真的有增进技术的心,测开就稍差了一些。
-
-相对来说 小米南京的offer 还不错,但还在池子里。
-
-如果逼签的话,优先考虑中孚吧。
-
-关于 好好沉淀准备春招 当然是可以的,但不是每个同学都是劲再去冲春招了,找工作都是一鼓作气,如果你还能高强度的准备面试冲刺春招,当然是好的。
-
-关键是 还有外部因素,毕业的事,以及大家该保研的保研了,找到工作了都吃喝玩乐了,天天聚餐,这个氛围下,你还能不能继续高强度冲刺了, 这才是你要考虑的。
-
-加油💪
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\235\236\347\247\221\347\217\2552021\347\247\213\346\213\233\346\200\273\347\273\223.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\235\236\347\247\221\347\217\2552021\347\247\213\346\213\233\346\200\273\347\273\223.md"
deleted file mode 100644
index b92420addb..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\235\236\347\247\221\347\217\2552021\347\247\213\346\213\233\346\200\273\347\273\223.md"
+++ /dev/null
@@ -1,117 +0,0 @@
-
-
-
-
-
-
-# 非科班,收获满满!
-
-九月份悄然已过,秋招已经快接近尾声了,星球里已经纷纷有录友开始写这次的秋招总结。
-
-其中一位录友写的很好,所以想分享出来 给公众号上的录友也看一看,相信对大家有所启发,特别是明年要找工作的录友,值得好好看一看。
-
-这篇总结首发在代码随想录[知识星球](https://programmercarl.com/other/kstar.html)上,立刻就获得了60个赞,很多评论,我这里放出一个截图:
-
-
-
-以下是星球上该录友的总结:
-
---------------------
-
-211本硕非科班菜鸡转码不到一年,从最开始的迷茫到后面认识到差距的焦虑再到后面逐渐恢复坦然的心态,一路起起伏伏,但也算连滚带爬的趟了过来,**感激一路上耐心为我答疑解惑的各路大佬,尤其是咱们卡哥哈哈**。
-
-秋招收获如下(按收到意向顺序):
-
-* 诺瓦星云 西安 软件开发工程师 提前批意向书
-* 字节跳动 抖音客户端开发工程师 提前批意向书
-* 滴滴 网约车部门后端开发工程师C++/Go 提前批意向书
-* 腾讯 IEG光子工作室技术中心测试开发 正式批意向书
-* 美团 基础研发平台后端开发工程师JAVA 正式批意向书
-* 阿里 阿里云计算平台后端开发工程师C++ 正式批意向书
-
-被终结的岗位:
-* 蔚来 提前批后端三面挂
-* 百度/京东 后端提前批无音讯,正式批笔试我就都拒了
-* 网易 正式批一面过,但感觉不喜欢,二面就放弃了
-* 虾皮 提前批二面挂(北京hc太少,卷不动,秋招甚至直接不放岗)
-* 蓝湖 提前批二面挂(面完字节三面之后半个小时紧接着蓝湖二面,字节面了两个小时,人都麻了,强烈建议大家面试不要排太挤!!)
-* 招银网络 正式批一面过,二面放弃
-* 商汤、oppo、知乎 提前批简历挂,后面懒得投了
-
-# 秋招历程
-
-## 开始准备
-
-最开始的准备是从**去年十月份左右开始**,那个时候刚开始意识到自己将来找工作的问题,就选择了一门自己相对来说有一些基础的C++开始入手学习,看到网上的很多经验都说语言不是最大的问题,能学好一门后面转型都很快,所以并没有在这个问题上有什么纠结,大家看我拿到的岗位也可以发现事实确实如此。
-
-**从十月份开始看C++ Primer这本书**,断断续续花了三个多月,确实拉胯。
-
-当时主要是事情太多,而且我在熟悉完主要的语法和特性之后,大概一个月的时间吧,就开始上手刷leetcode了,入门真的很痛苦,递归啥的看一遍忘一遍,一天吭哧吭哧弄下来也就两三道题,还基本都不是自己写出来的,因为我光理解别人的方法就需要很长时间。
-
-**后面刷题解刷到了卡哥,着实幸运,跟着卡哥的题解我也逐渐有了自己的方法模式和比较规范的代码风格**,所以到后面尽管有时候会发现也会有比卡哥更好的方法,我还是愿意先上手走一遍卡哥的思路,之后再补充别的。
-
-**不得不说,题解能跟住一个人确实很有必要**,有的题卡哥没有出题解,我也是先找到之前卡哥类似的题目找思路,再自己写出来。整个刷题的流程持续到过年,我就开始刷剑指offer了,基本简单题都可以撕,中等困难的还是只能看题解。
-
-我个人的经验是不愿意在思考题解上浪费太多时间,**如果打眼一看就知道自己不太可能做出来,我会选择直接看题解**,省下的时间哪怕自己多敲两遍也比对着屏幕发呆效率高,毕竟面试考察的不是你创造算法的能力,会用就行了,当然这只是个人见解,大佬息怒。
-
-刷完剑指offer大概花了半个月的时间,**时间也来到了三月份**,周围的不少人已经开始找春招实习,但更多的人还是和我一样,由于各种原因没法去找实习。
-
-这个时候各种情绪都会有,焦虑,迷茫,没办法,改变不了现实情况只能尽可能的提升自己。
-
-这个时候我的算法已经比较熟练了,基本一天不用太多时间也能过四道题,尽管其中仍有不少是对着题解敲的,但是对别人思路的理解确实已经练得轻车熟路了,我觉得这也是一种进步吧。
-
-**四月份开始准备项目**,没错,就是烂大街的webserver,大家都知道这个项目烂大街,但是经过后面的各种面试我也发现,不管你是什么项目,对于面试官来说都很基础。
-
-哪怕是大厂实习的人又有几个能接触到核心,当然这也是对大部分人说的,实习大佬勿喷。**所以面试考察的就是你对基础的掌握**,就算你讲了项目各种高大上的方法,虽然可能有加分,但也是在给自己埋坑。
-
-比如面试官可能问你有没有看过这个技术具体实现源码等等。
-
-**把自己捧的越高,一旦被发现基础有漏洞,摔得也越惨**,面试官对每个人都会有一个心理预期,比如我可能就因为非科班占了一些心理预期比较低的便宜,也就是不容易让对方失望,客户端我0基础,测开0基础,但是都过了,当然像我这样的坏处就是offer大概率只是白菜,所以综合来看有利有弊,大家自己权衡。
-
-
-**搞这个webserver连带各种计网,操作系统的学习花了两个月的时间**。之后便开始了面向面经自习的流程。
-
-这个时候算法题基本不再做新的了,力扣累计刷题已经接近400,我知道里面不少题就算再拿给我,我也不一定会做,所以做新题有什么意义呢,能把之前的高频题,热题刷好就已经超过很多人了。
-
-因为之前做题都是自己按标签做,后面看到卡哥出了系列题解,基本都是我之前做过的,所以**花了一周左右就把卡哥的pdf全过了一遍,帮助很大**。
-
-## 提前批
-
-**后面六七月份的提前批我参与的不是特别积极**,和大多数人一样,我也总想着自己还没准备好,但是总得跨出第一步,所以就先找了一些小厂练手。
-
-不过牛客上投的小厂基本都没有音讯,真正想投递还是官网最靠谱。
-
-诺瓦是我的第一份offer,所以尽管我大概率去不了,我依然心怀感激,**因为经历过的人都知道第一份offer对于一个迷茫的秋招生来说是多么的宝贵**。
-
-后面拿到字节和滴滴我着实没有想到,因为感觉自己的表现还有待提升,但能过肯定是开心的。
-
-**提前批没有笔试,所以七月份的提前批窗口确实非常宝贵**。
-
-## 正式秋招
-
-八月份之后就比较正常了,基本就是笔试,面试等等,**可以分享的一点是腾讯、阿里这种大厂不是特别看重笔试**,因为很多人在笔试之前就走完面试流程了,但是对于有一些劣势的同学还是希望能认真对待笔试,争取让自己多一个闪光点。
-
-后期的算法题主要都集中在高频题、热题上面,所以还是应该把剑指刷好,配合别人分享的面经来巩固,**卡哥的算法pdf可以多看多复习**,就算没有考到也会让自己安心一些。
-
-同时对于基础知识的整理更多的应该从面经中获取,我个人秋招下来总共看了得有至少500份面经,其实别人的面经和自己面试没啥区别。
-
-同时也因为看了太多的面经,我自己的面经就不需要太多的整理了,除了一些没答上来的关键问题,因为基本都被之前的涵盖了,所以与其海投参加一些不靠谱的面试,倒不如整理十份面经来得实在。
-
-参加面试主要是锻炼临场的心态,但是大厂和小厂的面试确实是有区别的,所以大厂的面经一定要多看多整理。
-
-
-## 感想
-
-一口气写下来就已经2000字了,最后还是多说两句,分享一点感想吧。
-
-**除了超级大佬,基本每一个秋招生都会有过迷茫,焦虑的心路历程**。
-
-一方面我们需要认识到自己的局限性,认清现实。
-
-另一方面也应该看到自己能改变的现实,许多人的时间真的只是在盲目的焦虑中虚耗,这是一个死循环,浪费的时间越多后面只会越焦虑,唯一的方法就是打起精神行动起来。当然也不用把自己逼的太紧,找到合适的排解方式,比如打会儿球打会儿游戏,让自己运行在一个正确的,符合自己节奏的轨道上不断前进。
-
-**面试是玄学,有些人的就是难,有些人的就是简单**,没有人能保证你每次都简单,但也不会每次都难,我们努力提升实力只是为了能创造更多机会,并在机会出现的时候把握住。
-
-我觉得一个人最大的幸运就是付出的努力能有收获,所以我希望大家都能幸运地结束秋招,也希望我能继续不忘初心,保持谦逊。秋招只是人生的一段小插曲,未来的路还有很长很长,写下这篇流水账结束我的秋招,也希望能与诸君共勉,加油!
-
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\235\236\347\247\221\347\217\255\347\232\204\345\233\260\346\211\260.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\235\236\347\247\221\347\217\255\347\232\204\345\233\260\346\211\260.md"
deleted file mode 100644
index 470b233b59..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\235\236\347\247\221\347\217\255\347\232\204\345\233\260\346\211\260.md"
+++ /dev/null
@@ -1,54 +0,0 @@
-
-
-
-
-
-# 非科班的困扰!
-
-在[知识星球](https://programmercarl.com/other/kstar.html) 里很多录友都是非科班转码的,也是要准备求职,或者准备明年秋招,非科班的录友其实对 准备找工作所需要的知识不太清楚,对其难度也不太清楚,所有总感觉准备起来心里没有底。
-
-例如星球里有这位录友的提问:
-
-
-
-我在知识星球里,给出了详细的C++后端学习路线,包括计算机基础的学习路线,
-
-
-
-
-当然路线很长,这里我就截图了一部分。
-
-但即使这样,其实非科班的录友也不容易量化具体的工作量究竟多大。
-
-以下是我对这位录友的回答:
-
-----------------
-
-具体学习时间 一般是不好量化的。
-
-一本书,有的同学 一周就看完了,有的同学 两个月看不明白,**基础程度,学习速度,每天的投入时间,以及效率**都是不一样的。
-
-**所以要靠自己 规划时间,自己是最了解自己的**。
-
-C++primer 不要耗太久,差不多就去看 effective C++。
-
-还有计算机求职需要准备的知识不是线性排列的。
-
-例如很多非科班的录友可能会这样安排: 几个月搞定编程语言,几个月搞定算法,几个月搞定操作系统!
-
-**如果这么学的话,那就凉凉了**。
-
-求职准备主要围绕这么几块:编程语言 + 算法 + 计算机基础(操作系统,数据库,网络,设计模式)+ 项目 。
-
-**这几块都可以一起搞的,一天都要分配点时间,知识错开点来学,效率更高**。
-
-每一块知识,每天都要投入一点,例如,例如一天算法几个小时,编程语言几个小时,计算机基础几个小时,项目几个小时,这样分配,当你发现 操作系统比你想象中的难,你就 下周及时调整计划,操作系统每天多分配时间。
-
-**正常的学习节奏是这样根据自己的掌握程度动态调整的**。
-
-在整体进度上,可以先保证都过一遍,第一遍看的时候,不要死扣细节,会非常耽误时间,先过一遍有整体性的把控 更重要。
-
-之后在针对面经或者八股文,去一个难点一个难点的攻克。
-
-希望对非科班的录友们有所启发,加油💪
-
diff --git "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\235\242\350\257\225\344\270\255\345\217\221\346\225\243\346\200\247\351\227\256\351\242\230.md" "b/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\235\242\350\257\225\344\270\255\345\217\221\346\225\243\346\200\247\351\227\256\351\242\230.md"
deleted file mode 100644
index 5cade944c1..0000000000
--- "a/problems/\347\237\245\350\257\206\346\230\237\347\220\203\347\262\276\351\200\211/\351\235\242\350\257\225\344\270\255\345\217\221\346\225\243\346\200\247\351\227\256\351\242\230.md"
+++ /dev/null
@@ -1,45 +0,0 @@
-
-
-
-
-
-# 面试中遇到发散性问题,应该怎么办?
-
-这周在[知识星球](https://programmercarl.com/other/kstar.html)有一位录友问了我这么一个问题,我感觉挺有代表性的,应该不少录友在面试中不论是社招还是校招都会遇到这一类的问题。
-
-问题如下:
-
-
-
-首先面试官评价:基本的知识都懂还可以,但是知识碎片化。
-
-因为现在基本大家都是背面经,所以对考点知识点掌握的都不错,确实对整体上理解不够。
-
-但如果想对整体上理解深入,需要花费很大精力的,而且要把书看的很透彻,那这种候选人基本都是拔尖的。
-
-关于操作系统啊,数据库等等,大多数录友应该都是靠面经,其实背面经也是最高效,性价比最高的方式的,如果真的去把书看透,一本《深入理解计算机系统》够看一年了。。。。
-
-所以面试官基本不会因为这个问题而把你pass掉,那位提问的录友也是顺利进入了三面。
-
-那么面试中遇到这种发散性问题应该怎么办呢?
-
-其实他问的这种问题,**就没指望你能说出 正确的答案,这是没有标准答案的**,例如淘宝京东的那种登录的场景 没有经历过 是不知道究竟怎么回事的。
-
-而问你对操作系统的理解,也是千人千面没有标准的。
-
-遇到这种问题,你就结合自己的想法,大胆说,不要说这个我不知道,那个我没遇到过之类的。
-
-你说的解决方案,一定是有问题的,面试官在质疑你的时候,**你要表现出很兴奋,积极和面试官讨论:为什么不对**,然后说出你的观点,结合你所知道的理论知识。
-
-大胆说,不要怕,一般情况你的理论知识都比面试官强,面试官工作好多年了基础知识早忘了,基本都是面试你前一天突击一个小时(暴漏真相了哈哈哈)
-
-**忌讳:面试官一质疑你,你就开始怀疑自己,心想那我说的不对吧,然后就不说话了。。。**
-
-最后这种发散性的问题,也没法去专门准备,因为这种问题主要是**考察候选人对技术的态度和沟通能力!**
-
-所以大家如果在面试遇到这一类发散性问题,一定要积极沟通,**表现出你对技术的追求和优秀的沟通能力**。
-
-**注意 和面试官讨论的时候要面带微笑,不要板着脸,面试官也会喜欢以后和这样的人做同事!**
-
-好咯,心法已经传授给大家了。
-
From c7e34a3f9b82d989b408b4303aee17bd192fb91f Mon Sep 17 00:00:00 2001
From: JackZJ <56966563+laerpeeK@users.noreply.github.com>
Date: Thu, 28 Apr 2022 08:28:12 +0800
Subject: [PATCH 108/551] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?=
=?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85JavaScript?=
=?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
更新JavaScript版本二维数组做法,目前文档上的是运行不了的。
---
...47\241\20001\350\203\214\345\214\205-1.md" | 53 +++++++++++--------
1 file changed, 31 insertions(+), 22 deletions(-)
diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
index fe940b4ca4..a844dcf53c 100644
--- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
+++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
@@ -380,31 +380,40 @@ func main() {
### javascript
```js
-function testweightbagproblem (wight, value, size) {
- const len = wight.length,
- dp = array.from({length: len + 1}).map(
- () => array(size + 1).fill(0)
- );
-
- for(let i = 1; i <= len; i++) {
- for(let j = 0; j <= size; j++) {
- if(wight[i - 1] <= j) {
- dp[i][j] = math.max(
- dp[i - 1][j],
- value[i - 1] + dp[i - 1][j - wight[i - 1]]
- )
- } else {
- dp[i][j] = dp[i - 1][j];
- }
- }
- }
-
-// console.table(dp);
+/**
+ *
+ * @param {Number []} weight
+ * @param {Number []} value
+ * @param {Number} size
+ * @returns
+ */
+
+function testWeightBagProblem(weight, value, size) {
+const len = weight.length,
+dp = Array.from({length: len}).map(
+() => Array(size + 1)) //JavaScript 数组是引用类型
+for(let i = 0; i < len; i++) { //初始化最左一列,即背包容量为0时的情况
+dp[i][0] = 0;
+}
+for(let j = 1; j < size+1; j++) { //初始化第0行, 只有一件物品的情况
+if(weight[0] <= j) {
+dp[0][j] = value[0];
+} else {
+dp[0][j] = 0;
+}
+}
+
+for(let i = 1; i < len; i++) { //dp[i][j]由其左上方元素推导得出
+for(let j = 1; j < size+1; j++) {
+if(j < weight[i]) dp[i][j] = dp[i - 1][j];
+else dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j - weight[i]] + value[i]);
+}
+}
- return dp[len][size];
+return dp[len-1][size] //满足条件的最大值
}
-function testWeightBagProblem2 (wight, value, size) {
+function testWeightBagProblem2 (wight, value, size)4 {
const len = wight.length,
dp = Array(size + 1).fill(0);
for(let i = 1; i <= len; i++) {
From 0694fac15bf6b7d18a53adc9f6ad0c33b51db016 Mon Sep 17 00:00:00 2001
From: JackZJ <56966563+laerpeeK@users.noreply.github.com>
Date: Thu, 28 Apr 2022 08:30:30 +0800
Subject: [PATCH 109/551] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?=
=?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85JavaScript?=
=?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
更新背包理论基础01背包JavaScript代码块 - 二维数组做法1, 目前主线上的是有问题的。
---
...\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
index a844dcf53c..d6bc552064 100644
--- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
+++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
@@ -413,7 +413,7 @@ else dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j - weight[i]] + value[i]);
return dp[len-1][size] //满足条件的最大值
}
-function testWeightBagProblem2 (wight, value, size)4 {
+function testWeightBagProblem2 (wight, value, size) {
const len = wight.length,
dp = Array(size + 1).fill(0);
for(let i = 1; i <= len; i++) {
From 5fd43cfd0a5770e0cb532687766f64306b5bbfed Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=91=A3xx?=
Date: Thu, 28 Apr 2022 22:20:22 +0800
Subject: [PATCH 110/551] =?UTF-8?q?0300.=E6=9C=80=E9=95=BF=E4=B8=8A?=
=?UTF-8?q?=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97-go=E5=8A=A8=E6=80=81?=
=?UTF-8?q?=E8=A7=84=E5=88=92=E6=B1=82=E8=A7=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...07\345\255\220\345\272\217\345\210\227.md" | 33 +++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md"
index dfdd51257c..f68edb5ae5 100644
--- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md"
@@ -168,6 +168,39 @@ func lengthOfLIS(nums []int ) int {
}
```
+```go
+// 动态规划求解
+func lengthOfLIS(nums []int) int {
+ // dp数组的定义 dp[i]表示取第i个元素的时候,表示子序列的长度,其中包括 nums[i] 这个元素
+ dp := make([]int, len(nums))
+
+ // 初始化,所有的元素都应该初始化为1
+ for i := range dp {
+ dp[i] = 1
+ }
+
+ ans := dp[0]
+ for i := 1; i < len(nums); i++ {
+ for j := 0; j < i; j++ {
+ if nums[i] > nums[j] {
+ dp[i] = max(dp[i], dp[j] + 1)
+ }
+ }
+ if dp[i] > ans {
+ ans = dp[i]
+ }
+ }
+ return ans
+}
+
+func max(x, y int) int {
+ if x > y {
+ return x
+ }
+ return y
+}
+```
+
Javascript
```javascript
const lengthOfLIS = (nums) => {
From 6f85c2ad3a6daf8e0098b08e61b7fdd68d78c757 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=91=A3xx?=
Date: Fri, 29 Apr 2022 00:50:22 +0800
Subject: [PATCH 111/551] =?UTF-8?q?0674.=E6=9C=80=E9=95=BF=E8=BF=9E?=
=?UTF-8?q?=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97-go=E5=8A=A8?=
=?UTF-8?q?=E6=80=81=E8=A7=84=E5=88=92=E6=B1=82=E8=A7=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...22\345\242\236\345\272\217\345\210\227.md" | 39 +++++++++++++++++++
1 file changed, 39 insertions(+)
diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
index e941d24263..56e95d97d8 100644
--- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
+++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
@@ -236,6 +236,45 @@ class Solution:
```
Go:
+> 动态规划:
+```go
+func findLengthOfLCIS(nums []int) int {
+ if len(nums) == 0 {return 0}
+ res, count := 1, 1
+ for i := 0; i < len(nums)-1; i++ {
+ if nums[i+1] > nums[i] {
+ count++
+ }else {
+ count = 1
+ }
+ if count > res {
+ res = count
+ }
+ }
+ return res
+}
+```
+
+> 贪心算法:
+```go
+func findLengthOfLCIS(nums []int) int {
+ if len(nums) == 0 {return 0}
+ dp := make([]int, len(nums))
+ for i := 0; i < len(dp); i++ {
+ dp[i] = 1
+ }
+ res := 1
+ for i := 0; i < len(nums)-1; i++ {
+ if nums[i+1] > nums[i] {
+ dp[i+1] = dp[i] + 1
+ }
+ if dp[i+1] > res {
+ res = dp[i+1]
+ }
+ }
+ return res
+}
+```
Javascript:
From 31eb619ea18d05e004da853d70b165ba47291c92 Mon Sep 17 00:00:00 2001
From: Anmizi <1845513904@qq.com>
Date: Fri, 29 Apr 2022 19:15:41 +0800
Subject: [PATCH 112/551] =?UTF-8?q?=E4=BC=98=E5=8C=96JS=E7=89=88=E6=9C=AC?=
=?UTF-8?q?=E4=BB=A3=E7=A0=81=20(0039.=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8C?=
=?UTF-8?q?.md)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
index 98b37b8467..e10a827f04 100644
--- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
+++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
@@ -370,18 +370,17 @@ func backtracking(startIndex,sum,target int,candidates,trcak []int,res *[][]int)
```js
var combinationSum = function(candidates, target) {
const res = [], path = [];
- candidates.sort(); // 排序
+ candidates.sort((a,b)=>a-b); // 排序
backtracking(0, 0);
return res;
function backtracking(j, sum) {
- if (sum > target) return;
if (sum === target) {
res.push(Array.from(path));
return;
}
for(let i = j; i < candidates.length; i++ ) {
const n = candidates[i];
- if(n > target - sum) continue;
+ if(n > target - sum) break;
path.push(n);
sum += n;
backtracking(i, sum);
From dac0b4a12e43670eb54eaa6da5fa3f07cb707424 Mon Sep 17 00:00:00 2001
From: GitHubQAQ <31883473+GitHubQAQ@users.noreply.github.com>
Date: Fri, 29 Apr 2022 21:03:27 +0800
Subject: [PATCH 113/551] =?UTF-8?q?Update=200106.=E4=BB=8E=E4=B8=AD?=
=?UTF-8?q?=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F?=
=?UTF-8?q?=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
优化代码高亮
---
...\204\351\200\240\344\272\214\345\217\211\346\240\221.md" | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md"
index 496de431ed..4396bc7666 100644
--- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md"
@@ -103,7 +103,7 @@ TreeNode* traversal (vector& inorder, vector& postorder) {
中序数组相对比较好切,找到切割点(后序数组的最后一个元素)在中序数组的位置,然后切割,如下代码中我坚持左闭右开的原则:
-```C++
+```CPP
// 找到中序遍历的切割点
int delimiterIndex;
for (delimiterIndex = 0; delimiterIndex < inorder.size(); delimiterIndex++) {
@@ -130,7 +130,7 @@ vector rightInorder(inorder.begin() + delimiterIndex + 1, inorder.end() );
代码如下:
-```
+```CPP
// postorder 舍弃末尾元素,因为这个元素就是中间节点,已经用过了
postorder.resize(postorder.size() - 1);
@@ -144,7 +144,7 @@ vector rightPostorder(postorder.begin() + leftInorder.size(), postorder.end
接下来可以递归了,代码如下:
-```
+```CPP
root->left = traversal(leftInorder, leftPostorder);
root->right = traversal(rightInorder, rightPostorder);
```
From 5b3607c6a151b6fd6dd548c1b26f3f1fb8514047 Mon Sep 17 00:00:00 2001
From: Anmizi <1845513904@qq.com>
Date: Fri, 29 Apr 2022 23:18:33 +0800
Subject: [PATCH 114/551] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200040.=E7=BB=84?=
=?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CII.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\204\345\220\210\346\200\273\345\222\214II.md" | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md"
index de13e031dd..34ac64e68d 100644
--- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md"
+++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md"
@@ -508,22 +508,27 @@ func backtracking(startIndex,sum,target int,candidates,trcak []int,res *[][]int)
*/
var combinationSum2 = function(candidates, target) {
const res = []; path = [], len = candidates.length;
- candidates.sort();
+ candidates.sort((a,b)=>a-b);
backtracking(0, 0);
return res;
function backtracking(sum, i) {
- if (sum > target) return;
if (sum === target) {
res.push(Array.from(path));
return;
}
- let f = -1;
for(let j = i; j < len; j++) {
const n = candidates[j];
- if(n > target - sum || n === f) continue;
+ if(j > i && candidates[j] === candidates[j-1]){
+ //若当前元素和前一个元素相等
+ //则本次循环结束,防止出现重复组合
+ continue;
+ }
+ //如果当前元素值大于目标值-总和的值
+ //由于数组已排序,那么该元素之后的元素必定不满足条件
+ //直接终止当前层的递归
+ if(n > target - sum) break;
path.push(n);
sum += n;
- f = n;
backtracking(sum, j + 1);
path.pop();
sum -= n;
From 55c78be1280054615f23c9e5859d139a7f840705 Mon Sep 17 00:00:00 2001
From: dmzlingyin
Date: Fri, 29 Apr 2022 23:38:34 +0800
Subject: [PATCH 115/551] =?UTF-8?q?update=20(0739.=E6=AF=8F=E6=97=A5?=
=?UTF-8?q?=E6=B8=A9=E5=BA=A6.md):=20python=E4=BB=A3=E7=A0=81=E9=AB=98?=
=?UTF-8?q?=E4=BA=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
index 710f5eb68f..7deab0a3c2 100644
--- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
+++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
@@ -233,7 +233,7 @@ class Solution {
}
```
Python:
-``` Python3
+```python
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
answer = [0]*len(temperatures)
From d0a79760b65a1078284b036be6dcf8fa8eec5076 Mon Sep 17 00:00:00 2001
From: dmzlingyin
Date: Fri, 29 Apr 2022 23:41:26 +0800
Subject: [PATCH 116/551] =?UTF-8?q?update=20(0739.=E6=AF=8F=E6=97=A5?=
=?UTF-8?q?=E6=B8=A9=E5=BA=A6.md):=20=E8=AF=AD=E8=A8=80=E8=A1=A8=E8=BF=B0?=
=?UTF-8?q?=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
index 7deab0a3c2..f0f782d235 100644
--- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
+++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
@@ -34,7 +34,7 @@
那么单调栈的原理是什么呢?为什么时间复杂度是O(n)就可以找到每一个元素的右边第一个比它大的元素位置呢?
-单调栈的本质是空间换时间,因为在遍历的过程中需要用一个栈来记录右边第一个比当前元素的元素,优点是只需要遍历一次。
+单调栈的本质是空间换时间,因为在遍历的过程中需要用一个栈来记录右边第一个比当前元素大的元素,优点是只需要遍历一次。
在使用单调栈的时候首先要明确如下几点:
From 636550d44c4ac65521865cca00b1c3c8b9f7c756 Mon Sep 17 00:00:00 2001
From: dmzlingyin
Date: Fri, 29 Apr 2022 23:50:31 +0800
Subject: [PATCH 117/551] =?UTF-8?q?update=20(0739.=E6=AF=8F=E6=97=A5?=
=?UTF-8?q?=E6=B8=A9=E5=BA=A6.md):=20=E5=A2=9E=E5=8A=A0=E6=9C=AA=E7=B2=BE?=
=?UTF-8?q?=E7=AE=80=E7=89=88=E6=9C=ACGo=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...17\346\227\245\346\270\251\345\272\246.md" | 30 ++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)
diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
index f0f782d235..206bebd234 100644
--- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
+++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
@@ -277,8 +277,36 @@ func dailyTemperatures(t []int) []int {
}
```
-> 单调栈法
+> 单调栈法(未精简版本)
+```go
+func dailyTemperatures(temperatures []int) []int {
+ res := make([]int, len(temperatures))
+ // 初始化栈顶元素为第一个下标索引0
+ stack := []int{0}
+
+ for i := 1; i < len(temperatures); i++ {
+ top := stack[len(stack)-1]
+ if temperatures[i] < temperatures[top] {
+ stack = append(stack, i)
+ } else if temperatures[i] == temperatures[top] {
+ stack = append(stack, i)
+ } else {
+ for len(stack) != 0 && temperatures[i] > temperatures[top] {
+ res[top] = i - top
+ stack = stack[:len(stack)-1]
+ if len(stack) != 0 {
+ top = stack[len(stack)-1]
+ }
+ }
+ stack = append(stack, i)
+ }
+ }
+ return res
+}
+```
+
+> 单调栈法(精简版本)
```go
// 单调递减栈
func dailyTemperatures(num []int) []int {
From 49ec574821f687ea8703c61245f815bf2dc76139 Mon Sep 17 00:00:00 2001
From: dmzlingyin
Date: Sat, 30 Apr 2022 11:04:17 +0800
Subject: [PATCH 118/551] =?UTF-8?q?update=20(0496.=E4=B8=8B=E4=B8=80?=
=?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0I):=20=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0=E6=9C=AA=E7=B2=BE=E7=AE=80Go=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4\345\244\247\345\205\203\347\264\240I.md" | 33 +++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md"
index f9dfa3081b..02339677bc 100644
--- "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md"
+++ "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md"
@@ -244,6 +244,39 @@ class Solution:
```
Go:
+
+> 未精简版本
+```go
+func nextGreaterElement(nums1 []int, nums2 []int) []int {
+ res := make([]int, len(nums1))
+ for i := range res { res[i] = -1 }
+ m := make(map[int]int, len(nums1))
+ for k, v := range nums1 { m[v] = k }
+
+ stack := []int{0}
+ for i := 1; i < len(nums2); i++ {
+ top := stack[len(stack)-1]
+ if nums2[i] < nums2[top] {
+ stack = append(stack, i)
+ } else if nums2[i] == nums2[top] {
+ stack = append(stack, i)
+ } else {
+ for len(stack) != 0 && nums2[i] > nums2[top] {
+ if v, ok := m[nums2[top]]; ok {
+ res[v] = nums2[i]
+ }
+ stack = stack[:len(stack)-1]
+ if len(stack) != 0 {
+ top = stack[len(stack)-1]
+ }
+ }
+ stack = append(stack, i)
+ }
+ }
+ return res
+}
+```
+> 精简版本
```go
func nextGreaterElement(nums1 []int, nums2 []int) []int {
res := make([]int, len(nums1))
From 82e8b9eaac23041c2a358af126ec4e268ee383fa Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 30 Apr 2022 21:18:55 +0800
Subject: [PATCH 119/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880509.?=
=?UTF-8?q?=E6=96=90=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...42\351\202\243\345\245\221\346\225\260.md" | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
index d339940cf0..1d17784d43 100644
--- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
+++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
@@ -245,7 +245,29 @@ var fib = function(n) {
};
```
+TypeScript
+
+```typescript
+function fib(n: number): number {
+ /**
+ dp[i]: 第i个斐波那契数
+ dp[0]: 0;
+ dp[1]:1;
+ ...
+ dp[i] = dp[i - 1] + dp[i - 2];
+ */
+ const dp: number[] = [];
+ dp[0] = 0;
+ dp[1] = 1;
+ for (let i = 2; i <= n; i++) {
+ dp[i] = dp[i - 1] + dp[i - 2];
+ }
+ return dp[n];
+};
+```
+
### C
+
动态规划:
```c
int fib(int n){
From 0cba2d22f2d032bbf478662ef622d8022af24f21 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 30 Apr 2022 21:53:14 +0800
Subject: [PATCH 120/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880070.?=
=?UTF-8?q?=E7=88=AC=E6=A5=BC=E6=A2=AF.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...0.\347\210\254\346\245\274\346\242\257.md" | 51 +++++++++++++++++++
1 file changed, 51 insertions(+)
diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md"
index da19ea0ec1..34d414413a 100644
--- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md"
+++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md"
@@ -308,7 +308,58 @@ var climbStairs = function(n) {
};
```
+TypeScript
+
+> 爬2阶
+
+```typescript
+function climbStairs(n: number): number {
+ /**
+ dp[i]: i阶楼梯的方法种数
+ dp[1]: 1;
+ dp[2]: 2;
+ ...
+ dp[i]: dp[i - 1] + dp[i - 2];
+ */
+ const dp: number[] = [];
+ dp[1] = 1;
+ dp[2] = 2;
+ for (let i = 3; i <= n; i++) {
+ dp[i] = dp[i - 1] + dp[i - 2];
+ }
+ return dp[n];
+};
+```
+
+> 爬m阶
+
+```typescript
+function climbStairs(n: number): number {
+ /**
+ 一次可以爬m阶
+ dp[i]: i阶楼梯的方法种数
+ dp[1]: 1;
+ dp[2]: 2;
+ dp[3]: dp[2] + dp[1];
+ ...
+ dp[i]: dp[i - 1] + dp[i - 2] + ... + dp[max(i - m, 1)]; 从i-1加到max(i-m, 1)
+ */
+ const m: number = 2; // 本题m为2
+ const dp: number[] = new Array(n + 1).fill(0);
+ dp[1] = 1;
+ dp[2] = 2;
+ for (let i = 3; i <= n; i++) {
+ const end: number = Math.max(i - m, 1);
+ for (let j = i - 1; j >= end; j--) {
+ dp[i] += dp[j];
+ }
+ }
+ return dp[n];
+};
+```
+
### C
+
```c
int climbStairs(int n){
//若n<=2,返回n
From 1e81ef27100c8fc76bc38ec0135a4b18554398af Mon Sep 17 00:00:00 2001
From: Beim <73528776+162-jld@users.noreply.github.com>
Date: Sun, 1 May 2022 11:51:11 +0800
Subject: [PATCH 121/551] =?UTF-8?q?Update=200383.=E8=B5=8E=E9=87=91?=
=?UTF-8?q?=E4=BF=A1.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...3.\350\265\216\351\207\221\344\277\241.md" | 28 ++++++++++---------
1 file changed, 15 insertions(+), 13 deletions(-)
diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md"
index 0070734779..56dcb8dd1d 100644
--- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md"
+++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md"
@@ -110,23 +110,25 @@ Java:
```Java
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
- //记录杂志字符串出现的次数
- int[] arr = new int[26];
- int temp;
- for (int i = 0; i < magazine.length(); i++) {
- temp = magazine.charAt(i) - 'a';
- arr[temp]++;
+ // 定义一个哈希映射数组
+ int[] record = new int[26];
+
+ // 遍历
+ for(char c : magazine.toCharArray()){
+ record[c - 'a'] += 1;
+ }
+
+ for(char c : ransomNote.toCharArray()){
+ record[c - 'a'] -= 1;
}
- for (int i = 0; i < ransomNote.length(); i++) {
- temp = ransomNote.charAt(i) - 'a';
- //对于金信中的每一个字符都在数组中查找
- //找到相应位减一,否则找不到返回false
- if (arr[temp] > 0) {
- arr[temp]--;
- } else {
+
+ // 如果数组中存在负数,说明ransomNote字符串总存在magazine中没有的字符
+ for(int i : record){
+ if(i < 0){
return false;
}
}
+
return true;
}
}
From 25e26f1f86bf246f93560710f41dcf5a26414770 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 1 May 2022 13:45:25 +0800
Subject: [PATCH 122/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0746.=E4=BD=BF?=
=?UTF-8?q?=E7=94=A8=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC?=
=?UTF-8?q?=E6=A2=AF.md)=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...71\347\210\254\346\245\274\346\242\257.md" | 23 +++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md"
index c356955ad4..5931fc8ac8 100644
--- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md"
+++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md"
@@ -266,7 +266,30 @@ var minCostClimbingStairs = function(cost) {
};
```
+### TypeScript
+
+```typescript
+function minCostClimbingStairs(cost: number[]): number {
+ /**
+ dp[i]: 走到第i阶需要花费的最少金钱
+ dp[0]: cost[0];
+ dp[1]: cost[1];
+ ...
+ dp[i]: min(dp[i - 1], dp[i - 2]) + cost[i];
+ */
+ const dp: number[] = [];
+ const length: number = cost.length;
+ dp[0] = cost[0];
+ dp[1] = cost[1];
+ for (let i = 2; i <= length; i++) {
+ dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];
+ }
+ return Math.min(dp[length - 1], dp[length - 2]);
+};
+```
+
### C
+
```c
int minCostClimbingStairs(int* cost, int costSize){
//开辟dp数组,大小为costSize
From fc28660b6189b010709bedc7dd5acf678eaf47ed Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 1 May 2022 14:42:12 +0800
Subject: [PATCH 123/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880062.?=
=?UTF-8?q?=E4=B8=8D=E5=90=8C=E8=B7=AF=E5=BE=84.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...15\345\220\214\350\267\257\345\276\204.md" | 30 ++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)
diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md"
index 4a9af12965..f59b7be8bc 100644
--- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md"
+++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md"
@@ -273,7 +273,7 @@ public:
return dp[m-1][n-1];
}
-```
+```
### Python
```python
@@ -347,7 +347,35 @@ var uniquePaths = function(m, n) {
};
```
+### TypeScript
+
+```typescript
+function uniquePaths(m: number, n: number): number {
+ /**
+ dp[i][j]: 到达(i, j)的路径数
+ dp[0][*]: 1;
+ dp[*][0]: 1;
+ ...
+ dp[i][j]: dp[i - 1][j] + dp[i][j - 1];
+ */
+ const dp: number[][] = new Array(m).fill(0).map(_ => []);
+ for (let i = 0; i < m; i++) {
+ dp[i][0] = 1;
+ }
+ for (let i = 0; i < n; i++) {
+ dp[0][i] = 1;
+ }
+ for (let i = 1; i < m; i++) {
+ for (let j = 1; j < n; j++) {
+ dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
+ }
+ }
+ return dp[m - 1][n - 1];
+};
+```
+
### C
+
```c
//初始化dp数组
int **initDP(int m, int n) {
From 8c596b161a140a76ea57130ac61a009308bd277c Mon Sep 17 00:00:00 2001
From: Beim <1497359184@qq.com>
Date: Sun, 1 May 2022 15:45:58 +0800
Subject: [PATCH 124/551] modify_problems_0383
---
README.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index 1d7f219d2c..3d44ca690f 100644
--- a/README.md
+++ b/README.md
@@ -31,7 +31,7 @@
-# LeetCode 刷题攻略
+# LeetCode 刷题攻略1111
## 刷题攻略的背景
@@ -254,7 +254,7 @@
33. [二叉树:构造一棵搜索树](./problems/0108.将有序数组转换为二叉搜索树.md)
34. [二叉树:搜索树转成累加树](./problems/0538.把二叉搜索树转换为累加树.md)
35. [二叉树:总结篇!(需要掌握的二叉树技能都在这里了)](./problems/二叉树总结篇.md)
-
+
## 回溯算法
题目分类大纲如下:
From d3e1f1d3b3406f28e7494340928fbc1866c0b6f6 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 1 May 2022 19:24:05 +0800
Subject: [PATCH 125/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880063.?=
=?UTF-8?q?=E4=B8=8D=E5=90=8C=E8=B7=AF=E5=BE=84II.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\345\220\214\350\267\257\345\276\204II.md" | 33 ++++++++++++++++++-
1 file changed, 32 insertions(+), 1 deletion(-)
diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
index a40ccedaa9..d09ea0e634 100644
--- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
+++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
@@ -352,7 +352,38 @@ var uniquePathsWithObstacles = function(obstacleGrid) {
};
```
-C
+### TypeScript
+
+```typescript
+function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
+ /**
+ dp[i][j]: 到达(i, j)的路径数
+ dp[0][*]: 用u表示第一个障碍物下标,则u之前为1,u之后(含u)为0
+ dp[*][0]: 同上
+ ...
+ dp[i][j]: obstacleGrid[i][j] === 1 ? 0 : dp[i-1][j] + dp[i][j-1];
+ */
+ const m: number = obstacleGrid.length;
+ const n: number = obstacleGrid[0].length;
+ const dp: number[][] = new Array(m).fill(0).map(_ => new Array(n).fill(0));
+ for (let i = 0; i < m && obstacleGrid[i][0] === 0; i++) {
+ dp[i][0] = 1;
+ }
+ for (let i = 0; i < n && obstacleGrid[0][i] === 0; i++) {
+ dp[0][i] = 1;
+ }
+ for (let i = 1; i < m; i++) {
+ for (let j = 1; j < n; j++) {
+ if (obstacleGrid[i][j] === 1) continue;
+ dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
+ }
+ }
+ return dp[m - 1][n - 1];
+};
+```
+
+### C
+
```c
//初始化dp数组
int **initDP(int m, int n, int** obstacleGrid) {
From dd514eb08787dd203cea4668f75477971b4e88fe Mon Sep 17 00:00:00 2001
From: wang <472146630@qq.com>
Date: Sun, 1 May 2022 21:08:57 +0800
Subject: [PATCH 126/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200053.=E6=9C=80?=
=?UTF-8?q?=E5=A4=A7=E5=AD=90=E5=BA=8F=E5=92=8C=E3=80=810135=E5=88=86?=
=?UTF-8?q?=E5=8F=91=E7=B3=96=E6=9E=9C=E5=92=8C0455=E5=88=86=E5=8F=91?=
=?UTF-8?q?=E9=A5=BC=E5=B9=B2=E7=9A=84=20Rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\345\255\220\345\272\217\345\222\214.md" | 20 +++++++++--
...06\345\217\221\347\263\226\346\236\234.md" | 29 ++++++++++++---
...06\345\217\221\351\245\274\345\271\262.md" | 36 ++++++++++++++-----
3 files changed, 69 insertions(+), 16 deletions(-)
diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
index b5fb7642c5..9dbc7313a0 100644
--- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
+++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
@@ -140,7 +140,7 @@ public:
## 其他语言版本
-### Java
+### Java
```java
class Solution {
public int maxSubArray(int[] nums) {
@@ -180,7 +180,7 @@ class Solution {
}
```
-### Python
+### Python
```python
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
@@ -195,7 +195,7 @@ class Solution:
return result
```
-### Go
+### Go
```go
func maxSubArray(nums []int) int {
@@ -212,6 +212,20 @@ func maxSubArray(nums []int) int {
}
```
+### Rust
+```rust
+pub fn max_sub_array(nums: Vec) -> i32 {
+ let mut max_sum = i32::MIN;
+ let mut curr = 0;
+ for n in nums.iter() {
+ curr += n;
+ max_sum = max_sum.max(curr);
+ curr = curr.max(0);
+ }
+ max_sum
+}
+```
+
### Javascript:
```Javascript
var maxSubArray = function(nums) {
diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md"
index ccdabc16b4..ce738689ac 100644
--- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md"
+++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md"
@@ -126,11 +126,11 @@ public:
## 其他语言版本
-### Java
+### Java
```java
class Solution {
- /**
- 分两个阶段
+ /**
+ 分两个阶段
1、起点下标1 从左往右,只要 右边 比 左边 大,右边的糖果=左边 + 1
2、起点下标 ratings.length - 2 从右往左, 只要左边 比 右边 大,此时 左边的糖果应该 取本身的糖果数(符合比它左边大) 和 右边糖果数 + 1 二者的最大值,这样才符合 它比它左边的大,也比它右边大
*/
@@ -160,7 +160,7 @@ class Solution {
}
```
-### Python
+### Python
```python
class Solution:
def candy(self, ratings: List[int]) -> int:
@@ -213,6 +213,25 @@ func findMax(num1 int ,num2 int) int{
}
```
+### Rust
+```rust
+pub fn candy(ratings: Vec) -> i32 {
+ let mut candies = vec![1i32; ratings.len()];
+ for i in 1..ratings.len() {
+ if ratings[i - 1] < ratings[i] {
+ candies[i] = candies[i - 1] + 1;
+ }
+ }
+
+ for i in (0..ratings.len()-1).rev() {
+ if ratings[i] > ratings[i + 1] {
+ candies[i] = candies[i].max(candies[i + 1] + 1);
+ }
+ }
+ candies.iter().sum()
+}
+```
+
### Javascript:
```Javascript
var candy = function(ratings) {
@@ -229,7 +248,7 @@ var candy = function(ratings) {
candys[i] = Math.max(candys[i], candys[i + 1] + 1)
}
}
-
+
let count = candys.reduce((a, b) => {
return a + b
})
diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md"
index d95a407a75..17db4a859e 100644
--- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md"
+++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md"
@@ -106,7 +106,7 @@ public:
## 其他语言版本
-### Java
+### Java
```java
class Solution {
// 思路1:优先考虑饼干,小饼干先喂饱小胃口
@@ -145,7 +145,7 @@ class Solution {
}
```
-### Python
+### Python
```python
class Solution:
# 思路1:优先考虑胃饼干
@@ -166,13 +166,13 @@ class Solution:
s.sort()
start, count = len(s) - 1, 0
for index in range(len(g) - 1, -1, -1): # 先喂饱大胃口
- if start >= 0 and g[index] <= s[start]:
+ if start >= 0 and g[index] <= s[start]:
start -= 1
count += 1
return count
```
-### Go
+### Go
```golang
//排序后,局部最优
func findContentChildren(g []int, s []int) int {
@@ -191,7 +191,27 @@ func findContentChildren(g []int, s []int) int {
}
```
-### Javascript
+### Rust
+```rust
+pub fn find_content_children(children: Vec, cookie: Vec) -> i32 {
+ let mut children = children;
+ let mut cookies = cookie;
+ children.sort();
+ cookies.sort();
+
+ let (mut child, mut cookie) = (0usize, 0usize);
+ while child < children.len() && cookie < cookies.len() {
+ // 优先选择最小饼干喂饱孩子
+ if children[child] <= cookies[cookie] {
+ child += 1;
+ }
+ cookie += 1
+ }
+ child as i32
+}
+```
+
+### Javascript
```js
var findContentChildren = function(g, s) {
g = g.sort((a, b) => a - b)
@@ -203,7 +223,7 @@ var findContentChildren = function(g, s) {
result++
index--
}
- }
+ }
return result
};
@@ -251,7 +271,7 @@ function findContentChildren(g: number[], s: number[]): number {
};
```
-### C
+### C
```c
int cmp(int* a, int* b) {
@@ -261,7 +281,7 @@ int cmp(int* a, int* b) {
int findContentChildren(int* g, int gSize, int* s, int sSize){
if(sSize == 0)
return 0;
-
+
//将两个数组排序为升序
qsort(g, gSize, sizeof(int), cmp);
qsort(s, sSize, sizeof(int), cmp);
From 4c03ad7a78fcb324da45e97a2cfca618af1086c8 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 1 May 2022 22:32:12 +0800
Subject: [PATCH 127/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880343.?=
=?UTF-8?q?=E6=95=B4=E6=95=B0=E6=8B=86=E5=88=86.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...64\346\225\260\346\213\206\345\210\206.md" | 28 ++++++++++++++++++-
1 file changed, 27 insertions(+), 1 deletion(-)
diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md"
index 4a7ba6ab0d..279f1d7182 100644
--- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md"
+++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md"
@@ -274,7 +274,33 @@ var integerBreak = function(n) {
};
```
-C:
+### TypeScript
+
+```typescript
+function integerBreak(n: number): number {
+ /**
+ dp[i]: i对应的最大乘积
+ dp[2]: 1;
+ ...
+ dp[i]: max(
+ 1 * dp[i - 1], 1 * (i - 1),
+ 2 * dp[i - 2], 2 * (i - 2),
+ ..., (i - 2) * dp[2], (i - 2) * 2
+ );
+ */
+ const dp: number[] = new Array(n + 1).fill(0);
+ dp[2] = 1;
+ for (let i = 3; i <= n; i++) {
+ for (let j = 1; j <= i - 2; j++) {
+ dp[i] = Math.max(dp[i], j * dp[i - j], j * (i - j));
+ }
+ }
+ return dp[n];
+};
+```
+
+### C
+
```c
//初始化DP数组
int *initDP(int num) {
From b1ef364ffb9c0082f401c34bdd6bf22c4f330fa5 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Mon, 2 May 2022 17:25:45 +0800
Subject: [PATCH 128/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880096.?=
=?UTF-8?q?=E4=B8=8D=E5=90=8C=E7=9A=84=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2?=
=?UTF-8?q?=E6=A0=91.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...11\346\220\234\347\264\242\346\240\221.md" | 28 ++++++++++++++++++-
1 file changed, 27 insertions(+), 1 deletion(-)
diff --git "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
index 41fcb8fe18..25561b506a 100644
--- "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
+++ "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
@@ -227,7 +227,33 @@ const numTrees =(n) => {
};
```
-C:
+TypeScript
+
+```typescript
+function numTrees(n: number): number {
+ /**
+ dp[i]: i个节点对应的种树
+ dp[0]: -1; 无意义;
+ dp[1]: 1;
+ ...
+ dp[i]: 2 * dp[i - 1] +
+ (dp[1] * dp[i - 2] + dp[2] * dp[i - 3] + ... + dp[i - 2] * dp[1]); 从1加到i-2
+ */
+ const dp: number[] = [];
+ dp[0] = -1; // 表示无意义
+ dp[1] = 1;
+ for (let i = 2; i <= n; i++) {
+ dp[i] = 2 * dp[i - 1];
+ for (let j = 1, end = i - 1; j < end; j++) {
+ dp[i] += dp[j] * dp[end - j];
+ }
+ }
+ return dp[n];
+};
+```
+
+### C
+
```c
//开辟dp数组
int *initDP(int n) {
From 5440c3d46b16565a265e30e498662829bf40b5e1 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Tue, 3 May 2022 11:48:03 +0800
Subject: [PATCH 129/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E8=83=8C?=
=?UTF-8?q?=E5=8C=85=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C?=
=?UTF-8?q?=E5=8C=85-1.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\241\20001\350\203\214\345\214\205-1.md" | 42 +++++++++++++++++++
1 file changed, 42 insertions(+)
diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
index fe940b4ca4..257e87e470 100644
--- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
+++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
@@ -423,5 +423,47 @@ function test () {
test();
```
+### TypeScript
+
+```typescript
+function testWeightBagProblem(
+ weight: number[],
+ value: number[],
+ size: number
+): number {
+ /**
+ * dp[i][j]: 前i个物品,背包容量为j,能获得的最大价值
+ * dp[0][*]: u=weight[0],u之前为0,u之后(含u)为value[0]
+ * dp[*][0]: 0
+ * ...
+ * dp[i][j]: max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i]);
+ */
+ const goodsNum: number = weight.length;
+ const dp: number[][] = new Array(goodsNum)
+ .fill(0)
+ .map((_) => new Array(size + 1).fill(0));
+ for (let i = weight[0]; i <= size; i++) {
+ dp[0][i] = value[0];
+ }
+ for (let i = 1; i < goodsNum; i++) {
+ for (let j = 1; j <= size; j++) {
+ if (j < weight[i]) {
+ dp[i][j] = dp[i - 1][j];
+ } else {
+ dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);
+ }
+ }
+ }
+ return dp[goodsNum - 1][size];
+}
+// test
+const weight = [1, 3, 4];
+const value = [15, 20, 30];
+const size = 4;
+console.log(testWeightBagProblem(weight, value, size));
+```
+
+
+
-----------------------
From 36f630b109a93685bb241884e5584b327028d5f5 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Tue, 3 May 2022 12:34:20 +0800
Subject: [PATCH 130/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E8=83=8C?=
=?UTF-8?q?=E5=8C=85=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C?=
=?UTF-8?q?=E5=8C=85-2.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\241\20001\350\203\214\345\214\205-2.md" | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md"
index dabdfb2d09..bb5e3a037b 100644
--- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md"
+++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md"
@@ -315,6 +315,30 @@ function test () {
test();
```
+### TypeScript
+
+```typescript
+function testWeightBagProblem(
+ weight: number[],
+ value: number[],
+ size: number
+): number {
+ const goodsNum: number = weight.length;
+ const dp: number[] = new Array(size + 1).fill(0);
+ for (let i = 0; i < goodsNum; i++) {
+ for (let j = size; j >= weight[i]; j--) {
+ dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
+ }
+ }
+ return dp[size];
+}
+const weight = [1, 3, 4];
+const value = [15, 20, 30];
+const size = 4;
+console.log(testWeightBagProblem(weight, value, size));
+
+```
+
-----------------------
From a38ee5f525af998683a2d0fb38f6c6cac1312f5d Mon Sep 17 00:00:00 2001
From: Frankheartusf <104822497+Frankheartusf@users.noreply.github.com>
Date: Tue, 3 May 2022 17:31:37 +0800
Subject: [PATCH 131/551] Add files via upload
---
...\350\267\203\346\270\270\346\210\217II.md" | 287 ++++++++++++++++++
1 file changed, 287 insertions(+)
create mode 100644 "0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
diff --git "a/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
new file mode 100644
index 0000000000..c0d3c3e522
--- /dev/null
+++ "b/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
@@ -0,0 +1,287 @@
+
+
+
+
+
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+
+
+> 相对于[贪心算法:跳跃游戏](https://mp.weixin.qq.com/s/606_N9j8ACKCODoCbV1lSA)难了不少,做好心里准备!
+
+# 45.跳跃游戏II
+
+[力扣题目链接](https://leetcode-cn.com/problems/jump-game-ii/)
+
+给定一个非负整数数组,你最初位于数组的第一个位置。
+
+数组中的每个元素代表你在该位置可以跳跃的最大长度。
+
+你的目标是使用最少的跳跃次数到达数组的最后一个位置。
+
+示例:
+* 输入: [2,3,1,1,4]
+* 输出: 2
+* 解释: 跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
+
+说明:
+假设你总是可以到达数组的最后一个位置。
+
+
+## 思路
+
+本题相对于[55.跳跃游戏](https://programmercarl.com/0055.跳跃游戏.html)还是难了不少。
+
+但思路是相似的,还是要看最大覆盖范围。
+
+本题要计算最小步数,那么就要想清楚什么时候步数才一定要加一呢?
+
+贪心的思路,局部最优:当前可移动距离尽可能多走,如果还没到终点,步数再加一。整体最优:一步尽可能多走,从而达到最小步数。
+
+思路虽然是这样,但在写代码的时候还不能真的就能跳多远跳远,那样就不知道下一步最远能跳到哪里了。
+
+**所以真正解题的时候,要从覆盖范围出发,不管怎么跳,覆盖范围内一定是可以跳到的,以最小的步数增加覆盖范围,覆盖范围一旦覆盖了终点,得到的就是最小步数!**
+
+**这里需要统计两个覆盖范围,当前这一步的最大覆盖和下一步最大覆盖**。
+
+如果移动下标达到了当前这一步的最大覆盖最远距离了,还没有到终点的话,那么就必须再走一步来增加覆盖范围,直到覆盖范围覆盖了终点。
+
+如图:
+
+
+
+**图中覆盖范围的意义在于,只要红色的区域,最多两步一定可以到!(不用管具体怎么跳,反正一定可以跳到)**
+
+## 方法一
+
+从图中可以看出来,就是移动下标达到了当前覆盖的最远距离下标时,步数就要加一,来增加覆盖距离。最后的步数就是最少步数。
+
+这里还是有个特殊情况需要考虑,当移动下标达到了当前覆盖的最远距离下标时
+
+* 如果当前覆盖最远距离下标不是是集合终点,步数就加一,还需要继续走。
+* 如果当前覆盖最远距离下标就是是集合终点,步数不用加一,因为不能再往后走了。
+
+C++代码如下:(详细注释)
+
+```CPP
+// 版本一
+class Solution {
+public:
+ int jump(vector& nums) {
+ if (nums.size() == 1) return 0;
+ int curDistance = 0; // 当前覆盖最远距离下标
+ int ans = 0; // 记录走的最大步数
+ int nextDistance = 0; // 下一步覆盖最远距离下标
+ for (int i = 0; i < nums.size(); i++) {
+ nextDistance = max(nums[i] + i, nextDistance); // 更新下一步覆盖最远距离下标
+ if (i == curDistance) { // 遇到当前覆盖最远距离下标
+ if (curDistance != nums.size() - 1) { // 如果当前覆盖最远距离下标不是终点
+ ans++; // 需要走下一步
+ curDistance = nextDistance; // 更新当前覆盖最远距离下标(相当于加油了)
+ if (nextDistance >= nums.size() - 1) break; // 下一步的覆盖范围已经可以达到终点,结束循环
+ } else break; // 当前覆盖最远距离下标是集合终点,不用做ans++操作了,直接结束
+ }
+ }
+ return ans;
+ }
+};
+```
+
+## 方法二
+
+依然是贪心,思路和方法一差不多,代码可以简洁一些。
+
+**针对于方法一的特殊情况,可以统一处理**,即:移动下标只要遇到当前覆盖最远距离的下标,直接步数加一,不考虑是不是终点的情况。
+
+想要达到这样的效果,只要让移动下标,最大只能移动到nums.size - 2的地方就可以了。
+
+因为当移动下标指向nums.size - 2时:
+
+* 如果移动下标等于当前覆盖最大距离下标, 需要再走一步(即ans++),因为最后一步一定是可以到的终点。(题目假设总是可以到达数组的最后一个位置),如图:
+
+
+* 如果移动下标不等于当前覆盖最大距离下标,说明当前覆盖最远距离就可以直接达到终点了,不需要再走一步。如图:
+
+
+
+代码如下:
+
+```CPP
+// 版本二
+class Solution {
+public:
+ int jump(vector& nums) {
+ int curDistance = 0; // 当前覆盖的最远距离下标
+ int ans = 0; // 记录走的最大步数
+ int nextDistance = 0; // 下一步覆盖的最远距离下标
+ for (int i = 0; i < nums.size() - 1; i++) { // 注意这里是小于nums.size() - 1,这是关键所在
+ nextDistance = max(nums[i] + i, nextDistance); // 更新下一步覆盖的最远距离下标
+ if (i == curDistance) { // 遇到当前覆盖的最远距离下标
+ curDistance = nextDistance; // 更新当前覆盖的最远距离下标
+ ans++;
+ }
+ }
+ return ans;
+ }
+};
+```
+
+可以看出版本二的代码相对于版本一简化了不少!
+
+其精髓在于控制移动下标i只移动到nums.size() - 2的位置,所以移动下标只要遇到当前覆盖最远距离的下标,直接步数加一,不用考虑别的了。
+
+## 总结
+
+相信大家可以发现,这道题目相当于[55.跳跃游戏](https://programmercarl.com/0055.跳跃游戏.html)难了不止一点。
+
+但代码又十分简单,贪心就是这么巧妙。
+
+理解本题的关键在于:**以最小的步数增加最大的覆盖范围,直到覆盖范围覆盖了终点**,这个范围内最小步数一定可以跳到,不用管具体是怎么跳的,不纠结于一步究竟跳一个单位还是两个单位。
+
+
+## 其他语言版本
+
+
+### Java
+```Java
+// 版本一
+class Solution {
+ public int jump(int[] nums) {
+ if (nums == null || nums.length == 0 || nums.length == 1) {
+ return 0;
+ }
+ //记录跳跃的次数
+ int count=0;
+ //当前的覆盖最大区域
+ int curDistance = 0;
+ //最大的覆盖区域
+ int maxDistance = 0;
+ for (int i = 0; i < nums.length; i++) {
+ //在可覆盖区域内更新最大的覆盖区域
+ maxDistance = Math.max(maxDistance,i+nums[i]);
+ //说明当前一步,再跳一步就到达了末尾
+ if (maxDistance>=nums.length-1){
+ count++;
+ break;
+ }
+ //走到当前覆盖的最大区域时,更新下一步可达的最大区域
+ if (i==curDistance){
+ curDistance = maxDistance;
+ count++;
+ }
+ }
+ return count;
+ }
+}
+```
+
+```java
+// 版本二
+class Solution {
+ public int jump(int[] nums) {
+ int result = 0;
+ // 当前覆盖的最远距离下标
+ int end = 0;
+ // 下一步覆盖的最远距离下标
+ int temp = 0;
+ for (int i = 0; i <= end && end < nums.length - 1; ++i) {
+ temp = Math.max(temp, i + nums[i]);
+ // 可达位置的改变次数就是跳跃次数
+ if (i == end) {
+ end = temp;
+ result++;
+ }
+ }
+ return result;
+ }
+}
+```
+
+### Python
+
+```python
+class Solution:
+ def jump(self, nums: List[int]) -> int:
+ if len(nums) == 1: return 0
+ ans = 0
+ curDistance = 0
+ nextDistance = 0
+ for i in range(len(nums)):
+ nextDistance = max(i + nums[i], nextDistance)
+ if i == curDistance:
+ if curDistance != len(nums) - 1:
+ ans += 1
+ curDistance = nextDistance
+ if nextDistance >= len(nums) - 1: break
+ return ans
+```
+
+### Go
+```Go
+func jump(nums []int) int {
+ dp := make([]int, len(nums))
+ dp[0] = 0//初始第一格跳跃数一定为0
+
+ for i := 1; i < len(nums); i++ {
+ dp[i] = i
+ for j := 0; j < i; j++ {
+ if nums[j] + j >= i {//nums[j]为起点,j为往右跳的覆盖范围,这行表示从j能跳到i
+ dp[i] = min(dp[j] + 1, dp[i])//更新最小能到i的跳跃次数
+ }
+ }
+ }
+ return dp[len(nums)-1]
+}
+
+func min(a, b int) int {
+ if a < b {
+ return a
+ } else {
+ return b
+ }
+}
+```
+
+### Javascript
+```Javascript
+var jump = function(nums) {
+ let curIndex = 0
+ let nextIndex = 0
+ let steps = 0
+ for(let i = 0; i < nums.length - 1; i++) {
+ nextIndex = Math.max(nums[i] + i, nextIndex)
+ if(i === curIndex) {
+ curIndex = nextIndex
+ steps++
+ }
+ }
+
+ return steps
+};
+```
+
+### TypeScript
+
+```typescript
+function jump(nums: number[]): number {
+ const length: number = nums.length;
+ let curFarthestIndex: number = 0,
+ nextFarthestIndex: number = 0;
+ let curIndex: number = 0;
+ let stepNum: number = 0;
+ while (curIndex < length - 1) {
+ nextFarthestIndex = Math.max(nextFarthestIndex, curIndex + nums[curIndex]);
+ if (curIndex === curFarthestIndex) {
+ curFarthestIndex = nextFarthestIndex;
+ stepNum++;
+ }
+ curIndex++;
+ }
+ return stepNum;
+};
+```
+
+
+
+
+
+-----------------------
+
From 64d477e3f05c2b7ebb8aa3b5b8ed27578b5a1b31 Mon Sep 17 00:00:00 2001
From: Frankheartusf <2332517004@qq.com>
Date: Tue, 3 May 2022 17:50:07 +0800
Subject: [PATCH 132/551] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200045=20=E8=B7=B3?=
=?UTF-8?q?=E8=B7=83=E9=97=AE=E9=A2=98II=20golang=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\350\267\203\346\270\270\346\210\217II.md" | 287 ------------------
...\350\267\203\346\270\270\346\210\217II.md" | 32 +-
2 files changed, 20 insertions(+), 299 deletions(-)
delete mode 100644 "0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
diff --git "a/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
deleted file mode 100644
index c0d3c3e522..0000000000
--- "a/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
+++ /dev/null
@@ -1,287 +0,0 @@
-
-
-
-
-
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
-
-
-> 相对于[贪心算法:跳跃游戏](https://mp.weixin.qq.com/s/606_N9j8ACKCODoCbV1lSA)难了不少,做好心里准备!
-
-# 45.跳跃游戏II
-
-[力扣题目链接](https://leetcode-cn.com/problems/jump-game-ii/)
-
-给定一个非负整数数组,你最初位于数组的第一个位置。
-
-数组中的每个元素代表你在该位置可以跳跃的最大长度。
-
-你的目标是使用最少的跳跃次数到达数组的最后一个位置。
-
-示例:
-* 输入: [2,3,1,1,4]
-* 输出: 2
-* 解释: 跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
-
-说明:
-假设你总是可以到达数组的最后一个位置。
-
-
-## 思路
-
-本题相对于[55.跳跃游戏](https://programmercarl.com/0055.跳跃游戏.html)还是难了不少。
-
-但思路是相似的,还是要看最大覆盖范围。
-
-本题要计算最小步数,那么就要想清楚什么时候步数才一定要加一呢?
-
-贪心的思路,局部最优:当前可移动距离尽可能多走,如果还没到终点,步数再加一。整体最优:一步尽可能多走,从而达到最小步数。
-
-思路虽然是这样,但在写代码的时候还不能真的就能跳多远跳远,那样就不知道下一步最远能跳到哪里了。
-
-**所以真正解题的时候,要从覆盖范围出发,不管怎么跳,覆盖范围内一定是可以跳到的,以最小的步数增加覆盖范围,覆盖范围一旦覆盖了终点,得到的就是最小步数!**
-
-**这里需要统计两个覆盖范围,当前这一步的最大覆盖和下一步最大覆盖**。
-
-如果移动下标达到了当前这一步的最大覆盖最远距离了,还没有到终点的话,那么就必须再走一步来增加覆盖范围,直到覆盖范围覆盖了终点。
-
-如图:
-
-
-
-**图中覆盖范围的意义在于,只要红色的区域,最多两步一定可以到!(不用管具体怎么跳,反正一定可以跳到)**
-
-## 方法一
-
-从图中可以看出来,就是移动下标达到了当前覆盖的最远距离下标时,步数就要加一,来增加覆盖距离。最后的步数就是最少步数。
-
-这里还是有个特殊情况需要考虑,当移动下标达到了当前覆盖的最远距离下标时
-
-* 如果当前覆盖最远距离下标不是是集合终点,步数就加一,还需要继续走。
-* 如果当前覆盖最远距离下标就是是集合终点,步数不用加一,因为不能再往后走了。
-
-C++代码如下:(详细注释)
-
-```CPP
-// 版本一
-class Solution {
-public:
- int jump(vector& nums) {
- if (nums.size() == 1) return 0;
- int curDistance = 0; // 当前覆盖最远距离下标
- int ans = 0; // 记录走的最大步数
- int nextDistance = 0; // 下一步覆盖最远距离下标
- for (int i = 0; i < nums.size(); i++) {
- nextDistance = max(nums[i] + i, nextDistance); // 更新下一步覆盖最远距离下标
- if (i == curDistance) { // 遇到当前覆盖最远距离下标
- if (curDistance != nums.size() - 1) { // 如果当前覆盖最远距离下标不是终点
- ans++; // 需要走下一步
- curDistance = nextDistance; // 更新当前覆盖最远距离下标(相当于加油了)
- if (nextDistance >= nums.size() - 1) break; // 下一步的覆盖范围已经可以达到终点,结束循环
- } else break; // 当前覆盖最远距离下标是集合终点,不用做ans++操作了,直接结束
- }
- }
- return ans;
- }
-};
-```
-
-## 方法二
-
-依然是贪心,思路和方法一差不多,代码可以简洁一些。
-
-**针对于方法一的特殊情况,可以统一处理**,即:移动下标只要遇到当前覆盖最远距离的下标,直接步数加一,不考虑是不是终点的情况。
-
-想要达到这样的效果,只要让移动下标,最大只能移动到nums.size - 2的地方就可以了。
-
-因为当移动下标指向nums.size - 2时:
-
-* 如果移动下标等于当前覆盖最大距离下标, 需要再走一步(即ans++),因为最后一步一定是可以到的终点。(题目假设总是可以到达数组的最后一个位置),如图:
-
-
-* 如果移动下标不等于当前覆盖最大距离下标,说明当前覆盖最远距离就可以直接达到终点了,不需要再走一步。如图:
-
-
-
-代码如下:
-
-```CPP
-// 版本二
-class Solution {
-public:
- int jump(vector& nums) {
- int curDistance = 0; // 当前覆盖的最远距离下标
- int ans = 0; // 记录走的最大步数
- int nextDistance = 0; // 下一步覆盖的最远距离下标
- for (int i = 0; i < nums.size() - 1; i++) { // 注意这里是小于nums.size() - 1,这是关键所在
- nextDistance = max(nums[i] + i, nextDistance); // 更新下一步覆盖的最远距离下标
- if (i == curDistance) { // 遇到当前覆盖的最远距离下标
- curDistance = nextDistance; // 更新当前覆盖的最远距离下标
- ans++;
- }
- }
- return ans;
- }
-};
-```
-
-可以看出版本二的代码相对于版本一简化了不少!
-
-其精髓在于控制移动下标i只移动到nums.size() - 2的位置,所以移动下标只要遇到当前覆盖最远距离的下标,直接步数加一,不用考虑别的了。
-
-## 总结
-
-相信大家可以发现,这道题目相当于[55.跳跃游戏](https://programmercarl.com/0055.跳跃游戏.html)难了不止一点。
-
-但代码又十分简单,贪心就是这么巧妙。
-
-理解本题的关键在于:**以最小的步数增加最大的覆盖范围,直到覆盖范围覆盖了终点**,这个范围内最小步数一定可以跳到,不用管具体是怎么跳的,不纠结于一步究竟跳一个单位还是两个单位。
-
-
-## 其他语言版本
-
-
-### Java
-```Java
-// 版本一
-class Solution {
- public int jump(int[] nums) {
- if (nums == null || nums.length == 0 || nums.length == 1) {
- return 0;
- }
- //记录跳跃的次数
- int count=0;
- //当前的覆盖最大区域
- int curDistance = 0;
- //最大的覆盖区域
- int maxDistance = 0;
- for (int i = 0; i < nums.length; i++) {
- //在可覆盖区域内更新最大的覆盖区域
- maxDistance = Math.max(maxDistance,i+nums[i]);
- //说明当前一步,再跳一步就到达了末尾
- if (maxDistance>=nums.length-1){
- count++;
- break;
- }
- //走到当前覆盖的最大区域时,更新下一步可达的最大区域
- if (i==curDistance){
- curDistance = maxDistance;
- count++;
- }
- }
- return count;
- }
-}
-```
-
-```java
-// 版本二
-class Solution {
- public int jump(int[] nums) {
- int result = 0;
- // 当前覆盖的最远距离下标
- int end = 0;
- // 下一步覆盖的最远距离下标
- int temp = 0;
- for (int i = 0; i <= end && end < nums.length - 1; ++i) {
- temp = Math.max(temp, i + nums[i]);
- // 可达位置的改变次数就是跳跃次数
- if (i == end) {
- end = temp;
- result++;
- }
- }
- return result;
- }
-}
-```
-
-### Python
-
-```python
-class Solution:
- def jump(self, nums: List[int]) -> int:
- if len(nums) == 1: return 0
- ans = 0
- curDistance = 0
- nextDistance = 0
- for i in range(len(nums)):
- nextDistance = max(i + nums[i], nextDistance)
- if i == curDistance:
- if curDistance != len(nums) - 1:
- ans += 1
- curDistance = nextDistance
- if nextDistance >= len(nums) - 1: break
- return ans
-```
-
-### Go
-```Go
-func jump(nums []int) int {
- dp := make([]int, len(nums))
- dp[0] = 0//初始第一格跳跃数一定为0
-
- for i := 1; i < len(nums); i++ {
- dp[i] = i
- for j := 0; j < i; j++ {
- if nums[j] + j >= i {//nums[j]为起点,j为往右跳的覆盖范围,这行表示从j能跳到i
- dp[i] = min(dp[j] + 1, dp[i])//更新最小能到i的跳跃次数
- }
- }
- }
- return dp[len(nums)-1]
-}
-
-func min(a, b int) int {
- if a < b {
- return a
- } else {
- return b
- }
-}
-```
-
-### Javascript
-```Javascript
-var jump = function(nums) {
- let curIndex = 0
- let nextIndex = 0
- let steps = 0
- for(let i = 0; i < nums.length - 1; i++) {
- nextIndex = Math.max(nums[i] + i, nextIndex)
- if(i === curIndex) {
- curIndex = nextIndex
- steps++
- }
- }
-
- return steps
-};
-```
-
-### TypeScript
-
-```typescript
-function jump(nums: number[]): number {
- const length: number = nums.length;
- let curFarthestIndex: number = 0,
- nextFarthestIndex: number = 0;
- let curIndex: number = 0;
- let stepNum: number = 0;
- while (curIndex < length - 1) {
- nextFarthestIndex = Math.max(nextFarthestIndex, curIndex + nums[curIndex]);
- if (curIndex === curFarthestIndex) {
- curFarthestIndex = nextFarthestIndex;
- stepNum++;
- }
- curIndex++;
- }
- return stepNum;
-};
-```
-
-
-
-
-
------------------------
-
diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
index 4caff04246..4e3ab24a83 100644
--- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
+++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
@@ -217,18 +217,26 @@ class Solution:
### Go
```Go
func jump(nums []int) int {
- dp:=make([]int ,len(nums))
- dp[0]=0
-
- for i:=1;ii{
- dp[i]=min(dp[j]+1,dp[i])
- }
- }
- }
- return dp[len(nums)-1]
+ dp := make([]int, len(nums))
+ dp[0] = 0//初始第一格跳跃数一定为0
+
+ for i := 1; i < len(nums); i++ {
+ dp[i] = i
+ for j := 0; j < i; j++ {
+ if nums[j] + j >= i {//nums[j]为起点,j为往右跳的覆盖范围,这行表示从j能跳到i
+ dp[i] = min(dp[j] + 1, dp[i])//更新最小能到i的跳跃次数
+ }
+ }
+ }
+ return dp[len(nums)-1]
+}
+
+func min(a, b int) int {
+ if a < b {
+ return a
+ } else {
+ return b
+ }
}
```
From d692ffe034aa1d7af9a4e5d76999ee6a506c23b9 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Tue, 3 May 2022 21:51:21 +0800
Subject: [PATCH 133/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880416.?=
=?UTF-8?q?=E5=88=86=E5=89=B2=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...11\345\222\214\345\255\220\351\233\206.md" | 55 +++++++++++++++++++
1 file changed, 55 insertions(+)
diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
index b24fb365f4..20b7782d67 100644
--- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
+++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
@@ -416,6 +416,61 @@ var canPartition = function(nums) {
};
```
+TypeScript:
+
+> 一维数组,简洁
+
+```typescript
+function canPartition(nums: number[]): boolean {
+ const sum: number = nums.reduce((pre, cur) => pre + cur);
+ if (sum % 2 === 1) return false;
+ const bagSize: number = sum / 2;
+ const goodsNum: number = nums.length;
+ const dp: number[] = new Array(bagSize + 1).fill(0);
+ for (let i = 0; i < goodsNum; i++) {
+ for (let j = bagSize; j >= nums[i]; j--) {
+ dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i]);
+ }
+ }
+ return dp[bagSize] === bagSize;
+};
+```
+
+> 二维数组,易懂
+
+```typescript
+function canPartition(nums: number[]): boolean {
+ /**
+ weightArr = nums;
+ valueArr = nums;
+ bagSize = sum / 2; (sum为nums各元素总和);
+ 按照0-1背包处理
+ */
+ const sum: number = nums.reduce((pre, cur) => pre + cur);
+ if (sum % 2 === 1) return false;
+ const bagSize: number = sum / 2;
+ const weightArr: number[] = nums;
+ const valueArr: number[] = nums;
+ const goodsNum: number = weightArr.length;
+ const dp: number[][] = new Array(goodsNum)
+ .fill(0)
+ .map(_ => new Array(bagSize + 1).fill(0));
+ for (let i = weightArr[0]; i <= bagSize; i++) {
+ dp[0][i] = valueArr[0];
+ }
+ for (let i = 1; i < goodsNum; i++) {
+ for (let j = 0; j <= bagSize; j++) {
+ if (j < weightArr[i]) {
+ dp[i][j] = dp[i - 1][j];
+ } else {
+ dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weightArr[i]] + valueArr[i]);
+ }
+ }
+ }
+ return dp[goodsNum - 1][bagSize] === bagSize;
+};
+```
+
From 65cde78559bf18cced2e91b57fba8f92a66437a4 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Wed, 4 May 2022 12:57:05 +0800
Subject: [PATCH 134/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881049.?=
=?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=80=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84?=
=?UTF-8?q?=E9=87=8D=E9=87=8FII.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0type?=
=?UTF-8?q?script=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\347\232\204\351\207\215\351\207\217II.md" | 21 +++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
index ee0ddef2d1..3d256c3dd1 100644
--- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
+++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
@@ -277,5 +277,26 @@ var lastStoneWeightII = function (stones) {
};
```
+TypeScript:
+
+```typescript
+function lastStoneWeightII(stones: number[]): number {
+ const sum: number = stones.reduce((pre, cur) => pre + cur);
+ const bagSize: number = Math.floor(sum / 2);
+ const weightArr: number[] = stones;
+ const valueArr: number[] = stones;
+ const goodsNum: number = weightArr.length;
+ const dp: number[] = new Array(bagSize + 1).fill(0);
+ for (let i = 0; i < goodsNum; i++) {
+ for (let j = bagSize; j >= weightArr[i]; j--) {
+ dp[j] = Math.max(dp[j], dp[j - weightArr[i]] + valueArr[i]);
+ }
+ }
+ return sum - dp[bagSize] * 2;
+};
+```
+
+
+
-----------------------
From ce28066ae92780a3829883b387dc004bc76df57d Mon Sep 17 00:00:00 2001
From: programmercarl <826123027@qq.com>
Date: Wed, 4 May 2022 18:06:13 +0800
Subject: [PATCH 135/551] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=B5=84=E6=BA=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 1d7f219d2c..501c5ed439 100644
--- a/README.md
+++ b/README.md
@@ -543,6 +543,7 @@
**来看看就知道了,你会发现相见恨晚!**
+
-
+
From aacbbfbd02613a7110e913fa00298ef0734107ac Mon Sep 17 00:00:00 2001
From: programmercarl <826123027@qq.com>
Date: Wed, 4 May 2022 18:11:46 +0800
Subject: [PATCH 136/551] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=B5=84=E6=BA=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index 501c5ed439..38418322b6 100644
--- a/README.md
+++ b/README.md
@@ -531,7 +531,8 @@
如果是已工作,备注:姓名-城市-岗位-组队刷题。如果学生,备注:姓名-学校-年级-组队刷题。**备注没有自我介绍不通过哦**
-
+
+
@@ -545,5 +546,5 @@
-
+
From b9b7c1530abf7a4b6b68b0f732b5a69cbaae772c Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Wed, 4 May 2022 23:48:12 +0800
Subject: [PATCH 137/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880494.?=
=?UTF-8?q?=E7=9B=AE=E6=A0=87=E5=92=8C.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4.\347\233\256\346\240\207\345\222\214.md" | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md"
index 99b768347d..8ce1f6f1ae 100644
--- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md"
+++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md"
@@ -351,6 +351,25 @@ const findTargetSumWays = (nums, target) => {
};
```
+TypeScript:
+
+```typescript
+function findTargetSumWays(nums: number[], target: number): number {
+ const sum: number = nums.reduce((pre, cur) => pre + cur);
+ if (Math.abs(target) > sum) return 0;
+ if ((target + sum) % 2 === 1) return 0;
+ const bagSize: number = (target + sum) / 2;
+ const dp: number[] = new Array(bagSize + 1).fill(0);
+ dp[0] = 1;
+ for (let i = 0; i < nums.length; i++) {
+ for (let j = bagSize; j >= nums[i]; j--) {
+ dp[j] += dp[j - nums[i]];
+ }
+ }
+ return dp[bagSize];
+};
+```
+
-----------------------
From 3c88974f92926f9280d71ea964bdb36b6f097044 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 5 May 2022 14:15:00 +0800
Subject: [PATCH 138/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880474.?=
=?UTF-8?q?=E4=B8=80=E5=92=8C=E9=9B=B6.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4.\344\270\200\345\222\214\351\233\266.md" | 123 ++++++++++++++++++
1 file changed, 123 insertions(+)
diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md"
index 964df4a829..d38ce03f89 100644
--- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md"
+++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md"
@@ -323,6 +323,129 @@ const findMaxForm = (strs, m, n) => {
};
```
+TypeScript:
+
+> 滚动数组,二维数组法
+
+```typescript
+type BinaryInfo = { numOfZero: number, numOfOne: number };
+function findMaxForm(strs: string[], m: number, n: number): number {
+ const goodsNum: number = strs.length;
+ const dp: number[][] = new Array(m + 1).fill(0)
+ .map(_ => new Array(n + 1).fill(0));
+ for (let i = 0; i < goodsNum; i++) {
+ const { numOfZero, numOfOne } = countBinary(strs[i]);
+ for (let j = m; j >= numOfZero; j--) {
+ for (let k = n; k >= numOfOne; k--) {
+ dp[j][k] = Math.max(dp[j][k], dp[j - numOfZero][k - numOfOne] + 1);
+ }
+ }
+ }
+ return dp[m][n];
+};
+function countBinary(str: string): BinaryInfo {
+ let numOfZero: number = 0,
+ numOfOne: number = 0;
+ for (let s of str) {
+ if (s === '0') {
+ numOfZero++;
+ } else {
+ numOfOne++;
+ }
+ }
+ return { numOfZero, numOfOne };
+}
+```
+
+> 传统背包,三维数组法
+
+```typescript
+type BinaryInfo = { numOfZero: number, numOfOne: number };
+function findMaxForm(strs: string[], m: number, n: number): number {
+ /**
+ dp[i][j][k]: 前i个物品中, 背包的0容量为j, 1容量为k, 最多能放的物品数量
+ */
+ const goodsNum: number = strs.length;
+ const dp: number[][][] = new Array(goodsNum).fill(0)
+ .map(_ => new Array(m + 1)
+ .fill(0)
+ .map(_ => new Array(n + 1).fill(0))
+ );
+ const { numOfZero, numOfOne } = countBinary(strs[0]);
+ for (let i = numOfZero; i <= m; i++) {
+ for (let j = numOfOne; j <= n; j++) {
+ dp[0][i][j] = 1;
+ }
+ }
+ for (let i = 1; i < goodsNum; i++) {
+ const { numOfZero, numOfOne } = countBinary(strs[i]);
+ for (let j = 0; j <= m; j++) {
+ for (let k = 0; k <= n; k++) {
+ if (j < numOfZero || k < numOfOne) {
+ dp[i][j][k] = dp[i - 1][j][k];
+ } else {
+ dp[i][j][k] = Math.max(dp[i - 1][j][k], dp[i - 1][j - numOfZero][k - numOfOne] + 1);
+ }
+ }
+ }
+ }
+ return dp[dp.length - 1][m][n];
+};
+function countBinary(str: string): BinaryInfo {
+ let numOfZero: number = 0,
+ numOfOne: number = 0;
+ for (let s of str) {
+ if (s === '0') {
+ numOfZero++;
+ } else {
+ numOfOne++;
+ }
+ }
+ return { numOfZero, numOfOne };
+}
+```
+
+> 回溯法(会超时)
+
+```typescript
+function findMaxForm(strs: string[], m: number, n: number): number {
+ /**
+ 思路:暴力枚举strs的所有子集,记录符合条件子集的最大长度
+ */
+ let resMax: number = 0;
+ backTrack(strs, m, n, 0, []);
+ return resMax;
+ function backTrack(
+ strs: string[], m: number, n: number,
+ startIndex: number, route: string[]
+ ): void {
+ if (startIndex === strs.length) return;
+ for (let i = startIndex, length = strs.length; i < length; i++) {
+ route.push(strs[i]);
+ if (isValidSubSet(route, m, n)) {
+ resMax = Math.max(resMax, route.length);
+ backTrack(strs, m, n, i + 1, route);
+ }
+ route.pop();
+ }
+ }
+};
+function isValidSubSet(strs: string[], m: number, n: number): boolean {
+ let zeroNum: number = 0,
+ oneNum: number = 0;
+ strs.forEach(str => {
+ for (let s of str) {
+ if (s === '0') {
+ zeroNum++;
+ } else {
+ oneNum++;
+ }
+ }
+ });
+ return zeroNum <= m && oneNum <= n;
+}
+```
+
-----------------------
From 1e9cecb665acd511660a9c8c8af48868e20bb871 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 5 May 2022 16:53:38 +0800
Subject: [PATCH 139/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E8=83=8C?=
=?UTF-8?q?=E5=8C=85=E9=97=AE=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80?=
=?UTF-8?q?=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...14\345\205\250\350\203\214\345\214\205.md" | 21 +++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md"
index faa1dc46e2..936a80c79b 100644
--- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md"
+++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md"
@@ -340,6 +340,27 @@ function test_completePack2() {
}
```
+TypeScript:
+
+```typescript
+// 先遍历物品,再遍历背包容量
+function test_CompletePack(): void {
+ const weight: number[] = [1, 3, 4];
+ const value: number[] = [15, 20, 30];
+ const bagSize: number = 4;
+ const dp: number[] = new Array(bagSize + 1).fill(0);
+ for (let i = 0; i < weight.length; i++) {
+ for (let j = weight[i]; j <= bagSize; j++) {
+ dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
+ }
+ }
+ console.log(dp);
+}
+test_CompletePack();
+```
+
+
+
-----------------------
From b82984e24b23940aa95da2a0283ae4cf3c706f7a Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 5 May 2022 18:11:41 +0800
Subject: [PATCH 140/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880518.?=
=?UTF-8?q?=E9=9B=B6=E9=92=B1=E5=85=91=E6=8D=A2II.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\266\351\222\261\345\205\221\346\215\242II.md" | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md"
index e72c5f8515..8faf66989e 100644
--- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md"
+++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md"
@@ -258,6 +258,21 @@ const change = (amount, coins) => {
}
```
+TypeScript:
+
+```typescript
+function change(amount: number, coins: number[]): number {
+ const dp: number[] = new Array(amount + 1).fill(0);
+ dp[0] = 1;
+ for (let i = 0, length = coins.length; i < length; i++) {
+ for (let j = coins[i]; j <= amount; j++) {
+ dp[j] += dp[j - coins[i]];
+ }
+ }
+ return dp[amount];
+};
+```
+
-----------------------
From cb7bf67a4e3a277ecf53ce37d0556c8e2e65f9f3 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 6 May 2022 11:08:47 +0800
Subject: [PATCH 141/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880377.?=
=?UTF-8?q?=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8C=E2=85=A3.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...10\346\200\273\345\222\214\342\205\243.md" | 20 +++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md"
index aaf27e6123..1d808a3a0a 100644
--- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md"
+++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md"
@@ -221,7 +221,27 @@ const combinationSum4 = (nums, target) => {
};
```
+TypeScript:
+
+```typescript
+function combinationSum4(nums: number[], target: number): number {
+ const dp: number[] = new Array(target + 1).fill(0);
+ dp[0] = 1;
+ // 遍历背包
+ for (let i = 1; i <= target; i++) {
+ // 遍历物品
+ for (let j = 0, length = nums.length; j < length; j++) {
+ if (i >= nums[j]) {
+ dp[i] += dp[i - nums[j]];
+ }
+ }
+ }
+ return dp[target];
+};
+```
+
Rust
+
```Rust
impl Solution {
pub fn combination_sum4(nums: Vec, target: i32) -> i32 {
From 100a4e2b46abf913dacc62637da7faf27bfe060e Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 6 May 2022 11:56:19 +0800
Subject: [PATCH 142/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880070.?=
=?UTF-8?q?=E7=88=AC=E6=A5=BC=E6=A2=AF=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85?=
=?UTF-8?q?=E7=89=88=E6=9C=AC.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?=
=?UTF-8?q?ript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...14\345\214\205\347\211\210\346\234\254.md" | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md"
index 2286de2d1f..0f482bb7cb 100644
--- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md"
+++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md"
@@ -199,6 +199,28 @@ var climbStairs = function(n) {
};
```
+TypeScript:
+
+```typescript
+function climbStairs(n: number): number {
+ const m: number = 2; // 本题m为2
+ const dp: number[] = new Array(n + 1).fill(0);
+ dp[0] = 1;
+ // 遍历背包
+ for (let i = 1; i <= n; i++) {
+ // 遍历物品
+ for (let j = 1; j <= m; j++) {
+ if (j <= i) {
+ dp[i] += dp[i - j];
+ }
+ }
+ }
+ return dp[n];
+};
+```
+
+
+
-----------------------
From cb6f015186ae9d6af13cddf9b929f7bbdfe9bc29 Mon Sep 17 00:00:00 2001
From: cy948 <67412196+cy948@users.noreply.github.com>
Date: Fri, 6 May 2022 14:43:58 +0800
Subject: [PATCH 143/551] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84?=
=?UTF-8?q?=E6=80=BB=E5=92=8C.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
修复了 0113.路径总和-ii 中Java递归解法中的类名大小写问题。
---
...0\267\257\345\276\204\346\200\273\345\222\214.md" | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md"
index 41463ec181..2fdd77417e 100644
--- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md"
+++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md"
@@ -377,22 +377,22 @@ class solution {
```java
class solution {
- public list> pathsum(treenode root, int targetsum) {
- list> res = new arraylist<>();
+ public List> pathsum(TreeNode root, int targetsum) {
+ List> res = new ArrayList<>();
if (root == null) return res; // 非空判断
-
- list path = new linkedlist<>();
+
+ List path = new LinkedList<>();
preorderdfs(root, targetsum, res, path);
return res;
}
- public void preorderdfs(treenode root, int targetsum, list> res, list path) {
+ public void preorderdfs(TreeNode root, int targetsum, List> res, List path) {
path.add(root.val);
// 遇到了叶子节点
if (root.left == null && root.right == null) {
// 找到了和为 targetsum 的路径
if (targetsum - root.val == 0) {
- res.add(new arraylist<>(path));
+ res.add(new ArrayList<>(path));
}
return; // 如果和不为 targetsum,返回
}
From 00b5e2aa64f49ce9273aca6aa8074d98fe4e489a Mon Sep 17 00:00:00 2001
From: eat to 160 pounds <2915390277@qq.com>
Date: Fri, 6 May 2022 15:06:32 +0800
Subject: [PATCH 144/551] =?UTF-8?q?131=E5=88=86=E5=89=B2=E5=AD=97=E7=AC=A6?=
=?UTF-8?q?=E4=B8=B2=E6=9B=B4=E6=B8=85=E6=A5=9A=E7=9A=84typescript?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...62\345\233\236\346\226\207\344\270\262.md" | 41 +++++++++++--------
1 file changed, 23 insertions(+), 18 deletions(-)
diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
index 10b747cb36..7a702898dc 100644
--- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
+++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
@@ -454,31 +454,36 @@ var partition = function(s) {
```typescript
function partition(s: string): string[][] {
- function isPalindromeStr(s: string, left: number, right: number): boolean {
- while (left < right) {
- if (s[left++] !== s[right--]) {
- return false;
+ const res: string[][] = []
+ const path: string[] = []
+ const isHuiwen = (
+ str: string,
+ startIndex: number,
+ endIndex: number
+ ): boolean => {
+ for (; startIndex < endIndex; startIndex++, endIndex--) {
+ if (str[startIndex] !== str[endIndex]) {
+ return false
}
}
- return true;
+ return true
}
- function backTracking(s: string, startIndex: number, route: string[]): void {
- let length: number = s.length;
- if (length === startIndex) {
- resArr.push(route.slice());
- return;
+ const rec = (str: string, index: number): void => {
+ if (index >= str.length) {
+ res.push([...path])
+ return
}
- for (let i = startIndex; i < length; i++) {
- if (isPalindromeStr(s, startIndex, i)) {
- route.push(s.slice(startIndex, i + 1));
- backTracking(s, i + 1, route);
- route.pop();
+ for (let i = index; i < str.length; i++) {
+ if (!isHuiwen(str, index, i)) {
+ continue
}
+ path.push(str.substring(index, i + 1))
+ rec(str, i + 1)
+ path.pop()
}
}
- const resArr: string[][] = [];
- backTracking(s, 0, []);
- return resArr;
+ rec(s, 0)
+ return res
};
```
From 82a58bc20249bb248dbbb0d90771db0ea60bf017 Mon Sep 17 00:00:00 2001
From: Jamcy123 <1219502823@qq.com>
Date: Fri, 6 May 2022 15:49:43 +0800
Subject: [PATCH 145/551] =?UTF-8?q?1005.K=E6=AC=A1=E5=8F=96=E5=8F=8D?=
=?UTF-8?q?=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84=E6=95=B0=E7=BB=84?=
=?UTF-8?q?=E5=92=8C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...2\204\346\225\260\347\273\204\345\222\214.md" | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md"
index 79767debcb..202534da8c 100644
--- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md"
+++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md"
@@ -209,6 +209,22 @@ var largestSumAfterKNegations = function(nums, k) {
return a + b
})
};
+
+// 版本二 (优化: 一次遍历)
+var largestSumAfterKNegations = function(nums, k) {
+ nums.sort((a, b) => Math.abs(b) - Math.abs(a)); // 排序
+ let sum = 0;
+ for(let i = 0; i < nums.length; i++) {
+ if(nums[i] < 0 && k-- > 0) { // 负数取反(k 数量足够时)
+ nums[i] = -nums[i];
+ }
+ sum += nums[i]; // 求和
+ }
+ if(k % 2 > 0) { // k 有多余的(k若消耗完则应为 -1)
+ sum -= 2 * nums[nums.length - 1]; // 减去两倍的最小值(因为之前加过一次)
+ }
+ return sum;
+};
```
From 08ae851e75f735f45f033a0c5455aef198d3cc92 Mon Sep 17 00:00:00 2001
From: wang <472146630@qq.com>
Date: Fri, 6 May 2022 20:45:29 +0800
Subject: [PATCH 146/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A01035.=E4=B8=8D?=
=?UTF-8?q?=E7=9B=B8=E4=BA=A4=E7=9A=84=E7=BA=BF=E5=92=8C1143.=E6=9C=80?=
=?UTF-8?q?=E9=95=BF=E5=85=AC=E5=85=B1=E5=AD=90=E5=BA=8F=E5=88=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...70\344\272\244\347\232\204\347\272\277.md" | 22 ++++-
...61\345\255\220\345\272\217\345\210\227.md" | 80 ++++++++++++-------
2 files changed, 71 insertions(+), 31 deletions(-)
diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md"
index 0602e111f8..279ed8162a 100644
--- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md"
+++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md"
@@ -111,7 +111,6 @@ class Solution:
Golang:
```go
-
func maxUncrossedLines(A []int, B []int) int {
m, n := len(A), len(B)
dp := make([][]int, m+1)
@@ -140,7 +139,26 @@ func max(a, b int) int {
}
```
-
+Rust:
+
+```rust
+pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> i32 {
+ let (n, m) = (nums1.len(), nums2.len());
+ let mut last = vec![0; m + 1]; // 记录滚动数组
+ let mut dp = vec![0; m + 1];
+ for i in 1..=n {
+ dp.swap_with_slice(&mut last);
+ for j in 1..=m {
+ if nums1[i - 1] == nums2[j - 1] {
+ dp[j] = last[j - 1] + 1;
+ } else {
+ dp[j] = last[j].max(dp[j - 1]);
+ }
+ }
+ }
+ dp[m]
+}
+```
JavaScript:
diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md"
index fdcc7619ba..ecedf89b6e 100644
--- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md"
@@ -4,40 +4,40 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
-## 1143.最长公共子序列
+## 1143.最长公共子序列
[力扣题目链接](https://leetcode-cn.com/problems/longest-common-subsequence/)
-给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度。
+给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度。
-一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。
+一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。
-例如,"ace" 是 "abcde" 的子序列,但 "aec" 不是 "abcde" 的子序列。两个字符串的「公共子序列」是这两个字符串所共同拥有的子序列。
+例如,"ace" 是 "abcde" 的子序列,但 "aec" 不是 "abcde" 的子序列。两个字符串的「公共子序列」是这两个字符串所共同拥有的子序列。
-若这两个字符串没有公共子序列,则返回 0。
+若这两个字符串没有公共子序列,则返回 0。
-示例 1:
+示例 1:
-输入:text1 = "abcde", text2 = "ace"
-输出:3
-解释:最长公共子序列是 "ace",它的长度为 3。
+输入:text1 = "abcde", text2 = "ace"
+输出:3
+解释:最长公共子序列是 "ace",它的长度为 3。
-示例 2:
-输入:text1 = "abc", text2 = "abc"
-输出:3
-解释:最长公共子序列是 "abc",它的长度为 3。
+示例 2:
+输入:text1 = "abc", text2 = "abc"
+输出:3
+解释:最长公共子序列是 "abc",它的长度为 3。
-示例 3:
-输入:text1 = "abc", text2 = "def"
-输出:0
-解释:两个字符串没有公共子序列,返回 0。
+示例 3:
+输入:text1 = "abc", text2 = "def"
+输出:0
+解释:两个字符串没有公共子序列,返回 0。
-提示:
+提示:
* 1 <= text1.length <= 1000
* 1 <= text2.length <= 1000
输入的字符串只含有小写英文字符。
-## 思路
+## 思路
本题和[动态规划:718. 最长重复子数组](https://programmercarl.com/0718.最长重复子数组.html)区别在于这里不要求是连续的了,但要有相对顺序,即:"ace" 是 "abcde" 的子序列,但 "aec" 不是 "abcde" 的子序列。
@@ -45,21 +45,21 @@
1. 确定dp数组(dp table)以及下标的含义
-dp[i][j]:长度为[0, i - 1]的字符串text1与长度为[0, j - 1]的字符串text2的最长公共子序列为dp[i][j]
+dp[i][j]:长度为[0, i - 1]的字符串text1与长度为[0, j - 1]的字符串text2的最长公共子序列为dp[i][j]
-有同学会问:为什么要定义长度为[0, i - 1]的字符串text1,定义为长度为[0, i]的字符串text1不香么?
+有同学会问:为什么要定义长度为[0, i - 1]的字符串text1,定义为长度为[0, i]的字符串text1不香么?
这样定义是为了后面代码实现方便,如果非要定义为为长度为[0, i]的字符串text1也可以,大家可以试一试!
2. 确定递推公式
-主要就是两大情况: text1[i - 1] 与 text2[j - 1]相同,text1[i - 1] 与 text2[j - 1]不相同
+主要就是两大情况: text1[i - 1] 与 text2[j - 1]相同,text1[i - 1] 与 text2[j - 1]不相同
-如果text1[i - 1] 与 text2[j - 1]相同,那么找到了一个公共元素,所以dp[i][j] = dp[i - 1][j - 1] + 1;
+如果text1[i - 1] 与 text2[j - 1]相同,那么找到了一个公共元素,所以dp[i][j] = dp[i - 1][j - 1] + 1;
如果text1[i - 1] 与 text2[j - 1]不相同,那就看看text1[0, i - 2]与text2[0, j - 1]的最长公共子序列 和 text1[0, i - 1]与text2[0, j - 2]的最长公共子序列,取最大的。
-即:dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
+即:dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
代码如下:
@@ -71,9 +71,9 @@ if (text1[i - 1] == text2[j - 1]) {
}
```
-3. dp数组如何初始化
+3. dp数组如何初始化
-先看看dp[i][0]应该是多少呢?
+先看看dp[i][0]应该是多少呢?
test1[0, i-1]和空串的最长公共子序列自然是0,所以dp[i][0] = 0;
@@ -101,7 +101,7 @@ vector> dp(text1.size() + 1, vector(text2.size() + 1, 0));

-最后红框dp[text1.size()][text2.size()]为最终结果
+最后红框dp[text1.size()][text2.size()]为最终结果
以上分析完毕,C++代码如下:
@@ -158,7 +158,7 @@ class Solution:
for i in range(1, len2):
for j in range(1, len1): # 开始列出状态转移方程
if text1[j-1] == text2[i-1]:
- dp[i][j] = dp[i-1][j-1]+1
+ dp[i][j] = dp[i-1][j-1]+1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
return dp[-1][-1]
@@ -189,10 +189,32 @@ func longestCommonSubsequence(text1 string, text2 string) int {
func max(a,b int)int {
if a>b{
- return a
+ return a
}
return b
}
+
+```
+
+Rust:
+```rust
+pub fn longest_common_subsequence(text1: String, text2: String) -> i32 {
+ let (n, m) = (text1.len(), text2.len());
+ let (s1, s2) = (text1.as_bytes(), text2.as_bytes());
+ let mut dp = vec![0; m + 1];
+ let mut last = vec![0; m + 1];
+ for i in 1..=n {
+ dp.swap_with_slice(&mut last);
+ for j in 1..=m {
+ dp[j] = if s1[i - 1] == s2[j - 1] {
+ last[j - 1] + 1
+ } else {
+ last[j].max(dp[j - 1])
+ };
+ }
+ }
+ dp[m]
+}
```
Javascript:
From 132e57e2ddd801d7360401e746d945957c7e0801 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=AD=9F=E4=BB=A4=E4=BB=A4?=
Date: Sat, 7 May 2022 09:57:27 +0800
Subject: [PATCH 147/551] =?UTF-8?q?Update=200332.=E9=87=8D=E6=96=B0?=
=?UTF-8?q?=E5=AE=89=E6=8E=92=E8=A1=8C=E7=A8=8B.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
恢复0332 原go代码
---
...11\346\216\222\350\241\214\347\250\213.md" | 58 +++++++++++++++++++
1 file changed, 58 insertions(+)
diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md"
index d087d97bc1..c71b2a9357 100644
--- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md"
+++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md"
@@ -342,6 +342,64 @@ class Solution:
return path
```
+### GO
+```go
+type pair struct {
+ target string
+ visited bool
+}
+type pairs []*pair
+
+func (p pairs) Len() int {
+ return len(p)
+}
+func (p pairs) Swap(i, j int) {
+ p[i], p[j] = p[j], p[i]
+}
+func (p pairs) Less(i, j int) bool {
+ return p[i].target < p[j].target
+}
+
+func findItinerary(tickets [][]string) []string {
+ result := []string{}
+ // map[出发机场] pair{目的地,是否被访问过}
+ targets := make(map[string]pairs)
+ for _, ticket := range tickets {
+ if targets[ticket[0]] == nil {
+ targets[ticket[0]] = make(pairs, 0)
+ }
+ targets[ticket[0]] = append(targets[ticket[0]], &pair{target: ticket[1], visited: false})
+ }
+ for k, _ := range targets {
+ sort.Sort(targets[k])
+ }
+ result = append(result, "JFK")
+ var backtracking func() bool
+ backtracking = func() bool {
+ if len(tickets)+1 == len(result) {
+ return true
+ }
+ // 取出起飞航班对应的目的地
+ for _, pair := range targets[result[len(result)-1]] {
+ if pair.visited == false {
+ result = append(result, pair.target)
+ pair.visited = true
+ if backtracking() {
+ return true
+ }
+ result = result[:len(result)-1]
+ pair.visited = false
+ }
+ }
+ return false
+ }
+
+ backtracking()
+
+ return result
+}
+```
+
### C语言
```C
From 29f41716f971506a001e8eb369b3f62964b43e8e Mon Sep 17 00:00:00 2001
From: GitHubQAQ <31883473+GitHubQAQ@users.noreply.github.com>
Date: Sat, 7 May 2022 10:12:58 +0800
Subject: [PATCH 148/551] =?UTF-8?q?Update=200093.=E5=A4=8D=E5=8E=9FIP?=
=?UTF-8?q?=E5=9C=B0=E5=9D=80.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
1. 优化C++版本剪枝
当字符串长度小于4时,无法组成正确的IP地址,直接返回。
关于判断是否为数字的部分,是否有必要(题目明确说明s仅由数字组成)
[从解题角度可以删去,保留的话增加容错,个人意见]
---
.../0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
index 7910fc50cc..6401824b47 100644
--- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
+++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
@@ -227,7 +227,7 @@ private:
public:
vector restoreIpAddresses(string s) {
result.clear();
- if (s.size() > 12) return result; // 算是剪枝了
+ if (s.size() < 4 || s.size() > 12) return result; // 算是剪枝了
backtracking(s, 0, 0);
return result;
}
From da464016574f14c43fe1e9f7c0d808ed7ea304fb Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 8 May 2022 10:29:33 +0800
Subject: [PATCH 149/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880322.?=
=?UTF-8?q?=E9=9B=B6=E9=92=B1=E5=85=91=E6=8D=A2.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...233\266\351\222\261\345\205\221\346\215\242.md" | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git "a/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" "b/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md"
index 3a8d0662ab..1df8d61339 100644
--- "a/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md"
+++ "b/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md"
@@ -322,7 +322,21 @@ const coinChange = (coins, amount) => {
}
```
+TypeScript:
+```typescript
+function coinChange(coins: number[], amount: number): number {
+ const dp: number[] = new Array(amount + 1).fill(Infinity);
+ dp[0] = 0;
+ for (let i = 0; i < coins.length; i++) {
+ for (let j = coins[i]; j <= amount; j++) {
+ if (dp[j - coins[i]] === Infinity) continue;
+ dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1);
+ }
+ }
+ return dp[amount] === Infinity ? -1 : dp[amount];
+};
+```
-----------------------
From 2722fe7b5eceec7d746f94a63683430205839469 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 8 May 2022 11:07:39 +0800
Subject: [PATCH 150/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880279.?=
=?UTF-8?q?=E5=AE=8C=E5=85=A8=E5=B9=B3=E6=96=B9=E6=95=B0.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...50\345\271\263\346\226\271\346\225\260.md" | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md"
index 9bad2085e2..5b15639c06 100644
--- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md"
+++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md"
@@ -355,5 +355,24 @@ var numSquares2 = function(n) {
};
```
+TypeScript:
+
+```typescript
+function numSquares(n: number): number {
+ const goodsNum: number = Math.floor(Math.sqrt(n));
+ const dp: number[] = new Array(n + 1).fill(Infinity);
+ dp[0] = 0;
+ for (let i = 1; i <= goodsNum; i++) {
+ const tempVal: number = i * i;
+ for (let j = tempVal; j <= n; j++) {
+ dp[j] = Math.min(dp[j], dp[j - tempVal] + 1);
+ }
+ }
+ return dp[n];
+};
+```
+
+
+
-----------------------
From 9b6a447674f3290541e46088962e1e3aa764d827 Mon Sep 17 00:00:00 2001
From: Hayden-Chang <62008508+Hayden-Chang@users.noreply.github.com>
Date: Sun, 8 May 2022 14:20:09 +0800
Subject: [PATCH 151/551] explain the reason for reverse traversal
---
...6\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" | 1 +
1 file changed, 1 insertion(+)
diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md"
index dabdfb2d09..ea7c53ad0d 100644
--- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md"
+++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md"
@@ -136,6 +136,7 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15
不可以!
因为一维dp的写法,背包容量一定是要倒序遍历(原因上面已经讲了),如果遍历背包容量放在上一层,那么每个dp[j]就只会放入一个物品,即:背包里只放入了一个物品。
+倒叙遍历的原因是,本质上还是一个对二维数组的遍历,并且右下角的值依赖上一层左上角的值,因此需要保证左边的值仍然是上一层的,从右向左覆盖。
(这里如果读不懂,就在回想一下dp[j]的定义,或者就把两个for循环顺序颠倒一下试试!)
From 80167289e4895c4116b61a5c2a4fa22e9e2e88b5 Mon Sep 17 00:00:00 2001
From: Hayden-Chang <62008508+Hayden-Chang@users.noreply.github.com>
Date: Sun, 8 May 2022 14:24:09 +0800
Subject: [PATCH 152/551] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?=
=?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-2.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md"
index ea7c53ad0d..eae01158a9 100644
--- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md"
+++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md"
@@ -136,7 +136,8 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15
不可以!
因为一维dp的写法,背包容量一定是要倒序遍历(原因上面已经讲了),如果遍历背包容量放在上一层,那么每个dp[j]就只会放入一个物品,即:背包里只放入了一个物品。
-倒叙遍历的原因是,本质上还是一个对二维数组的遍历,并且右下角的值依赖上一层左上角的值,因此需要保证左边的值仍然是上一层的,从右向左覆盖。
+
+倒序遍历的原因是,本质上还是一个对二维数组的遍历,并且右下角的值依赖上一层左上角的值,因此需要保证左边的值仍然是上一层的,从右向左覆盖。
(这里如果读不懂,就在回想一下dp[j]的定义,或者就把两个for循环顺序颠倒一下试试!)
From 2fb34b30b38ee73624d65d183955cef3a80caced Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 8 May 2022 18:28:32 +0800
Subject: [PATCH 153/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880139.?=
=?UTF-8?q?=E5=8D=95=E8=AF=8D=E6=8B=86=E5=88=86.md=EF=BC=89:=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...25\350\257\215\346\213\206\345\210\206.md" | 42 +++++++++++++++++++
1 file changed, 42 insertions(+)
diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md"
index ac834f047d..5b4e92b9aa 100644
--- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md"
+++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md"
@@ -345,6 +345,48 @@ const wordBreak = (s, wordDict) => {
}
```
+TypeScript:
+
+> 动态规划
+
+```typescript
+function wordBreak(s: string, wordDict: string[]): boolean {
+ const dp: boolean[] = new Array(s.length + 1).fill(false);
+ dp[0] = true;
+ for (let i = 1; i <= s.length; i++) {
+ for (let j = 0; j < i; j++) {
+ const tempStr: string = s.slice(j, i);
+ if (wordDict.includes(tempStr) && dp[j] === true) {
+ dp[i] = true;
+ break;
+ }
+ }
+ }
+ return dp[s.length];
+};
+```
+
+> 记忆化回溯
+
+```typescript
+function wordBreak(s: string, wordDict: string[]): boolean {
+ // 只需要记忆结果为false的情况
+ const memory: boolean[] = [];
+ return backTracking(s, wordDict, 0, memory);
+ function backTracking(s: string, wordDict: string[], startIndex: number, memory: boolean[]): boolean {
+ if (startIndex >= s.length) return true;
+ if (memory[startIndex] === false) return false;
+ for (let i = startIndex + 1, length = s.length; i <= length; i++) {
+ const str: string = s.slice(startIndex, i);
+ if (wordDict.includes(str) && backTracking(s, wordDict, i, memory))
+ return true;
+ }
+ memory[startIndex] = false;
+ return false;
+ }
+};
+```
+
-----------------------
From 21475ad21864ce33fb5a6a113581f2c346be78b6 Mon Sep 17 00:00:00 2001
From: languagege
Date: Mon, 9 May 2022 18:51:39 +0800
Subject: [PATCH 154/551] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BA=86=E5=85=B6?=
=?UTF-8?q?=E4=B8=AD=E4=B8=80=E4=B8=AA=E9=94=99=E5=88=AB=E5=AD=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
index 590cf0b924..3a93ac8820 100644
--- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
+++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
@@ -81,7 +81,7 @@ public:
**双指针法(快慢指针法)在数组和链表的操作中是非常常见的,很多考察数组、链表、字符串等操作的面试题,都使用双指针法。**
-后序都会一一介绍到,本题代码如下:
+后续都会一一介绍到,本题代码如下:
```CPP
// 时间复杂度:O(n)
From 75198263f91484020c4a4dde421d9ecd989422dd Mon Sep 17 00:00:00 2001
From: changjunkui <506678275@qq.com>
Date: Tue, 10 May 2022 08:16:53 +0800
Subject: [PATCH 155/551] =?UTF-8?q?Update=200019.=E5=88=A0=E9=99=A4?=
=?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN?=
=?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
方面改为方便
---
...225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md"
index 813e9b02fa..b3030a81d1 100644
--- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md"
+++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md"
@@ -39,7 +39,7 @@
分为如下几步:
-* 首先这里我推荐大家使用虚拟头结点,这样方面处理删除实际头结点的逻辑,如果虚拟头结点不清楚,可以看这篇: [链表:听说用虚拟头节点会方便很多?](https://programmercarl.com/0203.移除链表元素.html)
+* 首先这里我推荐大家使用虚拟头结点,这样方便处理删除实际头结点的逻辑,如果虚拟头结点不清楚,可以看这篇: [链表:听说用虚拟头节点会方便很多?](https://programmercarl.com/0203.移除链表元素.html)
* 定义fast指针和slow指针,初始值为虚拟头结点,如图:
From 8bab33d0bba8a080fb614bec6be3b0a5961d7727 Mon Sep 17 00:00:00 2001
From: wang <472146630@qq.com>
Date: Tue, 10 May 2022 21:27:54 +0800
Subject: [PATCH 156/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200300.=E6=9C=80?=
=?UTF-8?q?=E9=95=BF=E4=B8=8A=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97=E3=80=81?=
=?UTF-8?q?0322.=E9=9B=B6=E9=92=B1=E5=85=91=E6=8D=A2=E3=80=810518.?=
=?UTF-8?q?=E9=9B=B6=E9=92=B1=E5=85=91=E6=8D=A2II=20=E5=92=8C0674.?=
=?UTF-8?q?=E6=9C=80=E9=95=BF=E8=BF=9E=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F?=
=?UTF-8?q?=E5=88=97=E7=9A=84rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...07\345\255\220\345\272\217\345\210\227.md" | 17 ++++++++++++++++
...66\351\222\261\345\205\221\346\215\242.md" | 20 ++++++++++++++++++-
...\351\222\261\345\205\221\346\215\242II.md" | 16 +++++++++++++++
...22\345\242\236\345\272\217\345\210\227.md" | 19 ++++++++++++++++++
4 files changed, 71 insertions(+), 1 deletion(-)
diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md"
index dfdd51257c..f53d19a1ce 100644
--- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md"
@@ -168,6 +168,23 @@ func lengthOfLIS(nums []int ) int {
}
```
+Rust:
+```rust
+pub fn length_of_lis(nums: Vec) -> i32 {
+ let mut dp = vec![1; nums.len() + 1];
+ let mut result = 1;
+ for i in 1..nums.len() {
+ for j in 0..i {
+ if nums[j] < nums[i] {
+ dp[i] = dp[i].max(dp[j] + 1);
+ }
+ result = result.max(dp[i]);
+ }
+ }
+ result
+}
+```
+
Javascript
```javascript
const lengthOfLIS = (nums) => {
diff --git "a/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" "b/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md"
index 3a8d0662ab..43c735be47 100644
--- "a/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md"
+++ "b/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md"
@@ -220,7 +220,7 @@ class Solution:
for j in range(coin, amount + 1):
dp[j] = min(dp[j], dp[j - coin] + 1)
return dp[amount] if dp[amount] < amount + 1 else -1
-
+
def coinChange1(self, coins: List[int], amount: int) -> int:
'''版本二'''
# 初始化
@@ -302,6 +302,24 @@ func min(a, b int) int {
```
+Rust:
+
+```rust
+pub fn coin_change(coins: Vec, amount: i32) -> i32 {
+ let amount = amount as usize;
+ let mut dp = vec![i32::MAX; amount + 1];
+ dp[0] = 0;
+ for i in 0..coins.len() {
+ for j in coins[i] as usize..=amount {
+ if dp[j - coins[i] as usize] != i32::MAX {
+ dp[j] = dp[j].min(dp[j - coins[i] as usize] + 1);
+ }
+ }
+ }
+ if dp[amount] == i32::MAX { -1 } else { dp[amount] }
+}
+```
+
Javascript:
```javascript
const coinChange = (coins, amount) => {
diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md"
index e72c5f8515..0e4a398719 100644
--- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md"
+++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md"
@@ -242,6 +242,22 @@ func change(amount int, coins []int) int {
}
```
+Rust:
+```rust
+pub fn change(amount: i32, coins: Vec) -> i32 {
+ let amount = amount as usize;
+ let coins = coins.iter().map(|&c|c as usize).collect::>();
+ let mut dp = vec![0usize; amount + 1];
+ dp[0] = 1;
+ for i in 0..coins.len() {
+ for j in coins[i]..=amount {
+ dp[j] += dp[j - coins[i]];
+ }
+ }
+ dp[amount] as i32
+}
+```
+
Javascript:
```javascript
const change = (amount, coins) => {
diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
index e941d24263..3647149009 100644
--- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
+++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
@@ -218,6 +218,7 @@ class Solution:
return result
```
+
> 贪心法:
```python
class Solution:
@@ -237,6 +238,24 @@ class Solution:
Go:
+Rust:
+```rust
+pub fn find_length_of_lcis(nums: Vec) -> i32 {
+ if nums.is_empty() {
+ return 0;
+ }
+ let mut result = 1;
+ let mut dp = vec![1; nums.len()];
+ for i in 1..nums.len() {
+ if nums[i - 1] < nums[i] {
+ dp[i] = dp[i - 1] + 1;
+ result = result.max(dp[i]);
+ }
+ }
+ result
+}
+```
+
Javascript:
> 动态规划:
From c62d518e149295783d219ec1871da84af8e5546e Mon Sep 17 00:00:00 2001
From: FizzerYu <36132150+FizzerYu@users.noreply.github.com>
Date: Wed, 11 May 2022 01:17:08 +0800
Subject: [PATCH 157/551] fix bug
---
...\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md"
index df6a395444..50e39ade5e 100644
--- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md"
+++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md"
@@ -279,7 +279,7 @@ class Solution:
root.right = self.insertIntoBST(root.right, val)
# 返回更新后的以当前root为根节点的新树
- return roo
+ return root
```
**递归法** - 无返回值
From 0e5e657a6d5ba057e8dfa77019b25c5404fe9a9b Mon Sep 17 00:00:00 2001
From: 243wresfdxvc
Date: Tue, 10 May 2022 23:04:35 +0000
Subject: [PATCH 158/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200404.=E5=B7=A6?=
=?UTF-8?q?=E5=8F=B6=E5=AD=90=E4=B9=8B=E5=92=8C.md=20C=E8=AF=AD=E8=A8=80?=
=?UTF-8?q?=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...66\345\255\220\344\271\213\345\222\214.md" | 49 +++++++++++++++++++
1 file changed, 49 insertions(+)
diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md"
index 6420da819d..d7fd629e68 100644
--- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md"
+++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md"
@@ -466,6 +466,55 @@ func sumOfLeftLeaves(_ root: TreeNode?) -> Int {
}
```
+## C
+递归法:
+```c
+int sumOfLeftLeaves(struct TreeNode* root){
+ // 递归结束条件:若当前结点为空,返回0
+ if(!root)
+ return 0;
+
+ // 递归取左子树的左结点和和右子树的左结点和
+ int leftValue = sumOfLeftLeaves(root->left);
+ int rightValue = sumOfLeftLeaves(root->right);
+
+ // 若当前结点的左结点存在,且其为叶子结点。取它的值
+ int midValue = 0;
+ if(root->left && (!root->left->left && !root->left->right))
+ midValue = root->left->val;
+
+ return leftValue + rightValue + midValue;
+}
+```
+
+迭代法:
+```c
+int sumOfLeftLeaves(struct TreeNode* root){
+ struct TreeNode* stack[1000];
+ int stackTop = 0;
+
+ // 若传入root结点不为空,将其入栈
+ if(root)
+ stack[stackTop++] = root;
+
+ int sum = 0;
+ //若栈不为空,进行循环
+ while(stackTop) {
+ // 出栈栈顶元素
+ struct TreeNode *topNode = stack[--stackTop];
+ // 若栈顶元素的左孩子为左叶子结点,将其值加入sum中
+ if(topNode->left && (!topNode->left->left && !topNode->left->right))
+ sum += topNode->left->val;
+
+ // 若当前栈顶结点有左右孩子。将他们加入栈中进行遍历
+ if(topNode->right)
+ stack[stackTop++] = topNode->right;
+ if(topNode->left)
+ stack[stackTop++] = topNode->left;
+ }
+ return sum;
+}
+```
-----------------------
From d92aa2c52f0589cca9dbf1a7624f9312395b63fe Mon Sep 17 00:00:00 2001
From: unknown
Date: Wed, 11 May 2022 00:39:59 +0100
Subject: [PATCH 159/551] =?UTF-8?q?Add=200112.=E8=B7=AF=E5=BE=84=E6=80=BB?=
=?UTF-8?q?=E5=92=8C.md=20C=E8=AF=AD=E8=A8=80=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...57\345\276\204\346\200\273\345\222\214.md" | 57 +++++++++++++++++++
1 file changed, 57 insertions(+)
diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md"
index 41463ec181..6433996c57 100644
--- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md"
+++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md"
@@ -1006,6 +1006,63 @@ func traversal(_ cur: TreeNode?, count: Int) {
}
```
+## C
+0112.路径总和
+递归法:
+```c
+bool hasPathSum(struct TreeNode* root, int targetSum){
+ // 递归结束条件:若当前节点不存在,返回false
+ if(!root)
+ return false;
+ // 若当前节点为叶子节点,且targetSum-root的值为0。(当前路径上的节点值的和满足条件)返回true
+ if(!root->right && !root->left && targetSum == root->val)
+ return true;
+
+ // 查看左子树和右子树的所有节点是否满足条件
+ return hasPathSum(root->right, targetSum - root->val) || hasPathSum(root->left, targetSum - root->val);
+}
+```
+
+迭代法:
+```c
+// 存储一个节点以及当前的和
+struct Pair {
+ struct TreeNode* node;
+ int sum;
+};
+
+bool hasPathSum(struct TreeNode* root, int targetSum){
+ struct Pair stack[1000];
+ int stackTop = 0;
+
+ // 若root存在,则将节点和值封装成一个pair入栈
+ if(root) {
+ struct Pair newPair = {root, root->val};
+ stack[stackTop++] = newPair;
+ }
+
+ // 当栈不为空时
+ while(stackTop) {
+ // 出栈栈顶元素
+ struct Pair topPair = stack[--stackTop];
+ // 若栈顶元素为叶子节点,且和为targetSum时,返回true
+ if(!topPair.node->left && !topPair.node->right && topPair.sum == targetSum)
+ return true;
+
+ // 若当前栈顶节点有左右孩子,计算和并入栈
+ if(topPair.node->left) {
+ struct Pair newPair = {topPair.node->left, topPair.sum + topPair.node->left->val};
+ stack[stackTop++] = newPair;
+ }
+ if(topPair.node->right) {
+ struct Pair newPair = {topPair.node->right, topPair.sum + topPair.node->right->val};
+ stack[stackTop++] = newPair;
+ }
+ }
+ return false;
+}
+```
+
-----------------------
From cc2c2adb0987a5e2e82eed75a93be068da6388a4 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 11 May 2022 16:03:22 +0800
Subject: [PATCH 160/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200977.=E6=9C=89?=
=?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\347\232\204\345\271\263\346\226\271.md" | 36 ++++++++++++++++++-
1 file changed, 35 insertions(+), 1 deletion(-)
diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
index 24276bcff0..0e79a3d654 100644
--- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
+++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
@@ -358,7 +358,41 @@ class Solution {
}
}
```
-
+Scala:
+
+双指针:
+```scala
+object Solution {
+ def sortedSquares(nums: Array[Int]): Array[Int] = {
+ val res: Array[Int] = new Array[Int](nums.length)
+ var top = nums.length - 1
+ var i = 0
+ var j = nums.length - 1
+ while (i <= j) {
+ if (nums(i) * nums(i) <= nums(j) * nums(j)) {
+ // 当左侧平方小于等于右侧,res数组顶部放右侧的平方,并且top下移,j左移
+ res(top) = nums(j) * nums(j)
+ top -= 1
+ j -= 1
+ } else {
+ // 当左侧平方大于右侧,res数组顶部放左侧的平方,并且top下移,i右移
+ res(top) = nums(i) * nums(i)
+ top -= 1
+ i += 1
+ }
+ }
+ res
+ }
+}
+```
+骚操作(暴力思路):
+```scala
+object Solution {
+ def sortedSquares(nums: Array[Int]): Array[Int] = {
+ nums.map(x=>{x*x}).sortWith(_ < _)
+ }
+}
+```
-----------------------
From 842c04208b163c4782fb7070d949f5feb0c01bee Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Wed, 11 May 2022 16:35:58 +0800
Subject: [PATCH 161/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E8=83=8C?=
=?UTF-8?q?=E5=8C=85=E9=97=AE=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80?=
=?UTF-8?q?=E5=A4=9A=E9=87=8D=E8=83=8C=E5=8C=85.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...32\351\207\215\350\203\214\345\214\205.md" | 58 +++++++++++++++++++
1 file changed, 58 insertions(+)
diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md"
index a988db2c82..712380f4f4 100644
--- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md"
+++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md"
@@ -334,6 +334,64 @@ func Test_multiplePack(t *testing.T) {
PASS
```
+TypeScript:
+
+> 版本一(改变数据源):
+
+```typescript
+function testMultiPack() {
+ const bagSize: number = 10;
+ const weightArr: number[] = [1, 3, 4],
+ valueArr: number[] = [15, 20, 30],
+ amountArr: number[] = [2, 3, 2];
+ for (let i = 0, length = amountArr.length; i < length; i++) {
+ while (amountArr[i] > 1) {
+ weightArr.push(weightArr[i]);
+ valueArr.push(valueArr[i]);
+ amountArr[i]--;
+ }
+ }
+ const goodsNum: number = weightArr.length;
+ const dp: number[] = new Array(bagSize + 1).fill(0);
+ // 遍历物品
+ for (let i = 0; i < goodsNum; i++) {
+ // 遍历背包容量
+ for (let j = bagSize; j >= weightArr[i]; j--) {
+ dp[j] = Math.max(dp[j], dp[j - weightArr[i]] + valueArr[i]);
+ }
+ }
+ console.log(dp);
+}
+testMultiPack();
+```
+
+> 版本二(改变遍历方式):
+
+```typescript
+function testMultiPack() {
+ const bagSize: number = 10;
+ const weightArr: number[] = [1, 3, 4],
+ valueArr: number[] = [15, 20, 30],
+ amountArr: number[] = [2, 3, 2];
+ const goodsNum: number = weightArr.length;
+ const dp: number[] = new Array(bagSize + 1).fill(0);
+ // 遍历物品
+ for (let i = 0; i < goodsNum; i++) {
+ // 遍历物品个数
+ for (let j = 0; j < amountArr[i]; j++) {
+ // 遍历背包容量
+ for (let k = bagSize; k >= weightArr[i]; k--) {
+ dp[k] = Math.max(dp[k], dp[k - weightArr[i]] + valueArr[i]);
+ }
+ }
+ }
+ console.log(dp);
+}
+testMultiPack();
+```
+
+
+
-----------------------
From 871d96a2f9202504f2f5c754da7404f602a9f073 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 11 May 2022 16:48:59 +0800
Subject: [PATCH 162/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200209.=E9=95=BF?=
=?UTF-8?q?=E5=BA=A6=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\345\255\220\346\225\260\347\273\204.md" | 48 +++++++++++++++++++
1 file changed, 48 insertions(+)
diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
index fd72cf1b3a..78b8156cdd 100644
--- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
+++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
@@ -400,6 +400,54 @@ class Solution {
}
}
```
+Scala:
+
+滑动窗口:
+```scala
+object Solution {
+ def minSubArrayLen(target: Int, nums: Array[Int]): Int = {
+ var result = Int.MaxValue // 返回结果,默认最大值
+ var left = 0 // 慢指针,当sum>=target,向右移动
+ var sum = 0 // 窗口值的总和
+ for (right <- 0 until nums.length) {
+ sum += nums(right)
+ while (sum >= target) {
+ result = math.min(result, right - left + 1) // 产生新结果
+ sum -= nums(left) // 左指针移动,窗口总和减去左指针的值
+ left += 1 // 左指针向右移动
+ }
+ }
+ // 相当于三元运算符,return关键字可以省略
+ if (result == Int.MaxValue) 0 else result
+ }
+}
+```
+
+暴力解法:
+```scala
+object Solution {
+ def minSubArrayLen(target: Int, nums: Array[Int]): Int = {
+ import scala.util.control.Breaks
+ var res = Int.MaxValue
+ var subLength = 0
+ for (i <- 0 until nums.length) {
+ var sum = 0
+ Breaks.breakable(
+ for (j <- i until nums.length) {
+ sum += nums(j)
+ if (sum >= target) {
+ subLength = j - i + 1
+ res = math.min(subLength, res)
+ Breaks.break()
+ }
+ }
+ )
+ }
+ // 相当于三元运算符
+ if (res == Int.MaxValue) 0 else res
+ }
+}
+```
-----------------------
From cb2fea63e7f5e616f697a715b9d523b6ba877308 Mon Sep 17 00:00:00 2001
From: unknown
Date: Wed, 11 May 2022 09:50:47 +0100
Subject: [PATCH 163/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200113.=E8=B7=AF?=
=?UTF-8?q?=E5=BE=84=E6=80=BB=E5=92=8CII=20C=E8=AF=AD=E8=A8=80=E8=A7=A3?=
=?UTF-8?q?=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...57\345\276\204\346\200\273\345\222\214.md" | 65 ++++++++++++++++++-
1 file changed, 64 insertions(+), 1 deletion(-)
diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md"
index 6433996c57..de155b4524 100644
--- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md"
+++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md"
@@ -1007,7 +1007,7 @@ func traversal(_ cur: TreeNode?, count: Int) {
```
## C
-0112.路径总和
+> 0112.路径总和
递归法:
```c
bool hasPathSum(struct TreeNode* root, int targetSum){
@@ -1062,6 +1062,69 @@ bool hasPathSum(struct TreeNode* root, int targetSum){
return false;
}
```
+> 0113.路径总和 II
+```c
+int** ret;
+int* path;
+int* colSize;
+int retTop;
+int pathTop;
+
+void traversal(const struct TreeNode* const node, int count) {
+ // 若当前节点为叶子节点
+ if(!node->right && !node->left) {
+ // 若当前path上的节点值总和等于targetSum。
+ if(count == 0) {
+ // 复制当前path
+ int *curPath = (int*)malloc(sizeof(int) * pathTop);
+ memcpy(curPath, path, sizeof(int) * pathTop);
+ // 记录当前path的长度为pathTop
+ colSize[retTop] = pathTop;
+ // 将当前path加入到ret数组中
+ ret[retTop++] = curPath;
+ }
+ return;
+ }
+
+ // 若节点有左/右孩子
+ if(node->left) {
+ // 将左孩子的值加入path中
+ path[pathTop++] = node->left->val;
+ traversal(node->left, count - node->left->val);
+ // 回溯
+ pathTop--;
+ }
+ if(node->right) {
+ // 将右孩子的值加入path中
+ path[pathTop++] = node->right->val;
+ traversal(node->right, count - node->right->val);
+ // 回溯
+ --pathTop;
+ }
+}
+
+int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** returnColumnSizes){
+ // 初始化数组
+ ret = (int**)malloc(sizeof(int*) * 1000);
+ path = (int*)malloc(sizeof(int*) * 1000);
+ colSize = (int*)malloc(sizeof(int) * 1000);
+ retTop = pathTop = 0;
+ *returnSize = 0;
+
+ // 若根节点不存在,返回空的ret
+ if(!root)
+ return ret;
+ // 将根节点加入到path中
+ path[pathTop++] = root->val;
+ traversal(root, targetSum - root->val);
+
+ // 设置返回ret数组大小,以及其中每个一维数组元素的长度
+ *returnSize = retTop;
+ *returnColumnSizes = colSize;
+
+ return ret;
+}
+```
From c363e9da86973e5dafaa5e765c9c6318b5eb9723 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Wed, 11 May 2022 16:59:05 +0800
Subject: [PATCH 164/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880198.?=
=?UTF-8?q?=E6=89=93=E5=AE=B6=E5=8A=AB=E8=88=8D.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...23\345\256\266\345\212\253\350\210\215.md" | 23 +++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md"
index dfe1f3a062..a828b9a9d6 100644
--- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md"
+++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md"
@@ -189,6 +189,29 @@ const rob = nums => {
};
```
+TypeScript:
+
+```typescript
+function rob(nums: number[]): number {
+ /**
+ dp[i]: 前i个房屋能偷到的最大金额
+ dp[0]: nums[0];
+ dp[1]: max(nums[0], nums[1]);
+ ...
+ dp[i]: max(dp[i-1], dp[i-2]+nums[i]);
+ */
+ const length: number = nums.length;
+ if (length === 1) return nums[0];
+ const dp: number[] = [];
+ dp[0] = nums[0];
+ dp[1] = Math.max(nums[0], nums[1]);
+ for (let i = 2; i < length; i++) {
+ dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
+ }
+ return dp[length - 1];
+};
+```
+
From a8cfc460e7143e6b6d8486ad9e8d88e1c9759211 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 11 May 2022 17:37:12 +0800
Subject: [PATCH 165/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200059.=E8=9E=BA?=
=?UTF-8?q?=E6=97=8B=E7=9F=A9=E9=98=B5II.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\346\227\213\347\237\251\351\230\265II.md" | 53 ++++++++++++++++++-
1 file changed, 52 insertions(+), 1 deletion(-)
diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
index a7b19a34fe..f3b8e1ce24 100644
--- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
+++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
@@ -564,6 +564,57 @@ int** generateMatrix(int n, int* returnSize, int** returnColumnSizes){
return ans;
}
```
-
+Scala:
+```scala
+object Solution {
+ def generateMatrix(n: Int): Array[Array[Int]] = {
+ var res = Array.ofDim[Int](n, n) // 定义一个n*n的二维矩阵
+ var num = 1 // 标志当前到了哪个数字
+ var i = 0 // 横坐标
+ var j = 0 // 竖坐标
+
+ while (num <= n * n) {
+ // 向右:当j不越界,并且下一个要填的数字是空白时
+ while (j < n && res(i)(j) == 0) {
+ res(i)(j) = num // 当前坐标等于num
+ num += 1 // num++
+ j += 1 // 竖坐标+1
+ }
+ i += 1 // 下移一行
+ j -= 1 // 左移一列
+
+ // 剩下的都同上
+
+ // 向下
+ while (i < n && res(i)(j) == 0) {
+ res(i)(j) = num
+ num += 1
+ i += 1
+ }
+ i -= 1
+ j -= 1
+
+ // 向左
+ while (j >= 0 && res(i)(j) == 0) {
+ res(i)(j) = num
+ num += 1
+ j -= 1
+ }
+ i -= 1
+ j += 1
+
+ // 向上
+ while (i >= 0 && res(i)(j) == 0) {
+ res(i)(j) = num
+ num += 1
+ i -= 1
+ }
+ i += 1
+ j += 1
+ }
+ res
+ }
+}
+```
-----------------------
From 296485551542d296e27486e985406d7944f3aa9a Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Wed, 11 May 2022 18:35:43 +0800
Subject: [PATCH 166/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880213.?=
=?UTF-8?q?=E6=89=93=E5=AE=B6=E5=8A=AB=E8=88=8DII.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\345\256\266\345\212\253\350\210\215II.md" | 23 +++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md"
index 8e569e46cf..9e698d01c0 100644
--- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md"
+++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md"
@@ -165,7 +165,30 @@ const robRange = (nums, start, end) => {
return dp[end]
}
```
+TypeScript:
+
+```typescript
+function rob(nums: number[]): number {
+ const length: number = nums.length;
+ if (length === 0) return 0;
+ if (length === 1) return nums[0];
+ return Math.max(robRange(nums, 0, length - 2),
+ robRange(nums, 1, length - 1));
+};
+function robRange(nums: number[], start: number, end: number): number {
+ if (start === end) return nums[start];
+ const dp: number[] = [];
+ dp[start] = nums[start];
+ dp[start + 1] = Math.max(nums[start], nums[start + 1]);
+ for (let i = start + 2; i <= end; i++) {
+ dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
+ }
+ return dp[end];
+}
+```
+
Go:
+
```go
// 打家劫舍Ⅱ 动态规划
// 时间复杂度O(n) 空间复杂度O(n)
From 558dc3343150b1a91a1f307c0bb1fcb881abcd20 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 11 May 2022 19:11:53 +0800
Subject: [PATCH 167/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E9=93=BE=E8=A1=A8?=
=?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...250\347\220\206\350\256\272\345\237\272\347\241\200.md" | 7 +++++++
1 file changed, 7 insertions(+)
diff --git "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md"
index 095282f54f..9dddf1e232 100644
--- "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md"
+++ "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md"
@@ -210,6 +210,13 @@ type ListNode struct {
}
```
+Scala:
+```scala
+class ListNode(_x: Int = 0, _next: ListNode = null) {
+ var next: ListNode = _next
+ var x: Int = _x
+}
+```
-----------------------
From 8a562b7a2511b44705a6d9e28388738d6a452848 Mon Sep 17 00:00:00 2001
From: UndeadSheep
Date: Wed, 11 May 2022 19:44:09 +0800
Subject: [PATCH 168/551] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E5=93=88=E5=B8=8C?=
=?UTF-8?q?=E8=A1=A8=E9=83=A8=E5=88=86=E7=9A=84=20C#=E7=89=88?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...44\346\225\260\344\271\213\345\222\214.md" | 18 +++++++++++++
...2.\345\277\253\344\271\220\346\225\260.md" | 23 ++++++++++++++++
...15\345\274\202\344\275\215\350\257\215.md" | 19 +++++++++++++
...04\347\232\204\344\272\244\351\233\206.md" | 18 +++++++++++++
...3.\350\265\216\351\207\221\344\277\241.md" | 17 ++++++++++++
...\346\225\260\347\233\270\345\212\240II.md" | 27 +++++++++++++++++++
6 files changed, 122 insertions(+)
diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md"
index 9571a7737b..459a66ad8c 100644
--- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md"
@@ -274,6 +274,24 @@ class Solution {
}
}
```
+C#:
+```csharp
+public class Solution {
+ public int[] TwoSum(int[] nums, int target) {
+ Dictionary dic= new Dictionary();
+ for(int i=0;i
diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md"
index 741a735a5d..51a79aff96 100644
--- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md"
+++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md"
@@ -385,5 +385,28 @@ bool isHappy(int n){
return bHappy;
}
```
+
+C#:
+```csharp
+public class Solution {
+ private int getSum(int n) {
+ int sum = 0;
+ //每位数的换算
+ while (n > 0) {
+ sum += (n % 10) * (n % 10);
+ n /= 10;
+ }
+ return sum;
+ }
+ public bool IsHappy(int n) {
+ HashSet set = new HashSet();
+ while(n != 1 && !set.Contains(n)) { //判断避免循环
+ set.Add(n);
+ n = getSum(n);
+ }
+ return n == 1;
+ }
+}
+```
-----------------------
diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md"
index 080166fd34..878b24662c 100644
--- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md"
+++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md"
@@ -307,6 +307,25 @@ impl Solution {
}
}
```
+
+C#:
+```csharp
+ public bool IsAnagram(string s, string t) {
+ int sl=s.Length,tl=t.Length;
+ if(sl!=tl) return false;
+ int[] a = new int[26];
+ for(int i = 0; i < sl; i++){
+ a[s[i] - 'a']++;
+ a[t[i] - 'a']--;
+ }
+ foreach (int i in a)
+ {
+ if (i != 0)
+ return false;
+ }
+ return true;
+ }
+```
## 相关题目
* 383.赎金信
diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
index 45f19b6ef2..7f8958d232 100644
--- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
+++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
@@ -313,6 +313,24 @@ int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* re
}
```
+C#:
+```csharp
+ public int[] Intersection(int[] nums1, int[] nums2) {
+ if(nums1==null||nums1.Length==0||nums2==null||nums1.Length==0)
+ return new int[0]; //注意数组条件
+ HashSet one = Insert(nums1);
+ HashSet two = Insert(nums2);
+ one.IntersectWith(two);
+ return one.ToArray();
+ }
+ public HashSet Insert(int[] nums){
+ HashSet one = new HashSet();
+ foreach(int num in nums){
+ one.Add(num);
+ }
+ return one;
+ }
+```
## 相关题目
* 350.两个数组的交集 II
diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md"
index 5d9e8295c6..6d6efc552b 100644
--- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md"
+++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md"
@@ -361,5 +361,22 @@ impl Solution {
}
```
+C#:
+```csharp
+public bool CanConstruct(string ransomNote, string magazine) {
+ if(ransomNote.Length > magazine.Length) return false;
+ int[] letters = new int[26];
+ foreach(char c in magazine){
+ letters[c-'a']++;
+ }
+ foreach(char c in ransomNote){
+ letters[c-'a']--;
+ if(letters[c-'a']<0){
+ return false;
+ }
+ }
+ return true;
+ }
+```
-----------------------
diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md"
index a6cd413b19..962fe7a541 100644
--- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md"
+++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md"
@@ -318,5 +318,32 @@ impl Solution {
}
```
+C#:
+```csharp
+public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
+ Dictionary dic = new Dictionary();
+ foreach(var i in nums1){
+ foreach(var j in nums2){
+ int sum = i + j;
+ if(dic.ContainsKey(sum)){
+ dic[sum]++;
+ }else{
+ dic.Add(sum, 1);
+ }
+
+ }
+ }
+ int res = 0;
+ foreach(var a in nums3){
+ foreach(var b in nums4){
+ int sum = a+b;
+ if(dic.TryGetValue(-sum, out var result)){
+ res += result;
+ }
+ }
+ }
+ return res;
+ }
+```
-----------------------
From ef8564a5930805ace91776c5648fa7110800c2d7 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 11 May 2022 19:51:07 +0800
Subject: [PATCH 169/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200203.=E7=A7=BB?=
=?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...76\350\241\250\345\205\203\347\264\240.md" | 32 ++++++++++++++++++-
1 file changed, 31 insertions(+), 1 deletion(-)
diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
index c34831b768..53230c0b6c 100644
--- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
+++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
@@ -446,6 +446,36 @@ impl Solution {
}
}
```
-
+Scala:
+```scala
+/**
+ * Definition for singly-linked list.
+ * class ListNode(_x: Int = 0, _next: ListNode = null) {
+ * var next: ListNode = _next
+ * var x: Int = _x
+ * }
+ */
+object Solution {
+ def removeElements(head: ListNode, `val`: Int): ListNode = {
+ if (head == null) return head
+ var dummy = new ListNode(-1, head) // 定义虚拟头节点
+ var cur = head // cur 表示当前节点
+ var pre = dummy // pre 表示cur前一个节点
+ while (cur != null) {
+ if (cur.x == `val`) {
+ // 相等,就删除那么cur的前一个节点pre执行cur的下一个
+ pre.next = cur.next
+ } else {
+ // 不相等,pre就等于当前cur节点
+ pre = cur
+ }
+ // 向下迭代
+ cur = cur.next
+ }
+ // 最终返回dummy的下一个,就是链表的头
+ dummy.next
+ }
+}
+```
-----------------------
From 898016b8a98aa7eeb2445681e66f0302a79c28ae Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 11 May 2022 21:13:42 +0800
Subject: [PATCH 170/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200707.=E8=AE=BE?=
=?UTF-8?q?=E8=AE=A1=E9=93=BE=E8=A1=A8.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...76\350\256\241\351\223\276\350\241\250.md" | 68 +++++++++++++++++++
1 file changed, 68 insertions(+)
diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
index 37ce15adce..dcdb53f44e 100644
--- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
+++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
@@ -1154,7 +1154,75 @@ class MyLinkedList {
}
```
+Scala:
+```scala
+class ListNode(_x: Int = 0, _next: ListNode = null) {
+ var next: ListNode = _next
+ var x: Int = _x
+}
+
+class MyLinkedList() {
+
+ var size = 0 // 链表尺寸
+ var dummy: ListNode = new ListNode(0) // 虚拟头节点
+
+ // 获取第index个节点的值
+ def get(index: Int): Int = {
+ if (index < 0 || index >= size) {
+ return -1;
+ }
+ var cur = dummy
+ for (i <- 0 to index) {
+ cur = cur.next
+ }
+ cur.x // 返回cur的值
+ }
+
+ // 在链表最前面插入一个节点
+ def addAtHead(`val`: Int) {
+ addAtIndex(0, `val`)
+ }
+
+ // 在链表最后面插入一个节点
+ def addAtTail(`val`: Int) {
+ addAtIndex(size, `val`)
+ }
+
+ // 在第index个节点之前插入一个新节点
+ // 如果index等于链表长度,则说明新插入的节点是尾巴
+ // 如果index等于0,则说明新插入的节点是头
+ // 如果index>链表长度,则说明为空
+ def addAtIndex(index: Int, `val`: Int) {
+ if (index > size) {
+ return
+ }
+ var loc = index // 因为参数index是val不可变类型,所以需要赋值给一个可变类型
+ if (index < 0) {
+ loc = 0
+ }
+ size += 1 //链表尺寸+1
+ var pre = dummy
+ for (i <- 0 until loc) {
+ pre = pre.next
+ }
+ val node: ListNode = new ListNode(`val`, pre.next)
+ pre.next = node
+ }
+ // 删除第index个节点
+ def deleteAtIndex(index: Int) {
+ if (index < 0 || index >= size) {
+ return
+ }
+ size -= 1
+ var pre = dummy
+ for (i <- 0 until index) {
+ pre = pre.next
+ }
+ pre.next = pre.next.next
+ }
+}
+```
-----------------------
From 923ce563f1461876fbed667f8a79194ccb060096 Mon Sep 17 00:00:00 2001
From: yangtzech
Date: Wed, 11 May 2022 21:30:43 +0800
Subject: [PATCH 171/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=EF=BC=880102.?=
=?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D?=
=?UTF-8?q?=E5=8E=86.md=EF=BC=89=EF=BC=9A102.=E4=BA=8C=E5=8F=89=E6=A0=91?=
=?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=20=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0=20c++=20=E9=80=92=E5=BD=92=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...02\345\272\217\351\201\215\345\216\206.md" | 20 +++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
index ab8f2e572e..5f69f53d76 100644
--- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
+++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
@@ -82,6 +82,26 @@ public:
}
};
```
+```CPP
+# 递归法
+class Solution {
+public:
+ void order(TreeNode* cur, vector>& result, int depth)
+ {
+ if (cur == nullptr) return;
+ if (result.size() == depth) result.push_back(vector());
+ result[depth].push_back(cur->val);
+ order(cur->left, result, depth + 1);
+ order(cur->right, result, depth + 1);
+ }
+ vector> levelOrder(TreeNode* root) {
+ vector> result;
+ int depth = 0;
+ order(root, result, depth);
+ return result;
+ }
+};
+```
python3代码:
From 3606a624237334eddee98308e39daedbb2d250b0 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 11 May 2022 23:03:42 +0800
Subject: [PATCH 172/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200206.=E7=BF=BB?=
=?UTF-8?q?=E8=BD=AC=E9=93=BE=E8=A1=A8.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...73\350\275\254\351\223\276\350\241\250.md" | 34 +++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md"
index 941928baae..25b16907e4 100644
--- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md"
+++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md"
@@ -496,6 +496,40 @@ struct ListNode* reverseList(struct ListNode* head){
return reverse(NULL, head);
}
```
+Scala:
+双指针法:
+```scala
+object Solution {
+ def reverseList(head: ListNode): ListNode = {
+ var pre: ListNode = null
+ var cur = head
+ while (cur != null) {
+ var tmp = cur.next
+ cur.next = pre
+ pre = cur
+ cur = tmp
+ }
+ pre
+ }
+}
+```
+递归法:
+```scala
+object Solution {
+ def reverseList(head: ListNode): ListNode = {
+ reverse(null, head)
+ }
+
+ def reverse(pre: ListNode, cur: ListNode): ListNode = {
+ if (cur == null) {
+ return pre // 如果当前cur为空,则返回pre
+ }
+ val tmp: ListNode = cur.next
+ cur.next = pre
+ reverse(cur, tmp) // 此时cur成为前一个节点,tmp是当前节点
+ }
+}
+```
-----------------------
From 83086c903d77496e6fa34e7e4b0069e27ad1210c Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 12 May 2022 10:13:00 +0800
Subject: [PATCH 173/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200024.=E4=B8=A4?=
=?UTF-8?q?=E4=B8=A4=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84?=
=?UTF-8?q?=E8=8A=82=E7=82=B9.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...55\347\232\204\350\212\202\347\202\271.md" | 24 ++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md"
index ce75e0d79f..2289c22912 100644
--- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md"
+++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md"
@@ -311,7 +311,29 @@ func swapPairs(_ head: ListNode?) -> ListNode? {
return dummyHead.next
}
```
-
+Scala:
+```scala
+// 虚拟头节点
+object Solution {
+ def swapPairs(head: ListNode): ListNode = {
+ var dummy = new ListNode(0, head) // 虚拟头节点
+ var pre = dummy
+ var cur = head
+ // 当pre的下一个和下下个都不为空,才进行两两转换
+ while (pre.next != null && pre.next.next != null) {
+ var tmp: ListNode = cur.next.next // 缓存下一次要进行转换的第一个节点
+ pre.next = cur.next // 步骤一
+ cur.next.next = cur // 步骤二
+ cur.next = tmp // 步骤三
+ // 下面是准备下一轮的交换
+ pre = cur
+ cur = tmp
+ }
+ // 最终返回dummy虚拟头节点的下一个,return可以省略
+ dummy.next
+ }
+}
+```
-----------------------
From 5fd85215ea9c30c73e5ab84f41dbc9cf170e8617 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 12 May 2022 10:53:20 +0800
Subject: [PATCH 174/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200019.=E5=88=A0?=
=?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=AC?=
=?UTF-8?q?N=E4=B8=AA=E8=8A=82=E7=82=B9.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4N\344\270\252\350\212\202\347\202\271.md" | 24 ++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md"
index 813e9b02fa..8df6562762 100644
--- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md"
+++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md"
@@ -289,6 +289,28 @@ func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
return dummyHead.next
}
```
-
+Scala:
+```scala
+object Solution {
+ def removeNthFromEnd(head: ListNode, n: Int): ListNode = {
+ val dummy = new ListNode(-1, head) // 定义虚拟头节点
+ var fast = head // 快指针从头开始走
+ var slow = dummy // 慢指针从虚拟头开始头
+ // 因为参数 n 是不可变量,所以不能使用 while(n>0){n-=1}的方式
+ for (i <- 0 until n) {
+ fast = fast.next
+ }
+ // 快指针和满指针一起走,直到fast走到null
+ while (fast != null) {
+ slow = slow.next
+ fast = fast.next
+ }
+ // 删除slow的下一个节点
+ slow.next = slow.next.next
+ // 返回虚拟头节点的下一个
+ dummy.next
+ }
+}
+```
-----------------------
From 6992735d8de6d1875d0565413faed410e81c822e Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 12 May 2022 12:46:47 +0800
Subject: [PATCH 175/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E9=9D=A2=E8=AF=95?=
=?UTF-8?q?=E9=A2=9802.07.=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...76\350\241\250\347\233\270\344\272\244.md" | 50 ++++++++++++++++++-
1 file changed, 49 insertions(+), 1 deletion(-)
diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md"
index 2e7226deeb..0a38cc3309 100644
--- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md"
+++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md"
@@ -317,7 +317,55 @@ ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
}
```
-
+Scala:
+```scala
+object Solution {
+ def getIntersectionNode(headA: ListNode, headB: ListNode): ListNode = {
+ var lenA = 0 // headA链表的长度
+ var lenB = 0 // headB链表的长度
+ var tmp = headA // 临时变量
+ // 统计headA的长度
+ while (tmp != null) {
+ lenA += 1;
+ tmp = tmp.next
+ }
+ // 统计headB的长度
+ tmp = headB // 临时变量赋值给headB
+ while (tmp != null) {
+ lenB += 1
+ tmp = tmp.next
+ }
+ // 因为传递过来的参数是不可变量,所以需要重新定义
+ var listA = headA
+ var listB = headB
+ // 两个链表的长度差
+ // 如果gap>0,lenA>lenB,headA(listA)链表往前移动gap步
+ // 如果gap<0,lenA 0) {
+ // 因为不可以i-=1,所以可以使用for
+ for (i <- 0 until gap) {
+ listA = listA.next // 链表headA(listA) 移动
+ }
+ } else {
+ gap = math.abs(gap) // 此刻gap为负值,取绝对值
+ for (i <- 0 until gap) {
+ listB = listB.next
+ }
+ }
+ // 现在两个链表同时往前走,如果相等则返回
+ while (listA != null && listB != null) {
+ if (listA == listB) {
+ return listA
+ }
+ listA = listA.next
+ listB = listB.next
+ }
+ // 如果链表没有相交则返回null,return可以省略
+ null
+ }
+}
+```
-----------------------
From 2ce58ac6756775f8ec5139b36a23e8f3de232d30 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 12 May 2022 12:55:14 +0800
Subject: [PATCH 176/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880337.?=
=?UTF-8?q?=E6=89=93=E5=AE=B6=E5=8A=AB=E8=88=8DIII.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...345\256\266\345\212\253\350\210\215III.md" | 43 +++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md"
index a4d8f6b280..6f50723d8f 100644
--- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md"
+++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md"
@@ -429,7 +429,50 @@ const rob = root => {
};
```
+### TypeScript
+
+> 记忆化后序遍历
+
+```typescript
+const memory: Map = new Map();
+function rob(root: TreeNode | null): number {
+ if (root === null) return 0;
+ if (memory.has(root)) return memory.get(root);
+ // 不取当前节点
+ const res1: number = rob(root.left) + rob(root.right);
+ // 取当前节点
+ let res2: number = root.val;
+ if (root.left !== null) res2 += rob(root.left.left) + rob(root.left.right);
+ if (root.right !== null) res2 += rob(root.right.left) + rob(root.right.right);
+ const res: number = Math.max(res1, res2);
+ memory.set(root, res);
+ return res;
+};
+```
+
+> 状态标记化后序遍历
+
+```typescript
+function rob(root: TreeNode | null): number {
+ return Math.max(...robNode(root));
+};
+// [0]-不偷当前节点能获得的最大金额; [1]-偷~~
+type MaxValueArr = [number, number];
+function robNode(node: TreeNode | null): MaxValueArr {
+ if (node === null) return [0, 0];
+ const leftArr: MaxValueArr = robNode(node.left);
+ const rightArr: MaxValueArr = robNode(node.right);
+ // 不偷
+ const val1: number = Math.max(leftArr[0], leftArr[1]) +
+ Math.max(rightArr[0], rightArr[1]);
+ // 偷
+ const val2: number = leftArr[0] + rightArr[0] + node.val;
+ return [val1, val2];
+}
+```
+
### Go
+
```go
// 打家劫舍Ⅲ 动态规划
// 时间复杂度O(n) 空间复杂度O(logn)
From 5627d4ba8605f35b8d63cf4af363c64d721f69a3 Mon Sep 17 00:00:00 2001
From: Jamcy123 <1219502823@qq.com>
Date: Thu, 12 May 2022 16:04:42 +0800
Subject: [PATCH 177/551] =?UTF-8?q?=E8=83=8C=E5=8C=85=E7=90=86=E8=AE=BA?=
=?UTF-8?q?=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1=20js=20=E7=89=88?=
=?UTF-8?q?=E6=9C=AC=E4=BF=AE=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\241\20001\350\203\214\345\214\205-1.md" | 49 +++++++------------
1 file changed, 18 insertions(+), 31 deletions(-)
diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
index fe940b4ca4..e833ea802b 100644
--- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
+++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
@@ -380,44 +380,31 @@ func main() {
### javascript
```js
-function testweightbagproblem (wight, value, size) {
- const len = wight.length,
- dp = array.from({length: len + 1}).map(
- () => array(size + 1).fill(0)
- );
-
- for(let i = 1; i <= len; i++) {
- for(let j = 0; j <= size; j++) {
- if(wight[i - 1] <= j) {
- dp[i][j] = math.max(
- dp[i - 1][j],
- value[i - 1] + dp[i - 1][j - wight[i - 1]]
- )
- } else {
- dp[i][j] = dp[i - 1][j];
- }
+function testWeightBagProblem (weight, value, size) {
+ // 定义 dp 数组
+ const len = weight.length,
+ dp = Array(len).fill().map(() => Array(size + 1).fill(0));
+
+ // 初始化
+ for(let j = weight[0]; j <= size; j++) {
+ dp[0][j] = value[0];
}
- }
-// console.table(dp);
+ // weight 数组的长度len 就是物品个数
+ for(let i = 1; i < len; i++) { // 遍历物品
+ for(let j = 0; j <= size; j++) { // 遍历背包容量
+ if(j < weight[i]) dp[i][j] = dp[i - 1][j];
+ else dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);
+ }
+ }
- return dp[len][size];
-}
+ console.table(dp)
-function testWeightBagProblem2 (wight, value, size) {
- const len = wight.length,
- dp = Array(size + 1).fill(0);
- for(let i = 1; i <= len; i++) {
- for(let j = size; j >= wight[i - 1]; j--) {
- dp[j] = Math.max(dp[j], value[i - 1] + dp[j - wight[i - 1]]);
- }
- }
- return dp[size];
+ return dp[len - 1][size];
}
-
function test () {
- console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6));
+ console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6));
}
test();
From 40457f0f44d3981dc071fd56871f66d7e5946af1 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 12 May 2022 16:16:48 +0800
Subject: [PATCH 178/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880121.?=
=?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3?=
=?UTF-8?q?=E6=97=B6=E6=9C=BA.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?=
=?UTF-8?q?ript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...00\344\275\263\346\227\266\346\234\272.md" | 40 +++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md"
index f0bc3b9764..a2498bb6b6 100644
--- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md"
+++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md"
@@ -426,6 +426,46 @@ var maxProfit = function(prices) {
};
```
+TypeScript:
+
+> 贪心法
+
+```typescript
+function maxProfit(prices: number[]): number {
+ if (prices.length === 0) return 0;
+ let buy: number = prices[0];
+ let profitMax: number = 0;
+ for (let i = 1, length = prices.length; i < length; i++) {
+ profitMax = Math.max(profitMax, prices[i] - buy);
+ buy = Math.min(prices[i], buy);
+ }
+ return profitMax;
+};
+```
+
+> 动态规划
+
+```typescript
+function maxProfit(prices: number[]): number {
+ /**
+ dp[i][0]: 第i天持有股票的最大现金
+ dp[i][1]: 第i天不持有股票的最大现金
+ */
+ const length = prices.length;
+ if (length === 0) return 0;
+ const dp: number[][] = [];
+ dp[0] = [-prices[0], 0];
+ for (let i = 1; i < length; i++) {
+ dp[i] = [];
+ dp[i][0] = Math.max(dp[i - 1][0], -prices[i]);
+ dp[i][1] = Math.max(dp[i - 1][0] + prices[i], dp[i - 1][1]);
+ }
+ return dp[length - 1][1];
+};
+```
+
+
+
-----------------------
From 8ba775d04acd100e6cdac8ebd913aaead67fae15 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 12 May 2022 16:40:54 +0800
Subject: [PATCH 179/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880122.?=
=?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3?=
=?UTF-8?q?=E6=97=B6=E6=9C=BAII=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...01\350\247\204\345\210\222\357\274\211.md" | 36 +++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
index 5a165a1438..12b21fde30 100644
--- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
+++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
@@ -295,6 +295,42 @@ const maxProfit = (prices) => {
}
```
+TypeScript:
+
+> 动态规划
+
+```typescript
+function maxProfit(prices: number[]): number {
+ /**
+ dp[i][0]: 第i天持有股票
+ dp[i][1]: 第i天不持有股票
+ */
+ const length: number = prices.length;
+ if (length === 0) return 0;
+ const dp: number[][] = new Array(length).fill(0).map(_ => []);
+ dp[0] = [-prices[0], 0];
+ for (let i = 1; i < length; i++) {
+ dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
+ dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
+ }
+ return dp[length - 1][1];
+};
+```
+
+> 贪心法
+
+```typescript
+function maxProfit(prices: number[]): number {
+ let resProfit: number = 0;
+ for (let i = 1, length = prices.length; i < length; i++) {
+ if (prices[i] > prices[i - 1]) {
+ resProfit += prices[i] - prices[i - 1];
+ }
+ }
+ return resProfit;
+};
+```
+
-----------------------
From 0a58ef986de4c20c5d69a4b490b478f44b7f3f5c Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 12 May 2022 17:35:30 +0800
Subject: [PATCH 180/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200142.=E7=8E=AF?=
=?UTF-8?q?=E5=BD=A2=E9=93=BE=E8=A1=A8II.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\345\275\242\351\223\276\350\241\250II.md" | 26 ++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)
diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md"
index e8ca950dbb..f8e62d4572 100644
--- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md"
+++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md"
@@ -370,7 +370,31 @@ ListNode *detectCycle(ListNode *head) {
}
```
-
+Scala:
+```scala
+object Solution {
+ def detectCycle(head: ListNode): ListNode = {
+ var fast = head // 快指针
+ var slow = head // 慢指针
+ while (fast != null && fast.next != null) {
+ fast = fast.next.next // 快指针一次走两步
+ slow = slow.next // 慢指针一次走一步
+ // 如果相遇,fast快指针回到头
+ if (fast == slow) {
+ fast = head
+ // 两个指针一步一步的走,第一次相遇的节点必是入环节点
+ while (fast != slow) {
+ fast = fast.next
+ slow = slow.next
+ }
+ return fast
+ }
+ }
+ // 如果fast指向空值,必然无环返回null
+ null
+ }
+}
+```
-----------------------
From a25409d7ec351aaf2e6e513e3e091fc97cd17d60 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 12 May 2022 19:16:20 +0800
Subject: [PATCH 181/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200242.=E6=9C=89?=
=?UTF-8?q?=E6=95=88=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...15\345\274\202\344\275\215\350\257\215.md" | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md"
index 080166fd34..4f0d143ed2 100644
--- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md"
+++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md"
@@ -307,6 +307,31 @@ impl Solution {
}
}
```
+
+Scala:
+```scala
+object Solution {
+ def isAnagram(s: String, t: String): Boolean = {
+ // 如果两个字符串的长度不等,直接返回false
+ if (s.length != t.length) return false
+ val record = new Array[Int](26) // 记录每个单词出现了多少次
+ // 遍历字符串,对于s字符串单词对应的记录+=1,t字符串对应的记录-=1
+ for (i <- 0 until s.length) {
+ record(s(i) - 97) += 1
+ record(t(i) - 97) -= 1
+ }
+ // 如果不等于则直接返回false
+ for (i <- 0 until 26) {
+ if (record(i) != 0) {
+ return false
+ }
+ }
+ // 如果前面不返回false,说明匹配成功,返回true,return可以省略
+ true
+ }
+}
+```
+
## 相关题目
* 383.赎金信
From 74b76bfd120446653495d96135e697b5797bae35 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 12 May 2022 22:21:29 +0800
Subject: [PATCH 182/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201002.=E6=9F=A5?=
=?UTF-8?q?=E6=89=BE=E5=B8=B8=E7=94=A8=E5=AD=97=E7=AC=A6.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...70\347\224\250\345\255\227\347\254\246.md" | 34 ++++++++++++++++++-
1 file changed, 33 insertions(+), 1 deletion(-)
diff --git "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md"
index 36937b0b91..075b5ef1a6 100644
--- "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md"
+++ "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md"
@@ -418,6 +418,38 @@ char ** commonChars(char ** words, int wordsSize, int* returnSize){
return ret;
}
```
-
+Scala:
+```scala
+object Solution {
+ def commonChars(words: Array[String]): List[String] = {
+ // 声明返回结果的不可变List集合,因为res要重新赋值,所以声明为var
+ var res = List[String]()
+ var hash = new Array[Int](26) // 统计字符出现的最小频率
+ // 统计第一个字符串中字符出现的次数
+ for (i <- 0 until words(0).length) {
+ hash(words(0)(i) - 'a') += 1
+ }
+ // 统计其他字符串出现的频率
+ for (i <- 1 until words.length) {
+ // 统计其他字符出现的频率
+ var hashOtherStr = new Array[Int](26)
+ for (j <- 0 until words(i).length) {
+ hashOtherStr(words(i)(j) - 'a') += 1
+ }
+ // 更新hash,取26个字母最小出现的频率
+ for (k <- 0 until 26) {
+ hash(k) = math.min(hash(k), hashOtherStr(k))
+ }
+ }
+ // 根据hash的结果转换输出的形式
+ for (i <- 0 until 26) {
+ for (j <- 0 until hash(i)) {
+ res = res :+ (i + 'a').toChar.toString
+ }
+ }
+ res
+ }
+}
+```
-----------------------
From a71f7d2127bae83cc42dffd20b4c460865006d15 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 12 May 2022 23:23:59 +0800
Subject: [PATCH 183/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880123.?=
=?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3?=
=?UTF-8?q?=E6=97=B6=E6=9C=BAIII.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typ?=
=?UTF-8?q?escript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...344\275\263\346\227\266\346\234\272III.md" | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md"
index 56ade34338..67c9949719 100644
--- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md"
+++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md"
@@ -352,6 +352,36 @@ const maxProfit = prices => {
};
```
+TypeScript:
+
+> 版本一
+
+```typescript
+function maxProfit(prices: number[]): number {
+ /**
+ dp[i][0]: 无操作;
+ dp[i][1]: 第一次买入;
+ dp[i][2]: 第一次卖出;
+ dp[i][3]: 第二次买入;
+ dp[i][4]: 第二次卖出;
+ */
+ const length: number = prices.length;
+ if (length === 0) return 0;
+ const dp: number[][] = new Array(length).fill(0)
+ .map(_ => new Array(5).fill(0));
+ dp[0][1] = -prices[0];
+ dp[0][3] = -prices[0];
+ for (let i = 1; i < length; i++) {
+ dp[i][0] = dp[i - 1][0];
+ dp[i][1] = Math.max(dp[i - 1][1], -prices[i]);
+ dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]);
+ dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i]);
+ dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
+ }
+ return Math.max(dp[length - 1][2], dp[length - 1][4]);
+};
+```
+
Go:
> 版本一:
From 78c6602ff20c309132007795956f332d66ff7130 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 13 May 2022 10:05:27 +0800
Subject: [PATCH 184/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200349.=E4=B8=A4?=
=?UTF-8?q?=E4=B8=AA=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\347\232\204\344\272\244\351\233\206.md" | 44 ++++++++++++++++++-
1 file changed, 43 insertions(+), 1 deletion(-)
diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
index 45f19b6ef2..49cf4112cc 100644
--- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
+++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
@@ -312,7 +312,49 @@ int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* re
return result;
}
```
-
+Scala:
+
+正常解法:
+```scala
+object Solution {
+ def intersection(nums1: Array[Int], nums2: Array[Int]): Array[Int] = {
+ // 导入mutable
+ import scala.collection.mutable
+ // 临时Set,用于记录数组1出现的每个元素
+ val tmpSet: mutable.HashSet[Int] = new mutable.HashSet[Int]()
+ // 结果Set,存储最终结果
+ val resSet: mutable.HashSet[Int] = new mutable.HashSet[Int]()
+ // 遍历nums1,把每个元素添加到tmpSet
+ nums1.foreach(tmpSet.add(_))
+ // 遍历nums2,如果在tmpSet存在就添加到resSet
+ nums2.foreach(elem => {
+ if (tmpSet.contains(elem)) {
+ resSet.add(elem)
+ }
+ })
+ // 将结果转换为Array返回,return可以省略
+ resSet.toArray
+ }
+}
+```
+骚操作1:
+```scala
+object Solution {
+ def intersection(nums1: Array[Int], nums2: Array[Int]): Array[Int] = {
+ // 先转为Set,然后取交集,最后转换为Array
+ (nums1.toSet).intersect(nums2.toSet).toArray
+ }
+}
+```
+骚操作2:
+```scala
+object Solution {
+ def intersection(nums1: Array[Int], nums2: Array[Int]): Array[Int] = {
+ // distinct去重,然后取交集
+ (nums1.distinct).intersect(nums2.distinct)
+ }
+}
+```
## 相关题目
* 350.两个数组的交集 II
From 9f720470e8e2360047052897885261545d0e1ac7 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 13 May 2022 10:29:17 +0800
Subject: [PATCH 185/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200202.=E5=BF=AB?=
=?UTF-8?q?=E4=B9=90=E6=95=B0.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...2.\345\277\253\344\271\220\346\225\260.md" | 33 +++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md"
index 741a735a5d..2d0f7d274e 100644
--- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md"
+++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md"
@@ -385,5 +385,38 @@ bool isHappy(int n){
return bHappy;
}
```
+Scala:
+```scala
+object Solution {
+ // 引入mutable
+ import scala.collection.mutable
+ def isHappy(n: Int): Boolean = {
+ // 存放每次计算后的结果
+ val set: mutable.HashSet[Int] = new mutable.HashSet[Int]()
+ var tmp = n // 因为形参是不可变量,所以需要找到一个临时变量
+ // 开始进入循环
+ while (true) {
+ val sum = getSum(tmp) // 获取这个数每个值的平方和
+ if (sum == 1) return true // 如果最终等于 1,则返回true
+ // 如果set里面已经有这个值了,说明进入无限循环,可以返回false,否则添加这个值到set
+ if (set.contains(sum)) return false
+ else set.add(sum)
+ tmp = sum
+ }
+ // 最终需要返回值,直接返回个false
+ false
+ }
+
+ def getSum(n: Int): Int = {
+ var sum = 0
+ var tmp = n
+ while (tmp != 0) {
+ sum += (tmp % 10) * (tmp % 10)
+ tmp = tmp / 10
+ }
+ sum
+ }
+}
+```
-----------------------
From be5cc136c56cb194af8ce1d794871ca7af4b1e1d Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 13 May 2022 11:00:11 +0800
Subject: [PATCH 186/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200035.=E6=90=9C?=
=?UTF-8?q?=E7=B4=A2=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...22\345\205\245\344\275\215\347\275\256.md" | 21 ++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
index 9a770703e3..3e8cf5c877 100644
--- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
+++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
@@ -316,7 +316,26 @@ func searchInsert(_ nums: [Int], _ target: Int) -> Int {
return right + 1
}
```
-
+### Scala
+```scala
+object Solution {
+ def searchInsert(nums: Array[Int], target: Int): Int = {
+ var left = 0
+ var right = nums.length - 1
+ while (left <= right) {
+ var mid = left + (right - left) / 2
+ if (target == nums(mid)) {
+ return mid
+ } else if (target > nums(mid)) {
+ left = mid + 1
+ } else {
+ right = mid - 1
+ }
+ }
+ right + 1
+ }
+}
+```
From 95593634b341a951850fae3b5aa739ba7c5d3864 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 13 May 2022 11:01:04 +0800
Subject: [PATCH 187/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200704.=E4=BA=8C?=
=?UTF-8?q?=E5=88=86=E6=9F=A5=E6=89=BE.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...14\345\210\206\346\237\245\346\211\276.md" | 41 +++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
index 5562513089..1e474f9a96 100644
--- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
+++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
@@ -610,7 +610,48 @@ public class Solution{
}
}
```
+**Scala:**
+(版本一)左闭右闭区间
+```scala
+object Solution {
+ def search(nums: Array[Int], target: Int): Int = {
+ var left = 0
+ var right = nums.length - 1
+ while (left <= right) {
+ var mid = left + ((right - left) / 2)
+ if (target == nums(mid)) {
+ return mid
+ } else if (target < nums(mid)) {
+ right = mid - 1
+ } else {
+ left = mid + 1
+ }
+ }
+ -1
+ }
+}
+```
+(版本二)左闭右开区间
+```scala
+object Solution {
+ def search(nums: Array[Int], target: Int): Int = {
+ var left = 0
+ var right = nums.length
+ while (left < right) {
+ val mid = left + (right - left) / 2
+ if (target == nums(mid)) {
+ return mid
+ } else if (target < nums(mid)) {
+ right = mid
+ } else {
+ left = mid + 1
+ }
+ }
+ -1
+ }
+}
+```
-----------------------
From 9fc232aba413b43fd479ad5b4fd1a186ff90a359 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 13 May 2022 11:01:45 +0800
Subject: [PATCH 188/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200034.=E5=9C=A8?=
=?UTF-8?q?=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE?=
=?UTF-8?q?=E5=85=83=E7=B4=A0=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C?=
=?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...00\344\270\252\344\275\215\347\275\256.md" | 47 ++++++++++++++++++-
1 file changed, 46 insertions(+), 1 deletion(-)
diff --git "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md"
index dfd90b8280..260462c23b 100644
--- "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md"
+++ "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md"
@@ -480,7 +480,52 @@ var searchRange = function(nums, target) {
return [-1, -1];
};
```
-
+### Scala
+```scala
+object Solution {
+ def searchRange(nums: Array[Int], target: Int): Array[Int] = {
+ var left = getLeftBorder(nums, target)
+ var right = getRightBorder(nums, target)
+ if (left == -2 || right == -2) return Array(-1, -1)
+ if (right - left > 1) return Array(left + 1, right - 1)
+ Array(-1, -1)
+ }
+
+ // 寻找左边界
+ def getLeftBorder(nums: Array[Int], target: Int): Int = {
+ var leftBorder = -2
+ var left = 0
+ var right = nums.length - 1
+ while (left <= right) {
+ var mid = left + (right - left) / 2
+ if (nums(mid) >= target) {
+ right = mid - 1
+ leftBorder = right
+ } else {
+ left = mid + 1
+ }
+ }
+ leftBorder
+ }
+
+ // 寻找右边界
+ def getRightBorder(nums: Array[Int], target: Int): Int = {
+ var rightBorder = -2
+ var left = 0
+ var right = nums.length - 1
+ while (left <= right) {
+ var mid = left + (right - left) / 2
+ if (nums(mid) <= target) {
+ left = mid + 1
+ rightBorder = left
+ } else {
+ right = mid - 1
+ }
+ }
+ rightBorder
+ }
+}
+```
-----------------------
From f131f27744693b91f40df4ea0fb7c1578bd6647b Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 13 May 2022 11:02:19 +0800
Subject: [PATCH 189/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200027.=E7=A7=BB?=
=?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...7\273\351\231\244\345\205\203\347\264\240.md" | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
index 590cf0b924..5ff327fb75 100644
--- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
+++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
@@ -328,6 +328,20 @@ int removeElement(int* nums, int numsSize, int val){
return slow;
}
```
-
+Scala:
+```scala
+object Solution {
+ def removeElement(nums: Array[Int], `val`: Int): Int = {
+ var slow = 0
+ for (fast <- 0 until nums.length) {
+ if (`val` != nums(fast)) {
+ nums(slow) = nums(fast)
+ slow += 1
+ }
+ }
+ slow
+ }
+}
+```
-----------------------
From 495998e509527b53a8b84cc0527dfcff4fc1e1af Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 13 May 2022 12:07:52 +0800
Subject: [PATCH 190/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880188.?=
=?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3?=
=?UTF-8?q?=E6=97=B6=E6=9C=BAIV.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0type?=
=?UTF-8?q?script=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\344\275\263\346\227\266\346\234\272IV.md" | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md"
index 61c558a1a1..27eb38c365 100644
--- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md"
+++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md"
@@ -409,5 +409,27 @@ var maxProfit = function(k, prices) {
};
```
+TypeScript:
+
+```typescript
+function maxProfit(k: number, prices: number[]): number {
+ const length: number = prices.length;
+ if (length === 0) return 0;
+ const dp: number[][] = new Array(length).fill(0)
+ .map(_ => new Array(k * 2 + 1).fill(0));
+ for (let i = 1; i <= k; i++) {
+ dp[0][i * 2 - 1] = -prices[0];
+ }
+ for (let i = 1; i < length; i++) {
+ for (let j = 1; j < 2 * k + 1; j++) {
+ dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - 1] + Math.pow(-1, j) * prices[i]);
+ }
+ }
+ return dp[length - 1][2 * k];
+};
+```
+
+
+
-----------------------
From 127986e03a0b4acdedf80a6b38d3ded7d61514f3 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 13 May 2022 16:19:59 +0800
Subject: [PATCH 191/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880309.?=
=?UTF-8?q?=E6=9C=80=E4=BD=B3=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E6=97=B6?=
=?UTF-8?q?=E6=9C=BA=E5=90=AB=E5=86=B7=E5=86=BB=E6=9C=9F.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...53\345\206\267\345\206\273\346\234\237.md" | 60 +++++++++++++++++++
1 file changed, 60 insertions(+)
diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md"
index f3e7541bef..f037fe8558 100644
--- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md"
+++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md"
@@ -325,6 +325,66 @@ const maxProfit = (prices) => {
};
```
+TypeScript:
+
+> 版本一,与本文思路一致
+
+```typescript
+function maxProfit(prices: number[]): number {
+ /**
+ dp[i][0]: 持股状态;
+ dp[i][1]: 无股状态,当天为非冷冻期;
+ dp[i][2]: 无股状态,当天卖出;
+ dp[i][3]: 无股状态,当天为冷冻期;
+ */
+ const length: number = prices.length;
+ const dp: number[][] = new Array(length).fill(0).map(_ => []);
+ dp[0][0] = -prices[0];
+ dp[0][1] = dp[0][2] = dp[0][3] = 0;
+ for (let i = 1; i < length; i++) {
+ dp[i][0] = Math.max(
+ dp[i - 1][0],
+ Math.max(dp[i - 1][1], dp[i - 1][3]) - prices[i]
+ );
+ dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][3]);
+ dp[i][2] = dp[i - 1][0] + prices[i];
+ dp[i][3] = dp[i - 1][2];
+ }
+ const lastEl: number[] = dp[length - 1];
+ return Math.max(lastEl[1], lastEl[2], lastEl[3]);
+};
+```
+
+> 版本二,状态定义略有不同,可以帮助理解
+
+```typescript
+function maxProfit(prices: number[]): number {
+ /**
+ dp[i][0]: 持股状态,当天买入;
+ dp[i][1]: 持股状态,当天未买入;
+ dp[i][2]: 无股状态,当天卖出;
+ dp[i][3]: 无股状态,当天未卖出;
+
+ 买入有冷冻期限制,其实就是状态[0]只能由前一天的状态[3]得到;
+ 如果卖出有冷冻期限制,其实就是[2]由[1]得到。
+ */
+ const length: number = prices.length;
+ const dp: number[][] = new Array(length).fill(0).map(_ => []);
+ dp[0][0] = -prices[0];
+ dp[0][1] = -Infinity;
+ dp[0][2] = dp[0][3] = 0;
+ for (let i = 1; i < length; i++) {
+ dp[i][0] = dp[i - 1][3] - prices[i];
+ dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0]);
+ dp[i][2] = Math.max(dp[i - 1][0], dp[i - 1][1]) + prices[i];
+ dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2]);
+ }
+ return Math.max(dp[length - 1][2], dp[length - 1][3]);
+};
+```
+
+
+
-----------------------
From abe00238f44718c49b9442c26e7f7b01f84be3e4 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 13 May 2022 16:37:39 +0800
Subject: [PATCH 192/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200001.=E4=B8=A4?=
=?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...44\346\225\260\344\271\213\345\222\214.md" | 23 ++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md"
index 9571a7737b..141e66f34e 100644
--- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md"
@@ -274,6 +274,27 @@ class Solution {
}
}
```
-
+Scala:
+```scala
+object Solution {
+ // 导入包
+ import scala.collection.mutable
+ def twoSum(nums: Array[Int], target: Int): Array[Int] = {
+ // key存储值,value存储下标
+ val map = new mutable.HashMap[Int, Int]()
+ for (i <- nums.indices) {
+ val tmp = target - nums(i) // 计算差值
+ // 如果这个差值存在于map,则说明找到了结果
+ if (map.contains(tmp)) {
+ return Array(map.get(tmp).get, i)
+ }
+ // 如果不包含把当前值与其下标放到map
+ map.put(nums(i), i)
+ }
+ // 如果没有找到直接返回一个空的数组,return关键字可以省略
+ new Array[Int](2)
+ }
+}
+```
-----------------------
From 53379c023fe935398801f41aad3dd0399b2d7d73 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 13 May 2022 17:28:14 +0800
Subject: [PATCH 193/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200454.=E5=9B=9B?=
=?UTF-8?q?=E6=95=B0=E7=9B=B8=E5=8A=A0II.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\346\225\260\347\233\270\345\212\240II.md" | 36 +++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md"
index a6cd413b19..d4aba8fa46 100644
--- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md"
+++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md"
@@ -318,5 +318,41 @@ impl Solution {
}
```
+Scala:
+```scala
+object Solution {
+ // 导包
+ import scala.collection.mutable
+ def fourSumCount(nums1: Array[Int], nums2: Array[Int], nums3: Array[Int], nums4: Array[Int]): Int = {
+ // 定义一个HashMap,key存储值,value存储该值出现的次数
+ val map = new mutable.HashMap[Int, Int]()
+ // 遍历前两个数组,把他们所有可能的情况都记录到map
+ for (i <- nums1.indices) {
+ for (j <- nums2.indices) {
+ val tmp = nums1(i) + nums2(j)
+ // 如果包含该值,则对他的key加1,不包含则添加进去
+ if (map.contains(tmp)) {
+ map.put(tmp, map.get(tmp).get + 1)
+ } else {
+ map.put(tmp, 1)
+ }
+ }
+ }
+ var res = 0 // 结果变量
+ // 遍历后两个数组
+ for (i <- nums3.indices) {
+ for (j <- nums4.indices) {
+ val tmp = -(nums3(i) + nums4(j))
+ // 如果map中存在该值,结果就+=value
+ if (map.contains(tmp)) {
+ res += map.get(tmp).get
+ }
+ }
+ }
+ // 返回最终结果,可以省略关键字return
+ res
+ }
+}
+```
-----------------------
From 6c0d4365c6ed1e9a47c5f31e14d59912648f4afa Mon Sep 17 00:00:00 2001
From: lizhendong128
Date: Fri, 13 May 2022 17:26:03 +0800
Subject: [PATCH 194/551] =?UTF-8?q?=E4=BF=AE=E6=94=B90059=E8=9E=BA?=
=?UTF-8?q?=E6=97=8B=E7=9F=A9=E9=98=B5II=20Java=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
精简Java代码中不必要的变量,调整结构,减少代码量,使代码更加易读
---
...\346\227\213\347\237\251\351\230\265II.md" | 44 +++++--------------
1 file changed, 12 insertions(+), 32 deletions(-)
diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
index 9373589515..7afc15755b 100644
--- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
+++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
@@ -130,57 +130,37 @@ Java:
```Java
class Solution {
public int[][] generateMatrix(int n) {
+ int loop = 0; // 控制循环次数
int[][] res = new int[n][n];
+ int start = 0; // 每次循环的开始点(start, start)
+ int count = 1; // 定义填充数字
+ int i, j;
- // 循环次数
- int loop = n / 2;
-
- // 定义每次循环起始位置
- int startX = 0;
- int startY = 0;
-
- // 定义偏移量
- int offset = 1;
-
- // 定义填充数字
- int count = 1;
-
- // 定义中间位置
- int mid = n / 2;
- while (loop > 0) {
- int i = startX;
- int j = startY;
-
+ while (loop++ < n / 2) { // 判断边界后,loop从1开始
// 模拟上侧从左到右
- for (; j startY; j--) {
+ for (; j >= loop; j--) {
res[i][j] = count++;
}
// 模拟左侧从下到上
- for (; i > startX; i--) {
+ for (; i >= loop; i--) {
res[i][j] = count++;
}
-
- loop--;
-
- startX += 1;
- startY += 1;
-
- offset += 2;
+ start++;
}
if (n % 2 == 1) {
- res[mid][mid] = count;
+ res[start][start] = count;
}
return res;
From 16c6abff54ec4b85d0891ed12914f538da7b8332 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 13 May 2022 21:02:07 +0800
Subject: [PATCH 195/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200383.=E8=B5=8E?=
=?UTF-8?q?=E9=87=91=E4=BF=A1.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...3.\350\265\216\351\207\221\344\277\241.md" | 64 ++++++++++++++++++-
1 file changed, 63 insertions(+), 1 deletion(-)
diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md"
index 5d9e8295c6..8e4cbbf870 100644
--- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md"
+++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md"
@@ -360,6 +360,68 @@ impl Solution {
}
}
```
-
+Scala:
+
+版本一: 使用数组作为哈希表
+```scala
+object Solution {
+ def canConstruct(ransomNote: String, magazine: String): Boolean = {
+ // 如果magazine的长度小于ransomNote的长度,必然是false
+ if (magazine.length < ransomNote.length) {
+ return false
+ }
+ // 定义一个数组,存储magazine字符出现的次数
+ val map: Array[Int] = new Array[Int](26)
+ // 遍历magazine字符串,对应的字符+=1
+ for (i <- magazine.indices) {
+ map(magazine(i) - 'a') += 1
+ }
+ // 遍历ransomNote
+ for (i <- ransomNote.indices) {
+ if (map(ransomNote(i) - 'a') > 0)
+ map(ransomNote(i) - 'a') -= 1
+ else return false
+ }
+ // 如果上面没有返回false,直接返回true,关键字return可以省略
+ true
+ }
+}
+```
+版本二: 使用HashMap
+```scala
+object Solution {
+ import scala.collection.mutable
+ def canConstruct(ransomNote: String, magazine: String): Boolean = {
+ // 如果magazine的长度小于ransomNote的长度,必然是false
+ if (magazine.length < ransomNote.length) {
+ return false
+ }
+ // 定义map,key是字符,value是字符出现的次数
+ val map = new mutable.HashMap[Char, Int]()
+ // 遍历magazine,把所有的字符都记录到map里面
+ for (i <- magazine.indices) {
+ val tmpChar = magazine(i)
+ // 如果map包含该字符,那么对应的value++,否则添加该字符
+ if (map.contains(tmpChar)) {
+ map.put(tmpChar, map.get(tmpChar).get + 1)
+ } else {
+ map.put(tmpChar, 1)
+ }
+ }
+ // 遍历ransomNote
+ for (i <- ransomNote.indices) {
+ val tmpChar = ransomNote(i)
+ // 如果map包含并且该字符的value大于0,则匹配成功,map对应的--,否则直接返回false
+ if (map.contains(tmpChar) && map.get(tmpChar).get > 0) {
+ map.put(tmpChar, map.get(tmpChar).get - 1)
+ } else {
+ return false
+ }
+ }
+ // 如果上面没有返回false,直接返回true,关键字return可以省略
+ true
+ }
+}
+```
-----------------------
From ca2711164dfd39b51f1e6d96207673ffba29d3f1 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 13 May 2022 21:11:48 +0800
Subject: [PATCH 196/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880714.?=
=?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3?=
=?UTF-8?q?=E6=97=B6=E6=9C=BA=E5=90=AB=E6=89=8B=E7=BB=AD=E8=B4=B9=E5=8A=A8?=
=?UTF-8?q?=E6=80=81=E8=A7=84=E5=88=92.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...01\350\247\204\345\210\222\357\274\211.md" | 23 +++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
index 4ab63e7925..5625604b9d 100644
--- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
+++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
@@ -200,6 +200,29 @@ const maxProfit = (prices,fee) => {
}
```
+TypeScript:
+
+```typescript
+function maxProfit(prices: number[], fee: number): number {
+ /**
+ dp[i][0]:持有股票
+ dp[i][1]: 不持有
+ */
+ const length: number = prices.length;
+ if (length === 0) return 0;
+ const dp: number[][] = new Array(length).fill(0).map(_ => []);
+ dp[0][0] = -prices[0];
+ dp[0][1] = 0;
+ for (let i = 1; i < length; i++) {
+ dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
+ dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee);
+ }
+ return dp[length - 1][1];
+};
+```
+
+
+
-----------------------
From a53da7b4e205468bb7f870b5b46093c42923d429 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 14 May 2022 14:41:14 +0800
Subject: [PATCH 197/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200015.=E4=B8=89?=
=?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...11\346\225\260\344\271\213\345\222\214.md" | 45 ++++++++++++++++++-
1 file changed, 44 insertions(+), 1 deletion(-)
diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
index cc184c879e..1764d244b6 100644
--- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
@@ -616,6 +616,49 @@ public class Solution
}
}
```
-
+Scala:
+```scala
+object Solution {
+ // 导包
+ import scala.collection.mutable.ListBuffer
+ import scala.util.control.Breaks.{break, breakable}
+
+ def threeSum(nums: Array[Int]): List[List[Int]] = {
+ // 定义结果集,最后需要转换为List
+ val res = ListBuffer[List[Int]]()
+ val nums_tmp = nums.sorted // 对nums进行排序
+ for (i <- nums_tmp.indices) {
+ // 如果要排的第一个数字大于0,直接返回结果
+ if (nums_tmp(i) > 0) {
+ return res.toList
+ }
+ // 如果i大于0并且和前一个数字重复,则跳过本次循环,相当于continue
+ breakable {
+ if (i > 0 && nums_tmp(i) == nums_tmp(i - 1)) {
+ break
+ } else {
+ var left = i + 1
+ var right = nums_tmp.length - 1
+ while (left < right) {
+ var sum = nums_tmp(i) + nums_tmp(left) + nums_tmp(right) // 求三数之和
+ if (sum < 0) left += 1
+ else if (sum > 0) right -= 1
+ else {
+ res += List(nums_tmp(i), nums_tmp(left), nums_tmp(right)) // 如果等于0 添加进结果集
+ // 为了避免重复,对left和right进行移动
+ while (left < right && nums_tmp(left) == nums_tmp(left + 1)) left += 1
+ while (left < right && nums_tmp(right) == nums_tmp(right - 1)) right -= 1
+ left += 1
+ right -= 1
+ }
+ }
+ }
+ }
+ }
+ // 最终返回需要转换为List,return关键字可以省略
+ res.toList
+ }
+}
+```
-----------------------
From 68eed4af5b1f43e7ac65093dd00c7497f88885cc Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 14 May 2022 15:26:29 +0800
Subject: [PATCH 198/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200018.=E5=9B=9B?=
=?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...33\346\225\260\344\271\213\345\222\214.md" | 45 ++++++++++++++++++-
1 file changed, 44 insertions(+), 1 deletion(-)
diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
index ee70cb6909..6cbd40c288 100644
--- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
@@ -522,6 +522,49 @@ public class Solution
}
}
```
-
+Scala:
+```scala
+object Solution {
+ // 导包
+ import scala.collection.mutable.ListBuffer
+ import scala.util.control.Breaks.{break, breakable}
+ def fourSum(nums: Array[Int], target: Int): List[List[Int]] = {
+ val res = ListBuffer[List[Int]]()
+ val nums_tmp = nums.sorted // 先排序
+ for (i <- nums_tmp.indices) {
+ breakable {
+ if (i > 0 && nums_tmp(i) == nums_tmp(i - 1)) {
+ break // 如果该值和上次的值相同,跳过本次循环,相当于continue
+ } else {
+ for (j <- i + 1 until nums_tmp.length) {
+ breakable {
+ if (j > i + 1 && nums_tmp(j) == nums_tmp(j - 1)) {
+ break // 同上
+ } else {
+ // 双指针
+ var (left, right) = (j + 1, nums_tmp.length - 1)
+ while (left < right) {
+ var sum = nums_tmp(i) + nums_tmp(j) + nums_tmp(left) + nums_tmp(right)
+ if (sum == target) {
+ // 满足要求,直接加入到集合里面去
+ res += List(nums_tmp(i), nums_tmp(j), nums_tmp(left), nums_tmp(right))
+ while (left < right && nums_tmp(left) == nums_tmp(left + 1)) left += 1
+ while (left < right && nums_tmp(right) == nums_tmp(right - 1)) right -= 1
+ left += 1
+ right -= 1
+ } else if (sum < target) left += 1
+ else right -= 1
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ // 最终返回的res要转换为List,return关键字可以省略
+ res.toList
+ }
+}
+```
-----------------------
From e4da60add91e39177250f6808c3846c9b563612a Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 14 May 2022 15:44:42 +0800
Subject: [PATCH 199/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200344.=E5=8F=8D?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...5\254\345\255\227\347\254\246\344\270\262.md" | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md"
index 58bada05b9..e1f27bd7c5 100644
--- "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md"
@@ -266,6 +266,20 @@ public class Solution
}
}
```
-
+Scala:
+```scala
+object Solution {
+ def reverseString(s: Array[Char]): Unit = {
+ var (left, right) = (0, s.length - 1)
+ while (left < right) {
+ var tmp = s(left)
+ s(left) = s(right)
+ s(right) = tmp
+ left += 1
+ right -= 1
+ }
+ }
+}
+```
-----------------------
From f2dcdbe94757d3ef88278e4f8c80281ce7baf5c9 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 14 May 2022 16:32:23 +0800
Subject: [PATCH 200/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200541.=E5=8F=8D?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2II.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\345\255\227\347\254\246\344\270\262II.md" | 43 ++++++++++++++++++-
1 file changed, 42 insertions(+), 1 deletion(-)
diff --git "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md"
index 8c13a39021..99d6ebe04e 100644
--- "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md"
+++ "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md"
@@ -347,6 +347,47 @@ public class Solution
}
}
```
-
+Scala:
+
+版本一: (正常解法)
+```scala
+object Solution {
+ def reverseStr(s: String, k: Int): String = {
+ val res = s.toCharArray // 转换为Array好处理
+ for (i <- s.indices by 2 * k) {
+ // 如果i+k大于了res的长度,则需要全部翻转
+ if (i + k > res.length) {
+ reverse(res, i, s.length - 1)
+ } else {
+ reverse(res, i, i + k - 1)
+ }
+ }
+ new String(res)
+ }
+ // 翻转字符串,从start到end
+ def reverse(s: Array[Char], start: Int, end: Int): Unit = {
+ var (left, right) = (start, end)
+ while (left < right) {
+ var tmp = s(left)
+ s(left) = s(right)
+ s(right) = tmp
+ left += 1
+ right -= 1
+ }
+ }
+}
+```
+版本二: 首先利用sliding每隔k个进行分割,随后转换为数组,再使用zipWithIndex添加每个数组的索引,紧接着利用map做变换,如果索引%2==0则说明需要翻转,否则原封不动,最后再转换为String
+```scala
+object Solution {
+ def reverseStr(s: String, k: Int): String = {
+ s.sliding(k, k)
+ .toArray
+ .zipWithIndex
+ .map(v => if (v._2 % 2 == 0) v._1.reverse else v._1)
+ .mkString
+ }
+}
+```
-----------------------
From 037bebbe90eb7bd607314d5d3d5d63e506f5aeeb Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 14 May 2022 17:04:40 +0800
Subject: [PATCH 201/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=89=91=E6=8C=87?=
=?UTF-8?q?Offer05.=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...77\346\215\242\347\251\272\346\240\274.md" | 56 ++++++++++++++++++-
1 file changed, 55 insertions(+), 1 deletion(-)
diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md"
index 037bd42773..eecd7f0c56 100644
--- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md"
+++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md"
@@ -413,8 +413,62 @@ func replaceSpace(_ s: String) -> String {
}
```
+Scala:
-
+方式一: 双指针
+```scala
+object Solution {
+ def replaceSpace(s: String): String = {
+ var count = 0
+ s.foreach(c => if (c == ' ') count += 1) // 统计空格的数量
+ val sOldSize = s.length // 旧数组字符串长度
+ val sNewSize = s.length + count * 2 // 新数组字符串长度
+ val res = new Array[Char](sNewSize) // 新数组
+ var index = sNewSize - 1 // 新数组索引
+ // 逆序遍历
+ for (i <- (0 until sOldSize).reverse) {
+ if (s(i) == ' ') {
+ res(index) = '0'
+ index -= 1
+ res(index) = '2'
+ index -= 1
+ res(index) = '%'
+ } else {
+ res(index) = s(i)
+ }
+ index -= 1
+ }
+ res.mkString
+ }
+}
+```
+方式二: 使用一个集合,遇到空格就添加%20
+```scala
+object Solution {
+ import scala.collection.mutable.ListBuffer
+ def replaceSpace(s: String): String = {
+ val res: ListBuffer[Char] = ListBuffer[Char]()
+ for (i <- s.indices) {
+ if (s(i) == ' ') {
+ res += '%'
+ res += '2'
+ res += '0'
+ }else{
+ res += s(i)
+ }
+ }
+ res.mkString
+ }
+}
+```
+方式三: 使用map
+```scala
+object Solution {
+ def replaceSpace(s: String): String = {
+ s.map(c => if(c == ' ') "%20" else c).mkString
+ }
+}
+```
-----------------------
From 1c369bb83631f4449af7e646bc26bc86aacc5442 Mon Sep 17 00:00:00 2001
From: 3Xpl0it3r
Date: Sat, 14 May 2022 23:24:57 +0800
Subject: [PATCH 202/551] 102 in rust
---
...02\345\272\217\351\201\215\345\216\206.md" | 59 +++++++++++++++++++
1 file changed, 59 insertions(+)
diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
index ab8f2e572e..1326781959 100644
--- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
+++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
@@ -299,6 +299,36 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] {
}
```
+Rust:
+
+```rust
+pub fn level_order(root: Option>>) -> Vec> {
+ let mut ans = Vec::new();
+ let mut stack = Vec::new();
+ if root.is_none(){
+ return ans;
+ }
+ stack.push(root.unwrap());
+ while stack.is_empty()!= true{
+ let num = stack.len();
+ let mut level = Vec::new();
+ for _i in 0..num{
+ let tmp = stack.remove(0);
+ level.push(tmp.borrow_mut().val);
+ if tmp.borrow_mut().left.is_some(){
+ stack.push(tmp.borrow_mut().left.take().unwrap());
+ }
+ if tmp.borrow_mut().right.is_some(){
+ stack.push(tmp.borrow_mut().right.take().unwrap());
+ }
+ }
+ ans.push(level);
+ }
+ ans
+}
+```
+
+
**此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!**
@@ -528,6 +558,35 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] {
}
```
+Rust:
+
+```rust
+pub fn level_order(root: Option>>) -> Vec> {
+ let mut ans = Vec::new();
+ let mut stack = Vec::new();
+ if root.is_none(){
+ return ans;
+ }
+ stack.push(root.unwrap());
+ while stack.is_empty()!= true{
+ let num = stack.len();
+ let mut level = Vec::new();
+ for _i in 0..num{
+ let tmp = stack.remove(0);
+ level.push(tmp.borrow_mut().val);
+ if tmp.borrow_mut().left.is_some(){
+ stack.push(tmp.borrow_mut().left.take().unwrap());
+ }
+ if tmp.borrow_mut().right.is_some(){
+ stack.push(tmp.borrow_mut().right.take().unwrap());
+ }
+ }
+ ans.push(level);
+ }
+ ans
+}
+```
+
# 199.二叉树的右视图
[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-right-side-view/)
From 5da6c06ef9fd308e9ab77b8393e81377eec75821 Mon Sep 17 00:00:00 2001
From: unknown
Date: Sat, 14 May 2022 16:27:47 +0100
Subject: [PATCH 203/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C=E5=8C=85?=
=?UTF-8?q?=E7=90=86=E8=AE=BA=E8=82=8C=E9=86=8701=E8=83=8C=E5=8C=85=20C?=
=?UTF-8?q?=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\241\20001\350\203\214\345\214\205-1.md" | 46 +++++++++++++++++++
1 file changed, 46 insertions(+)
diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
index fe940b4ca4..43ad26be33 100644
--- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
+++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
@@ -423,5 +423,51 @@ function test () {
test();
```
+### C
+```c
+#include
+#include
+#include
+
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+#define ARR_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
+#define BAG_WEIGHT 4
+
+void backPack(int* weights, int weightSize, int* costs, int costSize, int bagWeight) {
+ // 开辟dp数组
+ int dp[weightSize][bagWeight + 1];
+ memset(dp, 0, sizeof(int) * weightSize * (bagWeight + 1));
+
+ int i, j;
+ // 当背包容量大于物品0的重量时,将物品0放入到背包中
+ for(j = weights[0]; j <= bagWeight; ++j) {
+ dp[0][j] = costs[0];
+ }
+
+ // 先遍历物品,再遍历重量
+ for(j = 1; j <= bagWeight; ++j) {
+ for(i = 1; i < weightSize; ++i) {
+ // 如果当前背包容量小于物品重量
+ if(j < weights[i])
+ // 背包物品的价值等于背包不放置当前物品时的价值
+ dp[i][j] = dp[i-1][j];
+ // 若背包当前重量可以放置物品
+ else
+ // 背包的价值等于放置该物品或不放置该物品的最大值
+ dp[i][j] = MAX(dp[i - 1][j], dp[i - 1][j - weights[i]] + costs[i]);
+ }
+ }
+
+ printf("%d\n", dp[weightSize - 1][bagWeight]);
+}
+
+int main(int argc, char* argv[]) {
+ int weights[] = {1, 3, 4};
+ int costs[] = {15, 20, 30};
+ backPack(weights, ARR_SIZE(weights), costs, ARR_SIZE(costs), BAG_WEIGHT);
+ return 0;
+}
+```
+
-----------------------
From ab4d42cf0c41ce9089838dee087e961aac1b6357 Mon Sep 17 00:00:00 2001
From: n4feng
Date: Sat, 14 May 2022 12:52:11 -0400
Subject: [PATCH 204/551] =?UTF-8?q?Update=200739.=E6=AF=8F=E6=97=A5?=
=?UTF-8?q?=E6=B8=A9=E5=BA=A6.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
少了一个关键词“高”
---
.../0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
index 5f53e4127d..367e521a6c 100644
--- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
+++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
@@ -34,7 +34,7 @@
那么单调栈的原理是什么呢?为什么时间复杂度是O(n)就可以找到每一个元素的右边第一个比它大的元素位置呢?
-单调栈的本质是空间换时间,因为在遍历的过程中需要用一个栈来记录右边第一个比当前元素的元素,优点是只需要遍历一次。
+单调栈的本质是空间换时间,因为在遍历的过程中需要用一个栈来记录右边第一个比当前元素高的元素,优点是只需要遍历一次。
在使用单调栈的时候首先要明确如下几点:
From 78b367e5c5ee493a96cfe4ebcb440059909db285 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 15 May 2022 12:57:39 +0800
Subject: [PATCH 205/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200151.=E7=BF=BB?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95?=
=?UTF-8?q?=E8=AF=8D.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...14\347\232\204\345\215\225\350\257\215.md" | 56 +++++++++++++++++++
1 file changed, 56 insertions(+)
diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
index d03de42146..0e25fc4d8c 100644
--- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
+++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
@@ -758,7 +758,63 @@ func reverseWord(_ s: inout [Character]) {
}
```
+Scala:
+
+```scala
+object Solution {
+ def reverseWords(s: String): String = {
+ var sb = removeSpace(s) // 移除多余的空格
+ reverseString(sb, 0, sb.length - 1) // 翻转字符串
+ reverseEachWord(sb)
+ sb.mkString
+ }
+
+ // 移除多余的空格
+ def removeSpace(s: String): Array[Char] = {
+ var start = 0
+ var end = s.length - 1
+ // 移除字符串前面的空格
+ while (start < s.length && s(start) == ' ') start += 1
+ // 移除字符串后面的空格
+ while (end >= 0 && s(end) == ' ') end -= 1
+ var sb = "" // String
+ // 当start小于等于end的时候,执行添加操作
+ while (start <= end) {
+ var c = s(start)
+ // 当前字符不等于空,sb的最后一个字符不等于空的时候添加到sb
+ if (c != ' ' || sb(sb.length - 1) != ' ') {
+ sb ++= c.toString
+ }
+ start += 1 // 指针向右移动
+ }
+ sb.toArray
+ }
+
+ // 翻转字符串
+ def reverseString(s: Array[Char], start: Int, end: Int): Unit = {
+ var (left, right) = (start, end)
+ while (left < right) {
+ var tmp = s(left)
+ s(left) = s(right)
+ s(right) = tmp
+ left += 1
+ right -= 1
+ }
+ }
+ // 翻转每个单词
+ def reverseEachWord(s: Array[Char]): Unit = {
+ var i = 0
+ while (i < s.length) {
+ var j = i + 1
+ // 向后迭代寻找每个单词的坐标
+ while (j < s.length && s(j) != ' ') j += 1
+ reverseString(s, i, j - 1) // 翻转每个单词
+ i = j + 1 // i往后更新
+ }
+ }
+}
+```
From c7d356f8559fd2c46eb4e9e2889fdbe0bdaafed1 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 15 May 2022 12:59:38 +0800
Subject: [PATCH 206/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=89=91=E6=8C=87?=
=?UTF-8?q?Offer58-II.=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97=E7=AC=A6?=
=?UTF-8?q?=E4=B8=B2.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...54\345\255\227\347\254\246\344\270\262.md" | 29 ++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md"
index fec83e1d5a..581ac1c7cc 100644
--- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md"
@@ -290,7 +290,34 @@ func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) {
}
```
-
+Scala:
+
+```scala
+object Solution {
+ def reverseLeftWords(s: String, n: Int): String = {
+ var str = s.toCharArray // 转换为Array
+ // abcdefg => ba cdefg
+ reverseString(str, 0, n - 1)
+ // ba cdefg => ba gfedc
+ reverseString(str, n, str.length - 1)
+ // ba gfedc => cdefgab
+ reverseString(str, 0, str.length - 1)
+ // 最终返回,return关键字可以省略
+ new String(str)
+ }
+ // 翻转字符串
+ def reverseString(s: Array[Char], start: Int, end: Int): Unit = {
+ var (left, right) = (start, end)
+ while (left < right) {
+ var tmp = s(left)
+ s(left) = s(right)
+ s(right) = tmp
+ left += 1
+ right -= 1
+ }
+ }
+}
+```
From a5db99363b1bca72345036228776a0320242c8c4 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Sun, 15 May 2022 15:35:08 +0800
Subject: [PATCH 207/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880035.?=
=?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0PHP=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...22\345\205\245\344\275\215\347\275\256.md" | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
index 9a770703e3..8a8f970614 100644
--- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
+++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
@@ -318,6 +318,31 @@ func searchInsert(_ nums: [Int], _ target: Int) -> Int {
```
+### PHP
+
+```php
+// 二分法(1):[左闭右闭]
+function searchInsert($nums, $target)
+{
+ $n = count($nums);
+ $l = 0;
+ $r = $n - 1;
+ while ($l <= $r) {
+ $mid = floor(($l + $r) / 2);
+ if ($nums[$mid] > $target) {
+ // 下次搜索在左区间:[$l,$mid-1]
+ $r = $mid - 1;
+ } else if ($nums[$mid] < $target) {
+ // 下次搜索在右区间:[$mid+1,$r]
+ $l = $mid + 1;
+ } else {
+ // 命中返回
+ return $mid;
+ }
+ }
+ return $r + 1;
+}
+```
-----------------------
From f1061d0f8962fe8e4a4c0691d8401ee5fe505f99 Mon Sep 17 00:00:00 2001
From: GitHubQAQ <31883473+GitHubQAQ@users.noreply.github.com>
Date: Sun, 15 May 2022 19:12:13 +0800
Subject: [PATCH 208/551] =?UTF-8?q?Update=200056.=E5=90=88=E5=B9=B6?=
=?UTF-8?q?=E5=8C=BA=E9=97=B4.md=20=20-=20-=E8=A7=84=E8=8C=83C++=E4=BB=A3?=
=?UTF-8?q?=E7=A0=81=E4=B8=AD=E7=9A=84lambda=E8=A1=A8=E8=BF=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
index b44d602c5c..52a98d707a 100644
--- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
+++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
@@ -96,7 +96,7 @@ public:
vector> merge(vector>& intervals) {
vector> result;
if (intervals.size() == 0) return result;
- // 排序的参数使用了lamda表达式
+ // 排序的参数使用了lambda表达式
sort(intervals.begin(), intervals.end(), [](const vector& a, const vector& b){return a[0] < b[0];});
result.push_back(intervals[0]);
From 224b3d293b6b2c9465e00b2127cf1d75f44775df Mon Sep 17 00:00:00 2001
From: wangning
Date: Sun, 15 May 2022 20:15:11 +0800
Subject: [PATCH 209/551] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A0404?=
=?UTF-8?q?=E5=B7=A6=E5=8F=B6=E5=AD=90=E4=B9=8B=E5=92=8Cjavascript?=
=?UTF-8?q?=E7=89=88=E6=9C=AC=E8=B0=83=E7=94=A8=E6=96=B9=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...67\246\345\217\266\345\255\220\344\271\213\345\222\214.md" | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md"
index d7fd629e68..4ef68d3b4e 100644
--- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md"
+++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md"
@@ -336,8 +336,8 @@ var sumOfLeftLeaves = function(root) {
if(node===null){
return 0;
}
- let leftValue = sumOfLeftLeaves(node.left);
- let rightValue = sumOfLeftLeaves(node.right);
+ let leftValue = nodesSum(node.left);
+ let rightValue = nodesSum(node.right);
// 3. 单层递归逻辑
let midValue = 0;
if(node.left&&node.left.left===null&&node.left.right===null){
From aa22b802a5986f06a123c59f395bf49aba779d1a Mon Sep 17 00:00:00 2001
From: unknown
Date: Sun, 15 May 2022 14:39:41 +0100
Subject: [PATCH 210/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C=E5=8C=85?=
=?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001-2.mc=20C=E8=AF=AD?=
=?UTF-8?q?=E8=A8=80=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\241\20001\350\203\214\345\214\205-2.md" | 33 +++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md"
index dabdfb2d09..a99a872b14 100644
--- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md"
+++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md"
@@ -315,6 +315,39 @@ function test () {
test();
```
+### C
+```c
+#include
+#include
+
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+#define ARR_SIZE(arr) ((sizeof((arr))) / sizeof((arr)[0]))
+#define BAG_WEIGHT 4
+
+void test_back_pack(int* weights, int weightSize, int* values, int valueSize, int bagWeight) {
+ int dp[bagWeight + 1];
+ memset(dp, 0, sizeof(int) * (bagWeight + 1));
+
+ int i, j;
+ // 先遍历物品
+ for(i = 0; i < weightSize; ++i) {
+ // 后遍历重量。从后向前遍历
+ for(j = bagWeight; j >= weights[i]; --j) {
+ dp[j] = MAX(dp[j], dp[j - weights[i]] + values[i]);
+ }
+ }
+
+ // 打印最优结果
+ printf("%d\n", dp[bagWeight]);
+}
+
+int main(int argc, char** argv) {
+ int weights[] = {1, 3, 4};
+ int values[] = {15, 20, 30};
+ test_back_pack(weights, ARR_SIZE(weights), values, ARR_SIZE(values), BAG_WEIGHT);
+ return 0;
+}
+```
-----------------------
From 19abe18019814af74ee51ba56e4e8ee6106b1253 Mon Sep 17 00:00:00 2001
From: unknown
Date: Sun, 15 May 2022 15:26:23 +0100
Subject: [PATCH 211/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200416.=E5=88=86?=
=?UTF-8?q?=E5=89=B2=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86.md=20C=E8=AF=AD?=
=?UTF-8?q?=E8=A8=80=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...11\345\222\214\345\255\220\351\233\206.md" | 100 ++++++++++++++++++
1 file changed, 100 insertions(+)
diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
index 6e93ae8e80..76cfd87f99 100644
--- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
+++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
@@ -416,6 +416,106 @@ var canPartition = function(nums) {
};
```
+C:
+二维dp:
+```c
+/**
+1. dp数组含义:dp[i][j]为背包重量为j时,从[0-i]元素和最大值
+2. 递推公式:dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - nums[i]] + nums[i])
+3. 初始化:dp[i][0]初始化为0。因为背包重量为0时,不可能放入元素。dp[0][j] = nums[0],当j >= nums[0] && j < target时
+4. 遍历顺序:先遍历物品,再遍历背包
+*/
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+
+int getSum(int* nums, int numsSize) {
+ int sum = 0;
+
+ int i;
+ for(i = 0; i < numsSize; ++i) {
+ sum += nums[i];
+ }
+ return sum;
+}
+
+bool canPartition(int* nums, int numsSize){
+ // 求出元素总和
+ int sum = getSum(nums, numsSize);
+ // 若元素总和为奇数,则不可能得到两个和相等的子数组
+ if(sum % 2)
+ return false;
+
+ // 若子数组的和等于target,则nums可以被分割
+ int target = sum / 2;
+ // 初始化dp数组
+ int dp[numsSize][target + 1];
+ // dp[j][0]都应被设置为0。因为当背包重量为0时,不可放入元素
+ memset(dp, 0, sizeof(int) * numsSize * (target + 1));
+
+ int i, j;
+ // 当背包重量j大于nums[0]时,可以在dp[0][j]中放入元素nums[0]
+ for(j = nums[0]; j <= target; ++j) {
+ dp[0][j] = nums[0];
+ }
+
+ for(i = 1; i < numsSize; ++i) {
+ for(j = 1; j <= target; ++j) {
+ // 若当前背包重量j小于nums[i],则其值等于只考虑0到i-1物品时的值
+ if(j < nums[i])
+ dp[i][j] = dp[i - 1][j];
+ // 否则,背包重量等于在背包中放入num[i]/不放入nums[i]的较大值
+ else
+ dp[i][j] = MAX(dp[i - 1][j], dp[i - 1][j - nums[i]] + nums[i]);
+ }
+ }
+ // 判断背包重量为target,且考虑到所有物品时,放入的元素和是否等于target
+ return dp[numsSize - 1][target] == target;
+}
+```
+滚动数组:
+```c
+/**
+1. dp数组含义:dp[j]为背包重量为j时,其中可放入元素的最大值
+2. 递推公式:dp[j] = max(dp[j], dp[j - nums[i]] + nums[i])
+3. 初始化:均初始化为0即可
+4. 遍历顺序:先遍历物品,再后序遍历背包
+*/
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+
+int getSum(int* nums, int numsSize) {
+ int sum = 0;
+
+ int i;
+ for(i = 0; i < numsSize; ++i) {
+ sum += nums[i];
+ }
+ return sum;
+}
+
+bool canPartition(int* nums, int numsSize){
+ // 求出元素总和
+ int sum = getSum(nums, numsSize);
+ // 若元素总和为奇数,则不可能得到两个和相等的子数组
+ if(sum % 2)
+ return false;
+ // 背包容量
+ int target = sum / 2;
+
+ // 初始化dp数组,元素均为0
+ int dp[target + 1];
+ memset(dp, 0, sizeof(int) * (target + 1));
+
+ int i, j;
+ // 先遍历物品,后遍历背包
+ for(i = 0; i < numsSize; ++i) {
+ for(j = target; j >= nums[i]; --j) {
+ dp[j] = MAX(dp[j], dp[j - nums[i]] + nums[i]);
+ }
+ }
+
+ // 查看背包容量为target时,元素总和是否等于target
+ return dp[target] == target;
+}
+```
From 14afd23b9ea5e491a60d91f07c0b8211f4f9ae00 Mon Sep 17 00:00:00 2001
From: HanMengnan <1448189829@qq.com>
Date: Mon, 16 May 2022 10:46:43 +0800
Subject: [PATCH 212/551] =?UTF-8?q?=E6=9B=B4=E6=96=B00516.=E6=9C=80?=
=?UTF-8?q?=E9=95=BF=E5=9B=9E=E6=96=87=E5=AD=90=E5=BA=8F=E5=88=97=E7=9A=84?=
=?UTF-8?q?Go=E5=AE=9E=E7=8E=B0=E6=8F=90=E4=BE=9B=E4=BA=86=E4=B8=80?=
=?UTF-8?q?=E7=A7=8D=E6=9B=B4=E4=BC=98=E7=9A=84=E6=96=B9=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...07\345\255\220\345\272\217\345\210\227.md" | 35 +++++++++----------
1 file changed, 17 insertions(+), 18 deletions(-)
diff --git "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md"
index 69536cef56..63120b141b 100644
--- "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md"
@@ -186,29 +186,28 @@ class Solution:
Go:
```Go
func longestPalindromeSubseq(s string) int {
- lenth:=len(s)
- dp:=make([][]int,lenth)
- for i:=0;i b {
+ return a
}
+ return b
}
- for i:=lenth-1;i>=0;i--{
- for j:=i+1;j= 0; i-- {
+ for j := i + 1; j < size; j++ {
+ if s[i] == s[j] {
+ dp[i][j] = dp[i+1][j-1] + 2
+ } else {
+ dp[i][j] = max(dp[i][j-1], dp[i+1][j])
}
}
}
-
- return dp[0][lenth-1]
+ return dp[0][size-1]
}
```
From 9ac50a9a9183a3f81fd0f026950bf6fbda21f55e Mon Sep 17 00:00:00 2001
From: h4 <20080114+tan-i-ham@users.noreply.github.com>
Date: Mon, 16 May 2022 14:51:05 +0900
Subject: [PATCH 213/551] chore: Add new solution to LeetCode 203
Without using pre Node
---
...76\350\241\250\345\205\203\347\264\240.md" | 21 +++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
index c34831b768..9cedfe93a9 100644
--- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
+++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
@@ -234,6 +234,27 @@ public ListNode removeElements(ListNode head, int val) {
}
return head;
}
+/**
+ * 不添加虚拟节点and pre Node方式
+ * 时间复杂度 O(n)
+ * 空间复杂度 O(1)
+ * @param head
+ * @param val
+ * @return
+ */
+public ListNode removeElements(ListNode head, int val) {
+ while(head!=null && head.val==val){
+ head = head.next;
+ }
+ ListNode curr = head;
+ while(curr!=null){
+ while(curr.next!=null && curr.next.val == val){
+ curr.next = curr.next.next;
+ }
+ curr = curr.next;
+ }
+ return head;
+}
```
Python:
From 08147d132c4611e190d03a47bc870294d3cc1292 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 16 May 2022 14:28:02 +0800
Subject: [PATCH 214/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200232.=E7=94=A8?=
=?UTF-8?q?=E6=A0=88=E5=AE=9E=E7=8E=B0=E9=98=9F=E5=88=97.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...36\347\216\260\351\230\237\345\210\227.md" | 41 ++++++++++++++++++-
1 file changed, 40 insertions(+), 1 deletion(-)
diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md"
index 1a56d9f386..d9ba8e2632 100644
--- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md"
+++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md"
@@ -495,6 +495,45 @@ void myQueueFree(MyQueue* obj) {
obj->stackOutTop = 0;
}
```
-
+Scala:
+```scala
+class MyQueue() {
+ import scala.collection.mutable
+ val stackIn = mutable.Stack[Int]() // 负责出栈
+ val stackOut = mutable.Stack[Int]() // 负责入栈
+
+ // 添加元素
+ def push(x: Int) {
+ stackIn.push(x)
+ }
+
+ // 复用代码,如果stackOut为空就把stackIn的所有元素都压入StackOut
+ def dumpStackIn(): Unit = {
+ if (!stackOut.isEmpty) return
+ while (!stackIn.isEmpty) {
+ stackOut.push(stackIn.pop())
+ }
+ }
+
+ // 弹出元素
+ def pop(): Int = {
+ dumpStackIn()
+ stackOut.pop()
+ }
+
+ // 获取队头
+ def peek(): Int = {
+ dumpStackIn()
+ val res: Int = stackOut.pop()
+ stackOut.push(res)
+ res
+ }
+
+ // 判断是否为空
+ def empty(): Boolean = {
+ stackIn.isEmpty && stackOut.isEmpty
+ }
+}
+```
-----------------------
From f4d98a744ee9c14ed88d07be559aa310508d89f9 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 16 May 2022 14:56:10 +0800
Subject: [PATCH 215/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200225.=E7=94=A8?=
=?UTF-8?q?=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...27\345\256\236\347\216\260\346\240\210.md" | 83 +++++++++++++++++++
1 file changed, 83 insertions(+)
diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md"
index 3457c4b368..3c134870ae 100644
--- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md"
+++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md"
@@ -815,6 +815,89 @@ class MyStack {
}
}
```
+Scala:
+使用两个队列模拟栈:
+```scala
+import scala.collection.mutable
+
+class MyStack() {
+
+ val queue1 = new mutable.Queue[Int]()
+ val queue2 = new mutable.Queue[Int]()
+
+ def push(x: Int) {
+ queue1.enqueue(x)
+ }
+
+ def pop(): Int = {
+ var size = queue1.size
+ // 将queue1中的每个元素都移动到queue2
+ for (i <- 0 until size - 1) {
+ queue2.enqueue(queue1.dequeue())
+ }
+ var res = queue1.dequeue()
+ // 再将queue2中的每个元素都移动到queue1
+ while (!queue2.isEmpty) {
+ queue1.enqueue(queue2.dequeue())
+ }
+ res
+ }
+
+ def top(): Int = {
+ var size = queue1.size
+ for (i <- 0 until size - 1) {
+ queue2.enqueue(queue1.dequeue())
+ }
+ var res = queue1.dequeue()
+ while (!queue2.isEmpty) {
+ queue1.enqueue(queue2.dequeue())
+ }
+ // 最终还需要把res送进queue1
+ queue1.enqueue(res)
+ res
+ }
+
+ def empty(): Boolean = {
+ queue1.isEmpty
+ }
+}
+```
+使用一个队列模拟:
+```scala
+import scala.collection.mutable
+
+class MyStack() {
+
+ val queue = new mutable.Queue[Int]()
+
+ def push(x: Int) {
+ queue.enqueue(x)
+ }
+
+ def pop(): Int = {
+ var size = queue.size
+ for (i <- 0 until size - 1) {
+ queue.enqueue(queue.head) // 把头添到队列最后
+ queue.dequeue() // 再出队
+ }
+ queue.dequeue()
+ }
+
+ def top(): Int = {
+ var size = queue.size
+ var res = 0
+ for (i <- 0 until size) {
+ queue.enqueue(queue.head) // 把头添到队列最后
+ res = queue.dequeue() // 再出队
+ }
+ res
+ }
+
+ def empty(): Boolean = {
+ queue.isEmpty
+ }
+}
+```
-----------------------
From 8df1b4b237552c189fa1a5788442c422c9296d07 Mon Sep 17 00:00:00 2001
From: madeai
Date: Mon, 16 May 2022 16:38:53 +0800
Subject: [PATCH 216/551] =?UTF-8?q?Update=200739.=E6=AF=8F=E6=97=A5?=
=?UTF-8?q?=E6=B8=A9=E5=BA=A6.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
index 5f53e4127d..2305d13579 100644
--- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
+++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
@@ -192,7 +192,7 @@ class Solution {
否则的话,可以直接入栈。
注意,单调栈里 加入的元素是 下标。
*/
- Stackstack=new Stack<>();
+ Deque stack=new LinkedList<>();
stack.push(0);
for(int i=1;istack=new Stack<>();
+ Deque stack=new LinkedList<>();
for(int i=0;itemperatures[stack.peek()]){
From 61f5d920d05ea01858272bc04f8bcea0a89d6991 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 16 May 2022 17:07:23 +0800
Subject: [PATCH 217/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200020.=E6=9C=89?=
=?UTF-8?q?=E6=95=88=E7=9A=84=E6=8B=AC=E5=8F=B7.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...10\347\232\204\346\213\254\345\217\267.md" | 23 ++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md"
index 7bb7f7463a..a0df0d07fc 100644
--- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md"
+++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md"
@@ -400,6 +400,27 @@ bool isValid(char * s){
return !stackTop;
}
```
-
+Scala:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def isValid(s: String): Boolean = {
+ if(s.length % 2 != 0) return false // 如果字符串长度是奇数直接返回false
+ val stack = mutable.Stack[Char]()
+ // 循环遍历字符串
+ for (i <- s.indices) {
+ val c = s(i)
+ if (c == '(' || c == '[' || c == '{') stack.push(c)
+ else if(stack.isEmpty) return false // 如果没有(、[、{则直接返回false
+ // 以下三种情况,不满足则直接返回false
+ else if(c==')' && stack.pop() != '(') return false
+ else if(c==']' && stack.pop() != '[') return false
+ else if(c=='}' && stack.pop() != '{') return false
+ }
+ // 如果为空则正确匹配,否则还有余孽就不匹配
+ stack.isEmpty
+ }
+}
+```
-----------------------
From 98bdccbe16bbb6c8c66e027d901f4fbd3baafffe Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 16 May 2022 17:24:47 +0800
Subject: [PATCH 218/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201047.=E5=88=A0?=
=?UTF-8?q?=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80?=
=?UTF-8?q?=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...73\351\207\215\345\244\215\351\241\271.md" | 23 ++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md"
index 638c8f4ed2..a92a3911e1 100644
--- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md"
+++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md"
@@ -374,6 +374,27 @@ func removeDuplicates(_ s: String) -> String {
return String(stack)
}
```
-
+Scala:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def removeDuplicates(s: String): String = {
+ var stack = mutable.Stack[Int]()
+ var str = "" // 保存最终结果
+ for (i <- s.indices) {
+ var tmp = s(i)
+ // 如果栈非空并且栈顶元素等于当前字符,那么删掉栈顶和字符串最后一个元素
+ if (!stack.isEmpty && tmp == stack.head) {
+ str = str.take(str.length - 1)
+ stack.pop()
+ } else {
+ stack.push(tmp)
+ str += tmp
+ }
+ }
+ str
+ }
+}
+```
-----------------------
From 8c394c9f8588a1e94250dc93bede24b7380996f8 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 17 May 2022 17:08:11 +0800
Subject: [PATCH 219/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200150.=E9=80=86?=
=?UTF-8?q?=E6=B3=A2=E5=85=B0=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...76\345\274\217\346\261\202\345\200\274.md" | 29 ++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
index fd3d69aa33..47da06f6df 100644
--- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
+++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
@@ -325,6 +325,33 @@ func evalRPN(_ tokens: [String]) -> Int {
return stack.last!
}
```
-
+Scala:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def evalRPN(tokens: Array[String]): Int = {
+ val stack = mutable.Stack[Int]() // 定义栈
+ // 抽取运算操作,需要传递x,y,和一个函数
+ def operator(x: Int, y: Int, f: (Int, Int) => Int): Int = f(x, y)
+ for (token <- tokens) {
+ // 模式匹配,匹配不同的操作符做什么样的运算
+ token match {
+ // 最后一个参数 _+_,代表x+y,遵循Scala的函数至简原则,以下运算同理
+ case "+" => stack.push(operator(stack.pop(), stack.pop(), _ + _))
+ case "-" => stack.push(operator(stack.pop(), stack.pop(), -_ + _))
+ case "*" => stack.push(operator(stack.pop(), stack.pop(), _ * _))
+ case "/" => {
+ var pop1 = stack.pop()
+ var pop2 = stack.pop()
+ stack.push(operator(pop2, pop1, _ / _))
+ }
+ case _ => stack.push(token.toInt) // 不是运算符就入栈
+ }
+ }
+ // 最后返回栈顶,不需要加return关键字
+ stack.pop()
+ }
+}
+```
-----------------------
From 349383321ff1ed393effb9ebaad075a7736cf98e Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Tue, 17 May 2022 17:45:40 +0800
Subject: [PATCH 220/551] =?UTF-8?q?=E5=A2=9E=E5=8A=A0KMP=20php=E7=89=88?=
=?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0028.\345\256\236\347\216\260strStr.md" | 75 +++++++++++++++++++
1 file changed, 75 insertions(+)
diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md"
index d67e5f70fc..1cdd5292a9 100644
--- "a/problems/0028.\345\256\236\347\216\260strStr.md"
+++ "b/problems/0028.\345\256\236\347\216\260strStr.md"
@@ -1166,5 +1166,80 @@ func strStr(_ haystack: String, _ needle: String) -> Int {
```
+PHP:
+
+> 前缀表统一减一
+```php
+function strStr($haystack, $needle) {
+ if (strlen($needle) == 0) return 0;
+ $next= [];
+ $this->getNext($next,$needle);
+
+ $j = -1;
+ for ($i = 0;$i < strlen($haystack); $i++) { // 注意i就从0开始
+ while($j >= 0 && $haystack[$i] != $needle[$j + 1]) {
+ $j = $next[$j];
+ }
+ if ($haystack[$i] == $needle[$j + 1]) {
+ $j++;
+ }
+ if ($j == (strlen($needle) - 1) ) {
+ return ($i - strlen($needle) + 1);
+ }
+ }
+ return -1;
+}
+
+function getNext(&$next, $s){
+ $j = -1;
+ $next[0] = $j;
+ for($i = 1; $i < strlen($s); $i++) { // 注意i从1开始
+ while ($j >= 0 && $s[$i] != $s[$j + 1]) {
+ $j = $next[$j];
+ }
+ if ($s[$i] == $s[$j + 1]) {
+ $j++;
+ }
+ $next[$i] = $j;
+ }
+}
+```
+
+> 前缀表统一不减一
+```php
+function strStr($haystack, $needle) {
+ if (strlen($needle) == 0) return 0;
+ $next= [];
+ $this->getNext($next,$needle);
+
+ $j = 0;
+ for ($i = 0;$i < strlen($haystack); $i++) { // 注意i就从0开始
+ while($j > 0 && $haystack[$i] != $needle[$j]) {
+ $j = $next[$j-1];
+ }
+ if ($haystack[$i] == $needle[$j]) {
+ $j++;
+ }
+ if ($j == strlen($needle)) {
+ return ($i - strlen($needle) + 1);
+ }
+ }
+ return -1;
+}
+
+function getNext(&$next, $s){
+ $j = 0;
+ $next[0] = $j;
+ for($i = 1; $i < strlen($s); $i++) { // 注意i从1开始
+ while ($j > 0 && $s[$i] != $s[$j]) {
+ $j = $next[$j-1];
+ }
+ if ($s[$i] == $s[$j]) {
+ $j++;
+ }
+ $next[$i] = $j;
+ }
+}
+```
-----------------------
From 748a728420be62c835866e7027171462ef90f888 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 17 May 2022 18:41:52 +0800
Subject: [PATCH 221/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200239.=E6=BB=91?=
=?UTF-8?q?=E5=8A=A8=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...43\346\234\200\345\244\247\345\200\274.md" | 47 +++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md"
index f269450f3b..eb32fdd279 100644
--- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md"
+++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md"
@@ -630,6 +630,53 @@ func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] {
return result
}
```
+Scala:
+```scala
+import scala.collection.mutable.ArrayBuffer
+object Solution {
+ def maxSlidingWindow(nums: Array[Int], k: Int): Array[Int] = {
+ var len = nums.length - k + 1 // 滑动窗口长度
+ var res: Array[Int] = new Array[Int](len) // 声明存储结果的数组
+ var index = 0 // 结果数组指针
+ val queue: MyQueue = new MyQueue // 自定义队列
+ // 将前k个添加到queue
+ for (i <- 0 until k) {
+ queue.add(nums(i))
+ }
+ res(index) = queue.peek // 第一个滑动窗口的最大值
+ index += 1
+ for (i <- k until nums.length) {
+ queue.poll(nums(i - k)) // 首先移除第i-k个元素
+ queue.add(nums(i)) // 添加当前数字到队列
+ res(index) = queue.peek() // 赋值
+ index+=1
+ }
+ // 最终返回res,return关键字可以省略
+ res
+ }
+}
+
+class MyQueue {
+ var queue = ArrayBuffer[Int]()
+
+ // 移除元素,如果传递进来的跟队头相等,那么移除
+ def poll(value: Int): Unit = {
+ if (!queue.isEmpty && queue.head == value) {
+ queue.remove(0)
+ }
+ }
+
+ // 添加元素,当队尾大于当前元素就删除
+ def add(value: Int): Unit = {
+ while (!queue.isEmpty && value > queue.last) {
+ queue.remove(queue.length - 1)
+ }
+ queue.append(value)
+ }
+
+ def peek(): Int = queue.head
+}
+```
-----------------------
From 47c4cc102123b1b43f09b7b766fdd2f1e1b979a9 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 17 May 2022 19:52:32 +0800
Subject: [PATCH 222/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200347.=E5=89=8DK?=
=?UTF-8?q?=E4=B8=AA=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...30\351\242\221\345\205\203\347\264\240.md" | 44 ++++++++++++++++++-
1 file changed, 43 insertions(+), 1 deletion(-)
diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md"
index 1d6a358bfc..20932b2814 100644
--- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md"
+++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md"
@@ -374,7 +374,49 @@ function topKFrequent(nums: number[], k: number): number[] {
};
```
-
+Scala:
+
+解法一: 优先级队列
+```scala
+object Solution {
+ import scala.collection.mutable
+ def topKFrequent(nums: Array[Int], k: Int): Array[Int] = {
+ val map = mutable.HashMap[Int, Int]()
+ // 将所有元素都放入Map
+ for (num <- nums) {
+ map.put(num, map.getOrElse(num, 0) + 1)
+ }
+ // 声明一个优先级队列,在函数柯里化那块需要指明排序方式
+ var queue = mutable.PriorityQueue[(Int, Int)]()(Ordering.fromLessThan((x, y) => x._2 > y._2))
+ // 将map里面的元素送入优先级队列
+ for (elem <- map) {
+ queue.enqueue(elem)
+ if(queue.size > k){
+ queue.dequeue // 如果队列元素大于k个,出队
+ }
+ }
+ // 最终只需要key的Array形式就可以了,return关键字可以省略
+ queue.map(_._1).toArray
+ }
+}
+```
+解法二: 相当于一个wordCount程序
+```scala
+object Solution {
+ def topKFrequent(nums: Array[Int], k: Int): Array[Int] = {
+ // 首先将数据变为(x,1),然后按照x分组,再使用map进行转换(x,sum),变换为Array
+ // 再使用sort针对sum进行排序,最后take前k个,再把数据变为x,y,z这种格式
+ nums.map((_, 1)).groupBy(_._1)
+ .map {
+ case (x, arr) => (x, arr.map(_._2).sum)
+ }
+ .toArray
+ .sortWith(_._2 > _._2)
+ .take(k)
+ .map(_._1)
+ }
+}
+```
-----------------------
From 6457d2d99775e9eddc1ab8386cdd5ca2d916b4b7 Mon Sep 17 00:00:00 2001
From: whusky <31883473+GitHubQAQ@users.noreply.github.com>
Date: Tue, 17 May 2022 21:22:17 +0800
Subject: [PATCH 223/551] =?UTF-8?q?Update=200063.=E4=B8=8D=E5=90=8C?=
=?UTF-8?q?=E8=B7=AF=E5=BE=84II.md=20=20=E6=B7=BB=E5=8A=A0=E9=A2=84?=
=?UTF-8?q?=E5=88=A4=E6=96=AD=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
1. 优化代码高亮格式
2. 对于C++的第一种解法添加预判断代码
---
...3.\344\270\215\345\220\214\350\267\257\345\276\204II.md" | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
index a40ccedaa9..0e51e0fedb 100644
--- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
+++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
@@ -66,7 +66,7 @@ dp[i][j] :表示从(0 ,0)出发,到(i, j) 有dp[i][j]条不同的路
所以代码为:
-```
+```cpp
if (obstacleGrid[i][j] == 0) { // 当(i, j)没有障碍的时候,再推导dp[i][j]
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
@@ -76,7 +76,7 @@ if (obstacleGrid[i][j] == 0) { // 当(i, j)没有障碍的时候,再推导dp[i
在[62.不同路径](https://programmercarl.com/0062.不同路径.html)不同路径中我们给出如下的初始化:
-```
+```cpp
vector> dp(m, vector(n, 0)); // 初始值为0
for (int i = 0; i < m; i++) dp[i][0] = 1;
for (int j = 0; j < n; j++) dp[0][j] = 1;
@@ -138,6 +138,8 @@ public:
int uniquePathsWithObstacles(vector>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
+ if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) //如果在起点或终点出现了障碍,直接返回0
+ return 0;
vector> dp(m, vector(n, 0));
for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) dp[i][0] = 1;
for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) dp[0][j] = 1;
From 83c2fd2454bbbbeda82725d2c38a2b577d7434c6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?=
Date: Wed, 18 May 2022 09:27:56 +0800
Subject: [PATCH 224/551] Update README.md
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 3d44ca690f..01cab0b06a 100644
--- a/README.md
+++ b/README.md
@@ -31,7 +31,7 @@
-# LeetCode 刷题攻略1111
+# LeetCode 刷题攻略
## 刷题攻略的背景
From 552e24b171bae840f32ab865ae4518c8674492e3 Mon Sep 17 00:00:00 2001
From: molonlu
Date: Wed, 18 May 2022 11:49:55 +0800
Subject: [PATCH 225/551] =?UTF-8?q?Update=200226.=E7=BF=BB=E8=BD=AC?=
=?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
通过tmp显示交换的方式改为golang风格的交换
---
...77\273\350\275\254\344\272\214\345\217\211\346\240\221.md" | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md"
index a3ebe24dd3..af5b8043a7 100644
--- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md"
@@ -368,9 +368,7 @@ func invertTree(root *TreeNode) *TreeNode {
if root ==nil{
return nil
}
- temp:=root.Left
- root.Left=root.Right
- root.Right=temp
+ root.Left,root.Right=root.Right,root.Left//交换
invertTree(root.Left)
invertTree(root.Right)
From f389dcd987a7f9b381f146de9846fc4abfdb92ae Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 18 May 2022 16:23:50 +0800
Subject: [PATCH 226/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...1\347\220\206\350\256\272\345\237\272\347\241\200.md" | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md"
index 9c151e3205..9e10ac20b3 100644
--- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md"
+++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md"
@@ -258,6 +258,13 @@ class TreeNode {
}
}
```
-
+Scala:
+```scala
+class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {
+ var value: Int = _value
+ var left: TreeNode = _left
+ var right: TreeNode = _right
+}
+```
-----------------------
From b2be41e4bcd2c1ef6bc4951519e5134209b55689 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 18 May 2022 16:54:15 +0800
Subject: [PATCH 227/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...22\345\275\222\351\201\215\345\216\206.md" | 51 +++++++++++++++++++
1 file changed, 51 insertions(+)
diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md"
index 186c39d3d8..29c0cfdaa4 100644
--- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md"
+++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md"
@@ -470,5 +470,56 @@ func postorder(_ root: TreeNode?, res: inout [Int]) {
res.append(root!.val)
}
```
+Scala: 前序遍历:(144.二叉树的前序遍历)
+```scala
+object Solution {
+ import scala.collection.mutable.ListBuffer
+ def preorderTraversal(root: TreeNode): List[Int] = {
+ val res = ListBuffer[Int]()
+ def traversal(curNode: TreeNode): Unit = {
+ if(curNode == null) return
+ res.append(curNode.value)
+ traversal(curNode.left)
+ traversal(curNode.right)
+ }
+ traversal(root)
+ res.toList
+ }
+}
+```
+中序遍历:(94. 二叉树的中序遍历)
+```scala
+object Solution {
+ import scala.collection.mutable.ListBuffer
+ def inorderTraversal(root: TreeNode): List[Int] = {
+ val res = ListBuffer[Int]()
+ def traversal(curNode: TreeNode): Unit = {
+ if(curNode == null) return
+ traversal(curNode.left)
+ res.append(curNode.value)
+ traversal(curNode.right)
+ }
+ traversal(root)
+ res.toList
+ }
+}
+```
+后序遍历:(145. 二叉树的后序遍历)
+```scala
+object Solution {
+ import scala.collection.mutable.ListBuffer
+ def postorderTraversal(root: TreeNode): List[Int] = {
+ val res = ListBuffer[Int]()
+ def traversal(curNode: TreeNode): Unit = {
+ if (curNode == null) return
+ traversal(curNode.left)
+ traversal(curNode.right)
+ res.append(curNode.value)
+ }
+ traversal(root)
+ res.toList
+ }
+}
+```
-----------------------
From d6d227c4affd046a47429454aa241bae12c7def2 Mon Sep 17 00:00:00 2001
From: areslk <543430610@qq.com>
Date: Wed, 18 May 2022 17:07:40 +0800
Subject: [PATCH 228/551] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1?=
=?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
修改注释
---
...\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md"
index d98ff8a90c..b759836551 100644
--- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md"
@@ -208,7 +208,7 @@ int getHeight(TreeNode* node) {
```CPP
class Solution {
public:
- // 返回以该节点为根节点的二叉树的高度,如果不是二叉搜索树了则返回-1
+ // 返回以该节点为根节点的二叉树的高度,如果不是平衡二叉树了则返回-1
int getHeight(TreeNode* node) {
if (node == NULL) {
return 0;
From 4aad0f9ca58c0188248af5a1043a21722aae0129 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 18 May 2022 17:26:22 +0800
Subject: [PATCH 229/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...55\344\273\243\351\201\215\345\216\206.md" | 65 +++++++++++++++++++
1 file changed, 65 insertions(+)
diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md"
index 13ba5f1e71..fac30f9989 100644
--- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md"
+++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md"
@@ -568,6 +568,71 @@ func inorderTraversal(_ root: TreeNode?) -> [Int] {
return result
}
```
+Scala:
+```scala
+// 前序遍历(迭代法)
+object Solution {
+ import scala.collection.mutable
+ def preorderTraversal(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ if (root == null) return res.toList
+ // 声明一个栈,泛型为TreeNode
+ val stack = mutable.Stack[TreeNode]()
+ stack.push(root) // 先把根节点压入栈
+ while (!stack.isEmpty) {
+ var curNode = stack.pop()
+ res.append(curNode.value) // 先把这个值压入栈
+ // 如果当前节点的左右节点不为空,则入栈,先放右节点,再放左节点
+ if (curNode.right != null) stack.push(curNode.right)
+ if (curNode.left != null) stack.push(curNode.left)
+ }
+ res.toList
+ }
+}
+// 中序遍历(迭代法)
+object Solution {
+ import scala.collection.mutable
+ def inorderTraversal(root: TreeNode): List[Int] = {
+ val res = mutable.ArrayBuffer[Int]()
+ if (root == null) return res.toList
+ val stack = mutable.Stack[TreeNode]()
+ var curNode = root
+ // 将左节点都入栈,当遍历到最左(到空)的时候,再弹出栈顶元素,加入res
+ // 再把栈顶元素的右节点加进来,继续下一轮遍历
+ while (curNode != null || !stack.isEmpty) {
+ if (curNode != null) {
+ stack.push(curNode)
+ curNode = curNode.left
+ } else {
+ curNode = stack.pop()
+ res.append(curNode.value)
+ curNode = curNode.right
+ }
+ }
+ res.toList
+ }
+}
+
+// 后序遍历(迭代法)
+object Solution {
+ import scala.collection.mutable
+ def postorderTraversal(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ if (root == null) return res.toList
+ val stack = mutable.Stack[TreeNode]()
+ stack.push(root)
+ while (!stack.isEmpty) {
+ val curNode = stack.pop()
+ res.append(curNode.value)
+ // 这次左节点先入栈,右节点再入栈
+ if(curNode.left != null) stack.push(curNode.left)
+ if(curNode.right != null) stack.push(curNode.right)
+ }
+ // 最后需要翻转List
+ res.reverse.toList
+ }
+}
+```
-----------------------
From b8b62ffc32de46005c4317150f952ad1aa483f5c Mon Sep 17 00:00:00 2001
From: 3Xpl0it3r
Date: Wed, 18 May 2022 18:31:37 +0800
Subject: [PATCH 230/551] =?UTF-8?q?=E6=A0=91=E6=B7=B1=E5=BA=A6=20rust?=
=?UTF-8?q?=E5=AE=9E=E7=8E=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...00\345\244\247\346\267\261\345\272\246.md" | 27 ++++++++
...00\345\260\217\346\267\261\345\272\246.md" | 64 +++++++++++++++++++
2 files changed, 91 insertions(+)
diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md"
index 2229a85434..65a155fbda 100644
--- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md"
+++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md"
@@ -192,6 +192,33 @@ public:
};
```
+rust:
+```rust
+impl Solution {
+ pub fn max_depth(root: Option>>) -> i32 {
+ if root.is_none(){
+ return 0;
+ }
+ let mut max_depth: i32 = 0;
+ let mut stack = vec![root.unwrap()];
+ while !stack.is_empty() {
+ let num = stack.len();
+ for _i in 0..num{
+ let top = stack.remove(0);
+ if top.borrow_mut().left.is_some(){
+ stack.push(top.borrow_mut().left.take().unwrap());
+ }
+ if top.borrow_mut().right.is_some(){
+ stack.push(top.borrow_mut().right.take().unwrap());
+ }
+ }
+ max_depth+=1;
+ }
+ max_depth
+ }
+```
+
+
那么我们可以顺便解决一下n叉树的最大深度问题
# 559.n叉树的最大深度
diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md"
index 224caa5e37..b13316591c 100644
--- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md"
+++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md"
@@ -488,5 +488,69 @@ func minDepth(_ root: TreeNode?) -> Int {
}
```
+rust:
+```rust
+impl Solution {
+ pub fn min_depth(root: Option>>) -> i32 {
+ return Solution::bfs(root)
+ }
+
+ // 递归
+ pub fn dfs(node: Option>>) -> i32{
+ if node.is_none(){
+ return 0;
+ }
+ let parent = node.unwrap();
+ let left_child = parent.borrow_mut().left.take();
+ let right_child = parent.borrow_mut().right.take();
+ if left_child.is_none() && right_child.is_none(){
+ return 1;
+ }
+ let mut min_depth = i32::MAX;
+ if left_child.is_some(){
+ let left_depth = Solution::dfs(left_child);
+ if left_depth <= min_depth{
+ min_depth = left_depth
+ }
+ }
+ if right_child.is_some(){
+ let right_depth = Solution::dfs(right_child);
+ if right_depth <= min_depth{
+ min_depth = right_depth
+ }
+ }
+ min_depth + 1
+
+ }
+
+ // 迭代
+ pub fn bfs(node: Option>>) -> i32{
+ let mut min_depth = 0;
+ if node.is_none(){
+ return min_depth
+ }
+ let mut stack = vec![node.unwrap()];
+ while !stack.is_empty(){
+ min_depth += 1;
+ let num = stack.len();
+ for _i in 0..num{
+ let top = stack.remove(0);
+ let left_child = top.borrow_mut().left.take();
+ let right_child = top.borrow_mut().right.take();
+ if left_child.is_none() && right_child.is_none(){
+ return min_depth;
+ }
+ if left_child.is_some(){
+ stack.push(left_child.unwrap());
+ }
+ if right_child.is_some(){
+ stack.push(right_child.unwrap());
+ }
+ }
+ }
+ min_depth
+ }
+```
+
-----------------------
From 54d10fc3b893d50c278349c9de9d09bab5924ae7 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 19 May 2022 00:21:29 +0800
Subject: [PATCH 231/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880300.?=
=?UTF-8?q?=E6=9C=80=E9=95=BF=E4=B8=8A=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97?=
=?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...07\345\255\220\345\272\217\345\210\227.md" | 21 +++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md"
index f68edb5ae5..5ccb2d769b 100644
--- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md"
@@ -220,6 +220,27 @@ const lengthOfLIS = (nums) => {
};
```
+TypeScript
+
+```typescript
+function lengthOfLIS(nums: number[]): number {
+ /**
+ dp[i]: 前i个元素中,以nums[i]结尾,最长子序列的长度
+ */
+ const dp: number[] = new Array(nums.length).fill(1);
+ let resMax: number = 0;
+ for (let i = 0, length = nums.length; i < length; i++) {
+ for (let j = 0; j < i; j++) {
+ if (nums[i] > nums[j]) {
+ dp[i] = Math.max(dp[i], dp[j] + 1);
+ }
+ }
+ resMax = Math.max(resMax, dp[i]);
+ }
+ return resMax;
+};
+```
+
From 52a486101d0525074a7b1f6a54a98b5940f9ab50 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 19 May 2022 00:43:00 +0800
Subject: [PATCH 232/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880674.?=
=?UTF-8?q?=E6=9C=80=E9=95=BF=E8=BF=9E=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F?=
=?UTF-8?q?=E5=88=97.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...22\345\242\236\345\272\217\345\210\227.md" | 39 +++++++++++++++++++
1 file changed, 39 insertions(+)
diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
index 56e95d97d8..6ec4a6c776 100644
--- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
+++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
@@ -319,6 +319,45 @@ const findLengthOfLCIS = (nums) => {
};
```
+TypeScript:
+
+> 动态规划:
+
+```typescript
+function findLengthOfLCIS(nums: number[]): number {
+ /**
+ dp[i]: 前i个元素,以nums[i]结尾,最长连续子序列的长度
+ */
+ const dp: number[] = new Array(nums.length).fill(1);
+ let resMax: number = 1;
+ for (let i = 1, length = nums.length; i < length; i++) {
+ if (nums[i] > nums[i - 1]) {
+ dp[i] = dp[i - 1] + 1;
+ }
+ resMax = Math.max(resMax, dp[i]);
+ }
+ return resMax;
+};
+```
+
+> 贪心:
+
+```typescript
+function findLengthOfLCIS(nums: number[]): number {
+ let resMax: number = 1;
+ let count: number = 1;
+ for (let i = 0, length = nums.length; i < length - 1; i++) {
+ if (nums[i] < nums[i + 1]) {
+ count++;
+ } else {
+ count = 1;
+ }
+ resMax = Math.max(resMax, count);
+ }
+ return resMax;
+};
+```
+
From 648014b28a54e953d335e822d7e7a5f98a82e3ea Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 10:19:22 +0800
Subject: [PATCH 233/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...00\350\277\255\344\273\243\346\263\225.md" | 74 +++++++++++++++++++
1 file changed, 74 insertions(+)
diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md"
index f6edf586ea..9ca6ac39c9 100644
--- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md"
+++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md"
@@ -591,6 +591,80 @@ function postorderTraversal(root: TreeNode | null): number[] {
return res;
};
```
+Scala:
+```scala
+// 前序遍历
+object Solution {
+ import scala.collection.mutable
+ def preorderTraversal(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ val stack = mutable.Stack[TreeNode]()
+ if (root != null) stack.push(root)
+ while (!stack.isEmpty) {
+ var curNode = stack.top
+ if (curNode != null) {
+ stack.pop()
+ if (curNode.right != null) stack.push(curNode.right)
+ if (curNode.left != null) stack.push(curNode.left)
+ stack.push(curNode)
+ stack.push(null)
+ } else {
+ stack.pop()
+ res.append(stack.pop().value)
+ }
+ }
+ res.toList
+ }
+}
+
+// 中序遍历
+object Solution {
+ import scala.collection.mutable
+ def inorderTraversal(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ val stack = mutable.Stack[TreeNode]()
+ if (root != null) stack.push(root)
+ while (!stack.isEmpty) {
+ var curNode = stack.top
+ if (curNode != null) {
+ stack.pop()
+ if (curNode.right != null) stack.push(curNode.right)
+ stack.push(curNode)
+ stack.push(null)
+ if (curNode.left != null) stack.push(curNode.left)
+ } else {
+ // 等于空的时候好办,弹出这个元素
+ stack.pop()
+ res.append(stack.pop().value)
+ }
+ }
+ res.toList
+ }
+}
+// 后序遍历
+object Solution {
+ import scala.collection.mutable
+ def postorderTraversal(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ val stack = mutable.Stack[TreeNode]()
+ if (root != null) stack.push(root)
+ while (!stack.isEmpty) {
+ var curNode = stack.top
+ if (curNode != null) {
+ stack.pop()
+ stack.push(curNode)
+ stack.push(null)
+ if (curNode.right != null) stack.push(curNode.right)
+ if (curNode.left != null) stack.push(curNode.left)
+ } else {
+ stack.pop()
+ res.append(stack.pop().value)
+ }
+ }
+ res.toList
+ }
+}
+```
-----------------------
From 33ed3d980eecec6824fb5b9edad83d7b916c4142 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 10:35:55 +0800
Subject: [PATCH 234/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20102.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?=
=?UTF-8?q?=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...02\345\272\217\351\201\215\345\216\206.md" | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
index ab8f2e572e..5afce73a64 100644
--- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
+++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
@@ -298,6 +298,31 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] {
return result
}
```
+Scala:
+```scala
+// 102.二叉树的层序遍历
+object Solution {
+ import scala.collection.mutable
+ def levelOrder(root: TreeNode): List[List[Int]] = {
+ val res = mutable.ListBuffer[List[Int]]()
+ if (root == null) return res.toList
+ val queue = mutable.Queue[TreeNode]() // 声明一个队列
+ queue.enqueue(root) // 把根节点加入queue
+ while (!queue.isEmpty) {
+ val tmp = mutable.ListBuffer[Int]()
+ val len = queue.size // 求出len的长度
+ for (i <- 0 until len) { // 从0到当前队列长度的所有节点都加入到结果集
+ val curNode = queue.dequeue()
+ tmp.append(curNode.value)
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ res.append(tmp.toList)
+ }
+ res.toList
+ }
+}
+```
**此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!**
From 04339562c7ce6c46dbd081551e51f36dcaf5fe8d Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 10:36:51 +0800
Subject: [PATCH 235/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20107.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E6=AC=A1=E9=81=8D=E5=8E=86?=
=?UTF-8?q?II=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...02\345\272\217\351\201\215\345\216\206.md" | 27 +++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
index 5afce73a64..e707ce27ee 100644
--- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
+++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
@@ -553,6 +553,33 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] {
}
```
+Scala:
+```scala
+// 107.二叉树的层次遍历II
+object Solution {
+ import scala.collection.mutable
+ def levelOrderBottom(root: TreeNode): List[List[Int]] = {
+ val res = mutable.ListBuffer[List[Int]]()
+ if (root == null) return res.toList
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val tmp = mutable.ListBuffer[Int]()
+ val len = queue.size
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ tmp.append(curNode.value)
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ res.append(tmp.toList)
+ }
+ // 最后翻转一下
+ res.reverse.toList
+ }
+}
+```
+
# 199.二叉树的右视图
[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-right-side-view/)
From ba51a4947adb197fcc4551e3157c509795fd0a82 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 11:03:09 +0800
Subject: [PATCH 236/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20199.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=8F=B3=E8=A7=86=E5=9B=BE=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...02\345\272\217\351\201\215\345\216\206.md" | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
index ab8f2e572e..b74f1a0b39 100644
--- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
+++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
@@ -750,6 +750,31 @@ func rightSideView(_ root: TreeNode?) -> [Int] {
}
```
+Scala:
+```scala
+// 199.二叉树的右视图
+object Solution {
+ import scala.collection.mutable
+ def rightSideView(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ if (root == null) return res.toList
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val len = queue.size
+ var curNode: TreeNode = null
+ for (i <- 0 until len) {
+ curNode = queue.dequeue()
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ res.append(curNode.value) // 把最后一个节点的值加入解集
+ }
+ res.toList // 最后需要把res转换为List,return关键字可以省略
+ }
+}
+```
+
# 637.二叉树的层平均值
[力扣题目链接](https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/)
From 0b88af0824b696be6d2f107adde3758f1df1c3f8 Mon Sep 17 00:00:00 2001
From: lizhendong128
Date: Thu, 19 May 2022 11:08:59 +0800
Subject: [PATCH 237/551] =?UTF-8?q?=E4=BF=AE=E6=94=B90150=E9=80=86?=
=?UTF-8?q?=E6=B3=A2=E5=85=B0=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC?=
=?UTF-8?q?=20Java=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
将for循环改为for each,使代码更加简洁。因为循环除了对token进行遍历,i并没有其他用途。
---
...0\276\276\345\274\217\346\261\202\345\200\274.md" | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
index fd3d69aa33..6ce7e2f9d1 100644
--- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
+++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
@@ -136,19 +136,19 @@ java:
class Solution {
public int evalRPN(String[] tokens) {
Deque stack = new LinkedList();
- for (int i = 0; i < tokens.length; ++i) {
- if ("+".equals(tokens[i])) { // leetcode 内置jdk的问题,不能使用==判断字符串是否相等
+ for (String s : tokens) {
+ if ("+".equals(s)) { // leetcode 内置jdk的问题,不能使用==判断字符串是否相等
stack.push(stack.pop() + stack.pop()); // 注意 - 和/ 需要特殊处理
- } else if ("-".equals(tokens[i])) {
+ } else if ("-".equals(s)) {
stack.push(-stack.pop() + stack.pop());
- } else if ("*".equals(tokens[i])) {
+ } else if ("*".equals(s)) {
stack.push(stack.pop() * stack.pop());
- } else if ("/".equals(tokens[i])) {
+ } else if ("/".equals(s)) {
int temp1 = stack.pop();
int temp2 = stack.pop();
stack.push(temp2 / temp1);
} else {
- stack.push(Integer.valueOf(tokens[i]));
+ stack.push(Integer.valueOf(s));
}
}
return stack.pop();
From 9af80d3007aed88bd8d09538ab6710218c3e9dfd Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 11:10:07 +0800
Subject: [PATCH 238/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20637.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=B9=B3=E5=9D=87=E5=80=BC?=
=?UTF-8?q?=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...02\345\272\217\351\201\215\345\216\206.md" | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
index b74f1a0b39..a99449fb00 100644
--- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
+++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
@@ -1006,6 +1006,30 @@ func averageOfLevels(_ root: TreeNode?) -> [Double] {
return result
}
```
+Scala:
+```scala
+// 637.二叉树的层平均值
+object Solution {
+ import scala.collection.mutable
+ def averageOfLevels(root: TreeNode): Array[Double] = {
+ val res = mutable.ArrayBuffer[Double]()
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ var sum = 0.0
+ var len = queue.size
+ for (i <- 0 until len) {
+ var curNode = queue.dequeue()
+ sum += curNode.value // 累加该层的值
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ res.append(sum / len) // 平均值即为sum/len
+ }
+ res.toArray // 最后需要转换为Array,return关键字可以省略
+ }
+}
+```
# 429.N叉树的层序遍历
From c922fbc0e3ee16bbd0f3406227ce51a44cfe636d Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 12:19:10 +0800
Subject: [PATCH 239/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20429.N=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...02\345\272\217\351\201\215\345\216\206.md" | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
index a99449fb00..6964e1f0ac 100644
--- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
+++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
@@ -1274,6 +1274,34 @@ func levelOrder(_ root: Node?) -> [[Int]] {
}
```
+Scala:
+```scala
+// 429.N叉树的层序遍历
+object Solution {
+ import scala.collection.mutable
+ def levelOrder(root: Node): List[List[Int]] = {
+ val res = mutable.ListBuffer[List[Int]]()
+ if (root == null) return res.toList
+ val queue = mutable.Queue[Node]()
+ queue.enqueue(root) // 根节点入队
+ while (!queue.isEmpty) {
+ val tmp = mutable.ListBuffer[Int]() // 存储每层节点
+ val len = queue.size
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ tmp.append(curNode.value) // 将该节点的值加入tmp
+ // 循环遍历该节点的子节点,加入队列
+ for (child <- curNode.children) {
+ queue.enqueue(child)
+ }
+ }
+ res.append(tmp.toList) // 将该层的节点放到结果集
+ }
+ res.toList
+ }
+}
+```
+
# 515.在每个树行中找最大值
[力扣题目链接](https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/)
From 80301657b305bcbd614d5294b1d483d31419755f Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 12:29:12 +0800
Subject: [PATCH 240/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20515.=E5=9C=A8?=
=?UTF-8?q?=E6=AF=8F=E4=B8=AA=E6=A0=91=E8=A1=8C=E4=B8=AD=E6=89=BE=E6=9C=80?=
=?UTF-8?q?=E5=A4=A7=E5=80=BC=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...02\345\272\217\351\201\215\345\216\206.md" | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
index ab8f2e572e..db27c73d68 100644
--- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
+++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
@@ -1433,6 +1433,32 @@ func largestValues(_ root: TreeNode?) -> [Int] {
}
```
+Scala:
+```scala
+// 515.在每个树行中找最大值
+object Solution {
+ import scala.collection.mutable
+ def largestValues(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ if (root == null) return res.toList
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ var max = Int.MinValue // 初始化max为系统最小值
+ val len = queue.size
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ max = math.max(max, curNode.value) // 对比求解最大值
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ res.append(max) // 将最大值放入结果集
+ }
+ res.toList
+ }
+}
+```
+
# 116.填充每个节点的下一个右侧节点指针
[力扣题目链接](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/)
From a3179499d03b05594b8808a1d11a0ab22048cf32 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 12:58:52 +0800
Subject: [PATCH 241/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20116.=E5=A1=AB?=
=?UTF-8?q?=E5=85=85=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B?=
=?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87?=
=?UTF-8?q?=E9=92=88=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...02\345\272\217\351\201\215\345\216\206.md" | 29 +++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
index db27c73d68..18fdfcb3e2 100644
--- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
+++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
@@ -1718,6 +1718,35 @@ func connect(_ root: Node?) -> Node? {
}
```
+Scala:
+```scala
+// 116.填充每个节点的下一个右侧节点指针
+object Solution {
+ import scala.collection.mutable
+
+ def connect(root: Node): Node = {
+ if (root == null) return root
+ val queue = mutable.Queue[Node]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val len = queue.size
+ val tmp = mutable.ListBuffer[Node]()
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ tmp.append(curNode)
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ // 处理next指针
+ for (i <- 0 until tmp.size - 1) {
+ tmp(i).next = tmp(i + 1)
+ }
+ tmp(tmp.size-1).next = null
+ }
+ root
+ }
+}
+```
# 117.填充每个节点的下一个右侧节点指针II
[力扣题目链接](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/)
From 7a62a75e399325cf439eebf507e8ce2d576e618d Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 12:59:45 +0800
Subject: [PATCH 242/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20117.=E5=A1=AB?=
=?UTF-8?q?=E5=85=85=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B?=
=?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87?=
=?UTF-8?q?=E9=92=88II=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...02\345\272\217\351\201\215\345\216\206.md" | 29 +++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
index 18fdfcb3e2..a2130f7e93 100644
--- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
+++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
@@ -1998,6 +1998,35 @@ func connect(_ root: Node?) -> Node? {
}
```
+Scala:
+```scala
+// 117.填充每个节点的下一个右侧节点指针II
+object Solution {
+ import scala.collection.mutable
+
+ def connect(root: Node): Node = {
+ if (root == null) return root
+ val queue = mutable.Queue[Node]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val len = queue.size
+ val tmp = mutable.ListBuffer[Node]()
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ tmp.append(curNode)
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ // 处理next指针
+ for (i <- 0 until tmp.size - 1) {
+ tmp(i).next = tmp(i + 1)
+ }
+ tmp(tmp.size-1).next = null
+ }
+ root
+ }
+}
+```
# 104.二叉树的最大深度
[力扣题目链接](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/)
From 32f0599243e7e9585a9b18a8b36fa0e50f120a24 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 13:38:22 +0800
Subject: [PATCH 243/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20104.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?=
=?UTF-8?q?=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...02\345\272\217\351\201\215\345\216\206.md" | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
index ab8f2e572e..95a6b5d619 100644
--- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
+++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
@@ -2160,6 +2160,30 @@ func maxDepth(_ root: TreeNode?) -> Int {
}
```
+Scala:
+```scala
+// 104.二叉树的最大深度
+object Solution {
+ import scala.collection.mutable
+ def maxDepth(root: TreeNode): Int = {
+ if (root == null) return 0
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ var depth = 0
+ while (!queue.isEmpty) {
+ val len = queue.length
+ depth += 1
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ }
+ depth
+ }
+}
+```
+
# 111.二叉树的最小深度
[力扣题目链接](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/)
From f5c5a58b137adad5490c04b57f82953101789999 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 13:42:12 +0800
Subject: [PATCH 244/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20111.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?=
=?UTF-8?q?=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...02\345\272\217\351\201\215\345\216\206.md" | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
index 95a6b5d619..b411816adf 100644
--- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
+++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
@@ -2403,6 +2403,30 @@ func minDepth(_ root: TreeNode?) -> Int {
}
```
+Scala:
+```scala
+// 111.二叉树的最小深度
+object Solution {
+ import scala.collection.mutable
+ def minDepth(root: TreeNode): Int = {
+ if (root == null) return 0
+ var depth = 0
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ depth += 1
+ val len = queue.size
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ if (curNode.left == null && curNode.right == null) return depth
+ }
+ }
+ depth
+ }
+}
+```
# 总结
From afcf6cd5a1093c3bc4f525b211858da02e4c47c6 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 19 May 2022 15:03:09 +0800
Subject: [PATCH 245/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880718.?=
=?UTF-8?q?=E6=9C=80=E9=95=BF=E9=87=8D=E5=A4=8D=E5=AD=90=E6=95=B0=E7=BB=84?=
=?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...15\345\255\220\346\225\260\347\273\204.md" | 50 +++++++++++++++++++
1 file changed, 50 insertions(+)
diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md"
index 87b1492ae9..0b7b519958 100644
--- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md"
+++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md"
@@ -297,6 +297,56 @@ const findLength = (nums1, nums2) => {
}
```
+TypeScript:
+
+> 动态规划:
+
+```typescript
+function findLength(nums1: number[], nums2: number[]): number {
+ /**
+ dp[i][j]:nums[i-1]和nums[j-1]结尾,最长重复子数组的长度
+ */
+ const length1: number = nums1.length,
+ length2: number = nums2.length;
+ const dp: number[][] = new Array(length1 + 1).fill(0)
+ .map(_ => new Array(length2 + 1).fill(0));
+ let resMax: number = 0;
+ for (let i = 1; i <= length1; i++) {
+ for (let j = 1; j <= length2; j++) {
+ if (nums1[i - 1] === nums2[j - 1]) {
+ dp[i][j] = dp[i - 1][j - 1] + 1;
+ resMax = Math.max(resMax, dp[i][j]);
+ }
+ }
+ }
+ return resMax;
+};
+```
+
+> 滚动数组:
+
+```typescript
+function findLength(nums1: number[], nums2: number[]): number {
+ const length1: number = nums1.length,
+ length2: number = nums2.length;
+ const dp: number[] = new Array(length1 + 1).fill(0);
+ let resMax: number = 0;
+ for (let i = 1; i <= length1; i++) {
+ for (let j = length2; j >= 1; j--) {
+ if (nums1[i - 1] === nums2[j - 1]) {
+ dp[j] = dp[j - 1] + 1;
+ resMax = Math.max(resMax, dp[j]);
+ } else {
+ dp[j] = 0;
+ }
+ }
+ }
+ return resMax;
+};
+```
+
+
+
-----------------------
From a4b024705b276e5cf69a2a521af8b923a32daaca Mon Sep 17 00:00:00 2001
From: xuxiaomeng <961592690@qq.com>
Date: Thu, 19 May 2022 20:25:16 +0800
Subject: [PATCH 246/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A00704.=E4=BA=8C?=
=?UTF-8?q?=E5=88=86=E6=9F=A5=E6=89=BE=20Kotlin=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...14\345\210\206\346\237\245\346\211\276.md" | 32 +++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
index 5562513089..9173d1da82 100644
--- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
+++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
@@ -611,6 +611,38 @@ public class Solution{
}
```
+**Kotlin:**
+```Kotlin
+// (版本一)左闭右开区间
+class Solution {
+ fun search(nums: IntArray, target: Int): Int {
+ var left = 0
+ var right = nums.size // [left,right) 右侧为开区间,right 设置为 nums.size
+ while (left < right) {
+ val mid = (left + right) / 2
+ if (nums[mid] < target) left = mid + 1
+ else if (nums[mid] > target) right = mid // 代码的核心,循环中 right 是开区间,这里也应是开区间
+ else return mid
+ }
+ return -1 // 没有找到 target ,返回 -1
+ }
+}
+// (版本二)左闭右闭区间
+class Solution {
+ fun search(nums: IntArray, target: Int): Int {
+ var left = 0
+ var right = nums.size - 1 // [left,right] 右侧为闭区间,right 设置为 nums.size - 1
+ while (left <= right) {
+ val mid = (left + right) / 2
+ if (nums[mid] < target) left = mid + 1
+ else if (nums[mid] > target) right = mid - 1 // 代码的核心,循环中 right 是闭区间,这里也应是闭区间
+ else return mid
+ }
+ return -1 // 没有找到 target ,返回 -1
+ }
+}
+```
+
-----------------------
From 568a569f0572cd500c152c7d836bc3aea42a59ad Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 19 May 2022 20:39:58 +0800
Subject: [PATCH 247/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881143.?=
=?UTF-8?q?=E6=9C=80=E9=95=BF=E5=85=AC=E5=85=B1=E5=AD=90=E5=BA=8F=E5=88=97?=
=?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...61\345\255\220\345\272\217\345\210\227.md" | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md"
index ecedf89b6e..d58330ec11 100644
--- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md"
@@ -236,6 +236,32 @@ const longestCommonSubsequence = (text1, text2) => {
};
```
+TypeScript:
+
+```typescript
+function longestCommonSubsequence(text1: string, text2: string): number {
+ /**
+ dp[i][j]: text1中前i-1个和text2中前j-1个,最长公共子序列的长度
+ */
+ const length1: number = text1.length,
+ length2: number = text2.length;
+ const dp: number[][] = new Array(length1 + 1).fill(0)
+ .map(_ => new Array(length2 + 1).fill(0));
+ for (let i = 1; i <= length1; i++) {
+ for (let j = 1; j <= length2; j++) {
+ if (text1[i - 1] === text2[j - 1]) {
+ dp[i][j] = dp[i - 1][j - 1] + 1;
+ } else {
+ dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
+ }
+ }
+ }
+ return dp[length1][length2];
+};
+```
+
+
+
-----------------------
From b3335f78a9a5c0910577835f8e61069594a23a0f Mon Sep 17 00:00:00 2001
From: zhouchaoyu1
Date: Thu, 19 May 2022 20:47:08 +0800
Subject: [PATCH 248/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20763.=E5=88=92?=
=?UTF-8?q?=E5=88=86=E5=AD=97=E6=AF=8D=E5=8C=BA=E9=97=B4=20=E8=A1=A5?=
=?UTF-8?q?=E5=85=85=E6=80=9D=E8=B7=AF=E7=9A=84Java=E4=BB=A3=E7=A0=81?=
=?UTF-8?q?=E5=AE=9E=E7=8E=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...27\346\257\215\345\214\272\351\227\264.md" | 65 +++++++++++++++++++
1 file changed, 65 insertions(+)
diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
index 2210cffad8..2f4d1b4861 100644
--- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
+++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
@@ -158,6 +158,71 @@ class Solution {
return list;
}
}
+
+class Solution{
+ /*解法二: 上述c++补充思路的Java代码实现*/
+
+ public int[][] findPartitions(String s) {
+ List temp = new ArrayList<>();
+ int[][] hash = new int[26][2];//26个字母2列 表示该字母对应的区间
+
+ for (int i = 0; i < s.length(); i++) {
+ //更新字符c对应的位置i
+ char c = s.charAt(i);
+ if (hash[c - 'a'][0] == 0) hash[c - 'a'][0] = i;
+
+ hash[c - 'a'][1] = i;
+
+ //第一个元素区别对待一下
+ hash[s.charAt(0) - 'a'][0] = 0;
+ }
+
+
+ List> h = new LinkedList<>();
+ //组装区间
+ for (int i = 0; i < 26; i++) {
+ //if (hash[i][0] != hash[i][1]) {
+ temp.clear();
+ temp.add(hash[i][0]);
+ temp.add(hash[i][1]);
+ //System.out.println(temp);
+ h.add(new ArrayList<>(temp));
+ // }
+ }
+ // System.out.println(h);
+ // System.out.println(h.size());
+ int[][] res = new int[h.size()][2];
+ for (int i = 0; i < h.size(); i++) {
+ List list = h.get(i);
+ res[i][0] = list.get(0);
+ res[i][1] = list.get(1);
+ }
+
+ return res;
+
+ }
+
+ public List partitionLabels(String s) {
+ int[][] partitions = findPartitions(s);
+ List res = new ArrayList<>();
+ Arrays.sort(partitions, (o1, o2) -> Integer.compare(o1[0], o2[0]));
+ int right = partitions[0][1];
+ int left = 0;
+ for (int i = 0; i < partitions.length; i++) {
+ if (partitions[i][0] > right) {
+ //左边界大于右边界即可纪委一次分割
+ res.add(right - left + 1);
+ left = partitions[i][0];
+ }
+ right = Math.max(right, partitions[i][1]);
+
+ }
+ //最右端
+ res.add(right - left + 1);
+ return res;
+
+ }
+}
```
### Python
From 30c1be11ff9ef77ff6cf0294f37b09ae50ea4619 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 19 May 2022 21:26:54 +0800
Subject: [PATCH 249/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881035.?=
=?UTF-8?q?=E4=B8=8D=E7=9B=B8=E4=BA=A4=E7=9A=84=E7=BA=BF.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...70\344\272\244\347\232\204\347\272\277.md" | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md"
index 279ed8162a..4463c5f713 100644
--- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md"
+++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md"
@@ -183,6 +183,30 @@ const maxUncrossedLines = (nums1, nums2) => {
};
```
+TypeScript:
+
+```typescript
+function maxUncrossedLines(nums1: number[], nums2: number[]): number {
+ /**
+ dp[i][j]: nums1前i-1个,nums2前j-1个,最大连线数
+ */
+ const length1: number = nums1.length,
+ length2: number = nums2.length;
+ const dp: number[][] = new Array(length1 + 1).fill(0)
+ .map(_ => new Array(length2 + 1).fill(0));
+ for (let i = 1; i <= length1; i++) {
+ for (let j = 1; j <= length2; j++) {
+ if (nums1[i - 1] === nums2[j - 1]) {
+ dp[i][j] = dp[i - 1][j - 1] + 1;
+ } else {
+ dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
+ }
+ }
+ }
+ return dp[length1][length2];
+};
+```
+
From 4d2b80ce7508553923c99a7786521c018c24ff8a Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 19 May 2022 21:59:33 +0800
Subject: [PATCH 250/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880053.?=
=?UTF-8?q?=E6=9C=80=E5=A4=A7=E5=AD=90=E5=BA=8F=E5=92=8C=E5=8A=A8=E6=80=81?=
=?UTF-8?q?=E8=A7=84=E5=88=92.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?=
=?UTF-8?q?ript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...201\350\247\204\345\210\222\357\274\211.md" | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
index 4c883cb66c..99aa7acfd5 100644
--- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
+++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
@@ -186,6 +186,24 @@ const maxSubArray = nums => {
};
```
+TypeScript:
+
+```typescript
+function maxSubArray(nums: number[]): number {
+ /**
+ dp[i]:以nums[i]结尾的最大和
+ */
+ const dp: number[] = []
+ dp[0] = nums[0];
+ let resMax: number = 0;
+ for (let i = 1; i < nums.length; i++) {
+ dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
+ resMax = Math.max(resMax, dp[i]);
+ }
+ return resMax;
+};
+```
+
-----------------------
From 3d6baa3ae3648ef41e7a96bd49cb68c3d9d2df2d Mon Sep 17 00:00:00 2001
From: dcj_hp <294487055@qq.com>
Date: Fri, 20 May 2022 00:14:16 +0800
Subject: [PATCH 251/551] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20java=20=E4=B8=80?=
=?UTF-8?q?=E7=BB=B4dp=E6=95=B0=E7=BB=84=E8=A7=A3=E6=B3=95=E4=BB=A3?=
=?UTF-8?q?=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...61\345\255\220\345\272\217\345\210\227.md" | 44 +++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md"
index ecedf89b6e..b4b8e6dbc1 100644
--- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md"
@@ -129,6 +129,9 @@ public:
Java:
```java
+/*
+ 二维dp数组
+*/
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
int[][] dp = new int[text1.length() + 1][text2.length() + 1]; // 先对dp数组做初始化操作
@@ -146,6 +149,47 @@ class Solution {
return dp[text1.length()][text2.length()];
}
}
+
+
+
+/**
+ 一维dp数组
+*/
+class Solution {
+ public int longestCommonSubsequence(String text1, String text2) {
+ int n1 = text1.length();
+ int n2 = text2.length();
+
+ // 多从二维dp数组过程分析
+ // 关键在于 如果记录 dp[i - 1][j - 1]
+ // 因为 dp[i - 1][j - 1] dp[j - 1] <=> dp[i][j - 1]
+ int [] dp = new int[n2 + 1];
+
+ for(int i = 1; i <= n1; i++){
+
+ // 这里pre相当于 dp[i - 1][j - 1]
+ int pre = dp[0];
+ for(int j = 1; j <= n2; j++){
+
+ //用于给pre赋值
+ int cur = dp[j];
+ if(text1.charAt(i - 1) == text2.charAt(j - 1)){
+ //这里pre相当于dp[i - 1][j - 1] 千万不能用dp[j - 1] !!
+ dp[j] = pre + 1;
+ } else{
+ // dp[j] 相当于 dp[i - 1][j]
+ // dp[j - 1] 相当于 dp[i][j - 1]
+ dp[j] = Math.max(dp[j], dp[j - 1]);
+ }
+
+ //更新dp[i - 1][j - 1], 为下次使用做准备
+ pre = cur;
+ }
+ }
+
+ return dp[n2];
+ }
+}
```
Python:
From 8759349af0001c6ee2f05ceece9cc0e331fbc08a Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 20 May 2022 00:35:32 +0800
Subject: [PATCH 252/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880392.?=
=?UTF-8?q?=E5=88=A4=E6=96=AD=E5=AD=90=E5=BA=8F=E5=88=97.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...55\345\255\220\345\272\217\345\210\227.md" | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md"
index 671576f7dc..3f7eb11d8d 100644
--- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md"
@@ -201,7 +201,32 @@ const isSubsequence = (s, t) => {
};
```
+TypeScript:
+
+```typescript
+function isSubsequence(s: string, t: string): boolean {
+ /**
+ dp[i][j]: s的前i-1个,t的前j-1个,最长公共子序列的长度
+ */
+ const sLen: number = s.length,
+ tLen: number = t.length;
+ const dp: number[][] = new Array(sLen + 1).fill(0)
+ .map(_ => new Array(tLen + 1).fill(0));
+ for (let i = 1; i <= sLen; i++) {
+ for (let j = 1; j <= tLen; j++) {
+ if (s[i - 1] === t[j - 1]) {
+ dp[i][j] = dp[i - 1][j - 1] + 1;
+ } else {
+ dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
+ }
+ }
+ }
+ return dp[sLen][tLen] === s.length;
+};
+```
+
Go:
+
```go
func isSubsequence(s string, t string) bool {
dp := make([][]int,len(s)+1)
From 4eefc546c62d0b5d63f08d1b51e5603ee722d7d5 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 20 May 2022 13:41:23 +0800
Subject: [PATCH 253/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880115.?=
=?UTF-8?q?=E4=B8=8D=E5=90=8C=E7=9A=84=E5=AD=90=E5=BA=8F=E5=88=97.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\345\255\220\345\272\217\345\210\227.md" | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md"
index 0f76296949..ca66e20d2a 100644
--- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md"
@@ -267,6 +267,36 @@ const numDistinct = (s, t) => {
};
```
+TypeScript:
+
+```typescript
+function numDistinct(s: string, t: string): number {
+ /**
+ dp[i][j]: s前i个字符,t前j个字符,s子序列中t出现的个数
+ dp[0][0]=1, 表示s前0个字符为'',t前0个字符为''
+ */
+ const sLen: number = s.length,
+ tLen: number = t.length;
+ const dp: number[][] = new Array(sLen + 1).fill(0)
+ .map(_ => new Array(tLen + 1).fill(0));
+ for (let m = 0; m < sLen; m++) {
+ dp[m][0] = 1;
+ }
+ for (let i = 1; i <= sLen; i++) {
+ for (let j = 1; j <= tLen; j++) {
+ if (s[i - 1] === t[j - 1]) {
+ dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
+ } else {
+ dp[i][j] = dp[i - 1][j];
+ }
+ }
+ }
+ return dp[sLen][tLen];
+};
+```
+
+
+
-----------------------
From 58c4ad4947506310ef7c05d9603972af684c9b96 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 20 May 2022 15:11:12 +0800
Subject: [PATCH 254/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880583.?=
=?UTF-8?q?=E4=B8=A4=E4=B8=AA=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0?=
=?UTF-8?q?=E9=99=A4=E6=93=8D=E4=BD=9C.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...40\351\231\244\346\223\215\344\275\234.md" | 61 +++++++++++++++++++
1 file changed, 61 insertions(+)
diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md"
index 53c1a125d7..00f1170009 100644
--- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md"
+++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md"
@@ -229,6 +229,67 @@ const minDistance = (word1, word2) => {
};
```
+TypeScript:
+
+> dp版本一:
+
+```typescript
+function minDistance(word1: string, word2: string): number {
+ /**
+ dp[i][j]: word1前i个字符,word2前j个字符,所需最小步数
+ dp[0][0]=0: word1前0个字符为'', word2前0个字符为''
+ */
+ const length1: number = word1.length,
+ length2: number = word2.length;
+ const dp: number[][] = new Array(length1 + 1).fill(0)
+ .map(_ => new Array(length2 + 1).fill(0));
+ for (let i = 0; i <= length1; i++) {
+ dp[i][0] = i;
+ }
+ for (let i = 0; i <= length2; i++) {
+ dp[0][i] = i;
+ }
+ for (let i = 1; i <= length1; i++) {
+ for (let j = 1; j <= length2; j++) {
+ if (word1[i - 1] === word2[j - 1]) {
+ dp[i][j] = dp[i - 1][j - 1];
+ } else {
+ dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + 1;
+ }
+ }
+ }
+ return dp[length1][length2];
+};
+```
+
+> dp版本二:
+
+```typescript
+function minDistance(word1: string, word2: string): number {
+ /**
+ dp[i][j]: word1前i个字符,word2前j个字符,最长公共子序列的长度
+ dp[0][0]=0: word1前0个字符为'', word2前0个字符为''
+ */
+ const length1: number = word1.length,
+ length2: number = word2.length;
+ const dp: number[][] = new Array(length1 + 1).fill(0)
+ .map(_ => new Array(length2 + 1).fill(0));
+ for (let i = 1; i <= length1; i++) {
+ for (let j = 1; j <= length2; j++) {
+ if (word1[i - 1] === word2[j - 1]) {
+ dp[i][j] = dp[i - 1][j - 1] + 1;
+ } else {
+ dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
+ }
+ }
+ }
+ const maxLen: number = dp[length1][length2];
+ return length1 + length2 - maxLen * 2;
+};
+```
+
+
+
-----------------------
From fa3a45c3dbd3cb58b9c0027cb2c8fe9e7727a439 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 20 May 2022 16:33:34 +0800
Subject: [PATCH 255/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880072.?=
=?UTF-8?q?=E7=BC=96=E8=BE=91=E8=B7=9D=E7=A6=BB.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...26\350\276\221\350\267\235\347\246\273.md" | 37 +++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md"
index 3802c228d6..530774eefc 100644
--- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md"
+++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md"
@@ -327,5 +327,42 @@ const minDistance = (word1, word2) => {
};
```
+TypeScript:
+
+```typescript
+function minDistance(word1: string, word2: string): number {
+ /**
+ dp[i][j]: word1前i个字符,word2前j个字符,最少操作数
+ dp[0][0]=0:表示word1前0个字符为'', word2前0个字符为''
+ */
+ const length1: number = word1.length,
+ length2: number = word2.length;
+ const dp: number[][] = new Array(length1 + 1).fill(0)
+ .map(_ => new Array(length2 + 1).fill(0));
+ for (let i = 0; i <= length1; i++) {
+ dp[i][0] = i;
+ }
+ for (let i = 0; i <= length2; i++) {
+ dp[0][i] = i;
+ }
+ for (let i = 1; i <= length1; i++) {
+ for (let j = 1; j <= length2; j++) {
+ if (word1[i - 1] === word2[j - 1]) {
+ dp[i][j] = dp[i - 1][j - 1];
+ } else {
+ dp[i][j] = Math.min(
+ dp[i - 1][j],
+ dp[i][j - 1],
+ dp[i - 1][j - 1]
+ ) + 1;
+ }
+ }
+ }
+ return dp[length1][length2];
+};
+```
+
+
+
-----------------------
From be7d6e1325009d5f404c8abeaae0cbed49ed2303 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 20 May 2022 19:36:10 +0800
Subject: [PATCH 256/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200226.=E7=BF=BB?=
=?UTF-8?q?=E8=BD=AC=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...54\344\272\214\345\217\211\346\240\221.md" | 48 +++++++++++++++++++
1 file changed, 48 insertions(+)
diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md"
index a3ebe24dd3..e378cbc106 100644
--- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md"
@@ -820,5 +820,53 @@ func invertTree(_ root: TreeNode?) -> TreeNode? {
}
```
+### Scala
+
+深度优先遍历(前序遍历):
+```scala
+object Solution {
+ def invertTree(root: TreeNode): TreeNode = {
+ if (root == null) return root
+ // 递归
+ def process(node: TreeNode): Unit = {
+ if (node == null) return
+ // 翻转节点
+ val curNode = node.left
+ node.left = node.right
+ node.right = curNode
+ process(node.left)
+ process(node.right)
+ }
+ process(root)
+ root
+ }
+}
+```
+
+广度优先遍历(层序遍历):
+```scala
+object Solution {
+ import scala.collection.mutable
+ def invertTree(root: TreeNode): TreeNode = {
+ if (root == null) return root
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val len = queue.size
+ for (i <- 0 until len) {
+ var curNode = queue.dequeue()
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ // 翻转
+ var tmpNode = curNode.left
+ curNode.left = curNode.right
+ curNode.right = tmpNode
+ }
+ }
+ root
+ }
+}
+```
+
-----------------------
From 81d4685e36603ec59e3e02d8243f60e660081541 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 20 May 2022 21:12:54 +0800
Subject: [PATCH 257/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200101.=E5=AF=B9?=
=?UTF-8?q?=E7=A7=B0=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...60\344\272\214\345\217\211\346\240\221.md" | 20 +++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md"
index e4e232c884..97ca0685de 100644
--- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md"
@@ -725,5 +725,25 @@ func isSymmetric3(_ root: TreeNode?) -> Bool {
}
```
+## Scala
+
+递归:
+```scala
+object Solution {
+ def isSymmetric(root: TreeNode): Boolean = {
+ if (root == null) return true // 如果等于空直接返回true
+ def compare(left: TreeNode, right: TreeNode): Boolean = {
+ if (left == null && right == null) return true // 如果左右都为空,则为true
+ if (left == null && right != null) return false // 如果左空右不空,不对称,返回false
+ if (left != null && right == null) return false // 如果左不空右空,不对称,返回false
+ // 如果左右的值相等,并且往下递归
+ left.value == right.value && compare(left.left, right.right) && compare(left.right, right.left)
+ }
+ // 分别比较左子树和右子树
+ compare(root.left, root.right)
+ }
+}
+```
+
-----------------------
From e7529aa31bf39bb1a3966ad8bd5fcbaa730b6447 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 09:14:39 +0800
Subject: [PATCH 258/551] =?UTF-8?q?=E9=87=8D=E5=86=99=200216.=E7=BB=84?=
=?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CIII=20JavaScript=20=E7=89=88?=
=?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
原代码冗余,建议重写
---
...345\220\210\346\200\273\345\222\214III.md" | 45 ++++++++-----------
1 file changed, 18 insertions(+), 27 deletions(-)
diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
index 32b1347e56..66ca7ff7e9 100644
--- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
+++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
@@ -360,39 +360,30 @@ func backTree(n,k,startIndex int,track *[]int,result *[][]int){
## javaScript
```js
-// 等差数列
-var maxV = k => k * (9 + 10 - k) / 2;
-var minV = k => k * (1 + k) / 2;
+/**
+ * @param {number} k
+ * @param {number} n
+ * @return {number[][]}
+ */
var combinationSum3 = function(k, n) {
- if (k > 9 || k < 1) return [];
- // if (n > maxV(k) || n < minV(k)) return [];
- // if (n === maxV(k)) return [Array.from({length: k}).map((v, i) => 9 - i)];
- // if (n === minV(k)) return [Array.from({length: k}).map((v, i) => i + 1)];
-
- const res = [], path = [];
- backtracking(k, n, 1, 0);
- return res;
- function backtracking(k, n, i, sum){
- const len = path.length;
- if (len > k || sum > n) return;
- if (maxV(k - len) < n - sum) return;
- if (minV(k - len) > n - sum) return;
-
- if(len === k && sum == n) {
- res.push(Array.from(path));
+ const backtrack = (start) => {
+ const l = path.length;
+ if (l === k) {
+ const sum = path.reduce((a, b) => a + b);
+ if (sum === n) {
+ res.push([...path]);
+ }
return;
}
-
- const min = Math.min(n - sum, 9 + len - k + 1);
-
- for(let a = i; a <= min; a++) {
- path.push(a);
- sum += a;
- backtracking(k, n, a + 1, sum);
+ for (let i = start; i <= 9 - (k - l) + 1; i++) {
+ path.push(i);
+ backtrack(i + 1);
path.pop();
- sum -= a;
}
}
+ let res = [], path = [];
+ backtrack(1);
+ return res;
};
```
From b4873836bacc8a65b84e8f9a7e8da7fd5d7dcd58 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 09:45:03 +0800
Subject: [PATCH 259/551] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=200131=20=E5=88=86?=
=?UTF-8?q?=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2=20JavaScript=20=E7=89=88?=
=?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
String 实例方法 substr 已弃用,请换成 slice
---
...\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
index 7a702898dc..6a370fb470 100644
--- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
+++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
@@ -442,7 +442,7 @@ var partition = function(s) {
}
for(let j = i; j < len; j++) {
if(!isPalindrome(s, i, j)) continue;
- path.push(s.substr(i, j - i + 1));
+ path.push(s.slice(i, j + 1));
backtracking(j + 1);
path.pop();
}
From ab6693d4b44e11dcf51771f5004e11e33c68c2cf Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 09:57:03 +0800
Subject: [PATCH 260/551] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=200093.=E5=A4=8D?=
=?UTF-8?q?=E5=88=B6IP=E5=9C=B0=E5=9D=80=20JavaScript=20=E7=89=88=E6=9C=AC?=
=?UTF-8?q?=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
String 实例 substr 方法已弃用,请使用 slice 方法
---
.../0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
index 6401824b47..d5eaa8abd0 100644
--- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
+++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
@@ -444,7 +444,7 @@ var restoreIpAddresses = function(s) {
return;
}
for(let j = i; j < s.length; j++) {
- const str = s.substr(i, j - i + 1);
+ const str = s.slice(i, j + 1);
if(str.length > 3 || +str > 255) break;
if(str.length > 1 && str[0] === "0") break;
path.push(str);
From fd194bd52fc58a22cf38627c3a6bc7d4e4335a11 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 10:06:05 +0800
Subject: [PATCH 261/551] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=200078.=E5=AD=90?=
=?UTF-8?q?=E9=9B=86=E9=97=AE=E9=A2=98=20JS=20=E3=80=81TS=20=E7=89=88?=
=?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
JS 和 TS 里面 数组深拷贝一般采用 ES6 扩展运算符 ... ,或者 Array.from() 方法,而不会采用实例方法 slice. slice方法用于数组分割等操作,请注意代码书写规范!
---
"problems/0078.\345\255\220\351\233\206.md" | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md"
index e1c52b5b81..2b1c7643b1 100644
--- "a/problems/0078.\345\255\220\351\233\206.md"
+++ "b/problems/0078.\345\255\220\351\233\206.md"
@@ -260,7 +260,7 @@ var subsets = function(nums) {
let result = []
let path = []
function backtracking(startIndex) {
- result.push(path.slice())
+ result.push([...path])
for(let i = startIndex; i < nums.length; i++) {
path.push(nums[i])
backtracking(i + 1)
@@ -280,7 +280,7 @@ function subsets(nums: number[]): number[][] {
backTracking(nums, 0, []);
return resArr;
function backTracking(nums: number[], startIndex: number, route: number[]): void {
- resArr.push(route.slice());
+ resArr.push([...route]);
let length = nums.length;
if (startIndex === length) return;
for (let i = startIndex; i < length; i++) {
From 13ed28dbcf8eb40e94fafab3da7955e967f298bd Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 10:10:36 +0800
Subject: [PATCH 262/551] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=200090.=E5=AD=90?=
=?UTF-8?q?=E9=9B=86II=20JS=E3=80=81TS=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
JS 和 TS 里面 数组深拷贝一般采用 ES6 扩展运算符 ... ,或者 Array.from() 方法,而不会采用实例方法 slice. slice方法用于数组分割等操作,请注意代码书写规范!
---
"problems/0090.\345\255\220\351\233\206II.md" | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md"
index 74ce000b0b..dd64d1998e 100644
--- "a/problems/0090.\345\255\220\351\233\206II.md"
+++ "b/problems/0090.\345\255\220\351\233\206II.md"
@@ -299,7 +299,7 @@ var subsetsWithDup = function(nums) {
return a - b
})
function backtracing(startIndex, sortNums) {
- result.push(path.slice(0))
+ result.push([...path])
if(startIndex > nums.length - 1) {
return
}
@@ -327,7 +327,7 @@ function subsetsWithDup(nums: number[]): number[][] {
backTraking(nums, 0, []);
return resArr;
function backTraking(nums: number[], startIndex: number, route: number[]): void {
- resArr.push(route.slice());
+ resArr.push([...route]);
let length: number = nums.length;
if (startIndex === length) return;
for (let i = startIndex; i < length; i++) {
From 2da33bfc93326e3cdf5a358e5ef18646f97982fb Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 10:46:39 +0800
Subject: [PATCH 263/551] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=200046.=E5=85=A8?=
=?UTF-8?q?=E6=8E=92=E5=88=97=20TS=20=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
JS 和 TS 里面 数组深拷贝一般采用 ES6 扩展运算符 ... ,或者 Array.from() 方法,而不会采用实例方法 slice. slice方法用于数组分割等操作,请注意代码书写规范!
---
"problems/0046.\345\205\250\346\216\222\345\210\227.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md"
index 836c36462f..6c55e8ef41 100644
--- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md"
+++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md"
@@ -341,7 +341,7 @@ function permute(nums: number[]): number[][] {
return resArr;
function backTracking(nums: number[], route: number[]): void {
if (route.length === nums.length) {
- resArr.push(route.slice());
+ resArr.push([...route]);
return;
}
let tempVal: number;
From 38b10bcbfdb3f90f6af93a5b196ca0f68463d744 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 10:48:51 +0800
Subject: [PATCH 264/551] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=200047.=E5=85=A8?=
=?UTF-8?q?=E6=8E=92=E5=88=97II=20JS=E3=80=81TS=20=E7=89=88=E6=9C=AC?=
=?UTF-8?q?=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
JS 和 TS 里面 数组深拷贝一般采用 ES6 扩展运算符 ... ,或者 Array.from() 方法,而不会采用实例方法 slice. slice方法用于数组分割等操作,请注意代码书写规范!
---
"problems/0047.\345\205\250\346\216\222\345\210\227II.md" | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md"
index cce25cd9a1..f635b8de40 100644
--- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md"
+++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md"
@@ -268,7 +268,7 @@ var permuteUnique = function (nums) {
function backtracing( used) {
if (path.length === nums.length) {
- result.push(path.slice())
+ result.push([...path])
return
}
for (let i = 0; i < nums.length; i++) {
@@ -303,7 +303,7 @@ function permuteUnique(nums: number[]): number[][] {
return resArr;
function backTracking(nums: number[], route: number[]): void {
if (route.length === nums.length) {
- resArr.push(route.slice());
+ resArr.push([...route]);
return;
}
for (let i = 0, length = nums.length; i < length; i++) {
From f36c1885cc8ccac0f38538b59b0a55e1b9f47913 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Sat, 21 May 2022 11:01:21 +0800
Subject: [PATCH 265/551] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E5=9B=9E=E6=BA=AF?=
=?UTF-8?q?=E7=AE=97=E6=B3=95=E5=8E=BB=E9=87=8D=E9=97=AE=E9=A2=98=E7=9A=84?=
=?UTF-8?q?=E5=8F=A6=E4=B8=80=E7=A7=8D=E5=86=99=E6=B3=95=20JavaScript=20?=
=?UTF-8?q?=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...00\347\247\215\345\206\231\346\263\225.md" | 84 ++++++++++++++++++-
1 file changed, 81 insertions(+), 3 deletions(-)
diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md"
index f48097e179..cbfe046a08 100644
--- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md"
+++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md"
@@ -365,6 +365,84 @@ class Solution:
return res
```
+JavaScript:
+
+**90.子集II**
+
+```javascript
+function subsetsWithDup(nums) {
+ nums.sort((a, b) => a - b);
+ const resArr = [];
+ backTraking(nums, 0, []);
+ return resArr;
+ function backTraking(nums, startIndex, route) {
+ resArr.push([...route]);
+ const helperSet = new Set();
+ for (let i = startIndex, length = nums.length; i < length; i++) {
+ if (helperSet.has(nums[i])) continue;
+ helperSet.add(nums[i]);
+ route.push(nums[i]);
+ backTraking(nums, i + 1, route);
+ route.pop();
+ }
+ }
+};
+```
+
+**40. 组合总和 II**
+
+```javascript
+function combinationSum2(candidates, target) {
+ candidates.sort((a, b) => a - b);
+ const resArr = [];
+ backTracking(candidates, target, 0, 0, []);
+ return resArr;
+ function backTracking( candidates, target, curSum, startIndex, route ) {
+ if (curSum > target) return;
+ if (curSum === target) {
+ resArr.push([...route]);
+ return;
+ }
+ const helperSet = new Set();
+ for (let i = startIndex, length = candidates.length; i < length; i++) {
+ let tempVal = candidates[i];
+ if (helperSet.has(tempVal)) continue;
+ helperSet.add(tempVal);
+ route.push(tempVal);
+ backTracking(candidates, target, curSum + tempVal, i + 1, route);
+ route.pop();
+ }
+ }
+};
+```
+
+**47. 全排列 II**
+
+```javaescript
+function permuteUnique(nums) {
+ const resArr = [];
+ const usedArr = [];
+ backTracking(nums, []);
+ return resArr;
+ function backTracking(nums, route) {
+ if (nums.length === route.length) {
+ resArr.push([...route]);
+ return;
+ }
+ const usedSet = new Set();
+ for (let i = 0, length = nums.length; i < length; i++) {
+ if (usedArr[i] === true || usedSet.has(nums[i])) continue;
+ usedSet.add(nums[i]);
+ route.push(nums[i]);
+ usedArr[i] = true;
+ backTracking(nums, route);
+ usedArr[i] = false;
+ route.pop();
+ }
+ }
+};
+```
+
TypeScript:
**90.子集II**
@@ -376,7 +454,7 @@ function subsetsWithDup(nums: number[]): number[][] {
backTraking(nums, 0, []);
return resArr;
function backTraking(nums: number[], startIndex: number, route: number[]): void {
- resArr.push(route.slice());
+ resArr.push([...route]);
const helperSet: Set = new Set();
for (let i = startIndex, length = nums.length; i < length; i++) {
if (helperSet.has(nums[i])) continue;
@@ -403,7 +481,7 @@ function combinationSum2(candidates: number[], target: number): number[][] {
) {
if (curSum > target) return;
if (curSum === target) {
- resArr.push(route.slice());
+ resArr.push([...route]);
return;
}
const helperSet: Set = new Set();
@@ -430,7 +508,7 @@ function permuteUnique(nums: number[]): number[][] {
return resArr;
function backTracking(nums: number[], route: number[]): void {
if (nums.length === route.length) {
- resArr.push(route.slice());
+ resArr.push([...route]);
return;
}
const usedSet: Set = new Set();
From f6501464cfdfe0c207e5f1026b1b3251d9b5c927 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 21 May 2022 11:48:18 +0800
Subject: [PATCH 266/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880647.?=
=?UTF-8?q?=E5=9B=9E=E6=96=87=E5=AD=90=E4=B8=B2.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...36\346\226\207\345\255\220\344\270\262.md" | 57 +++++++++++++++++++
1 file changed, 57 insertions(+)
diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md"
index 913aec655a..6045ba7b49 100644
--- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md"
+++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md"
@@ -406,6 +406,63 @@ const countSubstrings = (s) => {
}
```
+TypeScript:
+
+> 动态规划:
+
+```typescript
+function countSubstrings(s: string): number {
+ /**
+ dp[i][j]: [i,j]区间内的字符串是否为回文(左闭右闭)
+ */
+ const length: number = s.length;
+ const dp: boolean[][] = new Array(length).fill(0)
+ .map(_ => new Array(length).fill(false));
+ let resCount: number = 0;
+ // 自下而上,自左向右遍历
+ for (let i = length - 1; i >= 0; i--) {
+ for (let j = i; j < length; j++) {
+ if (
+ s[i] === s[j] &&
+ (j - i <= 1 || dp[i + 1][j - 1] === true)
+ ) {
+ dp[i][j] = true;
+ resCount++;
+ }
+ }
+ }
+ return resCount;
+};
+```
+
+> 双指针法:
+
+```typescript
+function countSubstrings(s: string): number {
+ const length: number = s.length;
+ let resCount: number = 0;
+ for (let i = 0; i < length; i++) {
+ resCount += expandRange(s, i, i);
+ resCount += expandRange(s, i, i + 1);
+ }
+ return resCount;
+};
+function expandRange(s: string, left: number, right: number): number {
+ let palindromeNum: number = 0;
+ while (
+ left >= 0 && right < s.length &&
+ s[left] === s[right]
+ ) {
+ palindromeNum++;
+ left--;
+ right++;
+ }
+ return palindromeNum;
+}
+```
+
+
+
-----------------------
From 8e7663c9c663db8c7d24aa1528d45c68fe6f5e29 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 21 May 2022 14:26:24 +0800
Subject: [PATCH 267/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880516.?=
=?UTF-8?q?=E6=9C=80=E9=95=BF=E5=9B=9E=E6=96=87=E5=AD=90=E5=BA=8F=E5=88=97?=
=?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...07\345\255\220\345\272\217\345\210\227.md" | 29 +++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md"
index 69536cef56..1b0ee9a300 100644
--- "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md"
@@ -236,6 +236,35 @@ const longestPalindromeSubseq = (s) => {
};
```
+TypeScript:
+
+```typescript
+function longestPalindromeSubseq(s: string): number {
+ /**
+ dp[i][j]:[i,j]区间内,最长回文子序列的长度
+ */
+ const length: number = s.length;
+ const dp: number[][] = new Array(length).fill(0)
+ .map(_ => new Array(length).fill(0));
+ for (let i = 0; i < length; i++) {
+ dp[i][i] = 1;
+ }
+ // 自下而上,自左往右遍历
+ for (let i = length - 1; i >= 0; i--) {
+ for (let j = i + 1; j < length; j++) {
+ if (s[i] === s[j]) {
+ dp[i][j] = dp[i + 1][j - 1] + 2;
+ } else {
+ dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
+ }
+ }
+ }
+ return dp[0][length - 1];
+};
+```
+
+
+
-----------------------
From f39d349d308a4657c7cbe247060c1fcec7272497 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 21 May 2022 16:19:25 +0800
Subject: [PATCH 268/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880739.?=
=?UTF-8?q?=E6=AF=8F=E6=97=A5=E6=B8=A9=E5=BA=A6.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...17\346\227\245\346\270\251\345\272\246.md" | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
index 58edd48963..5e7a52ac32 100644
--- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
+++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
@@ -371,6 +371,32 @@ var dailyTemperatures = function(temperatures) {
};
```
+TypeScript:
+
+> 精简版:
+
+```typescript
+function dailyTemperatures(temperatures: number[]): number[] {
+ const length: number = temperatures.length;
+ const stack: number[] = [];
+ const resArr: number[] = new Array(length).fill(0);
+ stack.push(0);
+ for (let i = 1; i < length; i++) {
+ let top = stack[stack.length - 1];
+ while (
+ stack.length > 0 &&
+ temperatures[top] < temperatures[i]
+ ) {
+ resArr[top] = i - top;
+ stack.pop();
+ top = stack[stack.length - 1];
+ }
+ stack.push(i);
+ }
+ return resArr;
+};
+```
+
From ef9cefe08939725adadbefd5e49c79d7ce9e9b63 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 21 May 2022 17:11:25 +0800
Subject: [PATCH 269/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200104.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...00\345\244\247\346\267\261\345\272\246.md" | 99 +++++++++++++++++--
1 file changed, 93 insertions(+), 6 deletions(-)
diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md"
index 2229a85434..40c65af988 100644
--- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md"
+++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md"
@@ -468,7 +468,7 @@ class solution:
## go
-
+### 104.二叉树的最大深度
```go
/**
* definition for a binary tree node.
@@ -521,6 +521,8 @@ func maxdepth(root *treenode) int {
## javascript
+### 104.二叉树的最大深度
+
```javascript
var maxdepth = function(root) {
if (root === null) return 0;
@@ -568,6 +570,8 @@ var maxDepth = function(root) {
};
```
+### 559.n叉树的最大深度
+
N叉树的最大深度 递归写法
```js
var maxDepth = function(root) {
@@ -600,9 +604,9 @@ var maxDepth = function(root) {
};
```
-## TypeScript:
+## TypeScript
-> 二叉树的最大深度:
+### 104.二叉树的最大深度
```typescript
// 后续遍历(自下而上)
@@ -645,7 +649,7 @@ function maxDepth(root: TreeNode | null): number {
};
```
-> N叉树的最大深度
+### 559.n叉树的最大深度
```typescript
// 后续遍历(自下而上)
@@ -675,6 +679,8 @@ function maxDepth(root: TreeNode | null): number {
## C
+### 104.二叉树的最大深度
+
二叉树最大深度递归
```c
int maxDepth(struct TreeNode* root){
@@ -731,7 +737,8 @@ int maxDepth(struct TreeNode* root){
## Swift
->二叉树最大深度
+### 104.二叉树的最大深度
+
```swift
// 递归 - 后序
func maxDepth1(_ root: TreeNode?) -> Int {
@@ -770,7 +777,8 @@ func maxDepth(_ root: TreeNode?) -> Int {
}
```
->N叉树最大深度
+### 559.n叉树的最大深度
+
```swift
// 递归
func maxDepth(_ root: Node?) -> Int {
@@ -806,5 +814,84 @@ func maxDepth1(_ root: Node?) -> Int {
}
```
+## Scala
+
+### 104.二叉树的最大深度
+递归法:
+```scala
+object Solution {
+ def maxDepth(root: TreeNode): Int = {
+ def process(curNode: TreeNode): Int = {
+ if (curNode == null) return 0
+ // 递归左节点和右节点,返回最大的,最后+1
+ math.max(process(curNode.left), process(curNode.right)) + 1
+ }
+ // 调用递归方法,return关键字可以省略
+ process(root)
+ }
+}
+```
+
+迭代法:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def maxDepth(root: TreeNode): Int = {
+ var depth = 0
+ if (root == null) return depth
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val len = queue.size
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ depth += 1 // 只要有层次就+=1
+ }
+ depth
+ }
+}
+```
+
+### 559.n叉树的最大深度
+
+递归法:
+```scala
+object Solution {
+ def maxDepth(root: Node): Int = {
+ if (root == null) return 0
+ var depth = 0
+ for (node <- root.children) {
+ depth = math.max(depth, maxDepth(node))
+ }
+ depth + 1
+ }
+}
+```
+
+迭代法: (层序遍历)
+```scala
+object Solution {
+ import scala.collection.mutable
+ def maxDepth(root: Node): Int = {
+ if (root == null) return 0
+ var depth = 0
+ val queue = mutable.Queue[Node]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val len = queue.size
+ depth += 1
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ for (node <- curNode.children) queue.enqueue(node)
+ }
+ }
+ depth
+ }
+}
+```
+
-----------------------
From 3a7afacdeb785d271dbd57c236c2bd344a88b343 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 21 May 2022 17:18:02 +0800
Subject: [PATCH 270/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200111.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...00\345\260\217\346\267\261\345\272\246.md" | 39 +++++++++++++++++++
1 file changed, 39 insertions(+)
diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md"
index 224caa5e37..a7eb913e13 100644
--- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md"
+++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md"
@@ -488,5 +488,44 @@ func minDepth(_ root: TreeNode?) -> Int {
}
```
+## Scala
+
+递归法:
+```scala
+object Solution {
+ def minDepth(root: TreeNode): Int = {
+ if (root == null) return 0
+ if (root.left == null && root.right != null) return 1 + minDepth(root.right)
+ if (root.left != null && root.right == null) return 1 + minDepth(root.left)
+ // 如果两侧都不为空,则取最小值,return关键字可以省略
+ 1 + math.min(minDepth(root.left), minDepth(root.right))
+ }
+}
+```
+
+迭代法:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def minDepth(root: TreeNode): Int = {
+ if (root == null) return 0
+ var depth = 0
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ depth += 1
+ val len = queue.size
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ if (curNode.left == null && curNode.right == null) return depth
+ }
+ }
+ depth
+ }
+}
+```
+
-----------------------
From 9cbd053e0500846fccac3936aeb497a75906133e Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 21 May 2022 19:49:59 +0800
Subject: [PATCH 271/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880496.?=
=?UTF-8?q?=E4=B8=8B=E4=B8=80=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0?=
=?UTF-8?q?I.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4\345\244\247\345\205\203\347\264\240I.md" | 31 +++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md"
index 02339677bc..274cc32b58 100644
--- "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md"
+++ "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md"
@@ -332,5 +332,36 @@ var nextGreaterElement = function (nums1, nums2) {
};
```
+TypeScript:
+
+```typescript
+function nextGreaterElement(nums1: number[], nums2: number[]): number[] {
+ const resArr: number[] = new Array(nums1.length).fill(-1);
+ const stack: number[] = [];
+ const helperMap: Map = new Map();
+ nums1.forEach((num, index) => {
+ helperMap.set(num, index);
+ })
+ stack.push(0);
+ for (let i = 1, length = nums2.length; i < length; i++) {
+ let top = stack[stack.length - 1];
+ while (stack.length > 0 && nums2[top] < nums2[i]) {
+ let index = helperMap.get(nums2[top]);
+ if (index !== undefined) {
+ resArr[index] = nums2[i];
+ }
+ stack.pop();
+ top = stack[stack.length - 1];
+ }
+ if (helperMap.get(nums2[i]) !== undefined) {
+ stack.push(i);
+ }
+ }
+ return resArr;
+};
+```
+
+
+
-----------------------
From 10ad5411d98765272a4119ecf97b6d72c8e85ada Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 21 May 2022 20:37:58 +0800
Subject: [PATCH 272/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880503.?=
=?UTF-8?q?=E4=B8=8B=E4=B8=80=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0?=
=?UTF-8?q?II.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\345\244\247\345\205\203\347\264\240II.md" | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md"
index ace4d40bd4..33807d265d 100644
--- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md"
+++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md"
@@ -182,5 +182,31 @@ var nextGreaterElements = function (nums) {
return res;
};
```
+TypeScript:
+
+```typescript
+function nextGreaterElements(nums: number[]): number[] {
+ const length: number = nums.length;
+ const stack: number[] = [];
+ stack.push(0);
+ const resArr: number[] = new Array(length).fill(-1);
+ for (let i = 1; i < length * 2; i++) {
+ const index = i % length;
+ let top = stack[stack.length - 1];
+ while (stack.length > 0 && nums[top] < nums[index]) {
+ resArr[top] = nums[index];
+ stack.pop();
+ top = stack[stack.length - 1];
+ }
+ if (i < length) {
+ stack.push(i);
+ }
+ }
+ return resArr;
+};
+```
+
+
+
-----------------------
From 3de2992235e43b6ce40a35624a42a08b5a5a866b Mon Sep 17 00:00:00 2001
From: xuxiaomeng <961592690@qq.com>
Date: Sat, 21 May 2022 21:02:59 +0800
Subject: [PATCH 273/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A00027.=E7=A7=BB?=
=?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0=20Kotlin=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\247\273\351\231\244\345\205\203\347\264\240.md" | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
index 590cf0b924..f0cb5375bf 100644
--- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
+++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
@@ -329,5 +329,16 @@ int removeElement(int* nums, int numsSize, int val){
}
```
+Kotlin:
+```kotlin
+fun removeElement(nums: IntArray, `val`: Int): Int {
+ var slowIndex = 0 // 初始化慢指针
+ for (fastIndex in nums.indices) {
+ if (nums[fastIndex] != `val`) nums[slowIndex++] = nums[fastIndex] // 在慢指针所在位置存储未被删除的元素
+ }
+ return slowIndex
+ }
+```
+
-----------------------
From 97211892492753b1903a882c68b4387051b652cb Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 22 May 2022 00:12:15 +0800
Subject: [PATCH 274/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880042.?=
=?UTF-8?q?=E6=8E=A5=E9=9B=A8=E6=B0=B4.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...2.\346\216\245\351\233\250\346\260\264.md" | 85 +++++++++++++++++++
1 file changed, 85 insertions(+)
diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md"
index b232ce2206..060c0b4578 100644
--- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md"
+++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md"
@@ -744,6 +744,91 @@ var trap = function(height) {
};
```
+### TypeScript
+
+双指针法:
+
+```typescript
+function trap(height: number[]): number {
+ const length: number = height.length;
+ let resVal: number = 0;
+ for (let i = 0; i < length; i++) {
+ let leftMaxHeight: number = height[i],
+ rightMaxHeight: number = height[i];
+ let leftIndex: number = i - 1,
+ rightIndex: number = i + 1;
+ while (leftIndex >= 0) {
+ if (height[leftIndex] > leftMaxHeight)
+ leftMaxHeight = height[leftIndex];
+ leftIndex--;
+ }
+ while (rightIndex < length) {
+ if (height[rightIndex] > rightMaxHeight)
+ rightMaxHeight = height[rightIndex];
+ rightIndex++;
+ }
+ resVal += Math.min(leftMaxHeight, rightMaxHeight) - height[i];
+ }
+ return resVal;
+};
+```
+
+动态规划:
+
+```typescript
+function trap(height: number[]): number {
+ const length: number = height.length;
+ const leftMaxHeightDp: number[] = [],
+ rightMaxHeightDp: number[] = [];
+ leftMaxHeightDp[0] = height[0];
+ rightMaxHeightDp[length - 1] = height[length - 1];
+ for (let i = 1; i < length; i++) {
+ leftMaxHeightDp[i] = Math.max(height[i], leftMaxHeightDp[i - 1]);
+ }
+ for (let i = length - 2; i >= 0; i--) {
+ rightMaxHeightDp[i] = Math.max(height[i], rightMaxHeightDp[i + 1]);
+ }
+ let resVal: number = 0;
+ for (let i = 0; i < length; i++) {
+ resVal += Math.min(leftMaxHeightDp[i], rightMaxHeightDp[i]) - height[i];
+ }
+ return resVal;
+};
+```
+
+单调栈:
+
+```typescript
+function trap(height: number[]): number {
+ const length: number = height.length;
+ const stack: number[] = [];
+ stack.push(0);
+ let resVal: number = 0;
+ for (let i = 1; i < length; i++) {
+ let top = stack[stack.length - 1];
+ if (height[top] > height[i]) {
+ stack.push(i);
+ } else if (height[top] === height[i]) {
+ stack.pop();
+ stack.push(i);
+ } else {
+ while (stack.length > 0 && height[top] < height[i]) {
+ let mid = stack.pop();
+ if (stack.length > 0) {
+ let left = stack[stack.length - 1];
+ let h = Math.min(height[left], height[i]) - height[mid];
+ let w = i - left - 1;
+ resVal += h * w;
+ top = stack[stack.length - 1];
+ }
+ }
+ stack.push(i);
+ }
+ }
+ return resVal;
+};
+```
+
### C:
一种更简便的双指针方法:
From 278011c2df448c295985c0ba4f2ddc0843f4efd7 Mon Sep 17 00:00:00 2001
From: programmercarl <826123027@qq.com>
Date: Sun, 22 May 2022 11:11:50 +0800
Subject: [PATCH 275/551] Update
---
...10\345\271\266\345\214\272\351\227\264.md" | 4 +-
"problems/0077.\347\273\204\345\220\210.md" | 25 +++++------
...60\344\272\214\345\217\211\346\240\221.md" | 4 +-
...15\345\217\240\345\214\272\351\227\264.md" | 2 +-
...25\347\210\206\346\260\224\347\220\203.md" | 4 +-
...43\346\263\225\346\233\264\346\226\260.md" | 41 -------------------
6 files changed, 20 insertions(+), 60 deletions(-)
delete mode 100644 "problems/\351\235\242\350\257\225\351\242\230 02.07. \350\247\243\346\263\225\346\233\264\346\226\260.md"
diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
index a9caeaf057..0d002b4689 100644
--- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
+++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
@@ -112,8 +112,8 @@ public:
};
```
-* 时间复杂度:$O(n\log n)$ ,有一个快排
-* 空间复杂度:$O(1)$,我没有算result数组(返回值所需容器占的空间)
+* 时间复杂度:O(nlog n) ,有一个快排
+* 空间复杂度:O(n),有一个快排,最差情况(倒序)时,需要n次递归调用。因此确实需要O(n)的栈空间
## 总结
diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md"
index 4560c5b7eb..9e0398aba1 100644
--- "a/problems/0077.\347\273\204\345\220\210.md"
+++ "b/problems/0077.\347\273\204\345\220\210.md"
@@ -27,7 +27,7 @@
也可以直接看我的B站视频:[带你学透回溯算法-组合问题(对应力扣题目:77.组合)](https://www.bilibili.com/video/BV1ti4y1L7cv#reply3733925949)
-# 思路
+## 思路
本题这是回溯法的经典题目。
@@ -232,7 +232,7 @@ void backtracking(参数) {
**对比一下本题的代码,是不是发现有点像!** 所以有了这个模板,就有解题的大体方向,不至于毫无头绪。
-# 总结
+## 总结
组合问题是回溯法解决的经典问题,我们开始的时候给大家列举一个很形象的例子,就是n为100,k为50的话,直接想法就需要50层for循环。
@@ -242,7 +242,7 @@ void backtracking(参数) {
接着用回溯法三部曲,逐步分析了函数参数、终止条件和单层搜索的过程。
-# 剪枝优化
+## 剪枝优化
我们说过,回溯法虽然是暴力搜索,但也有时候可以有点剪枝优化一下的。
@@ -324,7 +324,7 @@ public:
};
```
-# 剪枝总结
+## 剪枝总结
本篇我们准对求组合问题的回溯法代码做了剪枝优化,这个优化如果不画图的话,其实不好理解,也不好讲清楚。
@@ -334,10 +334,10 @@ public:
-# 其他语言版本
+## 其他语言版本
-## Java:
+### Java:
```java
class Solution {
List> result = new ArrayList<>();
@@ -366,6 +366,8 @@ class Solution {
}
```
+### Python
+
Python2:
```python
class Solution(object):
@@ -395,7 +397,6 @@ class Solution(object):
return result
```
-## Python
```python
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
@@ -432,7 +433,7 @@ class Solution:
```
-## javascript
+### javascript
剪枝:
```javascript
@@ -456,7 +457,7 @@ const combineHelper = (n, k, startIndex) => {
}
```
-## TypeScript
+### TypeScript
```typescript
function combine(n: number, k: number): number[][] {
@@ -479,7 +480,7 @@ function combine(n: number, k: number): number[][] {
-## Go
+### Go
```Go
var res [][]int
func combine(n int, k int) [][]int {
@@ -534,7 +535,7 @@ func backtrack(n,k,start int,track []int){
}
```
-## C
+### C
```c
int* path;
int pathTop;
@@ -642,7 +643,7 @@ int** combine(int n, int k, int* returnSize, int** returnColumnSizes){
}
```
-## Swift
+### Swift
```swift
func combine(_ n: Int, _ k: Int) -> [[Int]] {
diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md"
index e4e232c884..1eb435892d 100644
--- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md"
@@ -238,7 +238,7 @@ public:
};
```
-# 总结
+## 总结
这次我们又深度剖析了一道二叉树的“简单题”,大家会发现,真正的把题目搞清楚其实并不简单,leetcode上accept了和真正掌握了还是有距离的。
@@ -248,7 +248,7 @@ public:
如果已经做过这道题目的同学,读完文章可以再去看看这道题目,思考一下,会有不一样的发现!
-# 相关题目推荐
+## 相关题目推荐
这两道题目基本和本题是一样的,只要稍加修改就可以AC。
diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md"
index b24ca0246d..32790a3490 100644
--- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md"
+++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md"
@@ -93,7 +93,7 @@ public:
};
```
* 时间复杂度:O(nlog n) ,有一个快排
-* 空间复杂度:O(1)
+* 空间复杂度:O(n),有一个快排,最差情况(倒序)时,需要n次递归调用。因此确实需要O(n)的栈空间
大家此时会发现如此复杂的一个问题,代码实现却这么简单!
diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md"
index 33bbad5538..327694ca75 100644
--- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md"
+++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md"
@@ -105,8 +105,8 @@ public:
};
```
-* 时间复杂度:$O(n\log n)$,因为有一个快排
-* 空间复杂度:$O(1)$
+* 时间复杂度:O(nlog n),因为有一个快排
+* 空间复杂度:O(1),有一个快排,最差情况(倒序)时,需要n次递归调用。因此确实需要O(n)的栈空间
可以看出代码并不复杂。
diff --git "a/problems/\351\235\242\350\257\225\351\242\230 02.07. \350\247\243\346\263\225\346\233\264\346\226\260.md" "b/problems/\351\235\242\350\257\225\351\242\230 02.07. \350\247\243\346\263\225\346\233\264\346\226\260.md"
deleted file mode 100644
index 6115d02ee9..0000000000
--- "a/problems/\351\235\242\350\257\225\351\242\230 02.07. \350\247\243\346\263\225\346\233\264\346\226\260.md"
+++ /dev/null
@@ -1,41 +0,0 @@
-# 双指针,不计算链表长度
-设置指向headA和headB的指针pa、pb,分别遍历两个链表,每次循环同时更新pa和pb。
-* 当链表A遍历完之后,即pa为空时,将pa指向headB;
-* 当链表B遍历完之后,即pa为空时,将pb指向headA;
-* 当pa与pb相等时,即指向同一个节点,该节点即为相交起始节点。
-* 若链表不相交,则pa、pb同时为空时退出循环,即如果链表不相交,pa与pb在遍历过全部节点后同时指向结尾空节点,此时退出循环,返回空。
-# 证明思路
-设链表A不相交部分长度为a,链表B不相交部分长度为b,两个链表相交部分长度为c。
-在pa指向链表A时,即pa为空之前,pa经过链表A不相交部分和相交部分,走过的长度为a+c;
-pa指向链表B后,在移动相交节点之前经过链表B不相交部分,走过的长度为b,总合为a+c+b。
-同理,pb走过长度的总合为b+c+a。二者相等,即pa与pb可同时到达相交起始节点。
-该方法可避免计算具体链表长度。
-```cpp
-class Solution {
-public:
- ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
- //链表为空时,返回空指针
- if(headA == nullptr || headB == nullptr) return nullptr;
- ListNode* pa = headA;
- ListNode* pb = headB;
- //pa与pb在遍历过全部节点后,同时指向结尾空节点时退出循环
- while(pa != nullptr || pb != nullptr){
- //pa为空时,将pa指向headB
- if(pa == nullptr){
- pa = headB;
- }
- //pa为空时,将pb指向headA
- if(pb == nullptr){
- pb = headA;
- }
- //pa与pb相等时,返回相交起始节点
- if(pa == pb){
- return pa;
- }
- pa = pa->next;
- pb = pb->next;
- }
- return nullptr;
- }
-};
-```
From b06b83867b466fa2d72268c0a6ef50e4da52aac3 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 22 May 2022 17:09:55 +0800
Subject: [PATCH 276/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880084.?=
=?UTF-8?q?=E6=9F=B1=E7=8A=B6=E5=9B=BE=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84?=
=?UTF-8?q?=E7=9F=A9=E5=BD=A2.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?=
=?UTF-8?q?ript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\347\232\204\347\237\251\345\275\242.md" | 90 +++++++++++++++++++
1 file changed, 90 insertions(+)
diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md"
index 439a3bc5b7..8f10a58251 100644
--- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md"
+++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md"
@@ -486,5 +486,95 @@ var largestRectangleArea = function(heights) {
return maxArea;
};
```
+TypeScript:
+
+> 双指针法(会超时):
+
+```typescript
+function largestRectangleArea(heights: number[]): number {
+ let resMax: number = 0;
+ for (let i = 0, length = heights.length; i < length; i++) {
+ // 左开右开
+ let left: number = i - 1,
+ right: number = i + 1;
+ while (left >= 0 && heights[left] >= heights[i]) {
+ left--;
+ }
+ while (right < length && heights[right] >= heights[i]) {
+ right++;
+ }
+ resMax = Math.max(resMax, heights[i] * (right - left - 1));
+ }
+ return resMax;
+};
+```
+
+> 动态规划预处理:
+
+```typescript
+function largestRectangleArea(heights: number[]): number {
+ const length: number = heights.length;
+ const leftHeightDp: number[] = [],
+ rightHeightDp: number[] = [];
+ leftHeightDp[0] = -1;
+ rightHeightDp[length - 1] = length;
+ for (let i = 1; i < length; i++) {
+ let j = i - 1;
+ while (j >= 0 && heights[i] <= heights[j]) {
+ j = leftHeightDp[j];
+ }
+ leftHeightDp[i] = j;
+ }
+ for (let i = length - 2; i >= 0; i--) {
+ let j = i + 1;
+ while (j < length && heights[i] <= heights[j]) {
+ j = rightHeightDp[j];
+ }
+ rightHeightDp[i] = j;
+ }
+ let resMax: number = 0;
+ for (let i = 0; i < length; i++) {
+ let area = heights[i] * (rightHeightDp[i] - leftHeightDp[i] - 1);
+ resMax = Math.max(resMax, area);
+ }
+ return resMax;
+};
+```
+
+> 单调栈:
+
+```typescript
+function largestRectangleArea(heights: number[]): number {
+ heights.push(0);
+ const length: number = heights.length;
+ // 栈底->栈顶:严格单调递增
+ const stack: number[] = [];
+ stack.push(0);
+ let resMax: number = 0;
+ for (let i = 1; i < length; i++) {
+ let top = stack[stack.length - 1];
+ if (heights[top] < heights[i]) {
+ stack.push(i);
+ } else if (heights[top] === heights[i]) {
+ stack.pop();
+ stack.push(i);
+ } else {
+ while (stack.length > 0 && heights[top] > heights[i]) {
+ let mid = stack.pop();
+ let left = stack.length > 0 ? stack[stack.length - 1] : -1;
+ let w = i - left - 1;
+ let h = heights[mid];
+ resMax = Math.max(resMax, w * h);
+ top = stack[stack.length - 1];
+ }
+ stack.push(i);
+ }
+ }
+ return resMax;
+};
+```
+
+
+
-----------------------
From 10b440ec27b814715dad23110b919c33d59403b6 Mon Sep 17 00:00:00 2001
From: xuxiaomeng <961592690@qq.com>
Date: Mon, 23 May 2022 09:38:24 +0800
Subject: [PATCH 277/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A00977.=E6=9C=89?=
=?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9=20Kotlin?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\347\232\204\345\271\263\346\226\271.md" | 23 ++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
index 24276bcff0..9c06d8a3a1 100644
--- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
+++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
@@ -359,7 +359,28 @@ class Solution {
}
```
-
+Kotlin:
+```kotlin
+class Solution {
+ // 双指针法
+ fun sortedSquares(nums: IntArray): IntArray {
+ var res = IntArray(nums.size)
+ var left = 0 // 指向数组的最左端
+ var right = nums.size - 1 // 指向数组端最右端
+ // 选择平方数更大的那一个往 res 数组中倒序填充
+ for (index in nums.size - 1 downTo 0) {
+ if (nums[left] * nums[left] > nums[right] * nums[right]) {
+ res[index] = nums[left] * nums[left]
+ left++
+ } else {
+ res[index] = nums[right] * nums[right]
+ right--
+ }
+ }
+ return res
+ }
+}
+```
-----------------------
From 530422fa3ecf004d27f45552e909506947992902 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Mon, 23 May 2022 10:04:15 +0800
Subject: [PATCH 278/551] =?UTF-8?q?0416.=E5=88=86=E5=89=B2=E7=AD=89?=
=?UTF-8?q?=E5=92=8C=E5=AD=90=E9=9B=86=20=E6=96=B0=E5=A2=9E=20typescript?=
=?UTF-8?q?=20=E8=A7=A3=E6=B3=95=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...11\345\222\214\345\255\220\351\233\206.md" | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
index 6e93ae8e80..01ea825e8f 100644
--- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
+++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
@@ -416,7 +416,24 @@ var canPartition = function(nums) {
};
```
-
+TypeScript:
+
+```ts
+function canPartition(nums: number[]): boolean {
+ const sum: number = nums.reduce((a: number, b: number): number => a + b);
+ if (sum % 2 === 1) return false;
+ const target: number = sum / 2;
+ // dp[j]表示容量(总数和)为j的背包所能装下的数(下标[0, i]之间任意取)的总和(<= 容量)的最大值
+ const dp: number[] = new Array(target + 1).fill(0);
+ const n: number = nums.length;
+ for (let i: number = 0; i < n; i++) {
+ for (let j: number = target; j >= nums[i]; j--) {
+ dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i]);
+ }
+ }
+ return dp[target] === target;
+};
+```
-----------------------
From 8f9c8e5fb1f9d89830b92a06f1fc59d97fd84772 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Mon, 23 May 2022 10:10:48 +0800
Subject: [PATCH 279/551] =?UTF-8?q?1049.=E6=9C=80=E5=90=8E=E4=B8=80?=
=?UTF-8?q?=E5=9D=97=E7=9F=B3=E5=A4=B4=E9=87=8D=E9=87=8F=20=E6=96=B0?=
=?UTF-8?q?=E5=A2=9Etypescript=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4\347\232\204\351\207\215\351\207\217II.md" | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
index ee0ddef2d1..8d3eeb3b23 100644
--- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
+++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
@@ -277,5 +277,23 @@ var lastStoneWeightII = function (stones) {
};
```
+TypeScript版本
+
+```ts
+function lastStoneWeightII(stones: number[]): number {
+ const sum: number = stones.reduce((a: number, b:number): number => a + b);
+ const target: number = Math.floor(sum / 2);
+ const n: number = stones.length;
+ // dp[j]表示容量(总数和)为j的背包所能装下的数(下标[0, i]之间任意取)的总和(<= 容量)的最大值
+ const dp: number[] = new Array(target + 1).fill(0);
+ for (let i: number = 0; i < n; i++ ) {
+ for (let j: number = target; j >= stones[i]; j--) {
+ dp[j] = Math.max(dp[j], dp[j - stones[i]] + stones[i]);
+ }
+ }
+ return sum - dp[target] - dp[target];
+};
+```
+
-----------------------
From ffc91f1f1ca6099af747c77c5cdbfc21125fc140 Mon Sep 17 00:00:00 2001
From: Luo <82520819+Jerry-306@users.noreply.github.com>
Date: Mon, 23 May 2022 10:20:08 +0800
Subject: [PATCH 280/551] =?UTF-8?q?0494.=E7=9B=AE=E6=A0=87=E5=92=8C=20?=
=?UTF-8?q?=E6=96=B0=E5=A2=9Etypescript=E7=89=88=E6=9C=AC=E4=BB=A3?=
=?UTF-8?q?=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4.\347\233\256\346\240\207\345\222\214.md" | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md"
index 99b768347d..929b97d417 100644
--- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md"
+++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md"
@@ -351,6 +351,28 @@ const findTargetSumWays = (nums, target) => {
};
```
+TypeScript:
+
+```ts
+function findTargetSumWays(nums: number[], target: number): number {
+ // 把数组分成两个组合left, right.left + right = sum, left - right = target.
+ const sum: number = nums.reduce((a: number, b: number): number => a + b);
+ if ((sum + target) % 2 || Math.abs(target) > sum) return 0;
+ const left: number = (sum + target) / 2;
+
+ // 将问题转化为装满容量为left的背包有多少种方法
+ // dp[i]表示装满容量为i的背包有多少种方法
+ const dp: number[] = new Array(left + 1).fill(0);
+ dp[0] = 1; // 装满容量为0的背包有1种方法(什么也不装)
+ for (let i: number = 0; i < nums.length; i++) {
+ for (let j: number = left; j >= nums[i]; j--) {
+ dp[j] += dp[j - nums[i]];
+ }
+ }
+ return dp[left];
+};
+```
+
-----------------------
From 282cdc2b44b755098d223d0f9c8bd36bf0e85959 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Mon, 23 May 2022 11:18:04 +0800
Subject: [PATCH 281/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881365.?=
=?UTF-8?q?=E6=9C=89=E5=A4=9A=E5=B0=91=E5=B0=8F=E4=BA=8E=E5=BD=93=E5=89=8D?=
=?UTF-8?q?=E6=95=B0=E5=AD=97=E7=9A=84=E6=95=B0=E5=AD=97.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...27\347\232\204\346\225\260\345\255\227.md" | 40 +++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md"
index 78fa84c094..ce1e77dfa7 100644
--- "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md"
+++ "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md"
@@ -217,6 +217,46 @@ var smallerNumbersThanCurrent = function(nums) {
};
```
+TypeScript:
+
+> 暴力法:
+
+```typescript
+function smallerNumbersThanCurrent(nums: number[]): number[] {
+ const length: number = nums.length;
+ const resArr: number[] = [];
+ for (let i = 0; i < length; i++) {
+ let count: number = 0;
+ for (let j = 0; j < length; j++) {
+ if (nums[j] < nums[i]) {
+ count++;
+ }
+ }
+ resArr[i] = count;
+ }
+ return resArr;
+};
+```
+
+> 排序+hash
+
+```typescript
+function smallerNumbersThanCurrent(nums: number[]): number[] {
+ const length: number = nums.length;
+ const sortedArr: number[] = [...nums];
+ sortedArr.sort((a, b) => a - b);
+ const hashMap: Map = new Map();
+ for (let i = length - 1; i >= 0; i--) {
+ hashMap.set(sortedArr[i], i);
+ }
+ const resArr: number[] = [];
+ for (let i = 0; i < length; i++) {
+ resArr[i] = hashMap.get(nums[i]);
+ }
+ return resArr;
+};
+```
+
-----------------------
From f83d5edb6ebf5cd6de1eabba51a246a921f94e1b Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 23 May 2022 19:14:45 +0800
Subject: [PATCH 282/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200222.=E5=AE=8C?=
=?UTF-8?q?=E5=85=A8=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9?=
=?UTF-8?q?=E4=B8=AA=E6=95=B0.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...02\347\202\271\344\270\252\346\225\260.md" | 63 +++++++++++++++++++
1 file changed, 63 insertions(+)
diff --git "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md"
index ba7acc5a49..746d45cc4a 100644
--- "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md"
+++ "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md"
@@ -646,5 +646,68 @@ func countNodes(_ root: TreeNode?) -> Int {
}
```
+## Scala
+
+递归:
+```scala
+object Solution {
+ def countNodes(root: TreeNode): Int = {
+ if(root == null) return 0
+ 1 + countNodes(root.left) + countNodes(root.right)
+ }
+}
+```
+
+层序遍历:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def countNodes(root: TreeNode): Int = {
+ if (root == null) return 0
+ val queue = mutable.Queue[TreeNode]()
+ var node = 0
+ queue.enqueue(root)
+ while (!queue.isEmpty) {
+ val len = queue.size
+ for (i <- 0 until len) {
+ node += 1
+ val curNode = queue.dequeue()
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ }
+ node
+ }
+}
+```
+
+利用完全二叉树性质:
+```scala
+object Solution {
+ def countNodes(root: TreeNode): Int = {
+ if (root == null) return 0
+ var leftNode = root.left
+ var rightNode = root.right
+ // 向左向右往下探
+ var leftDepth = 0
+ while (leftNode != null) {
+ leftDepth += 1
+ leftNode = leftNode.left
+ }
+ var rightDepth = 0
+ while (rightNode != null) {
+ rightDepth += 1
+ rightNode = rightNode.right
+ }
+ // 如果相等就是一个满二叉树
+ if (leftDepth == rightDepth) {
+ return (2 << leftDepth) - 1
+ }
+ // 如果不相等就不是一个完全二叉树,继续向下递归
+ countNodes(root.left) + countNodes(root.right) + 1
+ }
+}
+```
+
-----------------------
From 45a3c91a7a954947d6b7373bee876d2f498e26bd Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 23 May 2022 19:49:18 +0800
Subject: [PATCH 283/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200257.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...00\346\234\211\350\267\257\345\276\204.md" | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md"
index 1362897c89..70a3c66f5d 100644
--- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md"
+++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md"
@@ -702,5 +702,35 @@ func binaryTreePaths(_ root: TreeNode?) -> [String] {
}
```
+Scala:
+
+递归:
+```scala
+object Solution {
+ import scala.collection.mutable.ListBuffer
+ def binaryTreePaths(root: TreeNode): List[String] = {
+ val res = ListBuffer[String]()
+ def traversal(curNode: TreeNode, path: ListBuffer[Int]): Unit = {
+ path.append(curNode.value)
+ if (curNode.left == null && curNode.right == null) {
+ res.append(path.mkString("->")) // mkString函数: 将数组的所有值按照指定字符串拼接
+ return // 处理完可以直接return
+ }
+
+ if (curNode.left != null) {
+ traversal(curNode.left, path)
+ path.remove(path.size - 1)
+ }
+ if (curNode.right != null) {
+ traversal(curNode.right, path)
+ path.remove(path.size - 1)
+ }
+ }
+ traversal(root, ListBuffer[Int]())
+ res.toList
+ }
+}
+```
+
-----------------------
From 6d826271dc78b310dc17f107915707115a5bd544 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Tue, 24 May 2022 11:11:51 +0800
Subject: [PATCH 284/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880941.?=
=?UTF-8?q?=E6=9C=89=E6=95=88=E7=9A=84=E5=B1=B1=E8=84=89=E6=95=B0=E7=BB=84?=
=?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...61\350\204\211\346\225\260\347\273\204.md" | 20 +++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md"
index 4b7a978c0e..7004e84c2f 100644
--- "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md"
+++ "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md"
@@ -157,6 +157,26 @@ var validMountainArray = function(arr) {
};
```
+## TypeScript
+
+```typescript
+function validMountainArray(arr: number[]): boolean {
+ const length: number = arr.length;
+ if (length < 3) return false;
+ let left: number = 0,
+ right: number = length - 1;
+ while (left < (length - 1) && arr[left] < arr[left + 1]) {
+ left++;
+ }
+ while (right > 0 && arr[right] < arr[right - 1]) {
+ right--;
+ }
+ if (left === right && left !== 0 && right !== length - 1)
+ return true;
+ return false;
+};
+```
+
From 211f9f28dd6ed9f9540cd5feac9c8e9bd9f80568 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Tue, 24 May 2022 14:39:04 +0800
Subject: [PATCH 285/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880344.?=
=?UTF-8?q?=E5=8F=8D=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9APHP=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...54\345\255\227\347\254\246\344\270\262.md" | 29 +++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md"
index 58bada05b9..583d4f7875 100644
--- "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md"
@@ -267,5 +267,34 @@ public class Solution
}
```
+PHP:
+```php
+// 双指针
+// 一:
+function reverseString(&$s) {
+ $left = 0;
+ $right = count($s)-1;
+ while($left<$right){
+ $temp = $s[$left];
+ $s[$left] = $s[$right];
+ $s[$right] = $temp;
+ $left++;
+ $right--;
+ }
+}
+
+// 二:
+function reverseString(&$s) {
+ $this->reverse($s,0,count($s)-1);
+}
+// 按指定位置交换元素
+function reverse(&$s, $start, $end) {
+ for ($i = $start, $j = $end; $i < $j; $i++, $j--) {
+ $tmp = $s[$i];
+ $s[$i] = $s[$j];
+ $s[$j] = $tmp;
+ }
+}
+```
-----------------------
From ded66cf2f18df921edccfbfbf0325635c4fb04ad Mon Sep 17 00:00:00 2001
From: wang2jun <91008685+wang2jun@users.noreply.github.com>
Date: Tue, 24 May 2022 14:53:33 +0800
Subject: [PATCH 286/551] =?UTF-8?q?Update=200052.N=E7=9A=87=E5=90=8EII.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
修改错别字:想-》详
---
"problems/0052.N\347\232\207\345\220\216II.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0052.N\347\232\207\345\220\216II.md" "b/problems/0052.N\347\232\207\345\220\216II.md"
index 67e439ca81..1c45a7f116 100644
--- "a/problems/0052.N\347\232\207\345\220\216II.md"
+++ "b/problems/0052.N\347\232\207\345\220\216II.md"
@@ -44,7 +44,7 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并
# 思路
-想看:[51.N皇后](https://mp.weixin.qq.com/s/lU_QwCMj6g60nh8m98GAWg) ,基本没有区别
+详看:[51.N皇后](https://mp.weixin.qq.com/s/lU_QwCMj6g60nh8m98GAWg) ,基本没有区别
# C++代码
From 3665c051d794a3abc3b350c8cc5cdb897ef54f76 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Tue, 24 May 2022 16:53:39 +0800
Subject: [PATCH 287/551] =?UTF-8?q?=E5=A2=9E=E5=8A=A0(=E5=89=91=E6=8C=87Of?=
=?UTF-8?q?fer05.=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC.md)=EF=BC=9Aphp?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...77\346\215\242\347\251\272\346\240\274.md" | 34 +++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md"
index 037bd42773..63fb021a15 100644
--- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md"
+++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md"
@@ -415,6 +415,40 @@ func replaceSpace(_ s: String) -> String {
+PHP:
+```php
+function replaceSpace($s){
+ $sLen = strlen($s);
+ $moreLen = $this->spaceLen($s) * 2;
+
+ $head = $sLen - 1;
+ $tail = $sLen + $moreLen - 1;
+
+ $s = $s . str_repeat(' ', $moreLen);
+ while ($head != $tail) {
+ if ($s[$head] == ' ') {
+ $s[$tail--] = '0';
+ $s[$tail--] = '2';
+ $s[$tail] = '%';
+ } else {
+ $s[$tail] = $s[$head];
+ }
+ $head--;
+ $tail--;
+ }
+ return $s;
+}
+// 统计空格个数
+function spaceLen($s){
+ $count = 0;
+ for ($i = 0; $i < strlen($s); $i++) {
+ if ($s[$i] == ' ') {
+ $count++;
+ }
+ }
+ return $count;
+}
+```
-----------------------
From a69ee03b3eea09f7e2bb113b631462eadd313482 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 24 May 2022 18:59:17 +0800
Subject: [PATCH 288/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200404.=E5=B7=A6?=
=?UTF-8?q?=E5=8F=B6=E5=AD=90=E4=B9=8B=E5=92=8C.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...66\345\255\220\344\271\213\345\222\214.md" | 38 +++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md"
index d7fd629e68..78fc58f378 100644
--- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md"
+++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md"
@@ -516,6 +516,44 @@ int sumOfLeftLeaves(struct TreeNode* root){
}
```
+## Scala
+
+**递归:**
+```scala
+object Solution {
+ def sumOfLeftLeaves(root: TreeNode): Int = {
+ if(root == null) return 0
+ var midValue = 0
+ if(root.left != null && root.left.left == null && root.left.right == null){
+ midValue = root.left.value
+ }
+ // return关键字可以省略
+ midValue + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right)
+ }
+}
+```
+
+**迭代:**
+```scala
+object Solution {
+ import scala.collection.mutable
+ def sumOfLeftLeaves(root: TreeNode): Int = {
+ val stack = mutable.Stack[TreeNode]()
+ if (root == null) return 0
+ stack.push(root)
+ var sum = 0
+ while (!stack.isEmpty) {
+ val curNode = stack.pop()
+ if (curNode.left != null && curNode.left.left == null && curNode.left.right == null) {
+ sum += curNode.left.value // 如果满足条件就累加
+ }
+ if (curNode.right != null) stack.push(curNode.right)
+ if (curNode.left != null) stack.push(curNode.left)
+ }
+ sum
+ }
+}
+```
-----------------------
From 591cf193b0effd7606a4a50860804a25f06dea7c Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Tue, 24 May 2022 19:06:30 +0800
Subject: [PATCH 289/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0151.=E7=BF=BB?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95?=
=?UTF-8?q?=E8=AF=8D.md)=EF=BC=9APHP=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...14\347\232\204\345\215\225\350\257\215.md" | 47 +++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
index d03de42146..e76e05dd57 100644
--- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
+++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
@@ -761,6 +761,53 @@ func reverseWord(_ s: inout [Character]) {
+PHP:
+```php
+function reverseWords($s) {
+ $this->removeExtraSpaces($s);
+ $this->reverseString($s, 0, strlen($s)-1);
+ // 将每个单词反转
+ $start = 0;
+ for ($i = 0; $i <= strlen($s); $i++) {
+ // 到达空格或者串尾,说明一个单词结束。进行翻转。
+ if ($i == strlen($s) || $s[$i] == ' ') {
+ // 翻转,注意是左闭右闭 []的翻转。
+ $this->reverseString($s, $start, $i-1);
+ // +1: 单词与单词直接有个空格
+ $start = $i + 1;
+ }
+ }
+ return $s;
+}
+
+// 移除多余空格
+function removeExtraSpaces(&$s){
+ $slow = 0;
+ for ($i = 0; $i < strlen($s); $i++) {
+ if ($s[$i] != ' ') {
+ if ($slow != 0){
+ $s[$slow++] = ' ';
+ }
+ while ($i < strlen($s) && $s[$i] != ' ') {
+ $s[$slow++] = $s[$i++];
+ }
+ }
+ }
+ // 移动覆盖处理,丢弃多余的脏数据。
+ $s = substr($s,0,$slow);
+ return ;
+}
+
+// 翻转字符串
+function reverseString(&$s, $start, $end) {
+ for ($i = $start, $j = $end; $i < $j; $i++, $j--) {
+ $tmp = $s[$i];
+ $s[$i] = $s[$j];
+ $s[$j] = $tmp;
+ }
+ return ;
+}
+```
-----------------------
From 17a2ea3c435677f9b7b21a413bac1939208fae22 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 24 May 2022 19:21:53 +0800
Subject: [PATCH 290/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200513.=E6=89=BE?=
=?UTF-8?q?=E6=A0=91=E5=B7=A6=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...13\350\247\222\347\232\204\345\200\274.md" | 43 +++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md"
index 12c62c7068..296fe47883 100644
--- "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md"
+++ "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md"
@@ -546,7 +546,50 @@ func findBottomLeftValue(_ root: TreeNode?) -> Int {
}
```
+## Scala
+
+递归版本:
+```scala
+object Solution {
+ def findBottomLeftValue(root: TreeNode): Int = {
+ var maxLeftValue = 0
+ var maxLen = Int.MinValue
+ // 递归方法
+ def traversal(node: TreeNode, leftLen: Int): Unit = {
+ // 如果左右都为空并且当前深度大于最大深度,记录最左节点的值
+ if (node.left == null && node.right == null && leftLen > maxLen) {
+ maxLen = leftLen
+ maxLeftValue = node.value
+ }
+ if (node.left != null) traversal(node.left, leftLen + 1)
+ if (node.right != null) traversal(node.right, leftLen + 1)
+ }
+ traversal(root, 0) // 调用方法
+ maxLeftValue // return关键字可以省略
+ }
+}
+```
+层序遍历:
+```scala
+ import scala.collection.mutable
+
+ def findBottomLeftValue(root: TreeNode): Int = {
+ val queue = mutable.Queue[TreeNode]()
+ queue.enqueue(root)
+ var res = 0 // 记录每层最左侧结果
+ while (!queue.isEmpty) {
+ val len = queue.size
+ for (i <- 0 until len) {
+ val curNode = queue.dequeue()
+ if (i == 0) res = curNode.value // 记录最最左侧的节点
+ if (curNode.left != null) queue.enqueue(curNode.left)
+ if (curNode.right != null) queue.enqueue(curNode.right)
+ }
+ }
+ res // 最终返回结果,return关键字可以省略
+ }
+```
-----------------------
From 4405be238d0041b3cd2719df2945e00fe114c030 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Tue, 24 May 2022 19:56:37 +0800
Subject: [PATCH 291/551] =?UTF-8?q?=E5=A2=9E=E5=8A=A0(0206.=E7=BF=BB?=
=?UTF-8?q?=E8=BD=AC=E9=93=BE=E8=A1=A8.md)=EF=BC=9Aphp=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...77\273\350\275\254\351\223\276\350\241\250.md" | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md"
index 941928baae..2e80972c00 100644
--- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md"
+++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md"
@@ -497,5 +497,20 @@ struct ListNode* reverseList(struct ListNode* head){
}
```
+PHP:
+```php
+// 双指针法:
+function reverseList($head) {
+ $cur = $head;
+ $pre = NULL;
+ while($cur){
+ $temp = $cur->next;
+ $cur->next = $pre;
+ $pre = $cur;
+ $cur = $temp;
+ }
+ return $pre;
+}
+```
-----------------------
From 5e0a01c9f3129b822c17ec33c6e46296df6ccfeb Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Tue, 24 May 2022 20:21:22 +0800
Subject: [PATCH 292/551] =?UTF-8?q?=E5=A2=9E=E5=8A=A0(0019.=E5=88=A0?=
=?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=AC?=
=?UTF-8?q?N=E4=B8=AA=E8=8A=82=E7=82=B9.md)=EF=BC=9APHP=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4N\344\270\252\350\212\202\347\202\271.md" | 21 +++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md"
index 813e9b02fa..b0669697b8 100644
--- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md"
+++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md"
@@ -290,5 +290,26 @@ func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
}
```
+PHP:
+```php
+function removeNthFromEnd($head, $n) {
+ // 设置虚拟头节点
+ $dummyHead = new ListNode();
+ $dummyHead->next = $head;
+
+ $slow = $fast = $dummyHead;
+ while($n-- && $fast != null){
+ $fast = $fast->next;
+ }
+ // fast 再走一步,让 slow 指向删除节点的上一个节点
+ $fast = $fast->next;
+ while ($fast != NULL) {
+ $fast = $fast->next;
+ $slow = $slow->next;
+ }
+ $slow->next = $slow->next->next;
+ return $dummyHead->next;
+}
+```
-----------------------
From 1a01fe3237e1b05d955daff920874f1386d1d1a0 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 24 May 2022 20:58:07 +0800
Subject: [PATCH 293/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200112.=E8=B7=AF?=
=?UTF-8?q?=E5=BE=84=E6=80=BB=E5=92=8C.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...57\345\276\204\346\200\273\345\222\214.md" | 107 ++++++++++++++++--
1 file changed, 95 insertions(+), 12 deletions(-)
diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md"
index 41463ec181..6bf32ae929 100644
--- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md"
+++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md"
@@ -300,7 +300,7 @@ public:
## java
-lc112
+### 0112.路径总和
```java
class solution {
public boolean haspathsum(treenode root, int targetsum) {
@@ -373,7 +373,7 @@ class solution {
```
-0113.路径总和-ii
+### 0113.路径总和-ii
```java
class solution {
@@ -436,7 +436,7 @@ class Solution {
## python
-0112.路径总和
+### 0112.路径总和
**递归**
```python
@@ -488,7 +488,7 @@ class solution:
return false
```
-0113.路径总和-ii
+### 0113.路径总和-ii
**递归**
```python
@@ -545,7 +545,7 @@ class Solution:
## go
-112. 路径总和
+### 112. 路径总和
```go
//递归法
@@ -570,7 +570,7 @@ func hasPathSum(root *TreeNode, targetSum int) bool {
}
```
-113. 路径总和 II
+### 113. 路径总和 II
```go
/**
@@ -612,7 +612,7 @@ func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) {
## javascript
-0112.路径总和
+### 0112.路径总和
**递归**
```javascript
@@ -673,7 +673,7 @@ let hasPathSum = function(root, targetSum) {
};
```
-0113.路径总和-ii
+### 0113.路径总和-ii
**递归**
```javascript
@@ -768,7 +768,7 @@ let pathSum = function(root, targetSum) {
## TypeScript
-> 0112.路径总和
+### 0112.路径总和
**递归法:**
@@ -850,7 +850,7 @@ function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
};
```
-> 0112.路径总和 ii
+### 0112.路径总和 ii
**递归法:**
@@ -888,7 +888,7 @@ function pathSum(root: TreeNode | null, targetSum: number): number[][] {
## Swift
-0112.路径总和
+### 0112.路径总和
**递归**
@@ -955,7 +955,7 @@ func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool {
}
```
-0113.路径总和 II
+### 0113.路径总和 II
**递归**
@@ -1006,7 +1006,90 @@ func traversal(_ cur: TreeNode?, count: Int) {
}
```
+## Scala
+### 0112.路径总和
+
+**递归:**
+```scala
+object Solution {
+ def hasPathSum(root: TreeNode, targetSum: Int): Boolean = {
+ if(root == null) return false
+ var res = false
+
+ def traversal(curNode: TreeNode, sum: Int): Unit = {
+ if (res) return // 如果直接标记为true了,就没有往下递归的必要了
+ if (curNode.left == null && curNode.right == null && sum == targetSum) {
+ res = true
+ return
+ }
+ // 往下递归
+ if (curNode.left != null) traversal(curNode.left, sum + curNode.left.value)
+ if (curNode.right != null) traversal(curNode.right, sum + curNode.right.value)
+ }
+
+ traversal(root, root.value)
+ res // return关键字可以省略
+ }
+}
+```
+
+**迭代:**
+```scala
+object Solution {
+ import scala.collection.mutable
+ def hasPathSum(root: TreeNode, targetSum: Int): Boolean = {
+ if (root == null) return false
+ val stack = mutable.Stack[(TreeNode, Int)]()
+ stack.push((root, root.value)) // 将根节点元素放入stack
+ while (!stack.isEmpty) {
+ val curNode = stack.pop() // 取出栈顶元素
+ // 如果遇到叶子节点,看当前的值是否等于targetSum,等于则返回true
+ if (curNode._1.left == null && curNode._1.right == null && curNode._2 == targetSum) {
+ return true
+ }
+ if (curNode._1.right != null) stack.push((curNode._1.right, curNode._2 + curNode._1.right.value))
+ if (curNode._1.left != null) stack.push((curNode._1.left, curNode._2 + curNode._1.left.value))
+ }
+ false //如果没有返回true,即可返回false,return关键字可以省略
+ }
+}
+```
+
+### 0113.路径总和 II
+
+**递归:**
+```scala
+object Solution {
+ import scala.collection.mutable.ListBuffer
+ def pathSum(root: TreeNode, targetSum: Int): List[List[Int]] = {
+ val res = ListBuffer[List[Int]]()
+ if (root == null) return res.toList
+ val path = ListBuffer[Int]();
+
+ def traversal(cur: TreeNode, count: Int): Unit = {
+ if (cur.left == null && cur.right == null && count == 0) {
+ res.append(path.toList)
+ return
+ }
+ if (cur.left != null) {
+ path.append(cur.left.value)
+ traversal(cur.left, count - cur.left.value)
+ path.remove(path.size - 1)
+ }
+ if (cur.right != null) {
+ path.append(cur.right.value)
+ traversal(cur.right, count - cur.right.value)
+ path.remove(path.size - 1)
+ }
+ }
+
+ path.append(root.value)
+ traversal(root, targetSum - root.value)
+ res.toList
+ }
+}
+```
-----------------------
From e28c733f758adc362095299a73b7f4dacd76be6e Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Wed, 25 May 2022 11:19:33 +0800
Subject: [PATCH 294/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881207.?=
=?UTF-8?q?=E7=8B=AC=E4=B8=80=E6=97=A0=E4=BA=8C=E7=9A=84=E5=87=BA=E7=8E=B0?=
=?UTF-8?q?=E6=AC=A1=E6=95=B0.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?=
=?UTF-8?q?ript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...72\347\216\260\346\254\241\346\225\260.md" | 34 +++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md"
index ba92552ab8..fc51dfd42f 100644
--- "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md"
+++ "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md"
@@ -150,5 +150,39 @@ var uniqueOccurrences = function(arr) {
};
```
+TypeScript:
+
+> 借用数组:
+
+```typescript
+function uniqueOccurrences(arr: number[]): boolean {
+ const countArr: number[] = new Array(2001).fill(0);
+ for (let i = 0, length = arr.length; i < length; i++) {
+ countArr[arr[i] + 1000]++;
+ }
+ const flagArr: boolean[] = new Array(1001).fill(false);
+ for (let count of countArr) {
+ if (count === 0) continue;
+ if (flagArr[count] === true) return false;
+ flagArr[count] = true;
+ }
+ return true;
+};
+```
+
+> 借用map、set
+
+```typescript
+function uniqueOccurrences(arr: number[]): boolean {
+ const countMap: Map = new Map();
+ arr.forEach(val => {
+ countMap.set(val, (countMap.get(val) || 0) + 1);
+ })
+ return countMap.size === new Set(countMap.values()).size;
+};
+```
+
+
+
-----------------------
From 0fe499ac4142d87634d43c8e447de48091b803c3 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 25 May 2022 21:01:54 +0800
Subject: [PATCH 295/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200106.=E4=BB=8E?=
=?UTF-8?q?=E4=B8=AD=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86?=
=?UTF-8?q?=E5=BA=8F=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...40\344\272\214\345\217\211\346\240\221.md" | 46 +++++++++++++++++++
1 file changed, 46 insertions(+)
diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md"
index 188ad3cb44..37a702b7ba 100644
--- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md"
@@ -1091,7 +1091,53 @@ class Solution_0106 {
}
```
+## Scala
+106 从中序与后序遍历序列构造二叉树
+
+```scala
+object Solution {
+ def buildTree(inorder: Array[Int], postorder: Array[Int]): TreeNode = {
+ // 1、如果长度为0,则直接返回null
+ var len = inorder.size
+ if (len == 0) return null
+ // 2、后序数组的最后一个元素是当前根元素
+ var rootValue = postorder(len - 1)
+ var root: TreeNode = new TreeNode(rootValue, null, null)
+ if (len == 1) return root // 如果数组只有一个节点,就直接返回
+ // 3、在中序数组中找到切割点的索引
+ var delimiterIndex: Int = inorder.indexOf(rootValue)
+ // 4、切分数组往下迭代
+ root.left = buildTree(inorder.slice(0, delimiterIndex), postorder.slice(0, delimiterIndex))
+ root.right = buildTree(inorder.slice(delimiterIndex + 1, len), postorder.slice(delimiterIndex, len - 1))
+ root // 返回root,return关键字可以省略
+ }
+}
+```
+
+105 从前序与中序遍历序列构造二叉树
+
+```scala
+object Solution {
+ def buildTree(preorder: Array[Int], inorder: Array[Int]): TreeNode = {
+ // 1、如果长度为0,直接返回空
+ var len = inorder.size
+ if (len == 0) return null
+ // 2、前序数组的第一个元素是当前子树根节点
+ var rootValue = preorder(0)
+ var root = new TreeNode(rootValue, null, null)
+ if (len == 1) return root // 如果数组元素只有一个,那么返回根节点
+ // 3、在中序数组中,找到切割点
+ var delimiterIndex = inorder.indexOf(rootValue)
+
+ // 4、切分数组往下迭代
+ root.left = buildTree(preorder.slice(1, delimiterIndex + 1), inorder.slice(0, delimiterIndex))
+ root.right = buildTree(preorder.slice(delimiterIndex + 1, preorder.size), inorder.slice(delimiterIndex + 1, len))
+
+ root
+ }
+}
+```
-----------------------
From 2137778d44d256795bd0ca6c9b880b2c3be180d1 Mon Sep 17 00:00:00 2001
From: damon <245211612@qq.com>
Date: Thu, 26 May 2022 00:29:02 +0800
Subject: [PATCH 296/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880015.?=
=?UTF-8?q?=E4=B8=89=E6=95=B0=E4=B9=8B=E5=92=8C.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0javascript=E7=89=88=E6=9C=ACnsum=E7=9A=84?=
=?UTF-8?q?=E9=80=9A=E7=94=A8=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...11\346\225\260\344\271\213\345\222\214.md" | 70 +++++++++++++++++++
1 file changed, 70 insertions(+)
diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
index cc184c879e..12d83d8f4b 100644
--- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
@@ -345,6 +345,76 @@ var threeSum = function(nums) {
return res
};
```
+
+解法二:nSum通用解法。递归
+
+```js
+/**
+ * nsum通用解法,支持2sum,3sum,4sum...等等
+ * 时间复杂度分析:
+ * 1. n = 2时,时间复杂度O(NlogN),排序所消耗的时间。、
+ * 2. n > 2时,时间复杂度为O(N^n-1),即N的n-1次方,至少是2次方,此时可省略排序所消耗的时间。举例:3sum为O(n^2),4sum为O(n^3)
+ * @param {number[]} nums
+ * @return {number[][]}
+ */
+var threeSum = function (nums) {
+ // nsum通用解法核心方法
+ function nSumTarget(nums, n, start, target) {
+ // 前提:nums要先排序好
+ let res = [];
+ if (n === 2) {
+ res = towSumTarget(nums, start, target);
+ } else {
+ for (let i = start; i < nums.length; i++) {
+ // 递归求(n - 1)sum
+ let subRes = nSumTarget(
+ nums,
+ n - 1,
+ i + 1,
+ target - nums[i]
+ );
+ for (let j = 0; j < subRes.length; j++) {
+ res.push([nums[i], ...subRes[j]]);
+ }
+ // 跳过相同元素
+ while (nums[i] === nums[i + 1]) i++;
+ }
+ }
+ return res;
+ }
+
+ function towSumTarget(nums, start, target) {
+ // 前提:nums要先排序好
+ let res = [];
+ let len = nums.length;
+ let left = start;
+ let right = len - 1;
+ while (left < right) {
+ let sum = nums[left] + nums[right];
+ if (sum < target) {
+ while (nums[left] === nums[left + 1]) left++;
+ left++;
+ } else if (sum > target) {
+ while (nums[right] === nums[right - 1]) right--;
+ right--;
+ } else {
+ // 相等
+ res.push([nums[left], nums[right]]);
+ // 跳过相同元素
+ while (nums[left] === nums[left + 1]) left++;
+ while (nums[right] === nums[right - 1]) right--;
+ left++;
+ right--;
+ }
+ }
+ return res;
+ }
+ nums.sort((a, b) => a - b);
+ // n = 3,此时求3sum之和
+ return nSumTarget(nums, 3, 0, 0);
+};
+```
+
TypeScript:
```typescript
From c70440ac4318e48cfd3500ec290678f8976bb008 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Thu, 26 May 2022 10:54:39 +0800
Subject: [PATCH 297/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880283.?=
=?UTF-8?q?=E7=A7=BB=E5=8A=A8=E9=9B=B6.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...3.\347\247\273\345\212\250\351\233\266.md" | 21 +++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" "b/problems/0283.\347\247\273\345\212\250\351\233\266.md"
index ed59d2c4aa..60eea3788a 100644
--- "a/problems/0283.\347\247\273\345\212\250\351\233\266.md"
+++ "b/problems/0283.\347\247\273\345\212\250\351\233\266.md"
@@ -133,6 +133,27 @@ var moveZeroes = function(nums) {
};
```
+TypeScript:
+
+```typescript
+function moveZeroes(nums: number[]): void {
+ const length: number = nums.length;
+ let slowIndex: number = 0,
+ fastIndex: number = 0;
+ while (fastIndex < length) {
+ if (nums[fastIndex] !== 0) {
+ nums[slowIndex++] = nums[fastIndex];
+ };
+ fastIndex++;
+ }
+ while (slowIndex < length) {
+ nums[slowIndex++] = 0;
+ }
+};
+```
+
+
+
-----------------------
From 36a1d71201a59a113a339a1555e2734b7e0cc764 Mon Sep 17 00:00:00 2001
From: Harrytsz <18810271846@163.com>
Date: Thu, 26 May 2022 16:35:42 +0800
Subject: [PATCH 298/551] =?UTF-8?q?Update=200541.=E5=8F=8D=E8=BD=AC?=
=?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2II.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
去掉 continue,增加 else 分支,逻辑更加清晰
---
...15\350\275\254\345\255\227\347\254\246\344\270\262II.md" | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md"
index 8c13a39021..38dc685393 100644
--- "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md"
+++ "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md"
@@ -53,10 +53,10 @@ public:
// 2. 剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符
if (i + k <= s.size()) {
reverse(s.begin() + i, s.begin() + i + k );
- continue;
+ } else {
+ // 3. 剩余字符少于 k 个,则将剩余字符全部反转。
+ reverse(s.begin() + i, s.end());
}
- // 3. 剩余字符少于 k 个,则将剩余字符全部反转。
- reverse(s.begin() + i, s.begin() + s.size());
}
return s;
}
From 8d3e5b46083fe83210b683aec67e471a4a00225e Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Thu, 26 May 2022 13:09:05 +0800
Subject: [PATCH 299/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0232.=E7=94=A8?=
=?UTF-8?q?=E6=A0=88=E5=AE=9E=E7=8E=B0=E9=98=9F=E5=88=97.md)=EF=BC=9APHP?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...36\347\216\260\351\230\237\345\210\227.md" | 44 +++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md"
index 1a56d9f386..6752651e2b 100644
--- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md"
+++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md"
@@ -496,5 +496,49 @@ void myQueueFree(MyQueue* obj) {
}
```
+PHP:
+```php
+// SplStack 类通过使用一个双向链表来提供栈的主要功能。[PHP 5 >= 5.3.0, PHP 7, PHP 8]
+// https://www.php.net/manual/zh/class.splstack.php
+class MyQueue {
+ // 双栈模拟队列:In栈存储数据;Out栈辅助处理
+ private $stackIn;
+ private $stackOut;
+
+ function __construct() {
+ $this->stackIn = new SplStack();
+ $this->stackOut = new SplStack();
+ }
+
+ // In: 1 2 3 <= push
+ function push($x) {
+ $this->stackIn->push($x);
+ }
+
+ function pop() {
+ $this->peek();
+ return $this->stackOut->pop();
+ }
+
+ function peek() {
+ if($this->stackOut->isEmpty()){
+ $this->shift();
+ }
+ return $this->stackOut->top();
+ }
+
+ function empty() {
+ return $this->stackOut->isEmpty() && $this->stackIn->isEmpty();
+ }
+
+ // 如果Out栈为空,把In栈数据压入Out栈
+ // In: 1 2 3 => pop push => 1 2 3 :Out
+ private function shift(){
+ while(!$this->stackIn->isEmpty()){
+ $this->stackOut->push($this->stackIn->pop());
+ }
+ }
+}
+```
-----------------------
From 5baecc0f7919912e10ea64651ddc880f8c61f3c9 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Fri, 27 May 2022 00:59:37 +0800
Subject: [PATCH 300/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0225.=E7=94=A8?=
=?UTF-8?q?=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88.md)=EF=BC=9APHP?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...27\345\256\236\347\216\260\346\240\210.md" | 78 +++++++++++++++++++
1 file changed, 78 insertions(+)
diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md"
index 3457c4b368..f946cd8692 100644
--- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md"
+++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md"
@@ -816,5 +816,83 @@ class MyStack {
}
```
+PHP
+> 双对列
+```php
+// SplQueue 类通过使用一个双向链表来提供队列的主要功能。(PHP 5 >= 5.3.0, PHP 7, PHP 8)
+// https://www.php.net/manual/zh/class.splqueue.php
+class MyStack {
+ public $queueMain; // 保存数据
+ public $queueTmp; // 辅助作用
+
+ function __construct() {
+ $this->queueMain=new SplQueue();
+ $this->queueTmp=new SplQueue();
+ }
+
+ // queueMain: 1,2,3 <= add
+ function push($x) {
+ $this->queueMain->enqueue($x);
+ }
+
+ function pop() {
+ $qmSize = $this->queueMain->Count();
+ $qmSize --;
+ // queueMain: 3,2,1 => pop =>2,1 => add => 2,1 :queueTmp
+ while($qmSize --){
+ $this->queueTmp->enqueue($this->queueMain->dequeue());
+ }
+ // queueMain: 3
+ $val = $this->queueMain->dequeue();
+ // queueMain <= queueTmp
+ $this->queueMain = $this->queueTmp;
+ // 清空queueTmp,下次使用
+ $this->queueTmp = new SplQueue();
+ return $val;
+ }
+
+ function top() {
+ // 底层是双链表实现:从双链表的末尾查看节点
+ return $this->queueMain->top();
+ }
+
+ function empty() {
+ return $this->queueMain->isEmpty();
+ }
+}
+```
+> 单对列
+```php
+class MyStack {
+ public $queue;
+
+ function __construct() {
+ $this->queue=new SplQueue();
+ }
+
+ function push($x) {
+ $this->queue->enqueue($x);
+ }
+
+ function pop() {
+ $qmSize = $this->queue->Count();
+ $qmSize --;
+ //queue: 3,2,1 => pop =>2,1 => add => 2,1,3 :queue
+ while($qmSize --){
+ $this->queue->enqueue($this->queue->dequeue());
+ }
+ $val = $this->queue->dequeue();
+ return $val;
+ }
+
+ function top() {
+ return $this->queue->top();
+ }
+
+ function empty() {
+ return $this->queue->isEmpty();
+ }
+}
+```
-----------------------
From 513ec5418910aee18a1b6722b9316ffaaeb2dec4 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 27 May 2022 12:18:24 +0800
Subject: [PATCH 301/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880189.?=
=?UTF-8?q?=E6=97=8B=E8=BD=AC=E6=95=B0=E7=BB=84.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...13\350\275\254\346\225\260\347\273\204.md" | 23 +++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git "a/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" "b/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md"
index 3ffed87745..23092f9cb7 100644
--- "a/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md"
+++ "b/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md"
@@ -7,6 +7,8 @@
# 189. 旋转数组
+[力扣题目链接](https://leetcode.cn/problems/rotate-array/)
+
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
进阶:
@@ -160,6 +162,27 @@ var rotate = function (nums, k) {
};
```
+## TypeScript
+
+```typescript
+function rotate(nums: number[], k: number): void {
+ const length: number = nums.length;
+ k %= length;
+ reverseByRange(nums, 0, length - 1);
+ reverseByRange(nums, 0, k - 1);
+ reverseByRange(nums, k, length - 1);
+};
+function reverseByRange(nums: number[], left: number, right: number): void {
+ while (left < right) {
+ const temp = nums[left];
+ nums[left] = nums[right];
+ nums[right] = temp;
+ left++;
+ right--;
+ }
+}
+```
+
-----------------------
From 156d8fdd92b07c6ba74ed68ee9e8cd092c106024 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Fri, 27 May 2022 14:21:41 +0800
Subject: [PATCH 302/551] =?UTF-8?q?=E5=A2=9E=E5=8A=A0(0020.=E6=9C=89?=
=?UTF-8?q?=E6=95=88=E7=9A=84=E6=8B=AC=E5=8F=B7.md)=EF=BC=9APHP=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...10\347\232\204\346\213\254\345\217\267.md" | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md"
index 7bb7f7463a..5e468549ca 100644
--- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md"
+++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md"
@@ -401,5 +401,33 @@ bool isValid(char * s){
}
```
+PHP:
+```php
+// https://www.php.net/manual/zh/class.splstack.php
+class Solution
+{
+ function isValid($s){
+ $stack = new SplStack();
+ for ($i = 0; $i < strlen($s); $i++) {
+ if ($s[$i] == "(") {
+ $stack->push(')');
+ } else if ($s[$i] == "{") {
+ $stack->push('}');
+ } else if ($s[$i] == "[") {
+ $stack->push(']');
+ // 2、遍历匹配过程中,发现栈内没有要匹配的字符 return false
+ // 3、遍历匹配过程中,栈已为空,没有匹配的字符了,说明右括号没有找到对应的左括号 return false
+ } else if ($stack->isEmpty() || $stack->top() != $s[$i]) {
+ return false;
+ } else {//$stack->top() == $s[$i]
+ $stack->pop();
+ }
+ }
+ // 1、遍历完,但是栈不为空,说明有相应的括号没有被匹配,return false
+ return $stack->isEmpty();
+ }
+}
+```
+
-----------------------
From edbff2f4f4746b09afa2d6dd8981f000d0bc75ae Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Fri, 27 May 2022 15:04:23 +0800
Subject: [PATCH 303/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(1047.=E5=88=A0?=
=?UTF-8?q?=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80?=
=?UTF-8?q?=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9.md)?=
=?UTF-8?q?=EF=BC=9Aphp=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...73\351\207\215\345\244\215\351\241\271.md" | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md"
index 638c8f4ed2..6542839486 100644
--- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md"
+++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md"
@@ -375,5 +375,30 @@ func removeDuplicates(_ s: String) -> String {
}
```
+PHP:
+```php
+class Solution {
+ function removeDuplicates($s) {
+ $stack = new SplStack();
+ for($i=0;$iisEmpty() || $s[$i] != $stack->top()){
+ $stack->push($s[$i]);
+ }else{
+ $stack->pop();
+ }
+ }
+
+ $result = "";
+ while(!$stack->isEmpty()){
+ $result.= $stack->top();
+ $stack->pop();
+ }
+
+ // 此时字符串需要反转一下
+ return strrev($result);
+ }
+}
+```
+
-----------------------
From 821ca656e14384fd63e937a0cd99fbe7bde813f0 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 27 May 2022 16:10:35 +0800
Subject: [PATCH 304/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200654.=E6=9C=80?=
=?UTF-8?q?=E5=A4=A7=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\344\272\214\345\217\211\346\240\221.md" | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md"
index 1c73354b00..1b0eac66d0 100644
--- "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md"
@@ -476,7 +476,33 @@ func traversal(_ nums: inout [Int], _ left: Int, _ right: Int) -> TreeNode? {
}
```
+## Scala
+
+```scala
+object Solution {
+ def constructMaximumBinaryTree(nums: Array[Int]): TreeNode = {
+ if (nums.size == 0) return null
+ // 找到数组最大值
+ var maxIndex = 0
+ var maxValue = Int.MinValue
+ for (i <- nums.indices) {
+ if (nums(i) > maxValue) {
+ maxIndex = i
+ maxValue = nums(i)
+ }
+ }
+
+ // 构建一棵树
+ var root = new TreeNode(maxValue, null, null)
+
+ // 递归寻找左右子树
+ root.left = constructMaximumBinaryTree(nums.slice(0, maxIndex))
+ root.right = constructMaximumBinaryTree(nums.slice(maxIndex + 1, nums.length))
+ root // 返回root
+ }
+}
+```
-----------------------
From de34170f5cc1526eb0c4b1860943a57e6f41c4e1 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 27 May 2022 16:37:53 +0800
Subject: [PATCH 305/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200617.=E5=90=88?=
=?UTF-8?q?=E5=B9=B6=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...66\344\272\214\345\217\211\346\240\221.md" | 53 +++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md"
index 55786ea988..d8bdc91c7f 100644
--- "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md"
@@ -631,7 +631,60 @@ function mergeTrees(root1: TreeNode | null, root2: TreeNode | null): TreeNode |
};
```
+## Scala
+
+递归:
+```scala
+object Solution {
+ def mergeTrees(root1: TreeNode, root2: TreeNode): TreeNode = {
+ if (root1 == null) return root2 // 如果root1为空,返回root2
+ if (root2 == null) return root1 // 如果root2为空,返回root1
+ // 新建一个节点,值为两个节点的和
+ var node = new TreeNode(root1.value + root2.value)
+ // 往下递归
+ node.left = mergeTrees(root1.left, root2.left)
+ node.right = mergeTrees(root1.right, root2.right)
+ node // 返回node,return关键字可以省略
+ }
+}
+```
+迭代:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def mergeTrees(root1: TreeNode, root2: TreeNode): TreeNode = {
+ if (root1 == null) return root2
+ if (root2 == null) return root1
+ var stack = mutable.Stack[TreeNode]()
+ // 先放node2再放node1
+ stack.push(root2)
+ stack.push(root1)
+ while (!stack.isEmpty) {
+ var node1 = stack.pop()
+ var node2 = stack.pop()
+ node1.value += node2.value
+ if (node1.right != null && node2.right != null) {
+ stack.push(node2.right)
+ stack.push(node1.right)
+ } else {
+ if(node1.right == null){
+ node1.right = node2.right
+ }
+ }
+ if (node1.left != null && node2.left != null) {
+ stack.push(node2.left)
+ stack.push(node1.left)
+ } else {
+ if(node1.left == null){
+ node1.left = node2.left
+ }
+ }
+ }
+ root1
+ }
+}
+```
-----------------------
From 9412e2e40529db526bbb16de1f3a9c45cf2c3949 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 27 May 2022 17:01:56 +0800
Subject: [PATCH 306/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200700.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C?=
=?UTF-8?q?=E7=B4=A2.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...55\347\232\204\346\220\234\347\264\242.md" | 27 +++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md"
index 40cf4ea1fc..16ddef9bc9 100644
--- "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md"
+++ "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md"
@@ -363,7 +363,34 @@ function searchBST(root: TreeNode | null, val: number): TreeNode | null {
};
```
+## Scala
+
+递归:
+```scala
+object Solution {
+ def searchBST(root: TreeNode, value: Int): TreeNode = {
+ if (root == null || value == root.value) return root
+ // 相当于三元表达式,在Scala中if...else有返回值
+ if (value < root.value) searchBST(root.left, value) else searchBST(root.right, value)
+ }
+}
+```
+迭代:
+```scala
+object Solution {
+ def searchBST(root: TreeNode, value: Int): TreeNode = {
+ // 因为root是不可变量,所以需要赋值给一个可变量
+ var node = root
+ while (node != null) {
+ if (value < node.value) node = node.left
+ else if (value > node.value) node = node.right
+ else return node
+ }
+ null // 没有返回就返回空
+ }
+}
+```
-----------------------
From d4a4eda4b8396603a70871ff215b48b2d17eef93 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 27 May 2022 17:29:44 +0800
Subject: [PATCH 307/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200098.=E9=AA=8C?=
=?UTF-8?q?=E8=AF=81=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...11\346\220\234\347\264\242\346\240\221.md" | 43 +++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
index a8f3c32402..06f1b8d15f 100644
--- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
+++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
@@ -589,7 +589,50 @@ function isValidBST(root: TreeNode | null): boolean {
};
```
+## Scala
+
+辅助数组解决:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def isValidBST(root: TreeNode): Boolean = {
+ var arr = new mutable.ArrayBuffer[Int]()
+ // 递归中序遍历二叉树,将节点添加到arr
+ def traversal(node: TreeNode): Unit = {
+ if (node == null) return
+ traversal(node.left)
+ arr.append(node.value)
+ traversal(node.right)
+ }
+ traversal(root)
+ // 这个数组如果是升序就代表是二叉搜索树
+ for (i <- 1 until arr.size) {
+ if (arr(i) <= arr(i - 1)) return false
+ }
+ true
+ }
+}
+```
+递归中解决:
+```scala
+object Solution {
+ def isValidBST(root: TreeNode): Boolean = {
+ var flag = true
+ var preValue:Long = Long.MinValue // 这里要使用Long类型
+
+ def traversal(node: TreeNode): Unit = {
+ if (node == null || flag == false) return
+ traversal(node.left)
+ if (node.value > preValue) preValue = node.value
+ else flag = false
+ traversal(node.right)
+ }
+ traversal(root)
+ flag
+ }
+}
+```
-----------------------
From 4e95d663a047d1745c57b3f46cba055836e8ec13 Mon Sep 17 00:00:00 2001
From: damon <245211612@qq.com>
Date: Fri, 27 May 2022 22:52:57 +0800
Subject: [PATCH 308/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880509.?=
=?UTF-8?q?=E6=96=90=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0javascript=E7=89=88=E6=9C=AC?=
=?UTF-8?q?=E4=BC=98=E5=8C=96=E7=A9=BA=E9=97=B4=E5=A4=8D=E6=9D=82=E5=BA=A6?=
=?UTF-8?q?=E5=88=B0O(1)=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...242\351\202\243\345\245\221\346\225\260.md" | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
index 1d17784d43..7c899195c8 100644
--- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
+++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
@@ -234,6 +234,7 @@ func fib(n int) int {
}
```
### Javascript
+解法一
```Javascript
var fib = function(n) {
let dp = [0, 1]
@@ -244,6 +245,23 @@ var fib = function(n) {
return dp[n]
};
```
+解法二:时间复杂度O(N),空间复杂度O(1)
+```Javascript
+var fib = function(n) {
+ // 动规状态转移中,当前结果只依赖前两个元素的结果,所以只要两个变量代替dp数组记录状态过程。将空间复杂度降到O(1)
+ let pre1 = 1
+ let pre2 = 0
+ let temp
+ if (n === 0) return 0
+ if (n === 1) return 1
+ for(let i = 2; i <= n; i++) {
+ temp = pre1
+ pre1 = pre1 + pre2
+ pre2 = temp
+ }
+ return pre1
+};
+```
TypeScript
From 992909385fd8bc0181a76bf3fa65eb10f9885190 Mon Sep 17 00:00:00 2001
From: damon <245211612@qq.com>
Date: Fri, 27 May 2022 22:52:57 +0800
Subject: [PATCH 309/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880509.?=
=?UTF-8?q?=E6=96=90=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0javascript=E7=89=88=E6=9C=AC?=
=?UTF-8?q?=E4=BC=98=E5=8C=96=E7=A9=BA=E9=97=B4=E5=A4=8D=E6=9D=82=E5=BA=A6?=
=?UTF-8?q?=E5=88=B0O(1)=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...242\351\202\243\345\245\221\346\225\260.md" | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
index 1d17784d43..7c899195c8 100644
--- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
+++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
@@ -234,6 +234,7 @@ func fib(n int) int {
}
```
### Javascript
+解法一
```Javascript
var fib = function(n) {
let dp = [0, 1]
@@ -244,6 +245,23 @@ var fib = function(n) {
return dp[n]
};
```
+解法二:时间复杂度O(N),空间复杂度O(1)
+```Javascript
+var fib = function(n) {
+ // 动规状态转移中,当前结果只依赖前两个元素的结果,所以只要两个变量代替dp数组记录状态过程。将空间复杂度降到O(1)
+ let pre1 = 1
+ let pre2 = 0
+ let temp
+ if (n === 0) return 0
+ if (n === 1) return 1
+ for(let i = 2; i <= n; i++) {
+ temp = pre1
+ pre1 = pre1 + pre2
+ pre2 = temp
+ }
+ return pre1
+};
+```
TypeScript
From a9f830267bdcd9a5bc6ec19dfbc1b4436fa41a1e Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 28 May 2022 11:12:53 +0800
Subject: [PATCH 310/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880724.?=
=?UTF-8?q?=E5=AF=BB=E6=89=BE=E6=95=B0=E7=BB=84=E7=9A=84=E4=B8=AD=E5=BF=83?=
=?UTF-8?q?=E7=B4=A2=E5=BC=95.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?=
=?UTF-8?q?ript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...255\345\277\203\347\264\242\345\274\225.md" | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git "a/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md" "b/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md"
index 14dcd2c08a..0052e5d462 100644
--- "a/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md"
+++ "b/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md"
@@ -140,6 +140,24 @@ var pivotIndex = function(nums) {
};
```
+### TypeScript
+
+```typescript
+function pivotIndex(nums: number[]): number {
+ const length: number = nums.length;
+ const sum: number = nums.reduce((a, b) => a + b);
+ let leftSum: number = 0;
+ for (let i = 0; i < length; i++) {
+ const rightSum: number = sum - leftSum - nums[i];
+ if (leftSum === rightSum) return i;
+ leftSum += nums[i];
+ }
+ return -1;
+};
+```
+
+
+
-----------------------
From 6f46d91676693a83c0620e49bfdf9b9a252d87a0 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 28 May 2022 13:55:52 +0800
Subject: [PATCH 311/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200530.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F?=
=?UTF-8?q?=E7=BB=9D=E5=AF=B9=E5=B7=AE.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...17\347\273\235\345\257\271\345\267\256.md" | 77 +++++++++++++++++++
1 file changed, 77 insertions(+)
diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md"
index 77699c9f4e..d7db85ae02 100644
--- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md"
+++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md"
@@ -431,7 +431,84 @@ function getMinimumDifference(root: TreeNode | null): number {
};
```
+## Scala
+
+构建二叉树的有序数组:
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def getMinimumDifference(root: TreeNode): Int = {
+ val arr = mutable.ArrayBuffer[Int]()
+ def traversal(node: TreeNode): Unit = {
+ if (node == null) return
+ traversal(node.left)
+ arr.append(node.value)
+ traversal(node.right)
+ }
+ traversal(root)
+ // 在有序数组上求最小差值
+ var result = Int.MaxValue
+ for (i <- 1 until arr.size) {
+ result = math.min(result, arr(i) - arr(i - 1))
+ }
+ result // 返回最小差值
+ }
+}
+```
+递归记录前一个节点:
+
+```scala
+object Solution {
+ def getMinimumDifference(root: TreeNode): Int = {
+ var result = Int.MaxValue // 初始化为最大值
+ var pre: TreeNode = null // 记录前一个节点
+
+ def traversal(cur: TreeNode): Unit = {
+ if (cur == null) return
+ traversal(cur.left)
+ if (pre != null) {
+ // 对比result与节点之间的差值
+ result = math.min(result, cur.value - pre.value)
+ }
+ pre = cur
+ traversal(cur.right)
+ }
+
+ traversal(root)
+ result // return关键字可以省略
+ }
+}
+```
+
+迭代解决:
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def getMinimumDifference(root: TreeNode): Int = {
+ var result = Int.MaxValue // 初始化为最大值
+ var pre: TreeNode = null // 记录前一个节点
+ var cur = root
+ var stack = mutable.Stack[TreeNode]()
+ while (cur != null || !stack.isEmpty) {
+ if (cur != null) {
+ stack.push(cur)
+ cur = cur.left
+ } else {
+ cur = stack.pop()
+ if (pre != null) {
+ result = math.min(result, cur.value - pre.value)
+ }
+ pre = cur
+ cur = cur.right
+ }
+ }
+ result // return关键字可以省略
+ }
+}
+```
-----------------------
From 779adf74cb1ec9b1f8b17db3cbf3d8fc3b88392f Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 28 May 2022 14:53:40 +0800
Subject: [PATCH 312/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200501.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97?=
=?UTF-8?q?=E6=95=B0.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...55\347\232\204\344\274\227\346\225\260.md" | 71 ++++++++++++++++++-
1 file changed, 70 insertions(+), 1 deletion(-)
diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md"
index 9cb5d0713a..c08f68d951 100644
--- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md"
+++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md"
@@ -9,7 +9,7 @@
# 501.二叉搜索树中的众数
-[力扣题目链接](https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/solution/)
+[力扣题目链接](https://leetcode.cn/problems/find-mode-in-binary-search-tree/)
给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。
@@ -798,7 +798,76 @@ function findMode(root: TreeNode | null): number[] {
};
```
+## Scala
+
+暴力:
+```scala
+object Solution {
+ // 导包
+ import scala.collection.mutable // 集合包
+ import scala.util.control.Breaks.{break, breakable} // 流程控制包
+ def findMode(root: TreeNode): Array[Int] = {
+ var map = mutable.HashMap[Int, Int]() // 存储节点的值,和该值出现的次数
+ def searchBST(curNode: TreeNode): Unit = {
+ if (curNode == null) return
+ var value = map.getOrElse(curNode.value, 0)
+ map.put(curNode.value, value + 1)
+ searchBST(curNode.left)
+ searchBST(curNode.right)
+ }
+ searchBST(root) // 前序遍历把每个节点的值加入到里面
+ // 将map转换为list,随后根据元组的第二个值进行排序
+ val list = map.toList.sortWith((map1, map2) => {
+ if (map1._2 > map2._2) true else false
+ })
+ var res = mutable.ArrayBuffer[Int]()
+ res.append(list(0)._1) // 将第一个加入结果集
+ breakable {
+ for (i <- 1 until list.size) {
+ // 如果值相同就加入结果集合,反之break
+ if (list(i)._2 == list(0)._2) res.append(list(i)._1)
+ else break
+ }
+ }
+ res.toArray // 最终返回res的Array格式,return关键字可以省略
+ }
+}
+```
+递归(利用二叉搜索树的性质):
+```scala
+object Solution {
+ import scala.collection.mutable
+ def findMode(root: TreeNode): Array[Int] = {
+ var maxCount = 0 // 最大频率
+ var count = 0 // 统计频率
+ var pre: TreeNode = null
+ var result = mutable.ArrayBuffer[Int]()
+
+ def searchBST(cur: TreeNode): Unit = {
+ if (cur == null) return
+ searchBST(cur.left)
+ if (pre == null) count = 1 // 等于空置为1
+ else if (pre.value == cur.value) count += 1 // 与上一个节点的值相同加1
+ else count = 1 // 与上一个节点的值不同
+ pre = cur
+
+ // 如果和最大值相同,则放入结果集
+ if (count == maxCount) result.append(cur.value)
+
+ // 如果当前计数大于最大值频率,更新最大值,清空结果集
+ if (count > maxCount) {
+ maxCount = count
+ result.clear()
+ result.append(cur.value)
+ }
+ searchBST(cur.right)
+ }
+ searchBST(root)
+ result.toArray // return关键字可以省略
+ }
+}
+```
-----------------------
From 95cc2b965807a6493fe64eb23d6bbfa66587f0fa Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 29 May 2022 10:18:40 +0800
Subject: [PATCH 313/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200236.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1?=
=?UTF-8?q?=E7=A5=96=E5=85=88.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...254\345\205\261\347\245\226\345\205\210.md" | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md"
index 69a6d0d6a7..a99180c30b 100644
--- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md"
+++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md"
@@ -343,7 +343,25 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree
};
```
+## Scala
+
+```scala
+object Solution {
+ def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = {
+ // 递归结束条件
+ if (root == null || root == p || root == q) {
+ return root
+ }
+
+ var left = lowestCommonAncestor(root.left, p, q)
+ var right = lowestCommonAncestor(root.right, p, q)
+ if (left != null && right != null) return root
+ if (left == null) return right
+ left
+ }
+}
+```
-----------------------
From 78930cdd0970c0850db6aa7c90ced0510fd6b1ec Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 29 May 2022 10:32:47 +0800
Subject: [PATCH 314/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200235.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91?=
=?UTF-8?q?=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...54\345\205\261\347\245\226\345\205\210.md" | 29 +++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md"
index f7f1427aad..a0f3099926 100644
--- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md"
+++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md"
@@ -381,7 +381,36 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree
};
```
+## Scala
+
+递归:
+
+```scala
+object Solution {
+ def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = {
+ // scala中每个关键字都有其返回值,于是可以不写return
+ if (root.value > p.value && root.value > q.value) lowestCommonAncestor(root.left, p, q)
+ else if (root.value < p.value && root.value < q.value) lowestCommonAncestor(root.right, p, q)
+ else root
+ }
+}
+```
+
+迭代:
+```scala
+object Solution {
+ def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = {
+ var curNode = root // 因为root是不可变量,所以要赋值给curNode一个可变量
+ while(curNode != null){
+ if(curNode.value > p.value && curNode.value > q.value) curNode = curNode.left
+ else if(curNode.value < p.value && curNode.value < q.value) curNode = curNode.right
+ else return curNode
+ }
+ null
+ }
+}
+```
-----------------------
From 45a01eed32598a6cf4c9a20d995aa46efe296dd5 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 29 May 2022 13:04:37 +0800
Subject: [PATCH 315/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200701.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92?=
=?UTF-8?q?=E5=85=A5=E6=93=8D=E4=BD=9C.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...22\345\205\245\346\223\215\344\275\234.md" | 37 +++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md"
index 50e39ade5e..135911b919 100644
--- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md"
+++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md"
@@ -585,6 +585,43 @@ function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null {
```
+## Scala
+
+递归:
+
+```scala
+object Solution {
+ def insertIntoBST(root: TreeNode, `val`: Int): TreeNode = {
+ if (root == null) return new TreeNode(`val`)
+ if (`val` < root.value) root.left = insertIntoBST(root.left, `val`)
+ else root.right = insertIntoBST(root.right, `val`)
+ root // 返回根节点
+ }
+}
+```
+
+迭代:
+
+```scala
+object Solution {
+ def insertIntoBST(root: TreeNode, `val`: Int): TreeNode = {
+ if (root == null) {
+ return new TreeNode(`val`)
+ }
+ var parent = root // 记录当前节点的父节点
+ var curNode = root
+ while (curNode != null) {
+ parent = curNode
+ if(`val` < curNode.value) curNode = curNode.left
+ else curNode = curNode.right
+ }
+ if(`val` < parent.value) parent.left = new TreeNode(`val`)
+ else parent.right = new TreeNode(`val`)
+ root // 最终返回根节点
+ }
+}
+```
+
-----------------------
From cc72b164cf7780f3dc4e119d30033dd0708eddd5 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 29 May 2022 22:48:13 +0800
Subject: [PATCH 316/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880034.?=
=?UTF-8?q?=E5=9C=A8=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5?=
=?UTF-8?q?=E6=89=BE=E5=85=83=E7=B4=A0=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA?=
=?UTF-8?q?=E5=92=8C=E6=9C=80=E5=90=8E=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE?=
=?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...00\344\270\252\344\275\215\347\275\256.md" | 55 +++++++++++++++++++
1 file changed, 55 insertions(+)
diff --git "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md"
index dfd90b8280..25db008349 100644
--- "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md"
+++ "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md"
@@ -481,6 +481,61 @@ var searchRange = function(nums, target) {
};
```
+### TypeScript
+
+```typescript
+function searchRange(nums: number[], target: number): number[] {
+ const leftBoard: number = getLeftBorder(nums, target);
+ const rightBoard: number = getRightBorder(nums, target);
+ // target 在nums区间左侧或右侧
+ if (leftBoard === (nums.length - 1) || rightBoard === 0) return [-1, -1];
+ // target 不存在与nums范围内
+ if (rightBoard - leftBoard <= 1) return [-1, -1];
+ // target 存在于nums范围内
+ return [leftBoard + 1, rightBoard - 1];
+};
+// 查找第一个大于target的元素下标
+function getRightBorder(nums: number[], target: number): number {
+ let left: number = 0,
+ right: number = nums.length - 1;
+ // 0表示target在nums区间的左边
+ let rightBoard: number = 0;
+ while (left <= right) {
+ let mid = Math.floor((left + right) / 2);
+ if (nums[mid] <= target) {
+ // 右边界一定在mid右边(不含mid)
+ left = mid + 1;
+ rightBoard = left;
+ } else {
+ // 右边界在mid左边(含mid)
+ right = mid - 1;
+ }
+ }
+ return rightBoard;
+}
+// 查找第一个小于target的元素下标
+function getLeftBorder(nums: number[], target: number): number {
+ let left: number = 0,
+ right: number = nums.length - 1;
+ // length-1表示target在nums区间的右边
+ let leftBoard: number = nums.length - 1;
+ while (left <= right) {
+ let mid = Math.floor((left + right) / 2);
+ if (nums[mid] >= target) {
+ // 左边界一定在mid左边(不含mid)
+ right = mid - 1;
+ leftBoard = right;
+ } else {
+ // 左边界在mid右边(含mid)
+ left = mid + 1;
+ }
+ }
+ return leftBoard;
+}
+```
+
+
+
-----------------------
From 8eb956b553b178d3d14555c270f8b726baea5870 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 30 May 2022 10:29:31 +0800
Subject: [PATCH 317/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200450.=E5=88=A0?=
=?UTF-8?q?=E9=99=A4=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD?=
=?UTF-8?q?=E7=9A=84=E8=8A=82=E7=82=B9.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...55\347\232\204\350\212\202\347\202\271.md" | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md"
index e8f7e54c4b..8367a14543 100644
--- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md"
+++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md"
@@ -582,7 +582,35 @@ function deleteNode(root: TreeNode | null, key: number): TreeNode | null {
};
```
+## Scala
+
+```scala
+object Solution {
+ def deleteNode(root: TreeNode, key: Int): TreeNode = {
+ if (root == null) return root // 第一种情况,没找到删除的节点,遍历到空节点直接返回
+ if (root.value == key) {
+ // 第二种情况: 左右孩子都为空,直接删除节点,返回null
+ if (root.left == null && root.right == null) return null
+ // 第三种情况: 左孩子为空,右孩子不为空,右孩子补位
+ else if (root.left == null && root.right != null) return root.right
+ // 第四种情况: 左孩子不为空,右孩子为空,左孩子补位
+ else if (root.left != null && root.right == null) return root.left
+ // 第五种情况: 左右孩子都不为空,将删除节点的左子树头节点(左孩子)放到
+ // 右子树的最左边节点的左孩子上,返回删除节点的右孩子
+ else {
+ var tmp = root.right
+ while (tmp.left != null) tmp = tmp.left
+ tmp.left = root.left
+ return root.right
+ }
+ }
+ if (root.value > key) root.left = deleteNode(root.left, key)
+ if (root.value < key) root.right = deleteNode(root.right, key)
+ root // 返回根节点,return关键字可以省略
+ }
+}
+```
-----------------------
From df2ff8cc2e77261a0360ac848949c233f3c6258a Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 30 May 2022 10:50:51 +0800
Subject: [PATCH 318/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200669.=E4=BF=AE?=
=?UTF-8?q?=E5=89=AA=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...7\211\346\220\234\347\264\242\346\240\221.md" | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
index 385b226863..988bff30d6 100644
--- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
+++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
@@ -453,7 +453,21 @@ function trimBST(root: TreeNode | null, low: number, high: number): TreeNode | n
};
```
-
+## Scala
+
+递归法:
+```scala
+object Solution {
+ def trimBST(root: TreeNode, low: Int, high: Int): TreeNode = {
+ if (root == null) return null
+ if (root.value < low) return trimBST(root.right, low, high)
+ if (root.value > high) return trimBST(root.left, low, high)
+ root.left = trimBST(root.left, low, high)
+ root.right = trimBST(root.right, low, high)
+ root
+ }
+}
+```
-----------------------
From 43d450d8271b6af68d209bda8f22ed71838418f1 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Mon, 30 May 2022 11:20:33 +0800
Subject: [PATCH 319/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880922.?=
=?UTF-8?q?=E6=8C=89=E5=A5=87=E5=81=B6=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84?=
=?UTF-8?q?II.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\345\272\217\346\225\260\347\273\204II.md" | 69 +++++++++++++++++++
1 file changed, 69 insertions(+)
diff --git "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md"
index cb564fb683..cb8cec73ac 100644
--- "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md"
+++ "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md"
@@ -260,6 +260,75 @@ var sortArrayByParityII = function(nums) {
};
```
+### TypeScript
+
+> 方法一:
+
+```typescript
+function sortArrayByParityII(nums: number[]): number[] {
+ const evenArr: number[] = [],
+ oddArr: number[] = [];
+ for (let num of nums) {
+ if (num % 2 === 0) {
+ evenArr.push(num);
+ } else {
+ oddArr.push(num);
+ }
+ }
+ const resArr: number[] = [];
+ for (let i = 0, length = nums.length / 2; i < length; i++) {
+ resArr.push(evenArr[i]);
+ resArr.push(oddArr[i]);
+ }
+ return resArr;
+};
+```
+
+> 方法二:
+
+```typescript
+function sortArrayByParityII(nums: number[]): number[] {
+ const length: number = nums.length;
+ const resArr: number[] = [];
+ let evenIndex: number = 0,
+ oddIndex: number = 1;
+ for (let i = 0; i < length; i++) {
+ if (nums[i] % 2 === 0) {
+ resArr[evenIndex] = nums[i];
+ evenIndex += 2;
+ } else {
+ resArr[oddIndex] = nums[i];
+ oddIndex += 2;
+ }
+ }
+ return resArr;
+};
+```
+
+> 方法三:
+
+```typescript
+function sortArrayByParityII(nums: number[]): number[] {
+ const length: number = nums.length;
+ let oddIndex: number = 1;
+ for (let evenIndex = 0; evenIndex < length; evenIndex += 2) {
+ if (nums[evenIndex] % 2 === 1) {
+ // 在偶数位遇到了奇数
+ while (oddIndex < length && nums[oddIndex] % 2 === 1) {
+ oddIndex += 2;
+ }
+ // 在奇数位遇到了偶数,交换
+ let temp = nums[evenIndex];
+ nums[evenIndex] = nums[oddIndex];
+ nums[oddIndex] = temp;
+ }
+ }
+ return nums;
+};
+```
+
+
+
-----------------------
From 3e885d3aff0b2c826fe522d7eb5d2cd039b62f13 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 30 May 2022 11:25:52 +0800
Subject: [PATCH 320/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200108.=E5=B0=86?=
=?UTF-8?q?=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA?=
=?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...11\346\220\234\347\264\242\346\240\221.md" | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
index 6ee3947bec..493118a6aa 100644
--- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
+++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
@@ -448,5 +448,27 @@ struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
}
```
+## Scala
+
+递归:
+
+```scala
+object Solution {
+ def sortedArrayToBST(nums: Array[Int]): TreeNode = {
+ def buildTree(left: Int, right: Int): TreeNode = {
+ if (left > right) return null // 当left大于right的时候,返回空
+ // 最中间的节点是当前节点
+ var mid = left + (right - left) / 2
+ var curNode = new TreeNode(nums(mid))
+ curNode.left = buildTree(left, mid - 1)
+ curNode.right = buildTree(mid + 1, right)
+ curNode
+ }
+ buildTree(0, nums.size - 1)
+ }
+}
+```
+
+
-----------------------
From d4b5abfe9bc2f32f99c92a1d3794b58d78c69141 Mon Sep 17 00:00:00 2001
From: Ezralin <10881430+ezralin@user.noreply.gitee.com>
Date: Tue, 31 May 2022 08:41:53 +0800
Subject: [PATCH 321/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A00704.=E4=BA=8C?=
=?UTF-8?q?=E5=88=86=E6=9F=A5=E6=89=BE.md=20Kotlin=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...14\345\210\206\346\237\245\346\211\276.md" | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
index 5562513089..3dcbd17519 100644
--- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
+++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
@@ -611,6 +611,36 @@ public class Solution{
}
```
+**Kotlin:**
+```kotlin
+class Solution {
+ fun search(nums: IntArray, target: Int): Int {
+ // leftBorder
+ var left:Int = 0
+ // rightBorder
+ var right:Int = nums.size - 1
+ // 使用左闭右闭区间
+ while (left <= right) {
+ var middle:Int = left + (right - left)/2
+ // taget 在左边
+ if (nums[middle] > target) {
+ right = middle - 1
+ }
+ else {
+ // target 在右边
+ if (nums[middle] < target) {
+ left = middle + 1
+ }
+ // 找到了,返回
+ else return middle
+ }
+ }
+ // 没找到,返回
+ return -1
+ }
+}
+```
+
-----------------------
From ab5b82969f30b767f2815883272f4b3a49ad0c82 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Tue, 31 May 2022 15:56:00 +0800
Subject: [PATCH 322/551] =?UTF-8?q?=E6=96=B0=E5=A2=9E(0150.=E9=80=86?=
=?UTF-8?q?=E6=B3=A2=E5=85=B0=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC?=
=?UTF-8?q?.md)=EF=BC=9Aphp=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...76\345\274\217\346\261\202\345\200\274.md" | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
index fd3d69aa33..7e142ba3c6 100644
--- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
+++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
@@ -326,5 +326,29 @@ func evalRPN(_ tokens: [String]) -> Int {
}
```
+PHP:
+```php
+class Solution {
+ function evalRPN($tokens) {
+ $st = new SplStack();
+ for($i = 0;$ipush($tokens[$i]);
+ }else{
+ // 是符号进行运算
+ $num1 = $st->pop();
+ $num2 = $st->pop();
+ if ($tokens[$i] == "+") $st->push($num2 + $num1);
+ if ($tokens[$i] == "-") $st->push($num2 - $num1);
+ if ($tokens[$i] == "*") $st->push($num2 * $num1);
+ // 注意处理小数部分
+ if ($tokens[$i] == "/") $st->push(intval($num2 / $num1));
+ }
+ }
+ return $st->pop();
+ }
+}
+```
-----------------------
From ca18b7d1f5c31a91c262ec4a1cf0b93cc00b3cf1 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 31 May 2022 22:13:58 +0800
Subject: [PATCH 323/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200538.=E6=8A=8A?=
=?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E8=BD=AC=E6=8D=A2?=
=?UTF-8?q?=E4=B8=BA=E7=B4=AF=E5=8A=A0=E6=A0=91.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...272\347\264\257\345\212\240\346\240\221.md" | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md"
index 37eb7d0f2c..136377843d 100644
--- "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md"
+++ "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md"
@@ -352,6 +352,24 @@ function convertBST(root: TreeNode | null): TreeNode | null {
};
```
+## Scala
+
+```scala
+object Solution {
+ def convertBST(root: TreeNode): TreeNode = {
+ var sum = 0
+ def convert(node: TreeNode): Unit = {
+ if (node == null) return
+ convert(node.right)
+ sum += node.value
+ node.value = sum
+ convert(node.left)
+ }
+ convert(root)
+ root
+ }
+}
+```
-----------------------
From 752cfda89351cc2d54897a6309a181f197cc6c50 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=A1=9C=E5=B0=8F=E8=B7=AF=E4=B8=83=E8=91=89?=
<20304773@qq.com>
Date: Tue, 31 May 2022 23:46:41 +0800
Subject: [PATCH 324/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880027.?=
=?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=85=83=E7=B4=A0.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0=20C#=20=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\273\351\231\244\345\205\203\347\264\240.md" | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
index 3a93ac8820..5e7742ee26 100644
--- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
+++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
@@ -329,5 +329,20 @@ int removeElement(int* nums, int numsSize, int val){
}
```
+C#:
+```csharp
+public class Solution {
+ public int RemoveElement(int[] nums, int val) {
+ int slow = 0;
+ for (int fast = 0; fast < nums.Length; fast++) {
+ if (val != nums[fast]) {
+ nums[slow++] = nums[fast];
+ }
+ }
+ return slow;
+ }
+}
+```
+
-----------------------
From 0281b82d48a4f21429d35a89270843357f801c15 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Mon, 16 May 2022 15:26:21 +0800
Subject: [PATCH 325/551] =?UTF-8?q?=E5=A2=9E=E5=8A=A0php=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...54\345\255\227\347\254\246\344\270\262.md" | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md"
index fec83e1d5a..8781ffb4f6 100644
--- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md"
@@ -290,6 +290,25 @@ func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) {
}
```
+### PHP
+
+```php
+function reverseLeftWords($s, $n) {
+ $this->reverse($s,0,$n-1); //反转区间为前n的子串
+ $this->reverse($s,$n,strlen($s)-1); //反转区间为n到末尾的子串
+ $this->reverse($s,0,strlen($s)-1); //反转整个字符串
+ return $s;
+}
+
+// 按指定进行翻转 【array、string都可】
+function reverse(&$s, $start, $end) {
+ for ($i = $start, $j = $end; $i < $j; $i++, $j--) {
+ $tmp = $s[$i];
+ $s[$i] = $s[$j];
+ $s[$j] = $tmp;
+ }
+}
+```
From f51d72b71baf6698d2078dd9b2ce5432aa7cfe41 Mon Sep 17 00:00:00 2001
From: SevenMonths
Date: Wed, 1 Jun 2022 18:45:16 +0800
Subject: [PATCH 326/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0239.=E6=BB=91?=
=?UTF-8?q?=E5=8A=A8=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md)?=
=?UTF-8?q?=EF=BC=9APHP=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...43\346\234\200\345\244\247\345\200\274.md" | 77 +++++++++++++++++++
1 file changed, 77 insertions(+)
diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md"
index f269450f3b..227ab71cca 100644
--- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md"
+++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md"
@@ -631,5 +631,82 @@ func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] {
}
```
+PHP:
+```php
+class Solution {
+ /**
+ * @param Integer[] $nums
+ * @param Integer $k
+ * @return Integer[]
+ */
+ function maxSlidingWindow($nums, $k) {
+ $myQueue = new MyQueue();
+ // 先将前k的元素放进队列
+ for ($i = 0; $i < $k; $i++) {
+ $myQueue->push($nums[$i]);
+ }
+
+ $result = [];
+ $result[] = $myQueue->max(); // result 记录前k的元素的最大值
+
+ for ($i = $k; $i < count($nums); $i++) {
+ $myQueue->pop($nums[$i - $k]); // 滑动窗口移除最前面元素
+ $myQueue->push($nums[$i]); // 滑动窗口前加入最后面的元素
+ $result[]= $myQueue->max(); // 记录对应的最大值
+ }
+ return $result;
+ }
+
+}
+
+// 单调对列构建
+class MyQueue{
+ private $queue;
+
+ public function __construct(){
+ $this->queue = new SplQueue(); //底层是双向链表实现。
+ }
+
+ public function pop($v){
+ // 判断当前对列是否为空
+ // 比较当前要弹出的数值是否等于队列出口元素的数值,如果相等则弹出。
+ // bottom 从链表前端查看元素, dequeue 从双向链表的开头移动一个节点
+ if(!$this->queue->isEmpty() && $v == $this->queue->bottom()){
+ $this->queue->dequeue(); //弹出队列
+ }
+ }
+
+ public function push($v){
+ // 判断当前对列是否为空
+ // 如果push的数值大于入口元素的数值,那么就将队列后端的数值弹出,直到push的数值小于等于队列入口元素的数值为止。
+ // 这样就保持了队列里的数值是单调从大到小的了。
+ while (!$this->queue->isEmpty() && $v > $this->queue->top()) {
+ $this->queue->pop(); // pop从链表末尾弹出一个元素,
+ }
+ $this->queue->enqueue($v);
+ }
+
+ // 查询当前队列里的最大值 直接返回队首
+ public function max(){
+ // bottom 从链表前端查看元素, top从链表末尾查看元素
+ return $this->queue->bottom();
+ }
+
+ // 辅助理解: 打印队列元素
+ public function println(){
+ // "迭代器移动到链表头部": 可理解为从头遍历链表元素做准备。
+ // 【PHP中没有指针概念,所以就没说指针。从数据结构上理解,就是把指针指向链表头部】
+ $this->queue->rewind();
+
+ echo "Println: ";
+ while($this->queue->valid()){
+ echo $this->queue->current()," -> ";
+ $this->queue->next();
+ }
+ echo "\n";
+ }
+}
+```
+
-----------------------
From 90075121ecd61558cb50d80668298fb4263c6cc0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=A1=9C=E5=B0=8F=E8=B7=AF=E4=B8=83=E8=91=89?=
<20304773@qq.com>
Date: Wed, 1 Jun 2022 19:47:53 +0800
Subject: [PATCH 327/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880977.?=
=?UTF-8?q?=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9?=
=?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0=20C#=20=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\347\232\204\345\271\263\346\226\271.md" | 20 ++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
index 0e79a3d654..f1b0e4ec4c 100644
--- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
+++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
@@ -394,6 +394,24 @@ object Solution {
}
```
-
+C#:
+```csharp
+public class Solution {
+ public int[] SortedSquares(int[] nums) {
+ int k = nums.Length - 1;
+ int[] result = new int[nums.Length];
+ for (int i = 0, j = nums.Length - 1;i <= j;){
+ if (nums[i] * nums[i] < nums[j] * nums[j]) {
+ result[k--] = nums[j] * nums[j];
+ j--;
+ } else {
+ result[k--] = nums[i] * nums[i];
+ i++;
+ }
+ }
+ return result;
+ }
+}
+```
-----------------------
From 0bdb37d7fa0138c6870b882a8577146ad43a01ca Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 2 Jun 2022 10:05:46 +0800
Subject: [PATCH 328/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200077.=E7=BB=84?=
=?UTF-8?q?=E5=90=88.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
"problems/0077.\347\273\204\345\220\210.md" | 28 +++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md"
index 9e0398aba1..f280c1764e 100644
--- "a/problems/0077.\347\273\204\345\220\210.md"
+++ "b/problems/0077.\347\273\204\345\220\210.md"
@@ -673,5 +673,33 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] {
}
```
+### Scala
+
+```scala
+object Solution {
+ import scala.collection.mutable // 导包
+ def combine(n: Int, k: Int): List[List[Int]] = {
+ var result = mutable.ListBuffer[List[Int]]() // 存放结果集
+ var path = mutable.ListBuffer[Int]() //存放符合条件的结果
+
+ def backtracking(n: Int, k: Int, startIndex: Int): Unit = {
+ if (path.size == k) {
+ // 如果path的size == k就达到题目要求,添加到结果集,并返回
+ result.append(path.toList)
+ return
+ }
+ for (i <- startIndex to n) { // 遍历从startIndex到n
+ path.append(i) // 先把数字添加进去
+ backtracking(n, k, i + 1) // 进行下一步回溯
+ path = path.take(path.size - 1) // 回溯完再删除掉刚刚添加的数字
+ }
+ }
+
+ backtracking(n, k, 1) // 执行回溯
+ result.toList // 最终返回result的List形式,return关键字可以省略
+ }
+}
+```
+
-----------------------
From e734d0a12265ac455691d5cd15efe2c7fbfb8491 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 2 Jun 2022 10:18:52 +0800
Subject: [PATCH 329/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200077.=E7=BB=84?=
=?UTF-8?q?=E5=90=88.md=20=E5=89=AA=E6=9E=9D=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
"problems/0077.\347\273\204\345\220\210.md" | 30 +++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md"
index f280c1764e..583822218b 100644
--- "a/problems/0077.\347\273\204\345\220\210.md"
+++ "b/problems/0077.\347\273\204\345\220\210.md"
@@ -675,6 +675,7 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] {
### Scala
+暴力:
```scala
object Solution {
import scala.collection.mutable // 导包
@@ -701,5 +702,34 @@ object Solution {
}
```
+剪枝:
+
+```scala
+object Solution {
+ import scala.collection.mutable // 导包
+ def combine(n: Int, k: Int): List[List[Int]] = {
+ var result = mutable.ListBuffer[List[Int]]() // 存放结果集
+ var path = mutable.ListBuffer[Int]() //存放符合条件的结果
+
+ def backtracking(n: Int, k: Int, startIndex: Int): Unit = {
+ if (path.size == k) {
+ // 如果path的size == k就达到题目要求,添加到结果集,并返回
+ result.append(path.toList)
+ return
+ }
+ // 剪枝优化
+ for (i <- startIndex to (n - (k - path.size) + 1)) {
+ path.append(i) // 先把数字添加进去
+ backtracking(n, k, i + 1) // 进行下一步回溯
+ path = path.take(path.size - 1) // 回溯完再删除掉刚刚添加的数字
+ }
+ }
+
+ backtracking(n, k, 1) // 执行回溯
+ result.toList // 最终返回result的List形式,return关键字可以省略
+ }
+}
+```
+
-----------------------
From 5ac414f466ac9d80511aa5c2c6659df8fa5419a0 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 2 Jun 2022 10:22:56 +0800
Subject: [PATCH 330/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200077.=E7=BB=84?=
=?UTF-8?q?=E5=90=88=E4=BC=98=E5=8C=96.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\345\220\210\344\274\230\345\214\226.md" | 29 +++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md"
index 94608ec18f..2c1a821b8e 100644
--- "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md"
+++ "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md"
@@ -346,5 +346,34 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] {
}
```
+Scala:
+
+```scala
+object Solution {
+ import scala.collection.mutable // 导包
+ def combine(n: Int, k: Int): List[List[Int]] = {
+ var result = mutable.ListBuffer[List[Int]]() // 存放结果集
+ var path = mutable.ListBuffer[Int]() //存放符合条件的结果
+
+ def backtracking(n: Int, k: Int, startIndex: Int): Unit = {
+ if (path.size == k) {
+ // 如果path的size == k就达到题目要求,添加到结果集,并返回
+ result.append(path.toList)
+ return
+ }
+ // 剪枝优化
+ for (i <- startIndex to (n - (k - path.size) + 1)) {
+ path.append(i) // 先把数字添加进去
+ backtracking(n, k, i + 1) // 进行下一步回溯
+ path = path.take(path.size - 1) // 回溯完再删除掉刚刚添加的数字
+ }
+ }
+
+ backtracking(n, k, 1) // 执行回溯
+ result.toList // 最终返回result的List形式,return关键字可以省略
+ }
+}
+```
+
-----------------------
From d0814a723c8e293e2fa7eee1503114584269faa7 Mon Sep 17 00:00:00 2001
From: Mrchenuo
Date: Thu, 2 Jun 2022 10:57:47 +0800
Subject: [PATCH 331/551] =?UTF-8?q?Update=200707.=E8=AE=BE=E8=AE=A1?=
=?UTF-8?q?=E9=93=BE=E8=A1=A8.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
修复CPP代码的addAtIndex函数中,当index<0时,while会死循环的问题。
---
.../0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" | 4 ++++
1 file changed, 4 insertions(+)
diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
index dcdb53f44e..a788939b5b 100644
--- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
+++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
@@ -104,10 +104,14 @@ public:
// 在第index个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。
// 如果index 等于链表的长度,则说明是新插入的节点为链表的尾结点
// 如果index大于链表的长度,则返回空
+ // 如果index小于0,则置为0,作为链表的新头节点。
void addAtIndex(int index, int val) {
if (index > _size) {
return;
}
+ else if (index < 0) {
+ index = 0;
+ }
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur = _dummyHead;
while(index--) {
From 7dd463e085c8b7ef66e75009b3a5cdec0d30a17c Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 2 Jun 2022 12:13:52 +0800
Subject: [PATCH 332/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200216.=E7=BB=84?=
=?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CIII.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...345\220\210\346\200\273\345\222\214III.md" | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
index 32b1347e56..9e7415bed1 100644
--- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
+++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
@@ -511,5 +511,35 @@ func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] {
}
```
+## Scala
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def combinationSum3(k: Int, n: Int): List[List[Int]] = {
+ var result = mutable.ListBuffer[List[Int]]()
+ var path = mutable.ListBuffer[Int]()
+
+ def backtracking(k: Int, n: Int, sum: Int, startIndex: Int): Unit = {
+ if (sum > n) return // 剪枝,如果sum>目标和,就返回
+ if (sum == n && path.size == k) {
+ result.append(path.toList)
+ return
+ }
+ // 剪枝
+ for (i <- startIndex to (9 - (k - path.size) + 1)) {
+ path.append(i)
+ backtracking(k, n, sum + i, i + 1)
+ path = path.take(path.size - 1)
+ }
+ }
+
+ backtracking(k, n, 0, 1) // 调用递归方法
+ result.toList // 最终返回结果集的List形式
+ }
+}
+```
+
+
-----------------------
From 2f0a1830377fe38fed12aba3bfd38424b9c67108 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 2 Jun 2022 16:28:28 +0800
Subject: [PATCH 333/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200017.=E7=94=B5?=
=?UTF-8?q?=E8=AF=9D=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84?=
=?UTF-8?q?=E5=90=88.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...27\346\257\215\347\273\204\345\220\210.md" | 31 +++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md"
index 9413656515..f638349db2 100644
--- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md"
+++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md"
@@ -557,6 +557,37 @@ func letterCombinations(_ digits: String) -> [String] {
}
```
+## Scala:
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def letterCombinations(digits: String): List[String] = {
+ var result = mutable.ListBuffer[String]()
+ if(digits == "") return result.toList // 如果参数为空,返回空结果集的List形式
+ var path = mutable.ListBuffer[Char]()
+ // 数字和字符的映射关系
+ val map = Array[String]("", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz")
+
+ def backtracking(index: Int): Unit = {
+ if (index == digits.size) {
+ result.append(path.mkString) // mkString语法:将数组类型直接转换为字符串
+ return
+ }
+ var digit = digits(index) - '0' // 这里使用toInt会报错!必须 -'0'
+ for (i <- 0 until map(digit).size) {
+ path.append(map(digit)(i))
+ backtracking(index + 1)
+ path = path.take(path.size - 1)
+ }
+ }
+
+ backtracking(0)
+ result.toList
+ }
+}
+```
+
-----------------------
From 3d83e14392b6f4c16ea3ea4bfaf175878dce27cf Mon Sep 17 00:00:00 2001
From: wenmimi
Date: Thu, 2 Jun 2022 18:23:14 +0800
Subject: [PATCH 334/551] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=83=8C=E5=8C=85?=
=?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1=20GO?=
=?UTF-8?q?=20=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81=20Bug?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...237\272\347\241\20001\350\203\214\345\214\205-1.md" | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
index a40a92aba6..e24824b9bb 100644
--- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
+++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
@@ -356,9 +356,13 @@ func test_2_wei_bag_problem1(weight, value []int, bagweight int) int {
// 递推公式
for i := 1; i < len(weight); i++ {
//正序,也可以倒序
- for j := weight[i];j<= bagweight ; j++ {
- dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i])
- }
+ for j := 0; j <= bagweight; j++ {
+ if j < weight[i] {
+ dp[i][j] = dp[i-1][j]
+ } else {
+ dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i])
+ }
+ }
}
return dp[len(weight)-1][bagweight]
}
From d2e9927e97519621ba04f1e3d82684ed3cfafc5f Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 3 Jun 2022 09:46:09 +0800
Subject: [PATCH 335/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200039.=E7=BB=84?=
=?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8C.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\345\220\210\346\200\273\345\222\214.md" | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
index e10a827f04..ca6312f5b3 100644
--- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
+++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
@@ -502,5 +502,35 @@ func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
}
```
+## Scala
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def combinationSum(candidates: Array[Int], target: Int): List[List[Int]] = {
+ var result = mutable.ListBuffer[List[Int]]()
+ var path = mutable.ListBuffer[Int]()
+
+ def backtracking(sum: Int, index: Int): Unit = {
+ if (sum == target) {
+ result.append(path.toList) // 如果正好等于target,就添加到结果集
+ return
+ }
+ // 应该是从当前索引开始的,而不是从0
+ // 剪枝优化:添加循环守卫,当sum + c(i) <= target的时候才循环,才可以进入下一次递归
+ for (i <- index until candidates.size if sum + candidates(i) <= target) {
+ path.append(candidates(i))
+ backtracking(sum + candidates(i), i)
+ path = path.take(path.size - 1)
+ }
+ }
+
+ backtracking(0, 0)
+ result.toList
+ }
+}
+```
+
+
-----------------------
From 301a703ece704650eeb0bfd1eb8ef2079ee274b5 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 3 Jun 2022 15:24:28 +0800
Subject: [PATCH 336/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200040.=E7=BB=84?=
=?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CII.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\345\220\210\346\200\273\345\222\214II.md" | 32 +++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md"
index 34ac64e68d..6b635f8c02 100644
--- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md"
+++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md"
@@ -693,5 +693,37 @@ func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] {
}
```
+
+## Scala
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def combinationSum2(candidates: Array[Int], target: Int): List[List[Int]] = {
+ var res = mutable.ListBuffer[List[Int]]()
+ var path = mutable.ListBuffer[Int]()
+ var candidate = candidates.sorted
+
+ def backtracking(sum: Int, startIndex: Int): Unit = {
+ if (sum == target) {
+ res.append(path.toList)
+ return
+ }
+
+ for (i <- startIndex until candidate.size if sum + candidate(i) <= target) {
+ if (!(i > startIndex && candidate(i) == candidate(i - 1))) {
+ path.append(candidate(i))
+ backtracking(sum + candidate(i), i + 1)
+ path = path.take(path.size - 1)
+ }
+ }
+ }
+
+ backtracking(0, 0)
+ res.toList
+ }
+}
+```
+
-----------------------
From 3605046879d02c5f6b931f7440935ef4142ef121 Mon Sep 17 00:00:00 2001
From: yalexu <61576631+alexgy1@users.noreply.github.com>
Date: Fri, 3 Jun 2022 18:24:51 +0800
Subject: [PATCH 337/551] Add JS correct reverse method
---
...\215\350\275\254\345\255\227\347\254\246\344\270\262.md" | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md"
index 58bada05b9..192a397c96 100644
--- "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md"
@@ -190,13 +190,13 @@ javaScript:
* @return {void} Do not return anything, modify s in-place instead.
*/
var reverseString = function(s) {
- return s.reverse();
+ //Do not return anything, modify s in-place instead.
+ reverse(s)
};
-var reverseString = function(s) {
+var reverse = function(s) {
let l = -1, r = s.length;
while(++l < --r) [s[l], s[r]] = [s[r], s[l]];
- return s;
};
```
From a2a2cf60272094c3c9d962723132502d93db097a Mon Sep 17 00:00:00 2001
From: lizhendong128
Date: Fri, 3 Jun 2022 21:42:36 +0800
Subject: [PATCH 338/551] =?UTF-8?q?=E4=BF=AE=E6=94=B90106=E4=BB=8E?=
=?UTF-8?q?=E4=B8=AD=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86?=
=?UTF-8?q?=E5=BA=8F=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91?=
=?UTF-8?q?=20Java=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
将0106和0105的Java版本进行了修改,采用了map来存储位置信息,加快定位;并且代码更容易看懂
---
...40\344\272\214\345\217\211\346\240\221.md" | 82 +++++++++----------
1 file changed, 37 insertions(+), 45 deletions(-)
diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md"
index 188ad3cb44..878c357269 100644
--- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md"
@@ -584,35 +584,29 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。
```java
class Solution {
+ Map map; // 方便根据数值查找位置
public TreeNode buildTree(int[] inorder, int[] postorder) {
- return buildTree1(inorder, 0, inorder.length, postorder, 0, postorder.length);
+ map = new HashMap<>();
+ for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置
+ map.put(inorder[i], i);
+ }
+
+ return findNode(inorder, 0, inorder.length, postorder,0, postorder.length); // 前闭后开
}
- public TreeNode buildTree1(int[] inorder, int inLeft, int inRight,
- int[] postorder, int postLeft, int postRight) {
- // 没有元素了
- if (inRight - inLeft < 1) {
+
+ public TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
+ // 参数里的范围都是前闭后开
+ if (inBegin >= inEnd || postBegin >= postEnd) { // 不满足左闭右开,说明没有元素,返回空树
return null;
}
- // 只有一个元素了
- if (inRight - inLeft == 1) {
- return new TreeNode(inorder[inLeft]);
- }
- // 后序数组postorder里最后一个即为根结点
- int rootVal = postorder[postRight - 1];
- TreeNode root = new TreeNode(rootVal);
- int rootIndex = 0;
- // 根据根结点的值找到该值在中序数组inorder里的位置
- for (int i = inLeft; i < inRight; i++) {
- if (inorder[i] == rootVal) {
- rootIndex = i;
- break;
- }
- }
- // 根据rootIndex划分左右子树
- root.left = buildTree1(inorder, inLeft, rootIndex,
- postorder, postLeft, postLeft + (rootIndex - inLeft));
- root.right = buildTree1(inorder, rootIndex + 1, inRight,
- postorder, postLeft + (rootIndex - inLeft), postRight - 1);
+ int rootIndex = map.get(postorder[postEnd - 1]); // 找到后序遍历的最后一个元素在中序遍历中的位置
+ TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点
+ int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定后序数列的个数
+ root.left = findNode(inorder, inBegin, rootIndex,
+ postorder, postBegin, postBegin + lenOfLeft);
+ root.right = findNode(inorder, rootIndex + 1, inEnd,
+ postorder, postBegin + lenOfLeft, postEnd - 1);
+
return root;
}
}
@@ -622,31 +616,29 @@ class Solution {
```java
class Solution {
+ Map map;
public TreeNode buildTree(int[] preorder, int[] inorder) {
- return helper(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
+ map = new HashMap<>();
+ for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置
+ map.put(inorder[i], i);
+ }
+
+ return findNode(preorder, 0, preorder.length, inorder, 0, inorder.length); // 前闭后开
}
- public TreeNode helper(int[] preorder, int preLeft, int preRight,
- int[] inorder, int inLeft, int inRight) {
- // 递归终止条件
- if (inLeft > inRight || preLeft > preRight) return null;
-
- // val 为前序遍历第一个的值,也即是根节点的值
- // idx 为根据根节点的值来找中序遍历的下标
- int idx = inLeft, val = preorder[preLeft];
- TreeNode root = new TreeNode(val);
- for (int i = inLeft; i <= inRight; i++) {
- if (inorder[i] == val) {
- idx = i;
- break;
- }
+ public TreeNode findNode(int[] preorder, int preBegin, int preEnd, int[] inorder, int inBegin, int inEnd) {
+ // 参数里的范围都是前闭后开
+ if (preBegin >= preEnd || inBegin >= inEnd) { // 不满足左闭右开,说明没有元素,返回空树
+ return null;
}
+ int rootIndex = map.get(preorder[preBegin]); // 找到前序遍历的第一个元素在中序遍历中的位置
+ TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点
+ int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定前序数列的个数
+ root.left = findNode(preorder, preBegin + 1, preBegin + lenOfLeft + 1,
+ inorder, inBegin, rootIndex);
+ root.right = findNode(preorder, preBegin + lenOfLeft + 1, preEnd,
+ inorder, rootIndex + 1, inEnd);
- // 根据 idx 来递归找左右子树
- root.left = helper(preorder, preLeft + 1, preLeft + (idx - inLeft),
- inorder, inLeft, idx - 1);
- root.right = helper(preorder, preLeft + (idx - inLeft) + 1, preRight,
- inorder, idx + 1, inRight);
return root;
}
}
From af1b3e3e32676d6037645c0553b4fc2bb2cf149d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=A1=9C=E5=B0=8F=E8=B7=AF=E4=B8=83=E8=91=89?=
<20304773@qq.com>
Date: Sat, 4 Jun 2022 00:22:14 +0800
Subject: [PATCH 339/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880209.?=
=?UTF-8?q?=E9=95=BF=E5=BA=A6=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0?=
=?UTF-8?q?=E7=BB=84.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0=20C#=20?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\345\255\220\346\225\260\347\273\204.md" | 23 ++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
index fbef7692cd..ceb06ce9a6 100644
--- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
+++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
@@ -448,6 +448,27 @@ object Solution {
}
}
```
-
+C#:
+```csharp
+public class Solution {
+ public int MinSubArrayLen(int s, int[] nums) {
+ int n = nums.Length;
+ int ans = int.MaxValue;
+ int start = 0, end = 0;
+ int sum = 0;
+ while (end < n) {
+ sum += nums[end];
+ while (sum >= s)
+ {
+ ans = Math.Min(ans, end - start + 1);
+ sum -= nums[start];
+ start++;
+ }
+ end++;
+ }
+ return ans == int.MaxValue ? 0 : ans;
+ }
+}
+```
-----------------------
From cccb4973aeccde06a00e5c693510c824ae98c99b Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 4 Jun 2022 10:44:25 +0800
Subject: [PATCH 340/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880035.?=
=?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE.md?=
=?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...22\345\205\245\344\275\215\347\275\256.md" | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
index 6c04e7dea7..3de7c4f72f 100644
--- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
+++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
@@ -283,6 +283,28 @@ var searchInsert = function (nums, target) {
};
```
+### TypeScript
+
+```typescript
+// 第一种二分法
+function searchInsert(nums: number[], target: number): number {
+ const length: number = nums.length;
+ let left: number = 0,
+ right: number = length - 1;
+ while (left <= right) {
+ const mid: number = Math.floor((left + right) / 2);
+ if (nums[mid] < target) {
+ left = mid + 1;
+ } else if (nums[mid] === target) {
+ return mid;
+ } else {
+ right = mid - 1;
+ }
+ }
+ return right + 1;
+};
+```
+
### Swift
```swift
From 6cd58dfef0a22f568bb5c6b03f4026520b7283de Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 4 Jun 2022 13:13:33 +0800
Subject: [PATCH 341/551] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=880024.?=
=?UTF-8?q?=E4=B8=A4=E4=B8=A4=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD?=
=?UTF-8?q?=E7=9A=84=E8=8A=82=E7=82=B9.md=EF=BC=89=EF=BC=9A=E4=BC=98?=
=?UTF-8?q?=E5=8C=96typescript=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?=
=?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=BC=BA=E6=98=93=E8=AF=BB=E6=80=A7?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...55\347\232\204\350\212\202\347\202\271.md" | 27 +++++++++----------
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md"
index 2289c22912..0d848e4d09 100644
--- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md"
+++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md"
@@ -254,20 +254,19 @@ TypeScript:
```typescript
function swapPairs(head: ListNode | null): ListNode | null {
- 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;
-}
+ const dummyNode: ListNode = new ListNode(0, head);
+ let curNode: ListNode | null = dummyNode;
+ while (curNode && curNode.next && curNode.next.next) {
+ let firstNode: ListNode = curNode.next,
+ secNode: ListNode = curNode.next.next,
+ thirdNode: ListNode | null = curNode.next.next.next;
+ curNode.next = secNode;
+ secNode.next = firstNode;
+ firstNode.next = thirdNode;
+ curNode = firstNode;
+ }
+ return dummyNode.next;
+};
```
Kotlin:
From 7d5856d0b82baad2266555f33bc1eae43538fd70 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 4 Jun 2022 15:56:22 +0800
Subject: [PATCH 342/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880234.?=
=?UTF-8?q?=E5=9B=9E=E6=96=87=E9=93=BE=E8=A1=A8.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...36\346\226\207\351\223\276\350\241\250.md" | 59 ++++++++++++++++++-
1 file changed, 58 insertions(+), 1 deletion(-)
diff --git "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md"
index db910d4ee0..b19a2408ac 100644
--- "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md"
+++ "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md"
@@ -273,7 +273,7 @@ class Solution:
return pre
```
-## Go
+### Go
```go
@@ -319,6 +319,63 @@ var isPalindrome = function(head) {
};
```
+### TypeScript
+
+> 数组模拟
+
+```typescript
+function isPalindrome(head: ListNode | null): boolean {
+ const helperArr: number[] = [];
+ let curNode: ListNode | null = head;
+ while (curNode !== null) {
+ helperArr.push(curNode.val);
+ curNode = curNode.next;
+ }
+ let left: number = 0,
+ right: number = helperArr.length - 1;
+ while (left < right) {
+ if (helperArr[left++] !== helperArr[right--]) return false;
+ }
+ return true;
+};
+```
+
+> 反转后半部分链表
+
+```typescript
+function isPalindrome(head: ListNode | null): boolean {
+ if (head === null || head.next === null) return true;
+ let fastNode: ListNode | null = head,
+ slowNode: ListNode = head,
+ preNode: ListNode = head;
+ while (fastNode !== null && fastNode.next !== null) {
+ preNode = slowNode;
+ slowNode = slowNode.next!;
+ fastNode = fastNode.next.next;
+ }
+ preNode.next = null;
+ let cur1: ListNode | null = head;
+ let cur2: ListNode | null = reverseList(slowNode);
+ while (cur1 !== null) {
+ if (cur1.val !== cur2!.val) return false;
+ cur1 = cur1.next;
+ cur2 = cur2!.next;
+ }
+ return true;
+};
+function reverseList(head: ListNode | null): ListNode | null {
+ let curNode: ListNode | null = head,
+ preNode: ListNode | null = null;
+ while (curNode !== null) {
+ let tempNode: ListNode | null = curNode.next;
+ curNode.next = preNode;
+ preNode = curNode;
+ curNode = tempNode;
+ }
+ return preNode;
+}
+```
+
-----------------------
From bb484a70ac2c8d2fcbbba08a1897bc053c9354d1 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 4 Jun 2022 21:01:59 +0800
Subject: [PATCH 343/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880143.?=
=?UTF-8?q?=E9=87=8D=E6=8E=92=E9=93=BE=E8=A1=A8.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...15\346\216\222\351\223\276\350\241\250.md" | 76 +++++++++++++++++++
1 file changed, 76 insertions(+)
diff --git "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md"
index 790bcb482a..c60fc0f968 100644
--- "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md"
+++ "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md"
@@ -6,6 +6,8 @@
# 143.重排链表
+[力扣题目链接](https://leetcode.cn/problems/reorder-list/submissions/)
+

## 思路
@@ -465,7 +467,81 @@ var reorderList = function(head, s = [], tmp) {
}
```
+### TypeScript
+
+> 辅助数组法:
+
+```typescript
+function reorderList(head: ListNode | null): void {
+ if (head === null) return;
+ const helperArr: ListNode[] = [];
+ let curNode: ListNode | null = head;
+ while (curNode !== null) {
+ helperArr.push(curNode);
+ curNode = curNode.next;
+ }
+ let node: ListNode = head;
+ let left: number = 1,
+ right: number = helperArr.length - 1;
+ let count: number = 0;
+ while (left <= right) {
+ if (count % 2 === 0) {
+ node.next = helperArr[right--];
+ } else {
+ node.next = helperArr[left++];
+ }
+ count++;
+ node = node.next;
+ }
+ node.next = null;
+};
+```
+
+> 分割链表法:
+
+```typescript
+function reorderList(head: ListNode | null): void {
+ if (head === null || head.next === null) return;
+ let fastNode: ListNode = head,
+ slowNode: ListNode = head;
+ while (fastNode.next !== null && fastNode.next.next !== null) {
+ slowNode = slowNode.next!;
+ fastNode = fastNode.next.next;
+ }
+ let head1: ListNode | null = head;
+ // 反转后半部分链表
+ let head2: ListNode | null = reverseList(slowNode.next);
+ // 分割链表
+ slowNode.next = null;
+ /**
+ 直接在head1链表上进行插入
+ head1 链表长度一定大于或等于head2,
+ 因此在下面的循环中,只要head2不为null, head1 一定不为null
+ */
+ while (head2 !== null) {
+ const tempNode1: ListNode | null = head1!.next,
+ tempNode2: ListNode | null = head2.next;
+ head1!.next = head2;
+ head2.next = tempNode1;
+ head1 = tempNode1;
+ head2 = tempNode2;
+ }
+};
+function reverseList(head: ListNode | null): ListNode | null {
+ let curNode: ListNode | null = head,
+ preNode: ListNode | null = null;
+ while (curNode !== null) {
+ const tempNode: ListNode | null = curNode.next;
+ curNode.next = preNode;
+ preNode = curNode;
+ curNode = tempNode;
+ }
+ return preNode;
+}
+```
+
### C
+
方法三:反转链表
```c
//翻转链表
From c0a73e2544202baa3839852777e053e82937004c Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 4 Jun 2022 21:35:24 +0800
Subject: [PATCH 344/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200131.=E5=88=86?=
=?UTF-8?q?=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...62\345\233\236\346\226\207\344\270\262.md" | 45 +++++++++++++++++++
1 file changed, 45 insertions(+)
diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
index 7a702898dc..f361d1ef87 100644
--- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
+++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
@@ -676,5 +676,50 @@ impl Solution {
}
}
```
+
+
+## Scala
+
+```scala
+object Solution {
+
+ import scala.collection.mutable
+
+ def partition(s: String): List[List[String]] = {
+ var result = mutable.ListBuffer[List[String]]()
+ var path = mutable.ListBuffer[String]()
+
+ // 判断字符串是否回文
+ def isPalindrome(start: Int, end: Int): Boolean = {
+ var (left, right) = (start, end)
+ while (left < right) {
+ if (s(left) != s(right)) return false
+ left += 1
+ right -= 1
+ }
+ true
+ }
+
+ // 回溯算法
+ def backtracking(startIndex: Int): Unit = {
+ if (startIndex >= s.size) {
+ result.append(path.toList)
+ return
+ }
+ // 添加循环守卫,如果当前分割是回文子串则进入回溯
+ for (i <- startIndex until s.size if isPalindrome(startIndex, i)) {
+ path.append(s.substring(startIndex, i + 1))
+ backtracking(i + 1)
+ path = path.take(path.size - 1)
+ }
+ }
+
+ backtracking(0)
+ result.toList
+ }
+}
+```
+
+
-----------------------
From a8e4d4da767758488dbbc20564eab01a08e3e2dc Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 4 Jun 2022 22:04:15 +0800
Subject: [PATCH 345/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200093.=E5=A4=8D?=
=?UTF-8?q?=E5=8E=9FIP=E5=9C=B0=E5=9D=80.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\345\216\237IP\345\234\260\345\235\200.md" | 42 +++++++++++++++++++
1 file changed, 42 insertions(+)
diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
index 6401824b47..48bb6df229 100644
--- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
+++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
@@ -659,6 +659,48 @@ func restoreIpAddresses(_ s: String) -> [String] {
}
```
+## Scala
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def restoreIpAddresses(s: String): List[String] = {
+ var result = mutable.ListBuffer[String]()
+ if (s.size < 4 || s.length > 12) return result.toList
+ var path = mutable.ListBuffer[String]()
+
+ // 判断IP中的一个字段是否为正确的
+ def isIP(sub: String): Boolean = {
+ if (sub.size > 1 && sub(0) == '0') return false
+ if (sub.toInt > 255) return false
+ true
+ }
+
+ def backtracking(startIndex: Int): Unit = {
+ if (startIndex >= s.size) {
+ if (path.size == 4) {
+ result.append(path.mkString(".")) // mkString方法可以把集合里的数据以指定字符串拼接
+ return
+ }
+ return
+ }
+ // subString
+ for (i <- startIndex until startIndex + 3 if i < s.size) {
+ var subString = s.substring(startIndex, i + 1)
+ if (isIP(subString)) { // 如果合法则进行下一轮
+ path.append(subString)
+ backtracking(i + 1)
+ path = path.take(path.size - 1)
+ }
+ }
+ }
+
+ backtracking(0)
+ result.toList
+ }
+}
+```
+
-----------------------
From f5268ee213dafe8e7ab1c28d722a6b7dc81f4029 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 4 Jun 2022 22:26:58 +0800
Subject: [PATCH 346/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880141.?=
=?UTF-8?q?=E7=8E=AF=E5=BD=A2=E9=93=BE=E8=A1=A8.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...57\345\275\242\351\223\276\350\241\250.md" | 21 ++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md"
index ddd83c9422..ce90b6c404 100644
--- "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md"
+++ "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md"
@@ -7,6 +7,8 @@
# 141. 环形链表
+[力扣题目链接](https://leetcode.cn/problems/linked-list-cycle/submissions/)
+
给定一个链表,判断链表中是否有环。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
@@ -103,7 +105,7 @@ class Solution:
return False
```
-## Go
+### Go
```go
func hasCycle(head *ListNode) bool {
@@ -139,6 +141,23 @@ var hasCycle = function(head) {
};
```
+### TypeScript
+
+```typescript
+function hasCycle(head: ListNode | null): boolean {
+ let slowNode: ListNode | null = head,
+ fastNode: ListNode | null = head;
+ while (fastNode !== null && fastNode.next !== null) {
+ slowNode = slowNode!.next;
+ fastNode = fastNode.next.next;
+ if (slowNode === fastNode) return true;
+ }
+ return false;
+};
+```
+
+
+
-----------------------
From 0245b3ae50cc612ac7765976171b670a954f6a34 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 4 Jun 2022 23:38:12 +0800
Subject: [PATCH 347/551] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=880142.?=
=?UTF-8?q?=E7=8E=AF=E5=BD=A2=E9=93=BE=E8=A1=A8II.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E4=BC=98=E5=8C=96typescript=E7=89=88=E6=9C=AC=E4=BB=A3?=
=?UTF-8?q?=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...2.\347\216\257\345\275\242\351\223\276\350\241\250II.md" | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md"
index f8e62d4572..7d3c8443a1 100644
--- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md"
+++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md"
@@ -301,13 +301,13 @@ function detectCycle(head: ListNode | null): ListNode | null {
let slowNode: ListNode | null = head,
fastNode: ListNode | null = head;
while (fastNode !== null && fastNode.next !== null) {
- slowNode = (slowNode as ListNode).next;
+ slowNode = slowNode!.next;
fastNode = fastNode.next.next;
if (slowNode === fastNode) {
slowNode = head;
while (slowNode !== fastNode) {
- slowNode = (slowNode as ListNode).next;
- fastNode = (fastNode as ListNode).next;
+ slowNode = slowNode!.next;
+ fastNode = fastNode!.next;
}
return slowNode;
}
From bc3d202797ee1a2c276efd2b0b86cc00bd0f744a Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 5 Jun 2022 10:54:31 +0800
Subject: [PATCH 348/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880205.?=
=?UTF-8?q?=E5=90=8C=E6=9E=84=E5=AD=97=E7=AC=A6=E4=B8=B2.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\345\255\227\347\254\246\344\270\262.md" | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git "a/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md"
index d4b71c59c4..337dcc733d 100644
--- "a/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md"
@@ -156,6 +156,28 @@ var isIsomorphic = function(s, t) {
};
```
+## TypeScript
+
+```typescript
+function isIsomorphic(s: string, t: string): boolean {
+ const helperMap1: Map = new Map();
+ const helperMap2: Map = new Map();
+ for (let i = 0, length = s.length; i < length; i++) {
+ let temp1: string | undefined = helperMap1.get(s[i]);
+ let temp2: string | undefined = helperMap2.get(t[i]);
+ if (temp1 === undefined && temp2 === undefined) {
+ helperMap1.set(s[i], t[i]);
+ helperMap2.set(t[i], s[i]);
+ } else if (temp1 !== t[i] || temp2 !== s[i]) {
+ return false;
+ }
+ }
+ return true;
+};
+```
+
+
+
-----------------------
From ccfb805417aa1f55efd1b30be662db8a92b46893 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 5 Jun 2022 13:50:27 +0800
Subject: [PATCH 349/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200078.=E5=AD=90?=
=?UTF-8?q?=E9=9B=86.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
"problems/0078.\345\255\220\351\233\206.md" | 54 +++++++++++++++++++++
1 file changed, 54 insertions(+)
diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md"
index e1c52b5b81..3fc396a2e0 100644
--- "a/problems/0078.\345\255\220\351\233\206.md"
+++ "b/problems/0078.\345\255\220\351\233\206.md"
@@ -373,6 +373,60 @@ func subsets(_ nums: [Int]) -> [[Int]] {
}
```
+## Scala
+
+思路一: 使用本题解思路
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def subsets(nums: Array[Int]): List[List[Int]] = {
+ var result = mutable.ListBuffer[List[Int]]()
+ var path = mutable.ListBuffer[Int]()
+
+ def backtracking(startIndex: Int): Unit = {
+ result.append(path.toList) // 存放结果
+ if (startIndex >= nums.size) {
+ return
+ }
+ for (i <- startIndex until nums.size) {
+ path.append(nums(i)) // 添加元素
+ backtracking(i + 1)
+ path.remove(path.size - 1) // 删除
+ }
+ }
+
+ backtracking(0)
+ result.toList
+ }
+}
+```
+
+思路二: 将原问题转换为二叉树,针对每一个元素都有**选或不选**两种选择,直到遍历到最后,所有的叶子节点即为本题的答案:
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def subsets(nums: Array[Int]): List[List[Int]] = {
+ var result = mutable.ListBuffer[List[Int]]()
+
+ def backtracking(path: mutable.ListBuffer[Int], startIndex: Int): Unit = {
+ if (startIndex == nums.length) {
+ result.append(path.toList)
+ return
+ }
+ path.append(nums(startIndex))
+ backtracking(path, startIndex + 1) // 选择元素
+ path.remove(path.size - 1)
+ backtracking(path, startIndex + 1) // 不选择元素
+ }
+
+ backtracking(mutable.ListBuffer[Int](), 0)
+ result.toList
+ }
+}
+```
+
-----------------------
From b3530189489235f1212b89bd9dba0281154f366c Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 5 Jun 2022 14:13:41 +0800
Subject: [PATCH 350/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200090.=E5=AD=90?=
=?UTF-8?q?=E9=9B=86II.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
"problems/0090.\345\255\220\351\233\206II.md" | 57 +++++++++++++++++++
1 file changed, 57 insertions(+)
diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md"
index 74ce000b0b..9047a80984 100644
--- "a/problems/0090.\345\255\220\351\233\206II.md"
+++ "b/problems/0090.\345\255\220\351\233\206II.md"
@@ -434,6 +434,63 @@ func subsetsWithDup(_ nums: [Int]) -> [[Int]] {
}
```
+### Scala
+
+不使用userd数组:
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def subsetsWithDup(nums: Array[Int]): List[List[Int]] = {
+ var result = mutable.ListBuffer[List[Int]]()
+ var path = mutable.ListBuffer[Int]()
+ var num = nums.sorted // 排序
+
+ def backtracking(startIndex: Int): Unit = {
+ result.append(path.toList)
+ if (startIndex >= num.size){
+ return
+ }
+ for (i <- startIndex until num.size) {
+ // 同一树层重复的元素不进入回溯
+ if (!(i > startIndex && num(i) == num(i - 1))) {
+ path.append(num(i))
+ backtracking(i + 1)
+ path.remove(path.size - 1)
+ }
+ }
+ }
+
+ backtracking(0)
+ result.toList
+ }
+}
+```
+
+使用Set去重:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def subsetsWithDup(nums: Array[Int]): List[List[Int]] = {
+ var result = mutable.Set[List[Int]]()
+ var num = nums.sorted
+ def backtracking(path: mutable.ListBuffer[Int], startIndex: Int): Unit = {
+ if (startIndex == num.length) {
+ result.add(path.toList)
+ return
+ }
+ path.append(num(startIndex))
+ backtracking(path, startIndex + 1) // 选择
+ path.remove(path.size - 1)
+ backtracking(path, startIndex + 1) // 不选择
+ }
+
+ backtracking(mutable.ListBuffer[Int](), 0)
+
+ result.toList
+ }
+}
+```
-----------------------
From 068cc095353ceb240c98a6c2b8598e98c445260b Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 5 Jun 2022 14:46:21 +0800
Subject: [PATCH 351/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200491.=E9=80=92?=
=?UTF-8?q?=E5=A2=9E=E5=AD=90=E5=BA=8F=E5=88=97.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...36\345\255\220\345\272\217\345\210\227.md" | 34 +++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md"
index 3ea2382b1f..a04d433b64 100644
--- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md"
@@ -522,5 +522,39 @@ func findSubsequences(_ nums: [Int]) -> [[Int]] {
```
+## Scala
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def findSubsequences(nums: Array[Int]): List[List[Int]] = {
+ var result = mutable.ListBuffer[List[Int]]()
+ var path = mutable.ListBuffer[Int]()
+
+ def backtracking(startIndex: Int): Unit = {
+ // 集合元素大于1,添加到结果集
+ if (path.size > 1) {
+ result.append(path.toList)
+ }
+
+ var used = new Array[Boolean](201)
+ // 使用循环守卫,当前层没有用过的元素才有资格进入回溯
+ for (i <- startIndex until nums.size if !used(nums(i) + 100)) {
+ // 如果path没元素或 当前循环的元素比path的最后一个元素大,则可以进入回溯
+ if (path.size == 0 || (!path.isEmpty && nums(i) >= path(path.size - 1))) {
+ used(nums(i) + 100) = true
+ path.append(nums(i))
+ backtracking(i + 1)
+ path.remove(path.size - 1)
+ }
+ }
+ }
+
+ backtracking(0)
+ result.toList
+ }
+}
+```
+
-----------------------
From 9f38af3e3a61562b17530302e8d14a6a02d1835f Mon Sep 17 00:00:00 2001
From: Hang <69448559+silaslll@users.noreply.github.com>
Date: Sun, 5 Jun 2022 17:59:15 -0400
Subject: [PATCH 352/551] =?UTF-8?q?Update=200452.=E7=94=A8=E6=9C=80?=
=?UTF-8?q?=E5=B0=91=E6=95=B0=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95=E7=88=86?=
=?UTF-8?q?=E6=B0=94=E7=90=83.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
更新了java代码,增加了一个 leftmostRightBound variable 记录最小的右边界使得代码可读性增加
加入了comment
解释了Arrays.sort(points, (x, y) -> Integer.compare(x[0], y[0])); 中不用 x[0] - y[0] 而是用Integer.compare(x[0], y[0]) 的原因
加入了时空复杂度和说明
---
...25\347\210\206\346\260\224\347\220\203.md" | 21 ++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md"
index d4bbe961ee..58422d4cf2 100644
--- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md"
+++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md"
@@ -136,17 +136,28 @@ public:
### Java
```java
+/**
+时间复杂度 : O(NlogN) 排序需要 O(NlogN) 的复杂度
+
+空间复杂度 : O(logN) java所使用的内置函数用的是快速排序需要 logN 的空间
+*/
class Solution {
public int findMinArrowShots(int[][] points) {
if (points.length == 0) return 0;
- Arrays.sort(points, (o1, o2) -> Integer.compare(o1[0], o2[0]));
-
+ //用x[0] - y[0] 会大于2147483647 造成整型溢出
+ Arrays.sort(points, (x, y) -> Integer.compare(x[0], y[0]));
+ //count = 1 因为最少需要一个箭来射击第一个气球
int count = 1;
- for (int i = 1; i < points.length; i++) {
- if (points[i][0] > points[i - 1][1]) {
+ //重叠气球的最小右边界
+ int leftmostRightBound = points[0][1];
+ //如果下一个气球的左边界大于最小右边界
+ if (points[i][0] > leftmostRightBound ) {
+ //增加一次射击
count++;
+ leftmostRightBound = points[i][1];
+ //不然就更新最小右边界
} else {
- points[i][1] = Math.min(points[i][1],points[i - 1][1]);
+ leftmostRightBound = Math.min(leftmostRightBound , points[i][1]);
}
}
return count;
From 6d804e2b244891ab3c1a12ac9b34d3eafd64654c Mon Sep 17 00:00:00 2001
From: Grant Yang
Date: Sun, 5 Jun 2022 21:24:56 -0400
Subject: [PATCH 353/551] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20111.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?=
=?UTF-8?q?=20=E7=9A=84=E6=8B=BC=E5=86=99=E9=94=99=E8=AF=AF=E5=8F=8A?=
=?UTF-8?q?=E6=94=B9=E8=BF=9B=E8=A7=84=E8=8C=83?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...5\261\202\345\272\217\351\201\215\345\216\206.md" | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
index 1743243de1..d9fd0b30d4 100644
--- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
+++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
@@ -2393,21 +2393,21 @@ JavaScript:
var minDepth = function(root) {
if (root === null) return 0;
let queue = [root];
- let deepth = 0;
+ let depth = 0;
while (queue.length) {
let n = queue.length;
- deepth++;
+ depth++;
for (let i=0; i
Date: Sun, 5 Jun 2022 22:31:41 -0400
Subject: [PATCH 354/551] =?UTF-8?q?Update=200056.=E5=90=88=E5=B9=B6?=
=?UTF-8?q?=E5=8C=BA=E9=97=B4.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...10\345\271\266\345\214\272\351\227\264.md" | 26 ++++++++++++++-----
1 file changed, 20 insertions(+), 6 deletions(-)
diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
index e444a22111..98848963d1 100644
--- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
+++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
@@ -136,24 +136,38 @@ public:
### Java
```java
+
+/**
+时间复杂度 : O(NlogN) 排序需要O(NlogN)
+空间复杂度 : O(logN) java 的内置排序是快速排序 需要 O(logN)空间
+
+*/
class Solution {
public int[][] merge(int[][] intervals) {
List res = new LinkedList<>();
- Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[0], o2[0]));
-
+ //按照左边界排序
+ Arrays.sort(intervals, (x, y) -> Integer.compare(x[0], y[0]));
+ //initial start 是最小左边界
int start = intervals[0][0];
+ int rightmostRightBound = intervals[0][1];
for (int i = 1; i < intervals.length; i++) {
- if (intervals[i][0] > intervals[i - 1][1]) {
- res.add(new int[]{start, intervals[i - 1][1]});
+ //如果左边界大于最大右边界
+ if (intervals[i][0] > rightmostRightBound) {
+ //加入区间 并且更新start
+ res.add(new int[]{start, rightmostRightBound});
start = intervals[i][0];
+ rightmostRightBound = intervals[i][1];
} else {
- intervals[i][1] = Math.max(intervals[i][1], intervals[i - 1][1]);
+ //更新最大右边界
+ rightmostRightBound = Math.max(rightmostRightBound, intervals[i][1]);
}
}
- res.add(new int[]{start, intervals[intervals.length - 1][1]});
+ res.add(new int[]{start, rightmostRightBound});
return res.toArray(new int[res.size()][]);
}
}
+
+}
```
```java
// 版本2
From dd00b57be87e9c4e25267c5ab6cc53f6d714a97e Mon Sep 17 00:00:00 2001
From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com>
Date: Mon, 6 Jun 2022 21:43:18 -0500
Subject: [PATCH 355/551] =?UTF-8?q?Update=200704.=E4=BA=8C=E5=88=86?=
=?UTF-8?q?=E6=9F=A5=E6=89=BE.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...14\345\210\206\346\237\245\346\211\276.md" | 24 ++++++++++---------
1 file changed, 13 insertions(+), 11 deletions(-)
diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
index 1e474f9a96..a468cc44b6 100644
--- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
+++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
@@ -218,19 +218,21 @@ class Solution:
(版本二)左闭右开区间
-```python
-class Solution:
+```class Solution:
def search(self, nums: List[int], target: int) -> int:
- left,right =0, len(nums)
- while left < right:
- mid = (left + right) // 2
- if nums[mid] < target:
- left = mid+1
- elif nums[mid] > target:
- right = mid
+ if nums is None or len(nums)==0:
+ return -1
+ l=0
+ r=len(nums)-1
+ while (l<=r):
+ m = round(l+(r-l)/2)
+ if nums[m] == target:
+ return m
+ elif nums[m] > target:
+ r=m-1
else:
- return mid
- return -1
+ l=m+1
+ return -1
```
**Go:**
From 091204c926a044f0ec86200cbdd3cca6deeaf97e Mon Sep 17 00:00:00 2001
From: HanMengnan <1448189829@qq.com>
Date: Tue, 7 Jun 2022 10:46:43 +0800
Subject: [PATCH 356/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880129.?=
=?UTF-8?q?=E6=B1=82=E6=A0=B9=E8=8A=82=E7=82=B9=E5=88=B0=E5=8F=B6=E8=8A=82?=
=?UTF-8?q?=E7=82=B9=E6=95=B0=E5=AD=97=E4=B9=8B=E5=92=8C.md=EF=BC=89?=
=?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0go=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...60\345\255\227\344\271\213\345\222\214.md" | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md"
index b271ca7de5..4191bb26f3 100644
--- "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md"
+++ "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md"
@@ -3,6 +3,9 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+
+
+
# 129. 求根节点到叶节点数字之和
[力扣题目链接](https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/)
@@ -245,6 +248,29 @@ class Solution:
```
Go:
+```go
+func sumNumbers(root *TreeNode) int {
+ sum = 0
+ travel(root, root.Val)
+ return sum
+}
+
+func travel(root *TreeNode, tmpSum int) {
+ if root.Left == nil && root.Right == nil {
+ sum += tmpSum
+ } else {
+ if root.Left != nil {
+ travel(root.Left, tmpSum*10+root.Left.Val)
+ }
+ if root.Right != nil {
+ travel(root.Right, tmpSum*10+root.Right.Val)
+ }
+ }
+}
+```
+
+
+
JavaScript:
```javascript
var sumNumbers = function(root) {
From f49b2e4a75c6f98c082923e1098aedc5cb27574b Mon Sep 17 00:00:00 2001
From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com>
Date: Mon, 6 Jun 2022 21:49:32 -0500
Subject: [PATCH 357/551] =?UTF-8?q?Update=200027.=E7=A7=BB=E9=99=A4?=
=?UTF-8?q?=E5=85=83=E7=B4=A0.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
the former code has not consider if nums is None or len(nums)==0
---
...73\351\231\244\345\205\203\347\264\240.md" | 38 +++++++++----------
1 file changed, 17 insertions(+), 21 deletions(-)
diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
index 4b50d66643..b239136c46 100644
--- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
+++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
@@ -173,28 +173,24 @@ class Solution {
Python:
-```python
+```python3
class Solution:
- """双指针法
- 时间复杂度:O(n)
- 空间复杂度:O(1)
- """
-
- @classmethod
- def removeElement(cls, nums: List[int], val: int) -> int:
- fast = slow = 0
-
- while fast < len(nums):
-
- if nums[fast] != val:
- nums[slow] = nums[fast]
- slow += 1
-
- # 当 fast 指针遇到要删除的元素时停止赋值
- # slow 指针停止移动, fast 指针继续前进
- fast += 1
-
- return slow
+ def removeElement(self, nums: List[int], val: int) -> int:
+ if nums is None or len(nums)==0:
+ return 0
+ l=0
+ r=len(nums)-1
+ while l
Date: Mon, 6 Jun 2022 22:11:16 -0500
Subject: [PATCH 358/551] =?UTF-8?q?Update=200977.=E6=9C=89=E5=BA=8F?=
=?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
python3 version of brutal force
---
...0\347\273\204\347\232\204\345\271\263\346\226\271.md" | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
index 0e79a3d654..d3da662f8c 100644
--- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
+++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
@@ -39,6 +39,15 @@ public:
}
};
```
+```python3
+class Solution:
+ def sortedSquares(self, nums: List[int]) -> List[int]:
+ res=[]
+ for num in nums:
+ res.append(num**2)
+ return sorted(res)
+```
+
这个时间复杂度是 O(n + nlogn), 可以说是O(nlogn)的时间复杂度,但为了和下面双指针法算法时间复杂度有鲜明对比,我记为 O(n + nlog n)。
From b10f7edba037886e736e58bbe0488ab078efdaa2 Mon Sep 17 00:00:00 2001
From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com>
Date: Mon, 6 Jun 2022 22:17:15 -0500
Subject: [PATCH 359/551] =?UTF-8?q?Update=200209.=E9=95=BF=E5=BA=A6?=
=?UTF-8?q?=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
滑动窗口 version of python3 code
---
...04\345\255\220\346\225\260\347\273\204.md" | 23 +++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)
diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
index fbef7692cd..160f93bb46 100644
--- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
+++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
@@ -162,8 +162,27 @@ class Solution:
index += 1
return 0 if res==float("inf") else res
```
-
-
+```python3
+#滑动窗口
+class Solution:
+ def minSubArrayLen(self, target: int, nums: List[int]) -> int:
+ if nums is None or len(nums)==0:
+ return 0
+ lenf=len(nums)+1
+ total=0
+ i=j=0
+ while (j=target):
+ lenf=min(lenf,j-i)
+ total=total-nums[i]
+ i+=1
+ if lenf==len(nums)+1:
+ return 0
+ else:
+ return lenf
+```
Go:
```go
func minSubArrayLen(target int, nums []int) int {
From 97fc88e533418bf9070bd9fb549a23a2499805b4 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 7 Jun 2022 16:59:55 +0800
Subject: [PATCH 360/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200046.=E5=85=A8?=
=?UTF-8?q?=E6=8E=92=E5=88=97.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...6.\345\205\250\346\216\222\345\210\227.md" | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md"
index 836c36462f..ec3adaa77d 100644
--- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md"
+++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md"
@@ -456,6 +456,36 @@ func permute(_ nums: [Int]) -> [[Int]] {
}
```
+### Scala
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def permute(nums: Array[Int]): List[List[Int]] = {
+ var result = mutable.ListBuffer[List[Int]]()
+ var path = mutable.ListBuffer[Int]()
+
+ def backtracking(used: Array[Boolean]): Unit = {
+ if (path.size == nums.size) {
+ // 如果path的长度和nums相等,那么可以添加到结果集
+ result.append(path.toList)
+ return
+ }
+ // 添加循环守卫,只有当当前数字没有用过的情况下才进入回溯
+ for (i <- nums.indices if used(i) == false) {
+ used(i) = true
+ path.append(nums(i))
+ backtracking(used) // 回溯
+ path.remove(path.size - 1)
+ used(i) = false
+ }
+ }
+
+ backtracking(new Array[Boolean](nums.size)) // 调用方法
+ result.toList // 最终返回结果集的List形式
+ }
+}
+```
-----------------------
From 8537401616f10ce85038bbd94aa951a20bb877d6 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 7 Jun 2022 17:24:41 +0800
Subject: [PATCH 361/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200047.=E5=85=A8?=
=?UTF-8?q?=E6=8E=92=E5=88=97II.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\345\205\250\346\216\222\345\210\227II.md" | 38 +++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md"
index cce25cd9a1..0a5debcc68 100644
--- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md"
+++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md"
@@ -422,5 +422,43 @@ int** permuteUnique(int* nums, int numsSize, int* returnSize, int** returnColumn
}
```
+### Scala
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def permuteUnique(nums: Array[Int]): List[List[Int]] = {
+ var result = mutable.ListBuffer[List[Int]]()
+ var path = mutable.ListBuffer[Int]()
+ var num = nums.sorted // 首先对数据进行排序
+
+ def backtracking(used: Array[Boolean]): Unit = {
+ if (path.size == num.size) {
+ // 如果path的size等于num了,那么可以添加到结果集
+ result.append(path.toList)
+ return
+ }
+ // 循环守卫,当前元素没被使用过就进入循环体
+ for (i <- num.indices if used(i) == false) {
+ // 当前索引为0,不存在和前一个数字相等可以进入回溯
+ // 当前索引值和上一个索引不相等,可以回溯
+ // 前一个索引对应的值没有被选,可以回溯
+ // 因为Scala没有continue,只能将逻辑反过来写
+ if (i == 0 || (i > 0 && num(i) != num(i - 1)) || used(i-1) == false) {
+ used(i) = true
+ path.append(num(i))
+ backtracking(used)
+ path.remove(path.size - 1)
+ used(i) = false
+ }
+ }
+ }
+
+ backtracking(new Array[Boolean](nums.length))
+ result.toList
+ }
+}
+```
+
-----------------------
From fa26fb332b43cbeec805944f4522472953232487 Mon Sep 17 00:00:00 2001
From: dcj_hp <294487055@qq.com>
Date: Tue, 7 Jun 2022 17:38:37 +0800
Subject: [PATCH 362/551] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20java=20dp=E8=A7=A3?=
=?UTF-8?q?=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\345\233\236\346\226\207\344\270\262II.md" | 50 +++++++++++++++++++
1 file changed, 50 insertions(+)
diff --git "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md"
index 87d3e4b434..4cb959012d 100644
--- "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md"
+++ "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md"
@@ -206,6 +206,55 @@ public:
## Java
```java
+class Solution {
+
+ public int minCut(String s) {
+ if(null == s || "".equals(s)){
+ return 0;
+ }
+ int len = s.length();
+ // 1.
+ // 记录子串[i..j]是否是回文串
+ boolean[][] isPalindromic = new boolean[len][len];
+ // 从下到上,从左到右
+ for(int i = len - 1; i >= 0; i--){
+ for(int j = i; j < len; j++){
+ if(s.charAt(i) == s.charAt(j)){
+ if(j - i <= 1){
+ isPalindromic[i][j] = true;
+ } else{
+ isPalindromic[i][j] = isPalindromic[i + 1][j - 1];
+ }
+ } else{
+ isPalindromic[i][j] = false;
+ }
+ }
+ }
+
+ // 2.
+ // dp[i] 表示[0..i]的最小分割次数
+ int[] dp = new int[len];
+ for(int i = 0; i < len; i++){
+ //初始考虑最坏的情况。 1个字符分割0次, len个字符分割 len - 1次
+ dp[i] = i;
+ }
+
+ for(int i = 1; i < len; i++){
+ if(isPalindromic[0][i]){
+ // s[0..i]是回文了,那 dp[i] = 0, 一次也不用分割
+ dp[i] = 0;
+ continue;
+ }
+ for(int j = 0; j < i; j++){
+ // 按文中的思路,不清楚就拿 "ababa" 为例,先写出 isPalindromic 数组,再进行求解
+ if(isPalindromic[j + 1][i]){
+ dp[i] = Math.min(dp[i], dp[j] + 1);
+ }
+ }
+ }
+ return dp[len - 1];
+ }
+}
```
## Python
@@ -240,6 +289,7 @@ class Solution:
## Go
```go
+
```
## JavaScript
From 7b95f173683f4eae7fd68c313839a678b2e1b79d Mon Sep 17 00:00:00 2001
From: Chris Chen
Date: Tue, 7 Jun 2022 13:21:32 +0100
Subject: [PATCH 363/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0001.=E4=B8=A4?=
=?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C.md=EF=BC=89=EF=BC=9ADart=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...270\244\346\225\260\344\271\213\345\222\214.md" | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md"
index 6969c2e27e..4ba92092f8 100644
--- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md"
@@ -317,6 +317,20 @@ public class Solution {
}
```
+Dart:
+```dart
+List twoSum(List nums, int target) {
+ var tmp = [];
+ for (var i = 0; i < nums.length; i++) {
+ var rest = target - nums[i];
+ if(tmp.contains(rest)){
+ return [tmp.indexOf(rest), i];
+ }
+ tmp.add(nums[i]);
+ }
+ return [0 , 0];
+}
+```
-----------------------
From 371564ba3b9a2b9641cafc05eb033f8d162aec81 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 7 Jun 2022 22:19:13 +0800
Subject: [PATCH 364/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200051.N=E7=9A=87?=
=?UTF-8?q?=E5=90=8E.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
"problems/0051.N\347\232\207\345\220\216.md" | 74 +++++++++++++++++++-
1 file changed, 73 insertions(+), 1 deletion(-)
diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md"
index c03e48c2e4..f65ccaf518 100644
--- "a/problems/0051.N\347\232\207\345\220\216.md"
+++ "b/problems/0051.N\347\232\207\345\220\216.md"
@@ -455,7 +455,7 @@ var solveNQueens = function(n) {
};
```
-## TypeScript
+### TypeScript
```typescript
function solveNQueens(n: number): string[][] {
@@ -683,5 +683,77 @@ char *** solveNQueens(int n, int* returnSize, int** returnColumnSizes){
}
```
+### Scala
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def solveNQueens(n: Int): List[List[String]] = {
+ var result = mutable.ListBuffer[List[String]]()
+
+ def judge(x: Int, y: Int, maze: Array[Array[Boolean]]): Boolean = {
+ // 正上方
+ var xx = x
+ while (xx >= 0) {
+ if (maze(xx)(y)) return false
+ xx -= 1
+ }
+ // 左边
+ var yy = y
+ while (yy >= 0) {
+ if (maze(x)(yy)) return false
+ yy -= 1
+ }
+ // 左上方
+ xx = x
+ yy = y
+ while (xx >= 0 && yy >= 0) {
+ if (maze(xx)(yy)) return false
+ xx -= 1
+ yy -= 1
+ }
+ xx = x
+ yy = y
+ // 右上方
+ while (xx >= 0 && yy < n) {
+ if (maze(xx)(yy)) return false
+ xx -= 1
+ yy += 1
+ }
+ true
+ }
+
+ def backtracking(row: Int, maze: Array[Array[Boolean]]): Unit = {
+ if (row == n) {
+ // 将结果转换为题目所需要的形式
+ var path = mutable.ListBuffer[String]()
+ for (x <- maze) {
+ var tmp = mutable.ListBuffer[String]()
+ for (y <- x) {
+ if (y == true) tmp.append("Q")
+ else tmp.append(".")
+ }
+ path.append(tmp.mkString)
+ }
+ result.append(path.toList)
+ return
+ }
+
+ for (j <- 0 until n) {
+ // 判断这个位置是否可以放置皇后
+ if (judge(row, j, maze)) {
+ maze(row)(j) = true
+ backtracking(row + 1, maze)
+ maze(row)(j) = false
+ }
+ }
+ }
+
+ backtracking(0, Array.ofDim[Boolean](n, n))
+ result.toList
+ }
+}
+```
+
-----------------------
From b61afe9aee786592c313afb42b07295d0c1f4e00 Mon Sep 17 00:00:00 2001
From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com>
Date: Tue, 7 Jun 2022 12:30:18 -0500
Subject: [PATCH 365/551] =?UTF-8?q?Update=200054.=E8=9E=BA=E6=97=8B?=
=?UTF-8?q?=E7=9F=A9=E9=98=B5.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
for loop version of python3 solution
---
...72\346\227\213\347\237\251\351\230\265.md" | 26 ++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)
diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md"
index ccf6f47161..27899d5181 100644
--- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md"
+++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md"
@@ -171,6 +171,30 @@ class Solution:
return res
```
-
+```python3
+class Solution:
+ def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
+ r=len(matrix)
+ if r == 0 or len(matrix[0])==0:
+ return []
+ c=len(matrix[0])
+ res=matrix[0]
+
+ if r>1:
+ for i in range (1,r):
+ res.append(matrix[i][c-1])
+ for j in range(c-2, -1, -1):
+ res.append(matrix[r-1][j])
+ if c>1:
+ for i in range(r-2, 0, -1):
+ res.append(matrix[i][0])
+
+ M=[]
+ for k in range(1, r-1):
+ e=matrix[k][1:-1]
+ M.append(e)
+
+ return res+self.spiralOrder(M)
+```
-----------------------
From 237dc9d6b3c648d4a76e905bbc7059c2f573e91a Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Wed, 8 Jun 2022 12:52:55 +0800
Subject: [PATCH 366/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880925.?=
=?UTF-8?q?=E9=95=BF=E6=8C=89=E9=94=AE=E5=85=A5.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...77\346\214\211\351\224\256\345\205\245.md" | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git "a/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" "b/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md"
index 0ef5a3d7f3..feb5739148 100644
--- "a/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md"
+++ "b/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md"
@@ -209,6 +209,31 @@ var isLongPressedName = function(name, typed) {
};
```
+### TypeScript
+
+```typescript
+function isLongPressedName(name: string, typed: string): boolean {
+ const nameLength: number = name.length,
+ typeLength: number = typed.length;
+ let i: number = 0,
+ j: number = 0;
+ while (i < nameLength && j < typeLength) {
+ if (name[i] !== typed[j]) return false;
+ i++;
+ j++;
+ if (i === nameLength || name[i] !== name[i - 1]) {
+ // 跳过typed中的连续相同字符
+ while (j < typeLength && typed[j] === typed[j - 1]) {
+ j++;
+ }
+ }
+ }
+ return i === nameLength && j === typeLength;
+};
+```
+
+
+
-----------------------
From cded6c5c803e5d6de7a47e020e94ce03fcc68432 Mon Sep 17 00:00:00 2001
From: ExplosiveBattery <641370196@qq.com>
Date: Thu, 9 Jun 2022 00:58:38 +0800
Subject: [PATCH 367/551] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1?=
=?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md=20=20python=20code=20via=20itera?=
=?UTF-8?q?te?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
In the original method, we need to traversal every node and write the function named getDepth to get the depth of all sub trees in traverse method too.
But there is more suitable uniform iteration traversal algorithm, I use the map struct in the code segment where the node is Null.
If you have problem in understand, please feel free to communicate with me.
---
...41\344\272\214\345\217\211\346\240\221.md" | 46 +++++++------------
1 file changed, 16 insertions(+), 30 deletions(-)
diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md"
index d98ff8a90c..1b997643f4 100644
--- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md"
@@ -531,40 +531,26 @@ class Solution:
迭代法:
```python
class Solution:
- def isBalanced(self, root: TreeNode) -> bool:
- st = []
+ def isBalanced(self, root: Optional[TreeNode]) -> bool:
if not root:
return True
- st.append(root)
- while st:
- node = st.pop() #中
- if abs(self.getDepth(node.left) - self.getDepth(node.right)) > 1:
- return False
- if node.right:
- st.append(node.right) #右(空节点不入栈)
- if node.left:
- st.append(node.left) #左(空节点不入栈)
- return True
-
- def getDepth(self, cur):
- st = []
- if cur:
- st.append(cur)
- depth = 0
- result = 0
- while st:
- node = st.pop()
+
+ height_map = {}
+ stack = [root]
+ while stack:
+ node = stack.pop()
if node:
- st.append(node) #中
- st.append(None)
- depth += 1
- if node.right: st.append(node.right) #右
- if node.left: st.append(node.left) #左
+ stack.append(node)
+ stack.append(None)
+ if node.left: stack.append(node.left)
+ if node.right: stack.append(node.right)
else:
- node = st.pop()
- depth -= 1
- result = max(result, depth)
- return result
+ real_node = stack.pop()
+ left, right = height_map.get(real_node.left, 0), height_map.get(real_node.right, 0)
+ if abs(left - right) > 1:
+ return False
+ height_map[real_node] = 1 + max(left, right)
+ return True
```
From e354cd6e25231fc9b255ae1d6ed3d1c58e4aa27d Mon Sep 17 00:00:00 2001
From: ExplosiveBattery <641370196@qq.com>
Date: Thu, 9 Jun 2022 02:33:55 +0800
Subject: [PATCH 368/551] =?UTF-8?q?Update=200101.=E5=AF=B9=E7=A7=B0?=
=?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md=20level=20order=20traversal?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This leetcode problem can use level order traversal method, the difference between with the normal version is we should judge for None.
There is my python answer, please feel free to contact with me if you have any problem.
---
...60\344\272\214\345\217\211\346\240\221.md" | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md"
index 1eb435892d..caf5d249e6 100644
--- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md"
@@ -437,6 +437,31 @@ class Solution:
return True
```
+层次遍历
+```python
+class Solution:
+ def isSymmetric(self, root: Optional[TreeNode]) -> bool:
+ if not root:
+ return True
+
+ que = [root]
+ while que:
+ this_level_length = len(que)
+ for i in range(this_level_length // 2):
+ # 要么其中一个是None但另外一个不是
+ if (not que[i] and que[this_level_length - 1 - i]) or (que[i] and not que[this_level_length - 1 - i]):
+ return False
+ # 要么两个都不是None
+ if que[i] and que[i].val != que[this_level_length - 1 - i].val:
+ return False
+ for i in range(this_level_length):
+ if not que[i]: continue
+ que.append(que[i].left)
+ que.append(que[i].right)
+ que = que[this_level_length:]
+ return True
+```
+
## Go
```go
From ba31161609d4131a5d039046435a8f21805e301d Mon Sep 17 00:00:00 2001
From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com>
Date: Wed, 8 Jun 2022 17:47:49 -0500
Subject: [PATCH 369/551] =?UTF-8?q?Update=20=E9=9D=A2=E8=AF=95=E9=A2=9802.?=
=?UTF-8?q?07.=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...3002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" | 2 ++
1 file changed, 2 insertions(+)
diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md"
index 0a38cc3309..ba6631a4c9 100644
--- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md"
+++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md"
@@ -160,6 +160,8 @@ class Solution:
那么,只要其中一个链表走完了,就去走另一条链表的路。如果有交点,他们最终一定会在同一个
位置相遇
"""
+ if headA is None or headB is None:
+ return None
cur_a, cur_b = headA, headB # 用两个指针代替a和b
From e06b53a88d8b26b4840428255b0e1f65f0eaf938 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 9 Jun 2022 21:57:15 +0800
Subject: [PATCH 370/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200037.=E8=A7=A3?=
=?UTF-8?q?=E6=95=B0=E7=8B=AC.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...7.\350\247\243\346\225\260\347\213\254.md" | 95 +++++++++++++++++++
1 file changed, 95 insertions(+)
diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md"
index c1ac15af89..e2c533a992 100644
--- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md"
+++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md"
@@ -602,5 +602,100 @@ func solveSudoku(_ board: inout [[Character]]) {
}
```
+### Scala
+
+详细写法:
+```scala
+object Solution {
+
+ def solveSudoku(board: Array[Array[Char]]): Unit = {
+ backtracking(board)
+ }
+
+ def backtracking(board: Array[Array[Char]]): Boolean = {
+ for (i <- 0 until 9) {
+ for (j <- 0 until 9) {
+ if (board(i)(j) == '.') { // 必须是为 . 的数字才放数字
+ for (k <- '1' to '9') { // 这个位置放k是否合适
+ if (isVaild(i, j, k, board)) {
+ board(i)(j) = k
+ if (backtracking(board)) return true // 找到了立刻返回
+ board(i)(j) = '.' // 回溯
+ }
+ }
+ return false // 9个数都试完了,都不行就返回false
+ }
+ }
+ }
+ true // 遍历完所有的都没返回false,说明找到了
+ }
+
+ def isVaild(x: Int, y: Int, value: Char, board: Array[Array[Char]]): Boolean = {
+ // 行
+ for (i <- 0 until 9 ) {
+ if (board(i)(y) == value) {
+ return false
+ }
+ }
+
+ // 列
+ for (j <- 0 until 9) {
+ if (board(x)(j) == value) {
+ return false
+ }
+ }
+
+ // 宫
+ var row = (x / 3) * 3
+ var col = (y / 3) * 3
+ for (i <- row until row + 3) {
+ for (j <- col until col + 3) {
+ if (board(i)(j) == value) {
+ return false
+ }
+ }
+ }
+
+ true
+ }
+}
+```
+
+遵循Scala至简原则写法:
+```scala
+object Solution {
+
+ def solveSudoku(board: Array[Array[Char]]): Unit = {
+ backtracking(board)
+ }
+
+ def backtracking(board: Array[Array[Char]]): Boolean = {
+ // 双重for循环 + 循环守卫
+ for (i <- 0 until 9; j <- 0 until 9 if board(i)(j) == '.') {
+ // 必须是为 . 的数字才放数字,使用循环守卫判断该位置是否可以放置当前循环的数字
+ for (k <- '1' to '9' if isVaild(i, j, k, board)) { // 这个位置放k是否合适
+ board(i)(j) = k
+ if (backtracking(board)) return true // 找到了立刻返回
+ board(i)(j) = '.' // 回溯
+ }
+ return false // 9个数都试完了,都不行就返回false
+ }
+ true // 遍历完所有的都没返回false,说明找到了
+ }
+
+ def isVaild(x: Int, y: Int, value: Char, board: Array[Array[Char]]): Boolean = {
+ // 行,循环守卫进行判断
+ for (i <- 0 until 9 if board(i)(y) == value) return false
+ // 列,循环守卫进行判断
+ for (j <- 0 until 9 if board(x)(j) == value) return false
+ // 宫,循环守卫进行判断
+ var row = (x / 3) * 3
+ var col = (y / 3) * 3
+ for (i <- row until row + 3; j <- col until col + 3 if board(i)(j) == value) return false
+ true // 最终没有返回false,就说明该位置可以填写true
+ }
+}
+```
+
-----------------------
From 730f58bccee54b576c2b5bc998e5b97fe4fb5567 Mon Sep 17 00:00:00 2001
From: hongyang <1664698982@qq.com>
Date: Thu, 9 Jun 2022 23:24:30 +0800
Subject: [PATCH 371/551] refactor: add golang solution to Intersection of Two
Linked Lists LCCI
---
...76\350\241\250\347\233\270\344\272\244.md" | 53 +++++++++++++------
1 file changed, 38 insertions(+), 15 deletions(-)
diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md"
index 0a38cc3309..f603925d4a 100644
--- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md"
+++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md"
@@ -13,21 +13,21 @@
图示两个链表在节点 c1 开始相交:
-
+
题目数据 保证 整个链式结构中不存在环。
-注意,函数返回结果后,链表必须 保持其原始结构 。
+注意,函数返回结果后,链表必须 保持其原始结构 。
-示例 1:
+示例 1:
-
+
示例 2:
-
+
-示例 3:
+示例 3:

@@ -100,7 +100,7 @@ public:
## 其他语言版本
-### Java
+### Java
```Java
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
@@ -144,11 +144,11 @@ public class Solution {
}
return null;
}
-
+
}
```
-### Python
+### Python
```python
class Solution:
@@ -162,15 +162,15 @@ class Solution:
"""
cur_a, cur_b = headA, headB # 用两个指针代替a和b
-
+
while cur_a != cur_b:
cur_a = cur_a.next if cur_a else headB # 如果a走完了,那么就切换到b走
cur_b = cur_b.next if cur_b else headA # 同理,b走完了就切换到a
-
+
return cur_a
```
-### Go
+### Go
```go
func getIntersectionNode(headA, headB *ListNode) *ListNode {
@@ -208,7 +208,30 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode {
}
```
-### javaScript
+递归版本:
+
+```go
+func getIntersectionNode(headA, headB *ListNode) *ListNode {
+ l1,l2 := headA, headB
+ for l1 != l2 {
+ if l1 != nil {
+ l1 = l1.Next
+ } else {
+ l1 = headB
+ }
+
+ if l2 != nil {
+ l2 = l2.Next
+ } else {
+ l2 = headA
+ }
+ }
+
+ return l1
+}
+```
+
+### javaScript
```js
var getListLen = function(head) {
@@ -218,9 +241,9 @@ var getListLen = function(head) {
cur = cur.next;
}
return len;
-}
+}
var getIntersectionNode = function(headA, headB) {
- let curA = headA,curB = headB,
+ let curA = headA,curB = headB,
lenA = getListLen(headA),
lenB = getListLen(headB);
if(lenA < lenB) {
From c9900267505297634675172ad670e86a2269cace Mon Sep 17 00:00:00 2001
From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com>
Date: Thu, 9 Jun 2022 21:11:08 -0500
Subject: [PATCH 372/551] =?UTF-8?q?Update=200234.=E5=9B=9E=E6=96=87?=
=?UTF-8?q?=E9=93=BE=E8=A1=A8.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
For both solution of python3, there are shorter and more efficient ways to write it.
For the #数组模拟, it can be solved more easily by convert the linked list to a list
#反转后半部分链表, the original version define to function, isPalindrome, and reverseList. That's too complicated... No need.
---
...36\346\226\207\351\223\276\350\241\250.md" | 74 +++++++------------
1 file changed, 28 insertions(+), 46 deletions(-)
diff --git "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md"
index db910d4ee0..bbfe4e9132 100644
--- "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md"
+++ "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md"
@@ -218,59 +218,41 @@ class Solution {
```python
#数组模拟
class Solution:
- def isPalindrome(self, head: ListNode) -> bool:
- length = 0
- tmp = head
- while tmp: #求链表长度
- length += 1
- tmp = tmp.next
-
- result = [0] * length
- tmp = head
- index = 0
- while tmp: #链表元素加入数组
- result[index] = tmp.val
- index += 1
- tmp = tmp.next
-
- i, j = 0, length - 1
- while i < j: # 判断回文
- if result[i] != result[j]:
+ def isPalindrome(self, head: Optional[ListNode]) -> bool:
+ list=[]
+ while head:
+ list.append(head.val)
+ head=head.next
+ l,r=0, len(list)-1
+ while l<=r:
+ if list[l]!=list[r]:
return False
- i += 1
- j -= 1
- return True
-
+ l+=1
+ r-=1
+ return True
+
#反转后半部分链表
class Solution:
- def isPalindrome(self, head: ListNode) -> bool:
- if head == None or head.next == None:
- return True
- slow, fast = head, head
+ def isPalindrome(self, head: Optional[ListNode]) -> bool:
+ fast = slow = head
+
+ # find mid point which including (first) mid point into the first half linked list
while fast and fast.next:
- pre = slow
- slow = slow.next
fast = fast.next.next
-
- pre.next = None # 分割链表
- cur1 = head # 前半部分
- cur2 = self.reverseList(slow) # 反转后半部分,总链表长度如果是奇数,cur2比cur1多一个节点
- while cur1:
- if cur1.val != cur2.val:
+ slow = slow.next
+ node = None
+
+ # reverse second half linked list
+ while slow:
+ slow.next, slow, node = node, slow.next, slow
+
+ # compare reversed and original half; must maintain reversed linked list is shorter than 1st half
+ while node:
+ if node.val != head.val:
return False
- cur1 = cur1.next
- cur2 = cur2.next
+ node = node.next
+ head = head.next
return True
-
- def reverseList(self, head: ListNode) -> ListNode:
- cur = head
- pre = None
- while(cur!=None):
- temp = cur.next # 保存一下cur的下一个节点
- cur.next = pre # 反转
- pre = cur
- cur = temp
- return pre
```
## Go
From f5f5f5a2a530845483708938064c05aae698487c Mon Sep 17 00:00:00 2001
From: programmercarl <826123027@qq.com>
Date: Fri, 10 Jun 2022 11:02:44 +0800
Subject: [PATCH 373/551] Update
---
...73\351\231\244\345\205\203\347\264\240.md" | 14 +-
...22\345\205\245\344\275\215\347\275\256.md" | 12 +-
...14\345\210\206\346\237\245\346\211\276.md" | 2 +
...04\347\232\204\345\271\263\346\226\271.md" | 2 +
problems/qita/gitserver.md | 312 ++++++++++++++++++
problems/qita/server.md | 126 +++++++
...16\346\234\254\351\241\271\347\233\256.md" | 0
7 files changed, 460 insertions(+), 8 deletions(-)
create mode 100644 problems/qita/gitserver.md
create mode 100644 problems/qita/server.md
rename "problems/\345\205\266\344\273\226/\345\217\202\344\270\216\346\234\254\351\241\271\347\233\256.md" => "problems/qita/\345\217\202\344\270\216\346\234\254\351\241\271\347\233\256.md" (100%)
diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
index 590cf0b924..72860ffd0d 100644
--- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
+++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
@@ -28,6 +28,8 @@
## 思路
+[本题B站视频讲解](https://www.bilibili.com/video/BV12A4y1Z7LP)
+
有的同学可能说了,多余的元素,删掉不就得了。
**要知道数组的元素在内存地址中是连续的,不能单独删除数组中的某个元素,只能覆盖。**
@@ -75,10 +77,20 @@ public:
双指针法(快慢指针法): **通过一个快指针和慢指针在一个for循环下完成两个for循环的工作。**
+定义快慢指针
+
+* 快指针:寻找新数组的元素 ,新数组就是不含有目标元素的数组
+* 慢指针:指向更新 新数组下标的位置
+
+很多同学这道题目做的很懵,就是不理解 快慢指针究竟都是什么含义,所以一定要明确含义,后面的思路就更容易理解了。
+
删除过程如下:

+很多同学不了解
+
+
**双指针法(快慢指针法)在数组和链表的操作中是非常常见的,很多考察数组、链表、字符串等操作的面试题,都使用双指针法。**
后序都会一一介绍到,本题代码如下:
@@ -104,8 +116,6 @@ public:
* 时间复杂度:O(n)
* 空间复杂度:O(1)
-旧文链接:[数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html)
-
```CPP
/**
* 相向双指针方法,基于元素顺序可以改变的题目描述改变了元素相对位置,确保了移动最少元素
diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
index 8a8f970614..ff1b02926b 100644
--- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
+++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
@@ -73,8 +73,8 @@ public:
};
```
-* 时间复杂度:$O(n)$
-* 空间复杂度:$O(1)$
+* 时间复杂度:O(n)
+* 空间复杂度:O(1)
效率如下:
@@ -135,14 +135,14 @@ public:
// 目标值在数组所有元素之前 [0, -1]
// 目标值等于数组中某一个元素 return middle;
// 目标值插入数组中的位置 [left, right],return right + 1
- // 目标值在数组所有元素之后的情况 [left, right], return right + 1
+ // 目标值在数组所有元素之后的情况 [left, right], 因为是右闭区间,所以 return right + 1
return right + 1;
}
};
```
-* 时间复杂度:$O(\log n)$
-* 时间复杂度:$O(1)$
+* 时间复杂度:O(log n)
+* 时间复杂度:O(1)
效率如下:

@@ -178,7 +178,7 @@ public:
// 目标值在数组所有元素之前 [0,0)
// 目标值等于数组中某一个元素 return middle
// 目标值插入数组中的位置 [left, right) ,return right 即可
- // 目标值在数组所有元素之后的情况 [left, right),return right 即可
+ // 目标值在数组所有元素之后的情况 [left, right),因为是右开区间,所以 return right
return right;
}
};
diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
index 5562513089..ce8253afc3 100644
--- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
+++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
@@ -36,6 +36,8 @@
## 思路
+为了易于大家理解,我还录制了视频,可以看这里:[手把手带你撕出正确的二分法](https://www.bilibili.com/video/BV1fA4y1o715)
+
**这道题目的前提是数组为有序数组**,同时题目还强调**数组中无重复元素**,因为一旦有重复元素,使用二分查找法返回的元素下标可能不是唯一的,这些都是使用二分法的前提条件,当大家看到题目描述满足如上条件的时候,可要想一想是不是可以用二分法了。
二分查找涉及的很多的边界条件,逻辑比较简单,但就是写不好。例如到底是 `while(left < right)` 还是 `while(left <= right)`,到底是`right = middle`呢,还是要`right = middle - 1`呢?
diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
index 24276bcff0..8811f3d790 100644
--- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
+++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
@@ -23,6 +23,8 @@
# 思路
+为了易于大家理解,我还特意录制了视频,[本题视频讲解](https://www.bilibili.com/video/BV1QB4y1D7ep)
+
## 暴力排序
最直观的想法,莫过于:每个数平方之后,排个序,美滋滋,代码如下:
diff --git a/problems/qita/gitserver.md b/problems/qita/gitserver.md
new file mode 100644
index 0000000000..9ee06ae4d9
--- /dev/null
+++ b/problems/qita/gitserver.md
@@ -0,0 +1,312 @@
+
+# 一文手把手教你搭建Git私服
+
+## 为什么要搭建Git私服
+
+很多同学都问文章,文档,资料怎么备份啊,自己电脑和公司电脑怎么随时同步资料啊等等,这里呢我写一个搭建自己的git私服的详细教程
+
+为什么要搭建一个Git私服呢,而不是用Github免费的私有仓库,有以下几点:
+* Github 私有仓库真的慢,文件一旦多了,或者有图片文件,git pull 的时候半天拉不下来
+* 自己的文档难免有自己个人信息,放在github心里也是担心的
+* 想建几个库就建几个,想几个人合作开发都可以,不香么?
+
+**网上可以搜到很多git搭建,但是说的模棱两可**,而且有的直接是在本地搭建git服务,既然是备份,搭建在本地哪有备份的意义,一定要有一个远端服务器, 而且自己的电脑和公司的电脑还是同步自己的文章,文档和资料等等。
+
+
+适合人群: 想通过git私服来备份自己的文章,Markdown,并做版本管理的同学
+最后,写好每篇 Chat 是对我的责任,也是对你的尊重。谢谢大家~
+
+正文如下:
+
+-----------------------------
+
+## 如何找到可以外网访问服务器
+
+有的同学问了,自己的电脑就不能作为服务器么?
+
+这里要说一下,安装家庭带宽,运营商默认是不会给我们独立分配公网IP的
+
+一般情况下是一片区域公用一个公网IP池,所以外网是不能访问到在家里我们使用的电脑的
+
+除非我们自己去做映射,这其实非常麻烦而且公网IP池 是不断变化的
+
+辛辛苦苦做了映射,运营商给IP一换,我们的努力就白扯了
+
+那我们如何才能找到一个外网可以访问的服务器呢,此时云计算拯救了我们。
+
+推荐大家选一家云厂商(阿里云,腾讯云,百度云都可以)在上面上买一台云服务器
+
+* [阿里云活动期间服务器购买](https://www.aliyun.com/minisite/goods?taskCode=shareNew2205&recordId=3641992&userCode=roof0wob)
+* [腾讯云活动期间服务器购买](https://curl.qcloud.com/EiaMXllu)
+
+云厂商经常做活动,如果从来没有买过云服务器的账号更便宜,低配一年一百块左右的样子,强烈推荐一起买个三年。
+
+买云服务器的时候推荐直接安装centos系统。
+
+这里要说一下,有了自己的云服务器之后 不仅仅可以用来做git私服
+
+**同时还可以做网站,做程序后台,跑程序,做测试**(这样我们自己的电脑就不会因为自己各种搭建环境下载各种包而搞的的烂糟糟),等等等。
+
+有自己云服务器和一个公网IP真的是一件非常非常幸福的事情,能体验到自己的服务随时可以部署上去提供给所有人使用的喜悦。
+
+外网可以访问的服务器解决了,接下来就要部署git服务了
+
+本文将采用centos系统来部署git私服
+
+## 服务器端安装Git
+
+切换至root账户
+
+```
+su root
+```
+
+看一下服务器有没有安装git,如果出现下面信息就说明是有git的
+```
+[root@instance-5fcyjde7 ~]# git
+usage: git [--version] [--help] [-c name=value]
+ [--exec-path[=]] [--html-path] [--man-path] [--info-path]
+ [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
+ [--git-dir=] [--work-tree=] [--namespace=]
+ []
+
+The most commonly used git commands are:
+ add Add file contents to the index
+ bisect Find by binary search the change that introduced a bug
+ branch List, create, or delete branches
+ checkout Checkout a branch or paths to the working tree
+ clone Clone a repository into a new directory
+ commit Record changes to the repository
+ diff Show changes between commits, commit and working tree, etc
+ fetch Download objects and refs from another repository
+ grep Print lines matching a pattern
+ init Create an empty Git repository or reinitialize an existing one
+ log Show commit logs
+ merge Join two or more development histories together
+ mv Move or rename a file, a directory, or a symlink
+ pull Fetch from and merge with another repository or a local branch
+ push Update remote refs along with associated objects
+ rebase Forward-port local commits to the updated upstream head
+ reset Reset current HEAD to the specified state
+ rm Remove files from the working tree and from the index
+ show Show various types of objects
+ status Show the working tree status
+ tag Create, list, delete or verify a tag object signed with GPG
+
+'git help -a' and 'git help -g' lists available subcommands and some
+concept guides. See 'git help ' or 'git help '
+to read about a specific subcommand or concept.
+```
+
+如果没有git,就安装一下,yum安装的版本默认是 `1.8.3.1`
+
+```
+yum install git
+```
+
+安装成功之后,看一下自己安装的版本
+
+```
+git --version
+```
+
+## 服务器端设置Git账户
+
+创建一个git的linux账户,这个账户只做git私服的操作,也是为了安全起见
+
+如果不新创建一个linux账户,在自己的常用的linux账户下创建的话,哪天手抖 来一个`rm -rf *` 操作 数据可全没了
+
+**这里linux git账户的密码设置的尽量复杂一些,我这里为了演示,就设置成为'gitpassword'**
+```
+adduser git
+passwd gitpassword
+```
+
+然后就要切换成git账户,进行后面的操作了
+```
+[root@instance-5fcyjde7 ~]# su - git
+```
+
+看一下自己所在的目录,是不是在git目录下面
+
+```
+[git@instance-5fcyjde7 ~]$ pwd
+/home/git
+```
+
+## 服务器端密钥管理
+
+创建`.ssh` 目录,如果`.ssh` 已经存在了,可以忽略这一项
+
+为啥用配置ssh公钥呢,同学们记不记得我急使用github上传上传代码的时候也要把自己的公钥配置上github上
+
+这也是方面每次操作git仓库的时候不用再去输入密码
+
+```
+cd ~/
+mkdir .ssh
+```
+
+进入.ssh 文件下,创建一个 `authorized_keys` 文件,这个文件就是后面就是要放我们客户端的公钥
+
+```
+cd ~/.ssh
+touch authorized_keys
+```
+
+别忘了`authorized_keys`给设置权限,很多同学发现自己不能免密登陆,都是因为忘记了给`authorized_keys` 设置权限
+
+```
+chmod 700 /home/git/.ssh
+chmod 600 /home/git/.ssh/authorized_keys
+```
+
+接下来我们要把客户端的公钥放在git服务器上,我们在回到客户端,创建一个公钥
+
+在我们自己的电脑上,有公钥和私钥 两个文件分别是:`id_rsa` 和 `id_rsa.pub`
+
+如果是`windows`系统公钥私钥的目录在`C:\Users\用户名\.ssh` 下
+
+如果是mac 或者 linux, 公钥和私钥的目录这里 `cd ~/.ssh/`, 如果发现自己的电脑上没有公钥私钥,那就自己创建一个
+
+创建密钥的命令
+
+```
+ssh-keygen -t rsa
+```
+
+创建密钥的过程中,一路点击回车就可以了。不需要填任何东西
+
+把公钥拷贝到git服务器上,将我们刚刚生成的`id_rsa.pub`,拷贝到git服务器的`/home/git/.ssh/`目录
+
+在git服务器上,将公钥添加到`authorized_keys` 文件中
+
+```
+cd /home/git/.ssh/
+cat id_rsa.pub >> authorized_keys
+```
+
+如何看我们配置的密钥是否成功呢, 在客户点直接登录git服务器,看看是否是免密登陆
+```
+ssh git@git服务器ip
+```
+
+例如:
+
+```
+ssh git@127.0.0.1
+```
+
+如果可以免密登录,那就说明服务器端密钥配置成功了
+
+## 服务器端部署Git 仓库
+
+我们在登陆到git 服务器端,切换为成 git账户
+
+如果是root账户切换成git账户
+```
+su - git
+```
+
+如果是其他账户切换为git账户
+```
+sudo su - git
+```
+
+进入git目录下
+```
+cd ~/git
+```
+
+创建我们的第一个Git私服的仓库,我们叫它为world仓库
+
+那么首先创建一个文件夹名为: world.git ,然后进入这个目录
+
+有同学问,为什么文件夹名字后面要放`.git`, 其实不这样命名也是可以的
+
+但是细心的同学可能注意到,我们平时在github上 `git clone` 其他人的仓库的时候,仓库名字后面,都是加上`.git`的
+
+例如下面这个例子,其实就是github对仓库名称的一个命名规则,所以我们也遵守github的命名规则。
+
+```
+git clone https://github.com/youngyangyang04/NoSQLAttack.git
+```
+
+所以我们的操作是
+```
+[git@localhost git]# mkdir world.git
+[git@localhost git]# cd world.git
+```
+
+初始化我们的`world`仓库
+
+```
+git init --bare
+
+```
+
+**如果我们想创建多个仓库,就在这里创建多个文件夹并初始化就可以了,和world仓库的操作过程是一样一样的**
+
+现在我们服务端的git仓库就部署完了,接下来就看看客户端,如何使用这个仓库呢
+
+## 客户端连接远程仓库
+
+我们在自己的电脑上创建一个文件夹 也叫做`world`吧
+
+其实这里命名是随意的,但是我们为了和git服务端的仓库名称保持同步。 这样更直观我们操作的是哪一个仓库。
+
+```
+mkdir world
+cd world
+```
+
+进入world文件,并初始化操作
+
+```
+cd world
+git init
+```
+
+在world目录上创建一个测试文件,并且将其添加到git版本管理中
+
+```
+touch test
+git add test
+git commit -m "add test file"
+```
+
+将次仓库和远端仓库同步
+
+```
+git remote add origin git@git服务器端的ip:world.git
+git push -u origin master
+```
+
+此时这个test测试文件就已经提交到我们的git远端私服上了
+
+## Git私服安全问题
+
+这里有两点安全问题
+
+### linux git的密码不要泄露出去
+
+否则,别人可以通过 ssh git@git服务器IP 来登陆到你的git私服服务器上
+
+当然了,这里同学们如果买的是云厂商的云服务器的话
+
+如果有人恶意想通过 尝试不同密码链接的方式来链接你的服务器,重试三次以上
+
+这个客户端的IP就会被封掉,同时邮件通知我们可以IP来自哪里
+
+所以大可放心 密码只要我们不泄露出去,基本上不会有人同时不断尝试密码的方式来登上我们的git私服服务器
+
+### 私钥文件`id_rsa` 不要给别人
+
+如果有人得到了这个私钥,就可以免密码登陆我们的git私服上了,我相信大家也不至于把自己的私钥主动给别人吧
+
+## 总结
+
+这里就是整个git私服搭建的全过程,安全问题我也给大家列举了出来,接下来好好享受自己的Git私服吧
+
+**enjoy!**
+
diff --git a/problems/qita/server.md b/problems/qita/server.md
new file mode 100644
index 0000000000..16995d703d
--- /dev/null
+++ b/problems/qita/server.md
@@ -0,0 +1,126 @@
+
+# 一台服务器有什么用!
+
+但在组织这场活动的时候,了解到大家都有一个共同的问题: **这个服务器究竟有啥用??**
+
+这真是一个好问题,而且我一句两句还说不清楚,所以就专门发文来讲一讲。
+
+同时我还录制的一期视频,哈哈我的视频号,大家可以关注一波。
+
+
+一说到服务器,可能很多人都说搞分布式,做计算,搞爬虫,做程序后台服务,多人合作等等。
+
+其实这些普通人都用不上,我来说一说大家能用上的吧。
+
+## 搭建git私服
+
+大家平时工作的时候一定有一个自己的工作文件夹,学生的话就是自己的课件,考试,准备面试的资料等等。
+
+已经工作的录友,会有一个文件夹放着自己重要的文档,Markdown,图片,简历等等。
+
+这么重要的文件夹,而且我们每天都要更新,也担心哪天电脑丢了,或者坏了,突然这些都不见了。
+
+所以我们想备份嘛。
+
+还有就是我们经常个人电脑和工作电脑要同步一些私人资料,而不是用微信传来传去。
+
+这些都是git私服的使用场景,而且很好用。
+
+大家也知道 github,gitee也可以搞私人仓库 用来备份,同步文件,但自己的文档可能放着很多重要的信息,包括自己的各种密码,密钥之类的,放到上面未必安全。你就不怕哪些重大bug把你的信息都泄漏了么[机智]
+
+更关键的是,github 和 gitee都限速的。毕竟人家的功能定位并不是网盘。
+
+项目里有大文件(几百M以上),例如pdf,ppt等等 其上传和下载速度会让你窒息。
+
+**后面我会发文专门来讲一讲,如何大家git私服!**
+
+## 搞一个文件存储
+
+这个可以用来生成文件的下载链接,也可以把本地文件传到服务器上。
+
+相当于自己做一个对象存储,其实云厂商也有对象存储的产品。
+
+不过我们自己也可以做一个,不够很多很同学应该都不知道对象存储怎么用吧,其实我们用服务器可以自己做一个类似的公司。
+
+我现在就用自己用go写的一个工具,部署在服务器上。 用来和服务器传文件,或者生成一些文件的临时下载链接。
+
+这些都是直接命令行操作的,
+
+操作方式这样,我把命令包 包装成一个shell命令,想传那个文件,直接 uploadtomyserver,然后就返回可以下载的链接,这个文件也同时传到了我的服务器上。
+
+
+
+我也把我的项目代码放在了github上:
+
+https://github.com/youngyangyang04/fileHttpServer
+
+感兴趣的录友可以去学习一波,顺便给个star 哈哈
+
+
+## 网站
+
+做网站,例如 大家知道用html 写几行代码,就可以生成一个网页,但怎么给别人展示呢?
+
+大家如果用自己的电脑做服务器,只能同一个路由器下的设备可以访问你的网站,可能这个设备出了这个屋子 都访问不了你的网站了。
+
+因为你的IP不是公网IP。
+
+如果有了一台云服务器,都是配公网IP,你的网站就可以让任何人访问了。
+
+或者说 你提供的一个服务就可以让任何人使用。
+
+例如第二个例子中,我们可以自己开发一个文件存储,这个服务,我只把把命令行给其他人,其他人都可以使用我的服务来生成链接,当然他们的文件也都传到了我的服务器上。
+
+再说一个使用场景。
+
+我之前在组织免费里服务器的活动的时候,阿里云给我一个excel,让面就是从我这里买服务器录友的名单,我直接把这个名单甩到群里,让大家自己检查,出现在名单里就可以找我返现,这样做是不是也可以。
+
+这么做有几个很大的问题:
+* 大家都要去下载excel,做对比,会有人改excel的内容然后就说是从你这里买的,我不可能挨个去比较excel有没有改动
+* excel有其他人的个人信息,这是不能暴漏的。
+* 如果每个人自己用excel查询,私信我返现,一个将近两千人找我返现,我微信根本处理不过来,这就变成体力活了。
+
+那应该怎么做呢,
+
+我就简单写一个查询的页面,后端逻辑就是读一个execel表格,大家在查询页面输入自己的阿里云ID,如果在excel里,页面就会返回返现群的二维码,大家就可以自主扫码加群了。
+
+这样,我最后就直接在返现群里 发等额红包就好了,是不是极大降低人力成本了
+
+当然我是把 17个返现群的二维码都生成好了,按照一定的规则,展现给查询通过的录友。
+
+就是这样一个非常普通的查询页面。
+
+
+
+查询通过之后,就会展现返现群二维码。
+
+
+
+但要部署在服务器上,因为没有公网IP,别人用不了你的服务。
+
+
+## 学习linux
+
+学习linux其实在自己的电脑上搞一台虚拟机,或者安装双系统也可以学习,不过这很考验你的电脑性能如何了。
+
+如果你有一个服务器,那就是独立的一台电脑,你怎么霍霍就怎么霍霍,而且一年都不用关机的,可以一直跑你的任务,和你本地电脑也完全隔离。
+
+更方便的是,你目前系统假如是centos,想做一个实验需要在unbantu上,如果是云服务器,更换系统就是在 后台点一下,一键重装,云厂商基本都是支持所有系统一件安装的。
+
+我们平时自己玩linux经常是配各种环境,然后这个linux就被自己玩坏了(一般都是毫无节制使用root权限导致的),总之就是环境配不起来了,基本就要重装了。
+
+那云服务器重装系统可太方便了。
+
+还有就是加入你好不容易配好的环境,如果以后把这个环境玩坏了,你先回退这之前配好的环境而不是重装系统在重新配一遍吧。
+
+那么可以用云服务器的镜像保存功能,就是你配好环境的那一刻就可以打一个镜像包,以后如果环境坏了,直接回退到上次镜像包的状态,这是不是就很香了。
+
+
+## 总结
+
+其实云服务器还有很多其他用处,不过我就说一说大家普遍能用的上的。
+
+
+* [阿里云活动期间服务器购买](https://www.aliyun.com/minisite/goods?taskCode=shareNew2205&recordId=3641992&userCode=roof0wob)
+* [腾讯云活动期间服务器购买](https://curl.qcloud.com/EiaMXllu)
+
diff --git "a/problems/\345\205\266\344\273\226/\345\217\202\344\270\216\346\234\254\351\241\271\347\233\256.md" "b/problems/qita/\345\217\202\344\270\216\346\234\254\351\241\271\347\233\256.md"
similarity index 100%
rename from "problems/\345\205\266\344\273\226/\345\217\202\344\270\216\346\234\254\351\241\271\347\233\256.md"
rename to "problems/qita/\345\217\202\344\270\216\346\234\254\351\241\271\347\233\256.md"
From ddcd31b2a7668214e94763709d2cb1c88a731dae Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 10 Jun 2022 11:27:26 +0800
Subject: [PATCH 374/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880844.?=
=?UTF-8?q?=E6=AF=94=E8=BE=83=E5=90=AB=E9=80=80=E6=A0=BC=E7=9A=84=E5=AD=97?=
=?UTF-8?q?=E7=AC=A6=E4=B8=B2.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?=
=?UTF-8?q?ript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\345\255\227\347\254\246\344\270\262.md" | 65 +++++++++++++++++++
1 file changed, 65 insertions(+)
diff --git "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md"
index dccc5404cc..017d4cefea 100644
--- "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md"
@@ -399,6 +399,71 @@ var backspaceCompare = function(s, t) {
```
+### TypeScript
+
+> 双栈法:
+
+```typescript
+function backspaceCompare(s: string, t: string): boolean {
+ const stack1: string[] = [],
+ stack2: string[] = [];
+ for (let c of s) {
+ if (c === '#') {
+ stack1.pop();
+ } else {
+ stack1.push(c);
+ }
+ }
+ for (let c of t) {
+ if (c === '#') {
+ stack2.pop();
+ } else {
+ stack2.push(c);
+ }
+ }
+ if (stack1.length !== stack2.length) return false;
+ for (let i = 0, length = stack1.length; i < length; i++) {
+ if (stack1[i] !== stack2[i]) return false;
+ }
+ return true;
+};
+```
+
+> 双指针法:
+
+```typescript
+function backspaceCompare(s: string, t: string): boolean {
+ let sIndex: number = s.length - 1,
+ tIndex: number = t.length - 1;
+ while (true) {
+ sIndex = getIndexAfterDel(s, sIndex);
+ tIndex = getIndexAfterDel(t, tIndex);
+ if (sIndex < 0 || tIndex < 0) break;
+ if (s[sIndex] !== t[tIndex]) return false;
+ sIndex--;
+ tIndex--;
+ }
+ return sIndex === -1 && tIndex === -1;
+};
+function getIndexAfterDel(s: string, startIndex: number): number {
+ let backspaceNum: number = 0;
+ while (startIndex >= 0) {
+ // 不可消除
+ if (s[startIndex] !== '#' && backspaceNum === 0) break;
+ // 可消除
+ if (s[startIndex] === '#') {
+ backspaceNum++;
+ } else {
+ backspaceNum--;
+ }
+ startIndex--;
+ }
+ return startIndex;
+}
+```
+
+
+
-----------------------
From 18f29ef1786e41fba506bc27f35c6a83a116ba14 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 10 Jun 2022 16:28:33 +0800
Subject: [PATCH 375/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200455.=E5=88=86?=
=?UTF-8?q?=E5=8F=91=E9=A5=BC=E5=B9=B2.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...06\345\217\221\351\245\274\345\271\262.md" | 21 +++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md"
index 17db4a859e..443ab6d755 100644
--- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md"
+++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md"
@@ -296,5 +296,26 @@ int findContentChildren(int* g, int gSize, int* s, int sSize){
}
```
+### Scala
+
+```scala
+object Solution {
+ def findContentChildren(g: Array[Int], s: Array[Int]): Int = {
+ var result = 0
+ var children = g.sorted
+ var cookie = s.sorted
+ // 遍历饼干
+ var j = 0
+ for (i <- cookie.indices) {
+ if (j < children.size && cookie(i) >= children(j)) {
+ j += 1
+ result += 1
+ }
+ }
+ result
+ }
+}
+```
+
-----------------------
From 7fd311f47bf257787caeea3bcbf73ec3ce29e978 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 10 Jun 2022 20:09:14 +0800
Subject: [PATCH 376/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200376.=E6=91=86?=
=?UTF-8?q?=E5=8A=A8=E5=BA=8F=E5=88=97.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...06\345\212\250\345\272\217\345\210\227.md" | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
index 6822896e73..fb4d6eff19 100644
--- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
+++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
@@ -375,7 +375,31 @@ function wiggleMaxLength(nums: number[]): number {
};
```
+### Scala
+
+```scala
+object Solution {
+ def wiggleMaxLength(nums: Array[Int]): Int = {
+ if (nums.length <= 1) return nums.length
+ var result = 1
+ var curDiff = 0 // 当前一对的差值
+ var preDiff = 0 // 前一对的差值
+
+ for (i <- 1 until nums.length) {
+ curDiff = nums(i) - nums(i - 1) // 计算当前这一对的差值
+ // 当 curDiff > 0 的情况,preDiff <= 0
+ // 当 curDiff < 0 的情况,preDiff >= 0
+ // 这两种情况算是两个峰值
+ if ((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0)) {
+ result += 1 // 结果集加 1
+ preDiff = curDiff // 当前差值赋值给上一轮
+ }
+ }
+ result
+ }
+}
+```
-----------------------
From 7ee790f4f532e56664642163a4612c7242f0fe11 Mon Sep 17 00:00:00 2001
From: ccchooko <648646891@qq.com>
Date: Fri, 10 Jun 2022 22:34:11 +0800
Subject: [PATCH 377/551] =?UTF-8?q?0042=20=E6=8E=A5=E9=9B=A8=E6=B0=B4?=
=?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E8=B0=83=E6=A0=88go=E8=A7=A3?=
=?UTF-8?q?=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...2.\346\216\245\351\233\250\346\260\264.md" | 36 +++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md"
index b232ce2206..e331967f6f 100644
--- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md"
+++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md"
@@ -640,8 +640,44 @@ func min(a,b int)int{
}
```
+单调栈解法
+```go
+func trap(height []int) int {
+ if len(height) <= 2 {
+ return 0
+ }
+ st := make([]int, 1, len(height)) // 切片模拟单调栈,st存储的是高度数组下标
+ var res int
+ for i := 1; i < len(height); i++ {
+ if height[i] < height[st[len(st)-1]] {
+ st = append(st, i)
+ } else if height[i] == height[st[len(st)-1]] {
+ st = st[:len(st)-1] // 比较的新元素和栈顶的元素相等,去掉栈中的,入栈新元素下标
+ st = append(st, i)
+ } else {
+ for len(st) != 0 && height[i] > height[st[len(st)-1]] {
+ top := st[len(st)-1]
+ st = st[:len(st)-1]
+ if len(st) != 0 {
+ tmp := (min(height[i], height[st[len(st)-1]]) - height[top]) * (i - st[len(st)-1] - 1)
+ res += tmp
+ }
+ }
+ st = append(st, i)
+ }
+ }
+ return res
+}
+func min(x, y int) int {
+ if x >= y {
+ return y
+ }
+ return x
+}
+```
+
### JavaScript:
```javascript
From 9a068272ce8fd245d0c0ef93aee689afe4ea3cce Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 10 Jun 2022 22:46:32 +0800
Subject: [PATCH 378/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880129.?=
=?UTF-8?q?=E6=B1=82=E6=A0=B9=E5=88=B0=E5=8F=B6=E5=AD=90=E8=8A=82=E7=82=B9?=
=?UTF-8?q?=E6=95=B0=E5=AD=97=E4=B9=8B=E5=92=8C.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...60\345\255\227\344\271\213\345\222\214.md" | 33 +++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md"
index b271ca7de5..a34e6921c6 100644
--- "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md"
+++ "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md"
@@ -289,7 +289,40 @@ var sumNumbers = function(root) {
};
```
+TypeScript:
+
+```typescript
+function sumNumbers(root: TreeNode | null): number {
+ if (root === null) return 0;
+ let resTotal: number = 0;
+ const route: number[] = [];
+ route.push(root.val);
+ recur(root, route);
+ return resTotal;
+ function recur(node: TreeNode, route: number[]): void {
+ if (node.left === null && node.right === null) {
+ resTotal += listToSum(route);
+ return;
+ }
+ if (node.left !== null) {
+ route.push(node.left.val);
+ recur(node.left, route);
+ route.pop();
+ };
+ if (node.right !== null) {
+ route.push(node.right.val);
+ recur(node.right, route);
+ route.pop();
+ };
+ }
+ function listToSum(nums: number[]): number {
+ return Number(nums.join(''));
+ }
+};
+```
+
C:
+
```c
//sum记录总和
int sum;
From 192beffb66d20fb51cdb61f11649e5cff6379bb8 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Fri, 10 Jun 2022 23:38:32 +0800
Subject: [PATCH 379/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881382.?=
=?UTF-8?q?=E5=B0=86=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E5=8F=98?=
=?UTF-8?q?=E5=B9=B3=E8=A1=A1.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?=
=?UTF-8?q?ript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...21\345\217\230\345\271\263\350\241\241.md" | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md"
index 57231ec46b..d4d60ebd00 100644
--- "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md"
+++ "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md"
@@ -148,6 +148,30 @@ var balanceBST = function(root) {
};
```
+TypeScript:
+
+```typescript
+function balanceBST(root: TreeNode | null): TreeNode | null {
+ const inorderArr: number[] = [];
+ inorderTraverse(root, inorderArr);
+ return buildTree(inorderArr, 0, inorderArr.length - 1);
+};
+function inorderTraverse(node: TreeNode | null, arr: number[]): void {
+ if (node === null) return;
+ inorderTraverse(node.left, arr);
+ arr.push(node.val);
+ inorderTraverse(node.right, arr);
+}
+function buildTree(arr: number[], left: number, right: number): TreeNode | null {
+ if (left > right) return null;
+ const mid = (left + right) >> 1;
+ const resNode: TreeNode = new TreeNode(arr[mid]);
+ resNode.left = buildTree(arr, left, mid - 1);
+ resNode.right = buildTree(arr, mid + 1, right);
+ return resNode;
+}
+```
+
-----------------------
From 2386694b5b67e1ec17e42ce2c1f9df784eaeab24 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 11 Jun 2022 00:43:30 +0800
Subject: [PATCH 380/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880100.?=
=?UTF-8?q?=E7=9B=B8=E5=90=8C=E7=9A=84=E6=A0=91.md=EF=BC=89=EF=BC=9A?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...70\345\220\214\347\232\204\346\240\221.md" | 40 +++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md"
index 5e805d0111..d2431f398b 100644
--- "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md"
+++ "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md"
@@ -240,6 +240,46 @@ Go:
JavaScript:
+TypeScript:
+
+> 递归法-先序遍历
+
+```typescript
+function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
+ if (p === null && q === null) return true;
+ if (p === null || q === null) return false;
+ if (p.val !== q.val) return false;
+ return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
+};
+```
+
+> 迭代法-层序遍历
+
+```typescript
+function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
+ const queue1: (TreeNode | null)[] = [],
+ queue2: (TreeNode | null)[] = [];
+ queue1.push(p);
+ queue2.push(q);
+ while (queue1.length > 0 && queue2.length > 0) {
+ const node1 = queue1.shift(),
+ node2 = queue2.shift();
+ if (node1 === null && node2 === null) continue;
+ if (
+ (node1 === null || node2 === null) ||
+ node1!.val !== node2!.val
+ ) return false;
+ queue1.push(node1!.left);
+ queue1.push(node1!.right);
+ queue2.push(node2!.left);
+ queue2.push(node2!.right);
+ }
+ return true;
+};
+```
+
+
+
-----------------------
From bd4f69d04b663dae39ab5a384722cd7d5aae3377 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 11 Jun 2022 16:35:06 +0800
Subject: [PATCH 381/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200053.=E6=9C=80?=
=?UTF-8?q?=E5=A4=A7=E5=AD=90=E5=BA=8F=E5=92=8C=EF=BC=88=E5=8A=A8=E6=80=81?=
=?UTF-8?q?=E8=A7=84=E5=88=92=EF=BC=89.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\201\350\247\204\345\210\222\357\274\211.md" | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
index 4c883cb66c..6712d0acca 100644
--- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
+++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
@@ -186,7 +186,22 @@ const maxSubArray = nums => {
};
```
-
+Scala:
+
+```scala
+object Solution {
+ def maxSubArray(nums: Array[Int]): Int = {
+ var dp = new Array[Int](nums.length)
+ var result = nums(0)
+ dp(0) = nums(0)
+ for (i <- 1 until nums.length) {
+ dp(i) = math.max(nums(i), dp(i - 1) + nums(i))
+ result = math.max(result, dp(i)) // 更新最大值
+ }
+ result
+ }
+}
+```
-----------------------
From 9031523bbb334cb2190b17f12333d3a5f1edda53 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 11 Jun 2022 16:40:33 +0800
Subject: [PATCH 382/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200053.=E6=9C=80?=
=?UTF-8?q?=E5=A4=A7=E5=AD=90=E5=BA=8F=E5=92=8C.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\345\255\220\345\272\217\345\222\214.md" | 33 +++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
index 73cac244db..4971133ffd 100644
--- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
+++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
@@ -333,8 +333,41 @@ function maxSubArray(nums: number[]): number {
};
```
+### Scala
+**贪心**
+
+```scala
+object Solution {
+ def maxSubArray(nums: Array[Int]): Int = {
+ var result = Int.MinValue
+ var count = 0
+ for (i <- nums.indices) {
+ count += nums(i) // count累加
+ if (count > result) result = count // 记录最大值
+ if (count <= 0) count = 0 // 一旦count为负,则count归0
+ }
+ result
+ }
+}
+```
+**动态规划**
+
+```scala
+object Solution {
+ def maxSubArray(nums: Array[Int]): Int = {
+ var dp = new Array[Int](nums.length)
+ var result = nums(0)
+ dp(0) = nums(0)
+ for (i <- 1 until nums.length) {
+ dp(i) = math.max(nums(i), dp(i - 1) + nums(i))
+ result = math.max(result, dp(i)) // 更新最大值
+ }
+ result
+ }
+}
+```
-----------------------
From ff233a8e3ccfb08e82ebaa662c93bf687b76f153 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 11 Jun 2022 16:53:35 +0800
Subject: [PATCH 383/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200122.=E4=B9=B0?=
=?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?=
=?UTF-8?q?=E6=9C=BAII.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\344\275\263\346\227\266\346\234\272II.md" | 35 ++++++++++++++-----
1 file changed, 27 insertions(+), 8 deletions(-)
diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
index 1e7b77d80a..1369ff5ba5 100644
--- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
+++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
@@ -133,8 +133,9 @@ public:
## 其他语言版本
-Java:
+### Java:
+贪心:
```java
// 贪心思路
class Solution {
@@ -148,6 +149,7 @@ class Solution {
}
```
+动态规划:
```java
class Solution { // 动态规划
public int maxProfit(int[] prices) {
@@ -169,8 +171,8 @@ class Solution { // 动态规划
}
```
-Python:
-
+### Python:
+贪心:
```python
class Solution:
def maxProfit(self, prices: List[int]) -> int:
@@ -180,7 +182,7 @@ class Solution:
return result
```
-python动态规划
+动态规划:
```python
class Solution:
def maxProfit(self, prices: List[int]) -> int:
@@ -194,7 +196,7 @@ class Solution:
return dp[-1][1]
```
-Go:
+### Go:
```golang
//贪心算法
@@ -231,7 +233,7 @@ func maxProfit(prices []int) int {
}
```
-Javascript:
+### Javascript:
贪心
```Javascript
@@ -268,7 +270,7 @@ const maxProfit = (prices) => {
};
```
-TypeScript:
+### TypeScript:
```typescript
function maxProfit(prices: number[]): number {
@@ -280,7 +282,7 @@ function maxProfit(prices: number[]): number {
};
```
-C:
+### C:
贪心:
```c
int maxProfit(int* prices, int pricesSize){
@@ -318,5 +320,22 @@ int maxProfit(int* prices, int pricesSize){
}
```
+### Scala
+
+贪心:
+```scala
+object Solution {
+ def maxProfit(prices: Array[Int]): Int = {
+ var result = 0
+ for (i <- 1 until prices.length) {
+ if (prices(i) > prices(i - 1)) {
+ result += prices(i) - prices(i - 1)
+ }
+ }
+ result
+ }
+}
+```
+
-----------------------
From b0664fcb81c475356ddf925b839df6e00c434e8a Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 11 Jun 2022 17:21:03 +0800
Subject: [PATCH 384/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200055.=E8=B7=B3?=
=?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8F.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\263\350\267\203\346\270\270\346\210\217.md" | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md"
index 17a3b4f4d3..345f8ebadc 100644
--- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md"
+++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md"
@@ -193,7 +193,22 @@ function canJump(nums: number[]): boolean {
};
```
-
+### Scala
+```scala
+object Solution {
+ def canJump(nums: Array[Int]): Boolean = {
+ var cover = 0
+ if (nums.length == 1) return true // 如果只有一个元素,那么必定到达
+ var i = 0
+ while (i <= cover) { // i表示下标,当前只能够走cover步
+ cover = math.max(i + nums(i), cover)
+ if (cover >= nums.length - 1) return true // 说明可以覆盖到终点,直接返回
+ i += 1
+ }
+ false // 如果上面没有返回就是跳不到
+ }
+}
+```
-----------------------
From ad24b1fb1fa50e568a7357bbe772df59fc008e74 Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sat, 11 Jun 2022 18:29:48 +0800
Subject: [PATCH 385/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880116.?=
=?UTF-8?q?=E5=A1=AB=E5=85=85=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84?=
=?UTF-8?q?=E4=B8=8B=E4=B8=80=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9?=
=?UTF-8?q?=E6=8C=87=E9=92=88.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?=
=?UTF-8?q?ript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...02\347\202\271\346\214\207\351\222\210.md" | 73 +++++++++++++++++++
1 file changed, 73 insertions(+)
diff --git "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md"
index 2c443de52a..1e5b227167 100644
--- "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md"
+++ "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md"
@@ -287,6 +287,79 @@ const connect = root => {
};
```
+## TypeScript
+
+(注:命名空间‘Node’与typescript中内置类型冲突,这里改成了‘NodePro’)
+
+> 递归法:
+
+```typescript
+class NodePro {
+ val: number
+ left: NodePro | null
+ right: NodePro | null
+ next: NodePro | null
+ constructor(val?: number, left?: NodePro, right?: NodePro, next?: NodePro) {
+ this.val = (val === undefined ? 0 : val)
+ this.left = (left === undefined ? null : left)
+ this.right = (right === undefined ? null : right)
+ this.next = (next === undefined ? null : next)
+ }
+}
+
+function connect(root: NodePro | null): NodePro | null {
+ if (root === null) return null;
+ root.next = null;
+ recur(root);
+ return root;
+};
+function recur(node: NodePro): void {
+ if (node.left === null || node.right === null) return;
+ node.left.next = node.right;
+ node.right.next = node.next && node.next.left;
+ recur(node.left);
+ recur(node.right);
+}
+```
+
+> 迭代法:
+
+```typescript
+class NodePro {
+ val: number
+ left: NodePro | null
+ right: NodePro | null
+ next: NodePro | null
+ constructor(val?: number, left?: NodePro, right?: NodePro, next?: NodePro) {
+ this.val = (val === undefined ? 0 : val)
+ this.left = (left === undefined ? null : left)
+ this.right = (right === undefined ? null : right)
+ this.next = (next === undefined ? null : next)
+ }
+}
+
+function connect(root: NodePro | null): NodePro | null {
+ if (root === null) return null;
+ const queue: NodePro[] = [];
+ queue.push(root);
+ while (queue.length > 0) {
+ for (let i = 0, length = queue.length; i < length; i++) {
+ const curNode: NodePro = queue.shift()!;
+ if (i === length - 1) {
+ curNode.next = null;
+ } else {
+ curNode.next = queue[0];
+ }
+ if (curNode.left !== null) queue.push(curNode.left);
+ if (curNode.right !== null) queue.push(curNode.right);
+ }
+ }
+ return root;
+};
+```
+
+
+
-----------------------
From 74a422c53b2817271b8f50a61bdd8ccdf77a5e25 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 11 Jun 2022 20:03:12 +0800
Subject: [PATCH 386/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200045.=E8=B7=B3?=
=?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8FII.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\350\267\203\346\270\270\346\210\217II.md" | 26 ++++++++++++++++++-
1 file changed, 25 insertions(+), 1 deletion(-)
diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
index 4e3ab24a83..f29403615f 100644
--- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
+++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
@@ -279,7 +279,31 @@ function jump(nums: number[]): number {
};
```
-
+### Scala
+
+```scala
+object Solution {
+ def jump(nums: Array[Int]): Int = {
+ if (nums.length == 0) return 0
+ var result = 0 // 记录走的最大步数
+ var curDistance = 0 // 当前覆盖最远距离下标
+ var nextDistance = 0 // 下一步覆盖最远距离下标
+ for (i <- nums.indices) {
+ nextDistance = math.max(nums(i) + i, nextDistance) // 更新下一步覆盖最远距离下标
+ if (i == curDistance) {
+ if (curDistance != nums.length - 1) {
+ result += 1
+ curDistance = nextDistance
+ if (nextDistance >= nums.length - 1) return result
+ } else {
+ return result
+ }
+ }
+ }
+ result
+ }
+}
+```
From 67f74cdc098c7e7b7e012dec82d626731a59b051 Mon Sep 17 00:00:00 2001
From: tianzhou
Date: Sat, 11 Jun 2022 23:08:31 +0800
Subject: [PATCH 387/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=9E=E6=97=B6=20?=
=?UTF-8?q?star=20history=20=E5=9B=BE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/README.md b/README.md
index 4e2993d861..620942dca6 100644
--- a/README.md
+++ b/README.md
@@ -523,6 +523,10 @@
[点此这里](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)查看LeetCode-Master的所有贡献者。感谢他们补充了LeetCode-Master的其他语言版本,让更多的读者收益于此项目。
+# Star 趋势
+
+[](https://star-history.com/#youngyangyang04/leetcode-master&Date)
+
# 关于作者
大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发,CSDN博客专家。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。
From 5d4046cacb2693a4e67fc311e2e83f73275d4909 Mon Sep 17 00:00:00 2001
From: guangyusong <15316444+guangyusong@users.noreply.github.com>
Date: Sat, 11 Jun 2022 15:22:31 -0400
Subject: [PATCH 388/551] =?UTF-8?q?=E6=9B=B4=E6=96=B0server.md=E4=B8=ADUbu?=
=?UTF-8?q?ntu=E7=9A=84=E6=8B=BC=E5=86=99=E9=94=99=E8=AF=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/qita/server.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/problems/qita/server.md b/problems/qita/server.md
index 16995d703d..0748c104c2 100644
--- a/problems/qita/server.md
+++ b/problems/qita/server.md
@@ -105,7 +105,7 @@ https://github.com/youngyangyang04/fileHttpServer
如果你有一个服务器,那就是独立的一台电脑,你怎么霍霍就怎么霍霍,而且一年都不用关机的,可以一直跑你的任务,和你本地电脑也完全隔离。
-更方便的是,你目前系统假如是centos,想做一个实验需要在unbantu上,如果是云服务器,更换系统就是在 后台点一下,一键重装,云厂商基本都是支持所有系统一件安装的。
+更方便的是,你目前系统假如是CentOS,想做一个实验需要在Ubuntu上,如果是云服务器,更换系统就是在 后台点一下,一键重装,云厂商基本都是支持所有系统一件安装的。
我们平时自己玩linux经常是配各种环境,然后这个linux就被自己玩坏了(一般都是毫无节制使用root权限导致的),总之就是环境配不起来了,基本就要重装了。
From 882c19c3e263161a18d4349f53ee2226a09a3c6b Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 12 Jun 2022 10:17:49 +0800
Subject: [PATCH 389/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201005.K=E6=AC=A1?=
=?UTF-8?q?=E5=8F=96=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84?=
=?UTF-8?q?=E6=95=B0=E7=BB=84=E5=92=8C.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...04\346\225\260\347\273\204\345\222\214.md" | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md"
index 202534da8c..71fc628f7e 100644
--- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md"
+++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md"
@@ -289,6 +289,28 @@ function largestSumAfterKNegations(nums: number[], k: number): number {
};
```
+### Scala
+
+```scala
+object Solution {
+ def largestSumAfterKNegations(nums: Array[Int], k: Int): Int = {
+ var num = nums.sortWith(math.abs(_) > math.abs(_))
+
+ var kk = k // 因为k是不可变量,所以要赋值给一个可变量
+ for (i <- num.indices) {
+ if (num(i) < 0 && kk > 0) {
+ num(i) *= -1 // 取反
+ kk -= 1
+ }
+ }
+
+ // kk对2取余,结果为0则为偶数不需要取反,结果为1为奇数,只需要对最后的数字进行反转就可以
+ if (kk % 2 == 1) num(num.size - 1) *= -1
+
+ num.sum // 最后返回数字的和
+ }
+}
+```
From dd20ca032fab47e0e05f9d32c05f528872dcf83a Mon Sep 17 00:00:00 2001
From: Steve2020 <841532108@qq.com>
Date: Sun, 12 Jun 2022 12:17:26 +0800
Subject: [PATCH 390/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880052.N?=
=?UTF-8?q?=E7=9A=87=E5=90=8EII.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0type?=
=?UTF-8?q?script=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0052.N\347\232\207\345\220\216II.md" | 54 +++++++++++++++++++
1 file changed, 54 insertions(+)
diff --git "a/problems/0052.N\347\232\207\345\220\216II.md" "b/problems/0052.N\347\232\207\345\220\216II.md"
index 67e439ca81..608aeda172 100644
--- "a/problems/0052.N\347\232\207\345\220\216II.md"
+++ "b/problems/0052.N\347\232\207\345\220\216II.md"
@@ -144,7 +144,61 @@ var totalNQueens = function(n) {
};
```
+TypeScript:
+
+```typescript
+// 0-该格为空,1-该格有皇后
+type GridStatus = 0 | 1;
+function totalNQueens(n: number): number {
+ let resCount: number = 0;
+ const chess: GridStatus[][] = new Array(n).fill(0)
+ .map(_ => new Array(n).fill(0));
+ backTracking(chess, n, 0);
+ return resCount;
+ function backTracking(chess: GridStatus[][], n: number, startRowIndex: number): void {
+ if (startRowIndex === n) {
+ resCount++;
+ return;
+ }
+ for (let j = 0; j < n; j++) {
+ if (checkValid(chess, startRowIndex, j, n) === true) {
+ chess[startRowIndex][j] = 1;
+ backTracking(chess, n, startRowIndex + 1);
+ chess[startRowIndex][j] = 0;
+ }
+ }
+ }
+};
+function checkValid(chess: GridStatus[][], i: number, j: number, n: number): boolean {
+ // 向上纵向检查
+ let tempI: number = i - 1,
+ tempJ: number = j;
+ while (tempI >= 0) {
+ if (chess[tempI][tempJ] === 1) return false;
+ tempI--;
+ }
+ // 斜向左上检查
+ tempI = i - 1;
+ tempJ = j - 1;
+ while (tempI >= 0 && tempJ >= 0) {
+ if (chess[tempI][tempJ] === 1) return false;
+ tempI--;
+ tempJ--;
+ }
+ // 斜向右上检查
+ tempI = i - 1;
+ tempJ = j + 1;
+ while (tempI >= 0 && tempJ < n) {
+ if (chess[tempI][tempJ] === 1) return false;
+ tempI--;
+ tempJ++;
+ }
+ return true;
+}
+```
+
C
+
```c
//path[i]为在i行,path[i]列上存在皇后
int *path;
From 1da68a332c35b178d62edf946fd7a4da4ac98d3c Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 12 Jun 2022 13:46:58 +0800
Subject: [PATCH 391/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200134.=E5=8A=A0?=
=?UTF-8?q?=E6=B2=B9=E7=AB=99.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4.\345\212\240\346\262\271\347\253\231.md" | 68 +++++++++++++++++++
1 file changed, 68 insertions(+)
diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md"
index a88f677d34..541be2930d 100644
--- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md"
+++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md"
@@ -471,5 +471,73 @@ int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){
}
```
+### Scala
+
+暴力解法:
+
+```scala
+object Solution {
+ def canCompleteCircuit(gas: Array[Int], cost: Array[Int]): Int = {
+ for (i <- cost.indices) {
+ var rest = gas(i) - cost(i)
+ var index = (i + 1) % cost.length // index为i的下一个节点
+ while (rest > 0 && i != index) {
+ rest += (gas(index) - cost(index))
+ index = (index + 1) % cost.length
+ }
+ if (rest >= 0 && index == i) return i
+ }
+ -1
+ }
+}
+```
+
+贪心算法,方法一:
+
+```scala
+object Solution {
+ def canCompleteCircuit(gas: Array[Int], cost: Array[Int]): Int = {
+ var curSum = 0
+ var min = Int.MaxValue
+ for (i <- gas.indices) {
+ var rest = gas(i) - cost(i)
+ curSum += rest
+ min = math.min(min, curSum)
+ }
+ if (curSum < 0) return -1 // 情况1: gas的总和小于cost的总和,不可能到达终点
+ if (min >= 0) return 0 // 情况2: 最小值>=0,从0号出发可以直接到达
+ // 情况3: min为负值,从后向前看,能把min填平的节点就是出发节点
+ for (i <- gas.length - 1 to 0 by -1) {
+ var rest = gas(i) - cost(i)
+ min += rest
+ if (min >= 0) return i
+ }
+ -1
+ }
+}
+```
+
+贪心算法,方法二:
+
+```scala
+object Solution {
+ def canCompleteCircuit(gas: Array[Int], cost: Array[Int]): Int = {
+ var curSum = 0
+ var totalSum = 0
+ var start = 0
+ for (i <- gas.indices) {
+ curSum += (gas(i) - cost(i))
+ totalSum += (gas(i) - cost(i))
+ if (curSum < 0) {
+ start = i + 1 // 起始位置更新
+ curSum = 0 // curSum从0开始
+ }
+ }
+ if (totalSum < 0) return -1 // 说明怎么走不可能跑一圈
+ start
+ }
+}
+```
+
-----------------------
From 998785bcab1346d652b8b3db9ccd166054194a83 Mon Sep 17 00:00:00 2001
From: Vincent
Date: Sun, 12 Jun 2022 16:59:08 -0700
Subject: [PATCH 392/551] =?UTF-8?q?Update=200034.=E5=9C=A8=E6=8E=92?=
=?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE=E5=85=83?=
=?UTF-8?q?=E7=B4=A0=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C=E6=9C=80?=
=?UTF-8?q?=E5=90=8E=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加力扣原题链接
---
...\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" | 2 ++
1 file changed, 2 insertions(+)
diff --git "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md"
index 260462c23b..b6e82262ff 100644
--- "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md"
+++ "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md"
@@ -7,6 +7,8 @@
# 34. 在排序数组中查找元素的第一个和最后一个位置
+[力扣链接](https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/)
+
给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。
如果数组中不存在目标值 target,返回 [-1, -1]。
From 0a3a2d4ae2213cad03d8f339f09f4e9de7420998 Mon Sep 17 00:00:00 2001
From: Rinko Taketsuki <33001553+RinkoTaketsuki@users.noreply.github.com>
Date: Mon, 13 Jun 2022 09:27:00 +0800
Subject: [PATCH 393/551] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D?=
=?UTF-8?q?=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
index a51c68eea7..1d8a0e643c 100644
--- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
@@ -49,7 +49,7 @@
数组长度为:len。
-如果len % (len - (next[len - 1] + 1)) == 0 ,则说明 (数组长度-最长相等前后缀的长度) 正好可以被 数组的长度整除,说明有该字符串有重复的子字符串。
+如果len % (len - (next[len - 1] + 1)) == 0 ,则说明数组的长度正好可以被 (数组长度-最长相等前后缀的长度) 整除 ,说明该字符串有重复的子字符串。
**数组长度减去最长相同前后缀的长度相当于是第一个周期的长度,也就是一个周期的长度,如果这个周期可以被整除,就说明整个数组就是这个周期的循环。**
From 2c1aa6ebf83cca21a8c6536922f2d652fa45c5cc Mon Sep 17 00:00:00 2001
From: programmercarl <826123027@qq.com>
Date: Mon, 13 Jun 2022 17:23:30 +0800
Subject: [PATCH 394/551] Update
---
...2.\345\277\253\344\271\220\346\225\260.md" | 1 +
...04\345\255\220\346\225\260\347\273\204.md" | 29 +++++++++++++++----
...04\347\232\204\344\272\244\351\233\206.md" | 2 ++
...3.\350\265\216\351\207\221\344\277\241.md" | 1 +
...\346\225\260\347\233\270\345\212\240II.md" | 1 +
problems/qita/server.md | 3 ++
6 files changed, 31 insertions(+), 6 deletions(-)
diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md"
index be8686f7e1..7738b2f695 100644
--- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md"
+++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md"
@@ -417,6 +417,7 @@ object Solution {
}
sum
}
+```
C#:
diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
index fbef7692cd..69e0da4f1d 100644
--- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
+++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
@@ -5,7 +5,7 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
-## 209.长度最小的子数组
+# 209.长度最小的子数组
[力扣题目链接](https://leetcode-cn.com/problems/minimum-size-subarray-sum/)
@@ -17,6 +17,9 @@
输出:2
解释:子数组 [4,3] 是该条件下的长度最小的子数组。
+# 思路
+
+为了易于大家理解,我特意录制了[拿下滑动窗口! | LeetCode 209 长度最小的子数组](https://www.bilibili.com/video/BV1tZ4y1q7XE)
## 暴力解法
@@ -47,8 +50,8 @@ public:
}
};
```
-时间复杂度:O(n^2)
-空间复杂度:O(1)
+* 时间复杂度:O(n^2)
+* 空间复杂度:O(1)
## 滑动窗口
@@ -56,6 +59,20 @@ public:
所谓滑动窗口,**就是不断的调节子序列的起始位置和终止位置,从而得出我们要想的结果**。
+在暴力解法中,是一个for循环滑动窗口的起始位置,一个for循环为滑动窗口的终止位置,用两个for循环 完成了一个不断搜索区间的过程。
+
+那么滑动窗口如何用一个for循环来完成这个操作呢。
+
+首先要思考 如果用一个for循环,那么应该表示 滑动窗口的起始位置,还是终止位置。
+
+如果只用一个for循环来表示 滑动窗口的起始位置,那么如何遍历剩下的终止位置?
+
+此时难免再次陷入 暴力解法的怪圈。
+
+所以 只用一个for循环,那么这个循环的索引,一定是表示 滑动窗口的终止位置。
+
+那么问题来了, 滑动窗口的起始位置如何移动呢?
+
这里还是以题目中的示例来举例,s=7, 数组是 2,3,1,2,4,3,来看一下查找的过程:

@@ -74,7 +91,7 @@ public:
窗口的起始位置如何移动:如果当前窗口的值大于s了,窗口就要向前移动了(也就是该缩小了)。
-窗口的结束位置如何移动:窗口的结束位置就是遍历数组的指针,窗口的起始位置设置为数组的起始位置就可以了。
+窗口的结束位置如何移动:窗口的结束位置就是遍历数组的指针,也就是for循环里的索引。
解题的关键在于 窗口的起始位置如何移动,如图所示:
@@ -107,8 +124,8 @@ public:
};
```
-时间复杂度:O(n)
-空间复杂度:O(1)
+* 时间复杂度:O(n)
+* 空间复杂度:O(1)
**一些录友会疑惑为什么时间复杂度是O(n)**。
diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
index f7dab3d7fa..4fbdd41460 100644
--- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
+++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
@@ -356,6 +356,8 @@ object Solution {
}
}
+```
+
C#:
```csharp
diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md"
index 75dafb7201..9c3dda8c4c 100644
--- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md"
+++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md"
@@ -425,6 +425,7 @@ object Solution {
true
}
}
+```
C#:
diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md"
index bfdee26e29..726fdb1569 100644
--- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md"
+++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md"
@@ -354,6 +354,7 @@ object Solution {
res
}
}
+```
C#:
```csharp
diff --git a/problems/qita/server.md b/problems/qita/server.md
index 16995d703d..1d7a1d6b92 100644
--- a/problems/qita/server.md
+++ b/problems/qita/server.md
@@ -1,6 +1,9 @@
# 一台服务器有什么用!
+* [阿里云活动期间服务器购买](https://www.aliyun.com/minisite/goods?taskCode=shareNew2205&recordId=3641992&userCode=roof0wob)
+* [腾讯云活动期间服务器购买](https://curl.qcloud.com/EiaMXllu)
+
但在组织这场活动的时候,了解到大家都有一个共同的问题: **这个服务器究竟有啥用??**
这真是一个好问题,而且我一句两句还说不清楚,所以就专门发文来讲一讲。
From 32018ff79afa8fb65cdf69445c9d93a34e648d54 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 13 Jun 2022 20:53:11 +0800
Subject: [PATCH 395/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200135.=E5=88=86?=
=?UTF-8?q?=E5=8F=91=E7=B3=96=E6=9E=9C.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...06\345\217\221\347\263\226\346\236\234.md" | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md"
index 3456a04c29..a805d0d446 100644
--- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md"
+++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md"
@@ -324,6 +324,31 @@ function candy(ratings: number[]): number {
};
```
+### Scala
+
+```scala
+object Solution {
+ def candy(ratings: Array[Int]): Int = {
+ var candyVec = new Array[Int](ratings.length)
+ for (i <- candyVec.indices) candyVec(i) = 1
+ // 从前向后
+ for (i <- 1 until candyVec.length) {
+ if (ratings(i) > ratings(i - 1)) {
+ candyVec(i) = candyVec(i - 1) + 1
+ }
+ }
+
+ // 从后向前
+ for (i <- (candyVec.length - 2) to 0 by -1) {
+ if (ratings(i) > ratings(i + 1)) {
+ candyVec(i) = math.max(candyVec(i), candyVec(i + 1) + 1)
+ }
+ }
+
+ candyVec.sum // 求和
+ }
+}
+```
-----------------------
From 6867c9c5bfd379a2f6e8afb09026cb0e759bc8dc Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 13 Jun 2022 21:21:22 +0800
Subject: [PATCH 396/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200860.=E6=9F=A0?=
=?UTF-8?q?=E6=AA=AC=E6=B0=B4=E6=89=BE=E9=9B=B6.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...54\346\260\264\346\211\276\351\233\266.md" | 31 +++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md"
index aa09e1c67d..4a676b43a2 100644
--- "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md"
+++ "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md"
@@ -328,6 +328,37 @@ function lemonadeChange(bills: number[]): boolean {
```
+### Scala
+
+```scala
+object Solution {
+ def lemonadeChange(bills: Array[Int]): Boolean = {
+ var fiveNum = 0
+ var tenNum = 0
+
+ for (i <- bills) {
+ if (i == 5) fiveNum += 1
+ if (i == 10) {
+ if (fiveNum <= 0) return false
+ tenNum += 1
+ fiveNum -= 1
+ }
+ if (i == 20) {
+ if (fiveNum > 0 && tenNum > 0) {
+ tenNum -= 1
+ fiveNum -= 1
+ } else if (fiveNum >= 3) {
+ fiveNum -= 3
+ } else {
+ return false
+ }
+ }
+ }
+ true
+ }
+}
+```
+
-----------------------
From 17208d89290c4bc10256414393671022b6b96ccf Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 13 Jun 2022 22:12:49 +0800
Subject: [PATCH 397/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200406.=E6=A0=B9?=
=?UTF-8?q?=E6=8D=AE=E8=BA=AB=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...15\345\273\272\351\230\237\345\210\227.md" | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md"
index 641086a93c..516df7d7fc 100644
--- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md"
+++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md"
@@ -354,8 +354,27 @@ function reconstructQueue(people: number[][]): number[][] {
};
```
+### Scala
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def reconstructQueue(people: Array[Array[Int]]): Array[Array[Int]] = {
+ val person = people.sortWith((a, b) => {
+ if (a(0) == b(0)) a(1) < b(1)
+ else a(0) > b(0)
+ })
+
+ var que = mutable.ArrayBuffer[Array[Int]]()
+ for (per <- person) {
+ que.insert(per(1), per)
+ }
+ que.toArray
+ }
+}
+```
-----------------------
From e391bf18f7f0c607a918413d26866a9f376ced1e Mon Sep 17 00:00:00 2001
From: van_fantasy <46948123+sexyxlyWAol@users.noreply.github.com>
Date: Mon, 13 Jun 2022 22:44:49 +0800
Subject: [PATCH 398/551] =?UTF-8?q?Update=201221.=E5=88=86=E5=89=B2?=
=?UTF-8?q?=E5=B9=B3=E8=A1=A1=E5=AD=97=E7=AC=A6=E4=B8=B2.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
added python and go solution to 1221
---
...41\345\255\227\347\254\246\344\270\262.md" | 27 +++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git "a/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" "b/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md"
index 1a9b34a29e..e18c6358d8 100644
--- "a/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md"
@@ -108,11 +108,38 @@ class Solution {
### Python
```python
+class Solution:
+ def balancedStringSplit(self, s: str) -> int:
+ diff = 0 #右左差值
+ ans = 0
+ for c in s:
+ if c == "L":
+ diff -= 1
+ else:
+ diff += 1
+ if tilt == 0:
+ ans += 1
+ return ans
```
### Go
```go
+func balancedStringSplit(s string) int {
+ diff := 0 // 右左差值
+ ans := 0
+ for _, c := range s {
+ if c == 'L' {
+ diff--
+ }else {
+ diff++
+ }
+ if diff == 0 {
+ ans++
+ }
+ }
+ return ans
+}
```
### JavaScript
From 8f52696f2206f2361917aad3e1cc7f87698c5cc0 Mon Sep 17 00:00:00 2001
From: Rinko Taketsuki <33001553+RinkoTaketsuki@users.noreply.github.com>
Date: Tue, 14 Jun 2022 09:24:58 +0800
Subject: [PATCH 399/551] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D?=
=?UTF-8?q?=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
index 1d8a0e643c..ba2fa16023 100644
--- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
@@ -63,7 +63,7 @@
next[len - 1] = 7,next[len - 1] + 1 = 8,8就是此时字符串asdfasdfasdf的最长相同前后缀的长度。
-(len - (next[len - 1] + 1)) 也就是: 12(字符串的长度) - 8(最长公共前后缀的长度) = 4, 4正好可以被 12(字符串的长度) 整除,所以说明有重复的子字符串(asdf)。
+(len - (next[len - 1] + 1)) 也就是: 12(字符串的长度) - 8(最长公共前后缀的长度) = 4, 12 正好可以被 4 整除,所以说明有重复的子字符串(asdf)。
C++代码如下:(这里使用了前缀表统一减一的实现方式)
From e8a8db47cfc42380b0979e8efcd7f9908216b2a6 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 14 Jun 2022 20:03:34 +0800
Subject: [PATCH 400/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200452.=E7=94=A8?=
=?UTF-8?q?=E6=9C=80=E5=B0=91=E6=95=B0=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95?=
=?UTF-8?q?=E7=88=86=E6=B0=94=E7=90=83.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...25\347\210\206\346\260\224\347\220\203.md" | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md"
index d4bbe961ee..e07aa6e699 100644
--- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md"
+++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md"
@@ -288,5 +288,30 @@ impl Solution {
}
}
```
+
+### Scala
+
+```scala
+object Solution {
+ def findMinArrowShots(points: Array[Array[Int]]): Int = {
+ if (points.length == 0) return 0
+ // 排序
+ var point = points.sortWith((a, b) => {
+ a(0) < b(0)
+ })
+
+ var result = 1 // points不为空就至少需要一只箭
+ for (i <- 1 until point.length) {
+ if (point(i)(0) > point(i - 1)(1)) {
+ result += 1
+ } else {
+ point(i)(1) = math.min(point(i - 1)(1), point(i)(1))
+ }
+ }
+ result // 返回结果
+ }
+}
+```
+
-----------------------
From ece2c3efb6e492529c4c8b9b4be1bdf7fbb1b8b6 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 14 Jun 2022 20:45:13 +0800
Subject: [PATCH 401/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200435.=E6=97=A0?=
=?UTF-8?q?=E9=87=8D=E5=8F=A0=E5=8C=BA=E9=97=B4.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...15\345\217\240\345\214\272\351\227\264.md" | 22 ++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)
diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md"
index 66aa124475..6313bc447d 100644
--- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md"
+++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md"
@@ -352,7 +352,27 @@ function eraseOverlapIntervals(intervals: number[][]): number {
};
```
-
+### Scala
+
+```scala
+object Solution {
+ def eraseOverlapIntervals(intervals: Array[Array[Int]]): Int = {
+ var result = 0
+ var interval = intervals.sortWith((a, b) => {
+ a(1) < b(1)
+ })
+ var edge = Int.MinValue
+ for (i <- 0 until interval.length) {
+ if (edge <= interval(i)(0)) {
+ edge = interval(i)(1)
+ } else {
+ result += 1
+ }
+ }
+ result
+ }
+}
+```
From 94350c0c99107f5343a589d36382e52a8a3c0059 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Tue, 14 Jun 2022 22:26:56 +0800
Subject: [PATCH 402/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200763.=E5=88=92?=
=?UTF-8?q?=E5=88=86=E5=AD=97=E6=AF=8D=E5=8C=BA=E9=97=B4.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...27\346\257\215\345\214\272\351\227\264.md" | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
index 2f4d1b4861..f7b16f5ccd 100644
--- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
+++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
@@ -317,7 +317,31 @@ function partitionLabels(s: string): number[] {
};
```
+### Scala
+
+```scala
+object Solution {
+ import scala.collection.mutable
+ def partitionLabels(s: String): List[Int] = {
+ var hash = new Array[Int](26)
+ for (i <- s.indices) {
+ hash(s(i) - 'a') = i
+ }
+
+ var res = mutable.ListBuffer[Int]()
+ var (left, right) = (0, 0)
+ for (i <- s.indices) {
+ right = math.max(hash(s(i) - 'a'), right)
+ if (i == right) {
+ res.append(right - left + 1)
+ left = i + 1
+ }
+ }
+ res.toList
+ }
+}
+```
-----------------------
From eddfde7c1cb4c7db4c11f7d6c76b8a732a65d313 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 15 Jun 2022 22:25:35 +0800
Subject: [PATCH 403/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200056.=E5=90=88?=
=?UTF-8?q?=E5=B9=B6=E5=8C=BA=E9=97=B4.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...10\345\271\266\345\214\272\351\227\264.md" | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
index e444a22111..38e3472cf6 100644
--- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
+++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
@@ -286,7 +286,37 @@ function merge(intervals: number[][]): number[][] {
};
```
+### Scala
+```scala
+object Solution {
+ import scala.collection.mutable
+ def merge(intervals: Array[Array[Int]]): Array[Array[Int]] = {
+ var res = mutable.ArrayBuffer[Array[Int]]()
+
+ // 排序
+ var interval = intervals.sortWith((a, b) => {
+ a(0) < b(0)
+ })
+
+ var left = interval(0)(0)
+ var right = interval(0)(1)
+
+ for (i <- 1 until interval.length) {
+ if (interval(i)(0) <= right) {
+ left = math.min(left, interval(i)(0))
+ right = math.max(right, interval(i)(1))
+ } else {
+ res.append(Array[Int](left, right))
+ left = interval(i)(0)
+ right = interval(i)(1)
+ }
+ }
+ res.append(Array[Int](left, right))
+ res.toArray // 返回res的Array形式
+ }
+}
+```
-----------------------
From 23c26135135f39757932a158d5c4b9f00f4bfe3b Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Wed, 15 Jun 2022 23:15:29 +0800
Subject: [PATCH 404/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200738.=E5=8D=95?=
=?UTF-8?q?=E8=B0=83=E9=80=92=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97.md=20Sca?=
=?UTF-8?q?la=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...36\347\232\204\346\225\260\345\255\227.md" | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md"
index 4e4079a76e..2911e1cc64 100644
--- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md"
+++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md"
@@ -246,7 +246,37 @@ function monotoneIncreasingDigits(n: number): number {
```
+### Scala
+
+直接转换为了整数数组:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def monotoneIncreasingDigits(n: Int): Int = {
+ var digits = mutable.ArrayBuffer[Int]()
+ // 提取每位数字
+ var temp = n // 因为 参数n 是不可变量所以需要赋值给一个可变量
+ while (temp != 0) {
+ digits.append(temp % 10)
+ temp = temp / 10
+ }
+ // 贪心
+ var flag = -1
+ for (i <- 0 until (digits.length - 1) if digits(i) < digits(i + 1)) {
+ flag = i
+ digits(i + 1) -= 1
+ }
+ for (i <- 0 to flag) digits(i) = 9
+ // 拼接
+ var res = 0
+ for (i <- 0 until digits.length) {
+ res += digits(i) * math.pow(10, i).toInt
+ }
+ res
+ }
+}
+```
-----------------------
From cff9cf34b0725b43598c82faccda24d83e0a06ea Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=96=9D=E9=86=89=E7=9A=84=E7=8E=A9=E5=85=B7=E7=86=8A=5F?=
=?UTF-8?q?=E7=8E=8B=E5=9D=87=E7=A5=A5?= <1033076925@qq.com>
Date: Thu, 16 Jun 2022 13:16:02 +0800
Subject: [PATCH 405/551] =?UTF-8?q?=E4=BF=AE=E6=AD=A30035.=E6=90=9C?=
=?UTF-8?q?=E7=B4=A2=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE=E9=94=99=E5=88=AB?=
=?UTF-8?q?=E5=AD=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
index 5cf44ded55..aef091af28 100644
--- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
+++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
@@ -142,7 +142,7 @@ public:
```
* 时间复杂度:O(log n)
-* 时间复杂度:O(1)
+* 空间复杂度:O(1)
效率如下:

From a031937e874267a3a4dcbaf42eba272b244fc0fc Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 16 Jun 2022 22:32:13 +0800
Subject: [PATCH 406/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200714.=E4=B9=B0?=
=?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?=
=?UTF-8?q?=E6=9C=BA=E5=90=AB=E6=89=8B=E7=BB=AD=E8=B4=B9.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...53\346\211\213\347\273\255\350\264\271.md" | 34 +++++++++++++++----
1 file changed, 27 insertions(+), 7 deletions(-)
diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md"
index b27631c68c..4bc21a7003 100644
--- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md"
+++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md"
@@ -153,7 +153,7 @@ public:
## 其他语言版本
-Java:
+### Java
```java
// 贪心思路
class Solution {
@@ -198,7 +198,7 @@ class Solution { // 动态规划
-Python:
+### Python
```python
class Solution: # 贪心思路
@@ -216,7 +216,7 @@ class Solution: # 贪心思路
return result
```
-Go:
+### Go
```golang
func maxProfit(prices []int, fee int) int {
var minBuy int = prices[0] //第一天买入
@@ -241,7 +241,7 @@ func maxProfit(prices []int, fee int) int {
return res
}
```
-Javascript:
+### Javascript
```Javascript
// 贪心思路
var maxProfit = function(prices, fee) {
@@ -293,7 +293,7 @@ var maxProfit = function(prices, fee) {
};
```
-TypeScript:
+### TypeScript
> 贪心
@@ -335,8 +335,28 @@ function maxProfit(prices: number[], fee: number): number {
};
```
-
-
+### Scala
+
+贪心思路:
+
+```scala
+object Solution {
+ def maxProfit(prices: Array[Int], fee: Int): Int = {
+ var result = 0
+ var minPrice = prices(0)
+ for (i <- 1 until prices.length) {
+ if (prices(i) < minPrice) {
+ minPrice = prices(i) // 比当前最小值还小
+ }
+ if (prices(i) > minPrice + fee) {
+ result += prices(i) - minPrice - fee
+ minPrice = prices(i) - fee
+ }
+ }
+ result
+ }
+}
+```
-----------------------
From 0b9737d7541870e8f68d728fd0a0197d281b4c1d Mon Sep 17 00:00:00 2001
From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com>
Date: Thu, 16 Jun 2022 21:24:24 -0500
Subject: [PATCH 407/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A00028.=E5=AE=9E?=
=?UTF-8?q?=E7=8E=B0strStr=20python=E7=89=88=E6=9C=AC=E6=9A=B4=E5=8A=9B?=
=?UTF-8?q?=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0028.\345\256\236\347\216\260strStr.md" | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md"
index 1cdd5292a9..00997907ff 100644
--- "a/problems/0028.\345\256\236\347\216\260strStr.md"
+++ "b/problems/0028.\345\256\236\347\216\260strStr.md"
@@ -685,7 +685,21 @@ class Solution {
```
Python3:
-
+```python
+//暴力解法:
+class Solution(object):
+ def strStr(self, haystack, needle):
+ """
+ :type haystack: str
+ :type needle: str
+ :rtype: int
+ """
+ m,n=len(haystack),len(needle)
+ for i in range(m):
+ if haystack[i:i+n]==needle:
+ return i
+ return -1
+```
```python
// 方法一
class Solution:
From fdd646e0e168aff1cd2772c0bca76cb062cd265c Mon Sep 17 00:00:00 2001
From: programmercarl <826123027@qq.com>
Date: Fri, 17 Jun 2022 16:55:39 +0800
Subject: [PATCH 408/551] Update
---
...0\236\272\346\227\213\347\237\251\351\230\265II.md" | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
index 2222930212..bec5be08c6 100644
--- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
+++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
@@ -24,6 +24,8 @@
## 思路
+为了利于录友们理解,我特意录制了视频,[拿下螺旋矩阵,《代码随想录》第五题!](https://www.bilibili.com/video/BV1SL4y1N7mV),结合本篇文章一起看,效果更佳。
+
这道题目可以说在面试中出现频率较高的题目,**本题并不涉及到什么算法,就是模拟过程,但却十分考察对代码的掌控能力。**
要如何画出这个螺旋排列的正方形矩阵呢?
@@ -74,7 +76,7 @@ public:
int loop = n / 2; // 每个圈循环几次,例如n为奇数3,那么loop = 1 只是循环一圈,矩阵中间的值需要单独处理
int mid = n / 2; // 矩阵中间的位置,例如:n为3, 中间的位置就是(1,1),n为5,中间位置为(2, 2)
int count = 1; // 用来给矩阵中每一个空格赋值
- int offset = 1; // 每一圈循环,需要控制每一条边遍历的长度
+ int offset = 1; // 需要控制每一条边遍历的长度,每次循环右边界收缩一位
int i,j;
while (loop --) {
i = startx;
@@ -82,11 +84,11 @@ public:
// 下面开始的四个for就是模拟转了一圈
// 模拟填充上行从左到右(左闭右开)
- for (j = starty; j < starty + n - offset; j++) {
+ for (j = starty; j < n - offset; j++) {
res[startx][j] = count++;
}
// 模拟填充右列从上到下(左闭右开)
- for (i = startx; i < startx + n - offset; i++) {
+ for (i = startx; i < n - offset; i++) {
res[i][j] = count++;
}
// 模拟填充下行从右到左(左闭右开)
@@ -103,7 +105,7 @@ public:
starty++;
// offset 控制每一圈里每一条边遍历的长度
- offset += 2;
+ offset += 1;
}
// 如果n为奇数的话,需要单独给矩阵最中间的位置赋值
From da559f0ef013bb0503919bda084b244975a73ed6 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 18 Jun 2022 19:17:49 +0800
Subject: [PATCH 409/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200968.=E7=9B=91?=
=?UTF-8?q?=E6=8E=A7=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\344\272\214\345\217\211\346\240\221.md" | 35 +++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md"
index 9a510a1b79..6c957eb20c 100644
--- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md"
@@ -544,5 +544,40 @@ int minCameraCover(struct TreeNode* root){
}
```
+### Scala
+
+```scala
+object Solution {
+ def minCameraCover(root: TreeNode): Int = {
+ var result = 0
+ def traversal(cur: TreeNode): Int = {
+ // 空节点,该节点有覆盖
+ if (cur == null) return 2
+ var left = traversal(cur.left)
+ var right = traversal(cur.right)
+ // 情况1,左右节点都有覆盖
+ if (left == 2 && right == 2) {
+ return 0
+ }
+ // 情况2
+ if (left == 0 || right == 0) {
+ result += 1
+ return 1
+ }
+ // 情况3
+ if (left == 1 || right == 1) {
+ return 2
+ }
+ -1
+ }
+
+ if (traversal(root) == 0) {
+ result += 1
+ }
+ result
+ }
+}
+```
+
-----------------------
From cf0affde0826eedf6328cf383d89d70028bf43a6 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 18 Jun 2022 19:30:51 +0800
Subject: [PATCH 410/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200509.=E6=96=90?=
=?UTF-8?q?=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...42\351\202\243\345\245\221\346\225\260.md" | 30 ++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)
diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
index 1d17784d43..d8e4e1d7f8 100644
--- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
+++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
@@ -245,7 +245,7 @@ var fib = function(n) {
};
```
-TypeScript
+### TypeScript
```typescript
function fib(n: number): number {
@@ -324,5 +324,33 @@ pub fn fib(n: i32) -> i32 {
return fib(n - 1) + fib(n - 2);
}
```
+
+### Scala
+
+动态规划:
+```scala
+object Solution {
+ def fib(n: Int): Int = {
+ if (n <= 1) return n
+ var dp = new Array[Int](n + 1)
+ dp(1) = 1
+ for (i <- 2 to n) {
+ dp(i) = dp(i - 1) + dp(i - 2)
+ }
+ dp(n)
+ }
+}
+```
+
+递归:
+```scala
+object Solution {
+ def fib(n: Int): Int = {
+ if (n <= 1) return n
+ fib(n - 1) + fib(n - 2)
+ }
+}
+```
+
-----------------------
From 5985aae83ce72f2d25b4976a836c1a5fd39ac1ef Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 18 Jun 2022 19:37:35 +0800
Subject: [PATCH 411/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200070.=E7=88=AC?=
=?UTF-8?q?=E6=A5=BC=E6=A2=AF.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...0.\347\210\254\346\245\274\346\242\257.md" | 32 +++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md"
index 34d414413a..097466b03a 100644
--- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md"
+++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md"
@@ -401,6 +401,38 @@ int climbStairs(int n){
}
```
+### Scala
+
+```scala
+object Solution {
+ def climbStairs(n: Int): Int = {
+ if (n <= 2) return n
+ var dp = new Array[Int](n + 1)
+ dp(1) = 1
+ dp(2) = 2
+ for (i <- 3 to n) {
+ dp(i) = dp(i - 1) + dp(i - 2)
+ }
+ dp(n)
+ }
+}
+```
+
+优化空间复杂度:
+```scala
+object Solution {
+ def climbStairs(n: Int): Int = {
+ if (n <= 2) return n
+ var (a, b) = (1, 2)
+ for (i <- 3 to n) {
+ var tmp = a + b
+ a = b
+ b = tmp
+ }
+ b // 最终返回b
+ }
+}
+```
-----------------------
From 6e92cd2417dea438d7671140fd73298a4b598c93 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 18 Jun 2022 20:12:19 +0800
Subject: [PATCH 412/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200746.=E4=BD=BF?=
=?UTF-8?q?=E7=94=A8=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC?=
=?UTF-8?q?=E6=A2=AF.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...71\347\210\254\346\245\274\346\242\257.md" | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md"
index 5931fc8ac8..abaeb9800f 100644
--- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md"
+++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md"
@@ -305,5 +305,35 @@ int minCostClimbingStairs(int* cost, int costSize){
return dp[i-1] < dp[i-2] ? dp[i-1] : dp[i-2];
}
```
+
+### Scala
+
+```scala
+object Solution {
+ def minCostClimbingStairs(cost: Array[Int]): Int = {
+ var dp = new Array[Int](cost.length)
+ dp(0) = cost(0)
+ dp(1) = cost(1)
+ for (i <- 2 until cost.length) {
+ dp(i) = math.min(dp(i - 1), dp(i - 2)) + cost(i)
+ }
+ math.min(dp(cost.length - 1), dp(cost.length - 2))
+ }
+}
+```
+
+第二种思路: dp[i] 表示爬到第i-1层所需的最小花费,状态转移方程为: dp[i] = min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2])
+```scala
+object Solution {
+ def minCostClimbingStairs(cost: Array[Int]): Int = {
+ var dp = new Array[Int](cost.length + 1)
+ for (i <- 2 until cost.length + 1) {
+ dp(i) = math.min(dp(i - 1) + cost(i - 1), dp(i - 2) + cost(i - 2))
+ }
+ dp(cost.length)
+ }
+}
+```
+
-----------------------
From 8e27191aec0f9c377fe79b61a1b6deffa2af5fe4 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 18 Jun 2022 20:29:53 +0800
Subject: [PATCH 413/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200062.=E4=B8=8D?=
=?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...0\215\345\220\214\350\267\257\345\276\204.md" | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md"
index f59b7be8bc..cccda7f120 100644
--- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md"
+++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md"
@@ -412,5 +412,21 @@ int uniquePaths(int m, int n){
}
```
+### Scala
+
+```scala
+object Solution {
+ def uniquePaths(m: Int, n: Int): Int = {
+ var dp = Array.ofDim[Int](m, n)
+ for (i <- 0 until m) dp(i)(0) = 1
+ for (j <- 1 until n) dp(0)(j) = 1
+ for (i <- 1 until m; j <- 1 until n) {
+ dp(i)(j) = dp(i - 1)(j) + dp(i)(j - 1)
+ }
+ dp(m - 1)(n - 1)
+ }
+}
+```
+
-----------------------
From a9de01b83c02ab57181b52df37f4252b5709772f Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 18 Jun 2022 20:49:46 +0800
Subject: [PATCH 414/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200063.=E4=B8=8D?=
=?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84II.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\345\220\214\350\267\257\345\276\204II.md" | 32 +++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
index 59c60156b8..88fce505af 100644
--- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
+++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
@@ -440,5 +440,37 @@ int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridSize, int* obst
}
```
+### Scala
+
+```scala
+object Solution {
+ import scala.util.control.Breaks._
+ def uniquePathsWithObstacles(obstacleGrid: Array[Array[Int]]): Int = {
+ var (m, n) = (obstacleGrid.length, obstacleGrid(0).length)
+ var dp = Array.ofDim[Int](m, n)
+
+ // 比如break、continue这些流程控制需要使用breakable
+ breakable(
+ for (i <- 0 until m) {
+ if (obstacleGrid(i)(0) != 1) dp(i)(0) = 1
+ else break()
+ }
+ )
+ breakable(
+ for (j <- 0 until n) {
+ if (obstacleGrid(0)(j) != 1) dp(0)(j) = 1
+ else break()
+ }
+ )
+
+ for (i <- 1 until m; j <- 1 until n; if obstacleGrid(i)(j) != 1) {
+ dp(i)(j) = dp(i - 1)(j) + dp(i)(j - 1)
+ }
+
+ dp(m - 1)(n - 1)
+ }
+}
+```
+
-----------------------
From 568e36bae8ba2f2812a47a2d7548b9ebd20041b5 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 18 Jun 2022 21:45:03 +0800
Subject: [PATCH 415/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200343.=E6=95=B4?=
=?UTF-8?q?=E6=95=B0=E6=8B=86=E5=88=86.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\264\346\225\260\346\213\206\345\210\206.md" | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md"
index 279f1d7182..9166f2cbb9 100644
--- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md"
+++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md"
@@ -335,5 +335,22 @@ int integerBreak(int n){
}
```
+### Scala
+
+```scala
+object Solution {
+ def integerBreak(n: Int): Int = {
+ var dp = new Array[Int](n + 1)
+ dp(2) = 1
+ for (i <- 3 to n) {
+ for (j <- 1 until i - 1) {
+ dp(i) = math.max(dp(i), math.max(j * (i - j), j * dp(i - j)))
+ }
+ }
+ dp(n)
+ }
+}
+```
+
-----------------------
From 374190e2232ec9daeffe20660d1020199a17c10b Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sat, 18 Jun 2022 22:25:08 +0800
Subject: [PATCH 416/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200096.=E4=B8=8D?=
=?UTF-8?q?=E5=90=8C=E7=9A=84=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...11\346\220\234\347\264\242\346\240\221.md" | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
index 25561b506a..a33421ae60 100644
--- "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
+++ "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
@@ -227,7 +227,7 @@ const numTrees =(n) => {
};
```
-TypeScript
+### TypeScript
```typescript
function numTrees(n: number): number {
@@ -282,5 +282,22 @@ int numTrees(int n){
}
```
+### Scala
+
+```scala
+object Solution {
+ def numTrees(n: Int): Int = {
+ var dp = new Array[Int](n + 1)
+ dp(0) = 1
+ for (i <- 1 to n) {
+ for (j <- 1 to i) {
+ dp(i) += dp(j - 1) * dp(i - j)
+ }
+ }
+ dp(n)
+ }
+}
+```
+
-----------------------
From cbaa9df25b535c7b1ff040bdc30631603e1cd3f7 Mon Sep 17 00:00:00 2001
From: JaneyLin <105125897+janeyziqinglin@users.noreply.github.com>
Date: Sat, 18 Jun 2022 16:58:58 -0500
Subject: [PATCH 417/551] =?UTF-8?q?=E4=BC=98=E5=8C=960925.=E9=95=BF?=
=?UTF-8?q?=E6=8C=89=E9=94=AE=E5=85=A5python=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...77\346\214\211\351\224\256\345\205\245.md" | 38 ++++++++-----------
1 file changed, 15 insertions(+), 23 deletions(-)
diff --git "a/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" "b/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md"
index 0ef5a3d7f3..7aab71a2fc 100644
--- "a/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md"
+++ "b/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md"
@@ -129,29 +129,21 @@ class Solution {
```
### Python
```python
-class Solution:
- def isLongPressedName(self, name: str, typed: str) -> bool:
- i, j = 0, 0
- m, n = len(name) , len(typed)
- while i< m and j < n:
- if name[i] == typed[j]: # 相同时向后匹配
- i += 1
- j += 1
- else: # 不相同
- if j == 0: return False # 如果第一位不相同,直接返回false
- # 判断边界为n-1,若为n会越界,例如name:"kikcxmvzi" typed:"kiikcxxmmvvzzz"
- while j < n - 1 and typed[j] == typed[j-1]: j += 1
- if name[i] == typed[j]:
- i += 1
- j += 1
- else: return False
- # 说明name没有匹配完
- if i < m: return False
- # 说明type没有匹配完
- while j < n:
- if typed[j] == typed[j-1]: j += 1
- else: return False
- return True
+ i = j = 0
+ while(i
Date: Sun, 19 Jun 2022 21:20:49 +0800
Subject: [PATCH 418/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C=E5=8C=85?=
=?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md?=
=?UTF-8?q?=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\241\20001\350\203\214\345\214\205-1.md" | 34 +++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
index a40a92aba6..f991666764 100644
--- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
+++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md"
@@ -498,7 +498,41 @@ const size = 4;
console.log(testWeightBagProblem(weight, value, size));
```
+### Scala
+```scala
+object Solution {
+ // 01背包
+ def test_2_wei_bag_problem1(): Unit = {
+ var weight = Array[Int](1, 3, 4)
+ var value = Array[Int](15, 20, 30)
+ var baseweight = 4
+
+ // 二维数组
+ var dp = Array.ofDim[Int](weight.length, baseweight + 1)
+
+ // 初始化
+ for (j <- weight(0) to baseweight) {
+ dp(0)(j) = value(0)
+ }
+
+ // 遍历
+ for (i <- 1 until weight.length; j <- 1 to baseweight) {
+ if (j - weight(i) >= 0) dp(i)(j) = dp(i - 1)(j - weight(i)) + value(i)
+ dp(i)(j) = math.max(dp(i)(j), dp(i - 1)(j))
+ }
+
+ // 打印数组
+ dp.foreach(x => println("[" + x.mkString(",") + "]"))
+
+ dp(weight.length - 1)(baseweight) // 最终返回
+ }
+
+ def main(args: Array[String]): Unit = {
+ test_2_wei_bag_problem1()
+ }
+}
+```
-----------------------
From a2d340b00279aac08df7cbba7a5733416dc28920 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 19 Jun 2022 21:32:10 +0800
Subject: [PATCH 419/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C=E5=8C=85?=
=?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-2.md?=
=?UTF-8?q?=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...47\241\20001\350\203\214\345\214\205-2.md" | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md"
index b66b74a671..81e61be42d 100644
--- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md"
+++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md"
@@ -375,7 +375,33 @@ console.log(testWeightBagProblem(weight, value, size));
```
+### Scala
+
+```scala
+object Solution {
+ // 滚动数组
+ def test_1_wei_bag_problem(): Unit = {
+ var weight = Array[Int](1, 3, 4)
+ var value = Array[Int](15, 20, 30)
+ var baseweight = 4
+
+ // dp数组
+ var dp = new Array[Int](baseweight + 1)
+
+ // 遍历
+ for (i <- 0 until weight.length; j <- baseweight to weight(i) by -1) {
+ dp(j) = math.max(dp(j), dp(j - weight(i)) + value(i))
+ }
+
+ // 打印数组
+ println("[" + dp.mkString(",") + "]")
+ }
+ def main(args: Array[String]): Unit = {
+ test_1_wei_bag_problem()
+ }
+}
+```
-----------------------
From 1739379b3d31dfe41b519ee4f2eca3da03e53b79 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 19 Jun 2022 21:57:51 +0800
Subject: [PATCH 420/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200416.=E5=88=86?=
=?UTF-8?q?=E5=89=B2=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86.md=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...11\345\222\214\345\255\220\351\233\206.md" | 55 +++++++++++++++++--
1 file changed, 49 insertions(+), 6 deletions(-)
diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
index eb6601e184..e14287e6c9 100644
--- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
+++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
@@ -183,7 +183,7 @@ public:
## 其他语言版本
-Java:
+### Java:
```Java
class Solution {
public boolean canPartition(int[] nums) {
@@ -316,7 +316,7 @@ class Solution {
}
}
```
-Python:
+### Python:
```python
class Solution:
def canPartition(self, nums: List[int]) -> bool:
@@ -329,7 +329,7 @@ class Solution:
dp[j] = max(dp[j], dp[j - nums[i]] + nums[i])
return target == dp[target]
```
-Go:
+### Go:
```go
// 分割等和子集 动态规划
// 时间复杂度O(n^2) 空间复杂度O(n)
@@ -397,7 +397,7 @@ func canPartition(nums []int) bool {
}
```
-javaScript:
+### javaScript:
```js
var canPartition = function(nums) {
@@ -417,7 +417,7 @@ var canPartition = function(nums) {
```
-C:
+### C:
二维dp:
```c
/**
@@ -518,7 +518,7 @@ bool canPartition(int* nums, int numsSize){
}
```
-TypeScript:
+### TypeScript:
> 一维数组,简洁
@@ -573,7 +573,50 @@ function canPartition(nums: number[]): boolean {
};
```
+### Scala
+滚动数组:
+```scala
+object Solution {
+ def canPartition(nums: Array[Int]): Boolean = {
+ var sum = nums.sum
+ if (sum % 2 != 0) return false
+ var half = sum / 2
+ var dp = new Array[Int](half + 1)
+
+ // 遍历
+ for (i <- 0 until nums.length; j <- half to nums(i) by -1) {
+ dp(j) = math.max(dp(j), dp(j - nums(i)) + nums(i))
+ }
+
+ if (dp(half) == half) true else false
+ }
+}
+```
+
+二维数组:
+```scala
+object Solution {
+ def canPartition(nums: Array[Int]): Boolean = {
+ var sum = nums.sum
+ if (sum % 2 != 0) return false
+ var half = sum / 2
+ var dp = Array.ofDim[Int](nums.length, half + 1)
+
+ // 初始化
+ for (j <- nums(0) to half) dp(0)(j) = nums(0)
+
+ // 遍历
+ for (i <- 1 until nums.length; j <- 1 to half) {
+ if (j - nums(i) >= 0) dp(i)(j) = nums(i) + dp(i - 1)(j - nums(i))
+ dp(i)(j) = math.max(dp(i)(j), dp(i - 1)(j))
+ }
+
+ // 如果等于half就返回ture,否则返回false
+ if (dp(nums.length - 1)(half) == half) true else false
+ }
+}
+```
-----------------------
From 910dc88d54a098b374b7c137873e3711710d68f9 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 19 Jun 2022 22:50:50 +0800
Subject: [PATCH 421/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201049.=E6=9C=80?=
=?UTF-8?q?=E5=90=8E=E4=B8=80=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84=E9=87=8D?=
=?UTF-8?q?=E9=87=8FII.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\347\232\204\351\207\215\351\207\217II.md" | 50 +++++++++++++++++--
1 file changed, 45 insertions(+), 5 deletions(-)
diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
index 3d256c3dd1..1e3b958fa4 100644
--- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
+++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
@@ -152,7 +152,7 @@ public:
## 其他语言版本
-Java:
+### Java:
一维数组版本
```Java
@@ -212,7 +212,7 @@ class Solution {
```
-Python:
+### Python:
```python
class Solution:
def lastStoneWeightII(self, stones: List[int]) -> int:
@@ -225,7 +225,7 @@ class Solution:
return sumweight - 2 * dp[target]
```
-Go:
+### Go:
```go
func lastStoneWeightII(stones []int) int {
// 15001 = 30 * 1000 /2 +1
@@ -254,7 +254,7 @@ func max(a, b int) int {
}
```
-JavaScript版本
+### JavaScript
```javascript
/**
@@ -277,7 +277,7 @@ var lastStoneWeightII = function (stones) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function lastStoneWeightII(stones: number[]): number {
@@ -296,7 +296,47 @@ function lastStoneWeightII(stones: number[]): number {
};
```
+### Scala
+滚动数组:
+```scala
+object Solution {
+ def lastStoneWeightII(stones: Array[Int]): Int = {
+ var sum = stones.sum
+ var half = sum / 2
+ var dp = new Array[Int](half + 1)
+
+ // 遍历
+ for (i <- 0 until stones.length; j <- half to stones(i) by -1) {
+ dp(j) = math.max(dp(j), dp(j - stones(i)) + stones(i))
+ }
+
+ sum - 2 * dp(half)
+ }
+}
+```
+
+二维数组:
+```scala
+object Solution {
+ def lastStoneWeightII(stones: Array[Int]): Int = {
+ var sum = stones.sum
+ var half = sum / 2
+ var dp = Array.ofDim[Int](stones.length, half + 1)
+
+ // 初始化
+ for (j <- stones(0) to half) dp(0)(j) = stones(0)
+
+ // 遍历
+ for (i <- 1 until stones.length; j <- 1 to half) {
+ if (j - stones(i) >= 0) dp(i)(j) = stones(i) + dp(i - 1)(j - stones(i))
+ dp(i)(j) = math.max(dp(i)(j), dp(i - 1)(j))
+ }
+
+ sum - 2 * dp(stones.length - 1)(half)
+ }
+}
+```
-----------------------
From 4cb3897549197115721472552bbfb46e3848d682 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 20 Jun 2022 21:19:31 +0800
Subject: [PATCH 422/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200494.=E7=9B=AE?=
=?UTF-8?q?=E6=A0=87=E5=92=8C.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4.\347\233\256\346\240\207\345\222\214.md" | 28 +++++++++++++++----
1 file changed, 23 insertions(+), 5 deletions(-)
diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md"
index 8ce1f6f1ae..639f8ee8e2 100644
--- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md"
+++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md"
@@ -250,7 +250,7 @@ dp[j] += dp[j - nums[i]];
## 其他语言版本
-Java:
+### Java
```java
class Solution {
public int findTargetSumWays(int[] nums, int target) {
@@ -271,7 +271,7 @@ class Solution {
}
```
-Python:
+### Python
```python
class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
@@ -287,7 +287,7 @@ class Solution:
return dp[bagSize]
```
-Go:
+### Go
```go
func findTargetSumWays(nums []int, target int) int {
sum := 0
@@ -322,7 +322,7 @@ func abs(x int) int {
}
```
-Javascript:
+### Javascript
```javascript
const findTargetSumWays = (nums, target) => {
@@ -351,7 +351,7 @@ const findTargetSumWays = (nums, target) => {
};
```
-TypeScript:
+### TypeScript
```typescript
function findTargetSumWays(nums: number[], target: number): number {
@@ -370,7 +370,25 @@ function findTargetSumWays(nums: number[], target: number): number {
};
```
+### Scala
+
+```scala
+object Solution {
+ def findTargetSumWays(nums: Array[Int], target: Int): Int = {
+ var sum = nums.sum
+ if (math.abs(target) > sum) return 0 // 此时没有方案
+ if ((sum + target) % 2 == 1) return 0 // 此时没有方案
+ var bagSize = (sum + target) / 2
+ var dp = new Array[Int](bagSize + 1)
+ dp(0) = 1
+ for (i <- 0 until nums.length; j <- bagSize to nums(i) by -1) {
+ dp(j) += dp(j - nums(i))
+ }
+ dp(bagSize)
+ }
+}
+```
-----------------------
From 2d9727d6202f802674c3cb399f837b66c05b6350 Mon Sep 17 00:00:00 2001
From: Parker999
Date: Mon, 20 Jun 2022 18:35:43 -0700
Subject: [PATCH 423/551] update the dp494
---
"problems/0494.\347\233\256\346\240\207\345\222\214.md" | 1 +
1 file changed, 1 insertion(+)
diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md"
index 99b768347d..60f721c21a 100644
--- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md"
+++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md"
@@ -213,6 +213,7 @@ public:
if (abs(S) > sum) return 0; // 此时没有方案
if ((S + sum) % 2 == 1) return 0; // 此时没有方案
int bagSize = (S + sum) / 2;
+ if(bagsize<0) return 0;
vector dp(bagSize + 1, 0);
dp[0] = 1;
for (int i = 0; i < nums.size(); i++) {
From 89c9044bf984ad638d07932f3b9ebe91e2e7589d Mon Sep 17 00:00:00 2001
From: xiaojun <13589818805@163.com>
Date: Thu, 23 Jun 2022 15:36:55 +0800
Subject: [PATCH 424/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881382.=20?=
=?UTF-8?q?=E5=B0=86=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E5=8F=98?=
=?UTF-8?q?=E5=B9=B3=E8=A1=A1=EF=BC=89=E7=9A=84go=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...21\345\217\230\345\271\263\350\241\241.md" | 40 +++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md"
index 57231ec46b..7c7f84840e 100644
--- "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md"
+++ "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md"
@@ -123,6 +123,46 @@ class Solution:
```
Go:
+```go
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ * Val int
+ * Left *TreeNode
+ * Right *TreeNode
+ * }
+ */
+func balanceBST(root *TreeNode) *TreeNode {
+ // 二叉搜索树中序遍历得到有序数组
+ nums := []int{}
+ // 中序递归遍历二叉树
+ var travel func(node *TreeNode)
+ travel = func(node *TreeNode) {
+ if node == nil {
+ return
+ }
+ travel(node.Left)
+ nums = append(nums, node.Val)
+ travel(node.Right)
+ }
+ // 二分法保证左右子树高度差不超过一(题目要求返回的仍是二叉搜索树)
+ var buildTree func(nums []int, left, right int) *TreeNode
+ buildTree = func(nums []int, left, right int) *TreeNode {
+ if left > right {
+ return nil
+ }
+ mid := left + (right-left) >> 1
+ root := &TreeNode{Val: nums[mid]}
+ root.Left = buildTree(nums, left, mid-1)
+ root.Right = buildTree(nums, mid+1, right)
+ return root
+ }
+ travel(root)
+ return buildTree(nums, 0, len(nums)-1)
+}
+
+```
+
JavaScript:
```javascript
var balanceBST = function(root) {
From beb6805c1d7fd77b66ba0870870fd12228d5a4cf Mon Sep 17 00:00:00 2001
From: unknown
Date: Thu, 23 Jun 2022 10:29:24 +0100
Subject: [PATCH 425/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201049.=E6=9C=80?=
=?UTF-8?q?=E5=90=8E=E4=B8=80=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84=E9=87=8D?=
=?UTF-8?q?=E9=87=8F.md=20C=E8=AF=AD=E8=A8=80=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\347\232\204\351\207\215\351\207\217II.md" | 31 +++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
index ee0ddef2d1..c49f0a1875 100644
--- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
+++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
@@ -277,5 +277,36 @@ var lastStoneWeightII = function (stones) {
};
```
+C版本
+```c
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+
+int getSum(int *stones, int stoneSize) {
+ int sum = 0, i;
+ for (i = 0; i < stoneSize; ++i)
+ sum += stones[i];
+ return sum;
+}
+
+int lastStoneWeightII(int* stones, int stonesSize){
+ int sum = getSum(stones, stonesSize);
+ int target = sum / 2;
+ int i, j;
+
+ // 初始化dp数组
+ int *dp = (int*)malloc(sizeof(int) * (target + 1));
+ memset(dp, 0, sizeof(int) * (target + 1));
+ for (j = stones[0]; j <= target; ++j)
+ dp[j] = stones[0];
+
+ // 递推公式:dp[j] = max(dp[j], dp[j - stones[i]] + stones[i])
+ for (i = 1; i < stonesSize; ++i) {
+ for (j = target; j >= stones[i]; --j)
+ dp[j] = MAX(dp[j], dp[j - stones[i]] + stones[i]);
+ }
+ return sum - dp[target] - dp[target];
+}
+```
+
-----------------------
From c99bf39601a06278f9cca2e5f6d61638e23f3ced Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 23 Jun 2022 21:34:24 +0800
Subject: [PATCH 426/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200474.=E4=B8=80?=
=?UTF-8?q?=E5=92=8C=E9=9B=B6.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4.\344\270\200\345\222\214\351\233\266.md" | 83 +++++++++++++++++--
1 file changed, 78 insertions(+), 5 deletions(-)
diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md"
index d38ce03f89..d6c598aa9c 100644
--- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md"
+++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md"
@@ -163,7 +163,7 @@ public:
## 其他语言版本
-Java:
+### Java
```Java
class Solution {
public int findMaxForm(String[] strs, int m, int n) {
@@ -192,7 +192,7 @@ class Solution {
}
```
-Python:
+### Python
```python
class Solution:
def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
@@ -208,7 +208,7 @@ class Solution:
return dp[m][n]
```
-Go:
+### Go
```go
func findMaxForm(strs []string, m int, n int) int {
// 定义数组
@@ -294,7 +294,7 @@ func getMax(a,b int)int{
}
```
-Javascript:
+### Javascript
```javascript
const findMaxForm = (strs, m, n) => {
const dp = Array.from(Array(m+1), () => Array(n+1).fill(0));
@@ -323,7 +323,7 @@ const findMaxForm = (strs, m, n) => {
};
```
-TypeScript:
+### TypeScript
> 滚动数组,二维数组法
@@ -446,7 +446,80 @@ function isValidSubSet(strs: string[], m: number, n: number): boolean {
}
```
+### Scala
+背包:
+```scala
+object Solution {
+ def findMaxForm(strs: Array[String], m: Int, n: Int): Int = {
+ var dp = Array.ofDim[Int](m + 1, n + 1)
+
+ var (oneNum, zeroNum) = (0, 0)
+
+ for (str <- strs) {
+ oneNum = 0
+ zeroNum = 0
+ for (i <- str.indices) {
+ if (str(i) == '0') zeroNum += 1
+ else oneNum += 1
+ }
+
+ for (i <- m to zeroNum by -1) {
+ for (j <- n to oneNum by -1) {
+ dp(i)(j) = math.max(dp(i)(j), dp(i - zeroNum)(j - oneNum) + 1)
+ }
+ }
+ }
+
+ dp(m)(n)
+ }
+}
+```
+
+回溯法(超时):
+```scala
+object Solution {
+ import scala.collection.mutable
+
+ var res = Int.MinValue
+
+ def test(str: String): (Int, Int) = {
+ var (zero, one) = (0, 0)
+ for (i <- str.indices) {
+ if (str(i) == '1') one += 1
+ else zero += 1
+ }
+ (zero, one)
+ }
+
+ def travsel(strs: Array[String], path: mutable.ArrayBuffer[String], m: Int, n: Int, startIndex: Int): Unit = {
+ if (startIndex > strs.length) {
+ return
+ }
+
+ res = math.max(res, path.length)
+
+ for (i <- startIndex until strs.length) {
+
+ var (zero, one) = test(strs(i))
+
+ // 如果0的个数小于m,1的个数小于n,则可以回溯
+ if (zero <= m && one <= n) {
+ path.append(strs(i))
+ travsel(strs, path, m - zero, n - one, i + 1)
+ path.remove(path.length - 1)
+ }
+ }
+ }
+
+ def findMaxForm(strs: Array[String], m: Int, n: Int): Int = {
+ res = Int.MinValue
+ var path = mutable.ArrayBuffer[String]()
+ travsel(strs, path, m, n, 0)
+ res
+ }
+}
+```
-----------------------
From e9edda44e0cf080701614a1df8ab680a264c4e40 Mon Sep 17 00:00:00 2001
From: shutengfei
Date: Sat, 25 Jun 2022 15:56:26 +0800
Subject: [PATCH 427/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880649.Dota2?=
=?UTF-8?q?=E5=8F=82=E8=AE=AE=E9=99=A2.md=EF=BC=89=EF=BC=9A=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...a2\345\217\202\350\256\256\351\231\242.md" | 38 +++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git "a/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" "b/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md"
index 6e84c9fde2..264a003a97 100644
--- "a/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md"
+++ "b/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md"
@@ -244,6 +244,44 @@ var predictPartyVictory = function(senateStr) {
};
```
+## TypeScript
+
+```typescript
+function predictPartyVictory(senate: string): string {
+ // 数量差:Count(Radiant) - Count(Dire)
+ let deltaRDCnt: number = 0;
+ let hasR: boolean = true,
+ hasD: boolean = true;
+ const senateArr: string[] = senate.split('');
+ while (hasR && hasD) {
+ hasR = false;
+ hasD = false;
+ for (let i = 0, length = senateArr.length; i < length; i++) {
+ if (senateArr[i] === 'R') {
+ if (deltaRDCnt < 0) {
+ senateArr[i] = '';
+ } else {
+ hasR = true;
+ }
+ deltaRDCnt++;
+ } else if (senateArr[i] === 'D') {
+ if (deltaRDCnt > 0) {
+ senateArr[i] = '';
+ } else {
+ hasD = true;
+ }
+ deltaRDCnt--;
+ }
+ }
+ }
+ return hasR ? 'Radiant' : 'Dire';
+};
+```
+
+
+
+
+
-----------------------
From dd31d67778cea30688bd60e4e0b23631758538e8 Mon Sep 17 00:00:00 2001
From: lesenelir
Date: Sun, 26 Jun 2022 11:24:35 +0800
Subject: [PATCH 428/551] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A(1049.?=
=?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=80=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84?=
=?UTF-8?q?=E9=87=8D=E9=87=8FII.md):=20=E4=BF=AE=E6=94=B9markdown=E6=96=87?=
=?UTF-8?q?=E6=A1=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" | 1 -
1 file changed, 1 deletion(-)
diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
index f3e7909cb1..1e87848e38 100644
--- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
+++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
@@ -3,7 +3,6 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
-# 动态规划:最后一块石头的重量 II
## 1049. 最后一块石头的重量 II
From 88ca27562e748e004ac75dfd9ad89c2fc8fc7f07 Mon Sep 17 00:00:00 2001
From: Yang
Date: Sun, 26 Jun 2022 10:08:22 +0200
Subject: [PATCH 429/551] Remove redundant `break` in the for loop
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Remove redundant `break` in the for loop of python script. When the `return` is triggered, the for loop will break automatically.
PS: really like your work! Thanks a lot 😄 .
---
...\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" | 2 --
1 file changed, 2 deletions(-)
diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md"
index 8fd9c6048d..f1f7e6cf45 100644
--- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md"
+++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md"
@@ -125,8 +125,6 @@ class Solution:
if record[i] != 0:
#record数组如果有的元素不为零0,说明字符串s和t 一定是谁多了字符或者谁少了字符。
return False
- #如果有一个元素不为零,则可以判断字符串s和t不是字母异位词
- break
return True
```
From ffe981fb6c5af5c7400f3b82c455b80ac8333fc0 Mon Sep 17 00:00:00 2001
From: AronJudge <2286381138@qq.com>
Date: Sun, 26 Jun 2022 23:50:32 +0800
Subject: [PATCH 430/551] =?UTF-8?q?0977.=20=E6=9C=89=E5=BA=8F=E6=95=B0?=
=?UTF-8?q?=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9=20=E6=B7=BB=E5=8A=A0Java?=
=?UTF-8?q?=E6=B3=A8=E9=87=8A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...6\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" | 1 +
1 file changed, 1 insertion(+)
diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
index 20bdf7b040..d274d7789c 100644
--- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
+++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
@@ -106,6 +106,7 @@ class Solution {
int index = result.length - 1;
while (left <= right) {
if (nums[left] * nums[left] > nums[right] * nums[right]) {
+ // 正数的相对位置是不变的, 需要调整的是负数平方后的相对位置
result[index--] = nums[left] * nums[left];
++left;
} else {
From 812aeeda30f36c9dfecd16c405b8d8ad8674d865 Mon Sep 17 00:00:00 2001
From: programmercarl <826123027@qq.com>
Date: Mon, 27 Jun 2022 09:54:38 +0800
Subject: [PATCH 431/551] Update
---
.../0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" | 2 +-
.../0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" | 2 +-
...\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" | 2 ++
3 files changed, 4 insertions(+), 2 deletions(-)
diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md"
index 6969c2e27e..fb3c1d4560 100644
--- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md"
@@ -7,7 +7,7 @@
## 1. 两数之和
-[力扣题目链接](https://leetcode-cn.com/problems/two-sum/)
+[力扣题目链接](https://leetcode.cn/problems/two-sum/)
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
index bec5be08c6..bf0a279e25 100644
--- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
+++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
@@ -8,7 +8,7 @@
## 59.螺旋矩阵II
-[力扣题目链接](https://leetcode-cn.com/problems/spiral-matrix-ii/)
+[力扣题目链接](https://leetcode.cn/problems/spiral-matrix-ii/)
给定一个正整数 n,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。
diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
index fe78ddab7c..975ca42926 100644
--- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
+++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
@@ -28,6 +28,8 @@
# 思路
+为了方便大家理解,我特意录制了视频:[手把手带你学会操作链表,移除链表元素](https://www.bilibili.com/video/BV18B4y1s7R9),结合视频在看本题解,事半功倍。
+
这里以链表 1 4 2 4 来举例,移除元素4。

From 1b16a934d662b73403dfb8b8036189a62a60c2eb Mon Sep 17 00:00:00 2001
From: programmercarl <826123027@qq.com>
Date: Mon, 27 Jun 2022 09:58:45 +0800
Subject: [PATCH 432/551] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=8A=9B=E6=89=A3?=
=?UTF-8?q?=E9=93=BE=E6=8E=A5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...36\346\226\207\345\255\220\344\270\262.md" | 2 +-
...11\346\225\260\344\271\213\345\222\214.md" | 2 +-
...27\346\257\215\347\273\204\345\220\210.md" | 2 +-
...33\346\225\260\344\271\213\345\222\214.md" | 2 +-
...4N\344\270\252\350\212\202\347\202\271.md" | 2 +-
...10\347\232\204\346\213\254\345\217\267.md" | 2 +-
...55\347\232\204\350\212\202\347\202\271.md" | 2 +-
...73\351\231\244\345\205\203\347\264\240.md" | 2 +-
.../0028.\345\256\236\347\216\260strStr.md" | 2 +-
...00\344\270\252\346\216\222\345\210\227.md" | 2 +-
...22\345\205\245\344\275\215\347\275\256.md" | 2 +-
...7.\350\247\243\346\225\260\347\213\254.md" | 2 +-
...04\345\220\210\346\200\273\345\222\214.md" | 2 +-
...\345\220\210\346\200\273\345\222\214II.md" | 2 +-
...2.\346\216\245\351\233\250\346\260\264.md" | 2 +-
...\350\267\203\346\270\270\346\210\217II.md" | 2 +-
...6.\345\205\250\346\216\222\345\210\227.md" | 2 +-
...\345\205\250\346\216\222\345\210\227II.md" | 2 +-
"problems/0051.N\347\232\207\345\220\216.md" | 2 +-
.../0052.N\347\232\207\345\220\216II.md" | 2 +-
...47\345\255\220\345\272\217\345\222\214.md" | 2 +-
...01\350\247\204\345\210\222\357\274\211.md" | 2 +-
...72\346\227\213\347\237\251\351\230\265.md" | 6 +++---
...63\350\267\203\346\270\270\346\210\217.md" | 2 +-
...10\345\271\266\345\214\272\351\227\264.md" | 2 +-
...15\345\220\214\350\267\257\345\276\204.md" | 2 +-
...\345\220\214\350\267\257\345\276\204II.md" | 2 +-
...0.\347\210\254\346\245\274\346\242\257.md" | 2 +-
...14\345\214\205\347\211\210\346\234\254.md" | 2 +-
...26\350\276\221\350\267\235\347\246\273.md" | 2 +-
"problems/0077.\347\273\204\345\220\210.md" | 2 +-
...04\345\220\210\344\274\230\345\214\226.md" | 2 +-
"problems/0078.\345\255\220\351\233\206.md" | 2 +-
...47\347\232\204\347\237\251\345\275\242.md" | 2 +-
"problems/0090.\345\255\220\351\233\206II.md" | 2 +-
...\345\216\237IP\345\234\260\345\235\200.md" | 2 +-
...11\346\220\234\347\264\242\346\240\221.md" | 2 +-
...11\346\220\234\347\264\242\346\240\221.md" | 2 +-
...70\345\220\214\347\232\204\346\240\221.md" | 2 +-
...60\344\272\214\345\217\211\346\240\221.md" | 2 +-
...02\345\272\217\351\201\215\345\216\206.md" | 20 +++++++++----------
...00\345\244\247\346\267\261\345\272\246.md" | 4 ++--
...40\344\272\214\345\217\211\346\240\221.md" | 4 ++--
...11\346\220\234\347\264\242\346\240\221.md" | 2 +-
...41\344\272\214\345\217\211\346\240\221.md" | 2 +-
...00\345\260\217\346\267\261\345\272\246.md" | 2 +-
...57\345\276\204\346\200\273\345\222\214.md" | 4 ++--
...04\345\255\220\345\272\217\345\210\227.md" | 2 +-
...02\347\202\271\346\214\207\351\222\210.md" | 2 +-
...00\344\275\263\346\227\266\346\234\272.md" | 2 +-
...\344\275\263\346\227\266\346\234\272II.md" | 2 +-
...01\350\247\204\345\210\222\357\274\211.md" | 2 +-
...344\275\263\346\227\266\346\234\272III.md" | 2 +-
...25\350\257\215\346\216\245\351\276\231.md" | 2 +-
...60\345\255\227\344\271\213\345\222\214.md" | 2 +-
...62\345\233\236\346\226\207\344\270\262.md" | 2 +-
...\345\233\236\346\226\207\344\270\262II.md" | 2 +-
...4.\345\212\240\346\262\271\347\253\231.md" | 2 +-
...06\345\217\221\347\263\226\346\236\234.md" | 2 +-
...25\350\257\215\346\213\206\345\210\206.md" | 2 +-
...\345\275\242\351\223\276\350\241\250II.md" | 2 +-
...76\345\274\217\346\261\202\345\200\274.md" | 2 +-
...14\347\232\204\345\215\225\350\257\215.md" | 4 ++--
...\344\275\263\346\227\266\346\234\272IV.md" | 2 +-
...23\345\256\266\345\212\253\350\210\215.md" | 2 +-
...2.\345\277\253\344\271\220\346\225\260.md" | 2 +-
...76\350\241\250\345\205\203\347\264\240.md" | 2 +-
...04\345\255\227\347\254\246\344\270\262.md" | 2 +-
...73\350\275\254\351\223\276\350\241\250.md" | 2 +-
...04\345\255\220\346\225\260\347\273\204.md" | 6 +++---
...\345\256\266\345\212\253\350\210\215II.md" | 2 +-
...345\220\210\346\200\273\345\222\214III.md" | 2 +-
...02\347\202\271\344\270\252\346\225\260.md" | 2 +-
...27\345\256\236\347\216\260\346\240\210.md" | 2 +-
...54\344\272\214\345\217\211\346\240\221.md" | 2 +-
...36\347\216\260\351\230\237\345\210\227.md" | 2 +-
...36\346\226\207\351\223\276\350\241\250.md" | 2 +-
...54\345\205\261\347\245\226\345\205\210.md" | 2 +-
...54\345\205\261\347\245\226\345\205\210.md" | 2 +-
...43\346\234\200\345\244\247\345\200\274.md" | 2 +-
...15\345\274\202\344\275\215\350\257\215.md" | 2 +-
...00\346\234\211\350\267\257\345\276\204.md" | 2 +-
...50\345\271\263\346\226\271\346\225\260.md" | 2 +-
...3.\347\247\273\345\212\250\351\233\266.md" | 2 +-
...07\345\255\220\345\272\217\345\210\227.md" | 2 +-
...53\345\206\267\345\206\273\346\234\237.md" | 2 +-
...66\351\222\261\345\205\221\346\215\242.md" | 2 +-
...11\346\216\222\350\241\214\347\250\213.md" | 2 +-
...345\256\266\345\212\253\350\210\215III.md" | 2 +-
...64\346\225\260\346\213\206\345\210\206.md" | 2 +-
...54\345\255\227\347\254\246\344\270\262.md" | 2 +-
...30\351\242\221\345\205\203\347\264\240.md" | 2 +-
...04\347\232\204\344\272\244\351\233\206.md" | 2 +-
...06\345\212\250\345\272\217\345\210\227.md" | 2 +-
...10\346\200\273\345\222\214\342\205\243.md" | 2 +-
...3.\350\265\216\351\207\221\344\277\241.md" | 2 +-
...55\345\255\220\345\272\217\345\210\227.md" | 2 +-
...66\345\255\220\344\271\213\345\222\214.md" | 2 +-
...15\345\273\272\351\230\237\345\210\227.md" | 2 +-
...11\345\222\214\345\255\220\351\233\206.md" | 2 +-
...15\345\217\240\345\214\272\351\227\264.md" | 2 +-
...55\347\232\204\350\212\202\347\202\271.md" | 2 +-
...25\347\210\206\346\260\224\347\220\203.md" | 2 +-
...\346\225\260\347\233\270\345\212\240II.md" | 2 +-
...06\345\217\221\351\245\274\345\271\262.md" | 2 +-
...20\345\255\227\347\254\246\344\270\262.md" | 2 +-
...77\347\232\204\345\221\250\351\225\277.md" | 2 +-
...4.\344\270\200\345\222\214\351\233\266.md" | 2 +-
...36\345\255\220\345\272\217\345\210\227.md" | 2 +-
...4.\347\233\256\346\240\207\345\222\214.md" | 2 +-
...4\345\244\247\345\205\203\347\264\240I.md" | 2 +-
...55\347\232\204\344\274\227\346\225\260.md" | 2 +-
...\345\244\247\345\205\203\347\264\240II.md" | 2 +-
...42\351\202\243\345\245\221\346\225\260.md" | 2 +-
...13\350\247\222\347\232\204\345\200\274.md" | 2 +-
...07\345\255\220\345\272\217\345\210\227.md" | 2 +-
...\351\222\261\345\205\221\346\215\242II.md" | 2 +-
...17\347\273\235\345\257\271\345\267\256.md" | 2 +-
...72\347\264\257\345\212\240\346\240\221.md" | 2 +-
...\345\255\227\347\254\246\344\270\262II.md" | 2 +-
...40\351\231\244\346\223\215\344\275\234.md" | 2 +-
...66\344\272\214\345\217\211\346\240\221.md" | 2 +-
...36\346\226\207\345\255\220\344\270\262.md" | 2 +-
...a2\345\217\202\350\256\256\351\231\242.md" | 2 +-
...47\344\272\214\345\217\211\346\240\221.md" | 2 +-
...24\345\233\236\345\216\237\347\202\271.md" | 2 +-
...11\346\220\234\347\264\242\346\240\221.md" | 2 +-
...27\347\232\204\344\270\252\346\225\260.md" | 2 +-
...22\345\242\236\345\272\217\345\210\227.md" | 2 +-
...27\344\275\231\350\277\236\346\216\245.md" | 2 +-
...\344\275\231\350\277\236\346\216\245II.md" | 2 +-
...55\347\232\204\346\220\234\347\264\242.md" | 2 +-
...22\345\205\245\346\223\215\344\275\234.md" | 2 +-
...14\345\210\206\346\237\245\346\211\276.md" | 2 +-
...76\350\256\241\351\223\276\350\241\250.md" | 2 +-
...53\346\211\213\347\273\255\350\264\271.md" | 2 +-
...01\350\247\204\345\210\222\357\274\211.md" | 2 +-
...15\345\255\220\346\225\260\347\273\204.md" | 2 +-
...55\345\277\203\347\264\242\345\274\225.md" | 2 +-
...36\347\232\204\346\225\260\345\255\227.md" | 2 +-
...17\346\227\245\346\270\251\345\272\246.md" | 2 +-
...71\347\210\254\346\245\274\346\242\257.md" | 2 +-
...27\346\257\215\345\214\272\351\227\264.md" | 2 +-
...31\345\222\214\346\210\277\351\227\264.md" | 2 +-
...04\345\255\227\347\254\246\344\270\262.md" | 2 +-
...54\346\260\264\346\211\276\351\233\266.md" | 2 +-
...\345\272\217\346\225\260\347\273\204II.md" | 2 +-
...77\346\214\211\351\224\256\345\205\245.md" | 2 +-
...61\350\204\211\346\225\260\347\273\204.md" | 2 +-
...47\344\272\214\345\217\211\346\240\221.md" | 2 +-
...04\347\232\204\345\271\263\346\226\271.md" | 2 +-
...70\347\224\250\345\255\227\347\254\246.md" | 2 +-
...04\346\225\260\347\273\204\345\222\214.md" | 2 +-
...70\344\272\244\347\232\204\347\272\277.md" | 2 +-
...73\351\207\215\345\244\215\351\241\271.md" | 2 +-
...\347\232\204\351\207\215\351\207\217II.md" | 2 +-
...61\345\255\220\345\272\217\345\210\227.md" | 2 +-
...72\347\216\260\346\254\241\346\225\260.md" | 2 +-
...41\345\255\227\347\254\246\344\270\262.md" | 2 +-
...60\347\233\256\346\216\222\345\272\217.md" | 4 ++--
...27\347\232\204\346\225\260\345\255\227.md" | 2 +-
...21\345\217\230\345\271\263\350\241\241.md" | 2 +-
...55\344\273\243\351\201\215\345\216\206.md" | 6 +++---
...22\345\275\222\351\201\215\345\216\206.md" | 6 +++---
...77\346\215\242\347\251\272\346\240\274.md" | 2 +-
...54\345\255\227\347\254\246\344\270\262.md" | 2 +-
...76\350\241\250\347\233\270\344\272\244.md" | 2 +-
167 files changed, 189 insertions(+), 189 deletions(-)
diff --git "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md"
index eaebb5ab4d..d53acf63e6 100644
--- "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md"
+++ "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md"
@@ -8,7 +8,7 @@
# 5.最长回文子串
-[力扣题目链接](https://leetcode-cn.com/problems/longest-palindromic-substring/)
+[力扣题目链接](https://leetcode.cn/problems/longest-palindromic-substring/)
给你一个字符串 s,找到 s 中最长的回文子串。
diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
index 1764d244b6..e6dc82ddd7 100644
--- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
@@ -10,7 +10,7 @@
# 第15题. 三数之和
-[力扣题目链接](https://leetcode-cn.com/problems/3sum/)
+[力扣题目链接](https://leetcode.cn/problems/3sum/)
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。
diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md"
index 9413656515..2b5cb978a2 100644
--- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md"
+++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md"
@@ -7,7 +7,7 @@
# 17.电话号码的字母组合
-[力扣题目链接](https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/)
+[力扣题目链接](https://leetcode.cn/problems/letter-combinations-of-a-phone-number/)
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。
diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
index 6cbd40c288..2146a11469 100644
--- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
@@ -10,7 +10,7 @@
# 第18题. 四数之和
-[力扣题目链接](https://leetcode-cn.com/problems/4sum/)
+[力扣题目链接](https://leetcode.cn/problems/4sum/)
题意:给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md"
index c36900bcde..9278ff1ade 100644
--- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md"
+++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md"
@@ -9,7 +9,7 @@
## 19.删除链表的倒数第N个节点
-[力扣题目链接](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/)
+[力扣题目链接](https://leetcode.cn/problems/remove-nth-node-from-end-of-list/)
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md"
index a0df0d07fc..b44d21ed83 100644
--- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md"
+++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md"
@@ -10,7 +10,7 @@
# 20. 有效的括号
-[力扣题目链接](https://leetcode-cn.com/problems/valid-parentheses/)
+[力扣题目链接](https://leetcode.cn/problems/valid-parentheses/)
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md"
index 2289c22912..e636bfff58 100644
--- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md"
+++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md"
@@ -7,7 +7,7 @@
## 24. 两两交换链表中的节点
-[力扣题目链接](https://leetcode-cn.com/problems/swap-nodes-in-pairs/)
+[力扣题目链接](https://leetcode.cn/problems/swap-nodes-in-pairs/)
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
index f7142bad94..03c58b43ab 100644
--- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
+++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
@@ -7,7 +7,7 @@
## 27. 移除元素
-[力扣题目链接](https://leetcode-cn.com/problems/remove-element/)
+[力扣题目链接](https://leetcode.cn/problems/remove-element/)
给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md"
index 1cdd5292a9..a95a52f814 100644
--- "a/problems/0028.\345\256\236\347\216\260strStr.md"
+++ "b/problems/0028.\345\256\236\347\216\260strStr.md"
@@ -9,7 +9,7 @@
# 28. 实现 strStr()
-[力扣题目链接](https://leetcode-cn.com/problems/implement-strstr/)
+[力扣题目链接](https://leetcode.cn/problems/implement-strstr/)
实现 strStr() 函数。
diff --git "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md"
index 2219e24dba..bce8adef88 100644
--- "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md"
+++ "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md"
@@ -9,7 +9,7 @@
# 31.下一个排列
-[力扣题目链接](https://leetcode-cn.com/problems/next-permutation/)
+[力扣题目链接](https://leetcode.cn/problems/next-permutation/)
实现获取 下一个排列 的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。
diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
index 5cf44ded55..ed7c49b47f 100644
--- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
+++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md"
@@ -9,7 +9,7 @@
# 35.搜索插入位置
-[力扣题目链接](https://leetcode-cn.com/problems/search-insert-position/)
+[力扣题目链接](https://leetcode.cn/problems/search-insert-position/)
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md"
index c1ac15af89..b074b98f34 100644
--- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md"
+++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md"
@@ -9,7 +9,7 @@
# 37. 解数独
-[力扣题目链接](https://leetcode-cn.com/problems/sudoku-solver/)
+[力扣题目链接](https://leetcode.cn/problems/sudoku-solver/)
编写一个程序,通过填充空格来解决数独问题。
diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
index e10a827f04..fef9b676aa 100644
--- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
+++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
@@ -7,7 +7,7 @@
# 39. 组合总和
-[力扣题目链接](https://leetcode-cn.com/problems/combination-sum/)
+[力扣题目链接](https://leetcode.cn/problems/combination-sum/)
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md"
index 34ac64e68d..ee4371cd6b 100644
--- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md"
+++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md"
@@ -9,7 +9,7 @@
# 40.组合总和II
-[力扣题目链接](https://leetcode-cn.com/problems/combination-sum-ii/)
+[力扣题目链接](https://leetcode.cn/problems/combination-sum-ii/)
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md"
index b232ce2206..a2d6f044f0 100644
--- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md"
+++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md"
@@ -9,7 +9,7 @@
# 42. 接雨水
-[力扣题目链接](https://leetcode-cn.com/problems/trapping-rain-water/)
+[力扣题目链接](https://leetcode.cn/problems/trapping-rain-water/)
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
index 4e3ab24a83..9e61fbb88a 100644
--- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
+++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md"
@@ -9,7 +9,7 @@
# 45.跳跃游戏II
-[力扣题目链接](https://leetcode-cn.com/problems/jump-game-ii/)
+[力扣题目链接](https://leetcode.cn/problems/jump-game-ii/)
给定一个非负整数数组,你最初位于数组的第一个位置。
diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md"
index 836c36462f..70ab21d32f 100644
--- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md"
+++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md"
@@ -7,7 +7,7 @@
# 46.全排列
-[力扣题目链接](https://leetcode-cn.com/problems/permutations/)
+[力扣题目链接](https://leetcode.cn/problems/permutations/)
给定一个 没有重复 数字的序列,返回其所有可能的全排列。
diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md"
index cce25cd9a1..bb8610d7bf 100644
--- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md"
+++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md"
@@ -8,7 +8,7 @@
## 47.全排列 II
-[力扣题目链接](https://leetcode-cn.com/problems/permutations-ii/)
+[力扣题目链接](https://leetcode.cn/problems/permutations-ii/)
给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。
diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md"
index c03e48c2e4..798902aeb5 100644
--- "a/problems/0051.N\347\232\207\345\220\216.md"
+++ "b/problems/0051.N\347\232\207\345\220\216.md"
@@ -7,7 +7,7 @@
# 第51题. N皇后
-[力扣题目链接](https://leetcode-cn.com/problems/n-queens/)
+[力扣题目链接](https://leetcode.cn/problems/n-queens/)
n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
diff --git "a/problems/0052.N\347\232\207\345\220\216II.md" "b/problems/0052.N\347\232\207\345\220\216II.md"
index 67e439ca81..486340751a 100644
--- "a/problems/0052.N\347\232\207\345\220\216II.md"
+++ "b/problems/0052.N\347\232\207\345\220\216II.md"
@@ -8,7 +8,7 @@
# 52. N皇后II
-题目链接:https://leetcode-cn.com/problems/n-queens-ii/
+题目链接:https://leetcode.cn/problems/n-queens-ii/
n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
index 73cac244db..cbc91481bc 100644
--- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
+++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md"
@@ -7,7 +7,7 @@
# 53. 最大子序和
-[力扣题目链接](https://leetcode-cn.com/problems/maximum-subarray/)
+[力扣题目链接](https://leetcode.cn/problems/maximum-subarray/)
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
index 99aa7acfd5..345abc27d3 100644
--- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
+++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
@@ -6,7 +6,7 @@
## 53. 最大子序和
-[力扣题目链接](https://leetcode-cn.com/problems/maximum-subarray/)
+[力扣题目链接](https://leetcode.cn/problems/maximum-subarray/)
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md"
index ccf6f47161..0d79fdf929 100644
--- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md"
+++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md"
@@ -8,7 +8,7 @@
## 54.螺旋矩阵
-[力扣题目链接](https://leetcode-cn.com/problems/spiral-matrix/)
+[力扣题目链接](https://leetcode.cn/problems/spiral-matrix/)
给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。
@@ -128,8 +128,8 @@ public:
## 类似题目
-* [59.螺旋矩阵II](https://leetcode-cn.com/problems/spiral-matrix-ii/)
-* [剑指Offer 29.顺时针打印矩阵](https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/)
+* [59.螺旋矩阵II](https://leetcode.cn/problems/spiral-matrix-ii/)
+* [剑指Offer 29.顺时针打印矩阵](https://leetcode.cn/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/)
## 其他语言版本
Python:
diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md"
index 17a3b4f4d3..6fa8349514 100644
--- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md"
+++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md"
@@ -7,7 +7,7 @@
# 55. 跳跃游戏
-[力扣题目链接](https://leetcode-cn.com/problems/jump-game/)
+[力扣题目链接](https://leetcode.cn/problems/jump-game/)
给定一个非负整数数组,你最初位于数组的第一个位置。
diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
index e444a22111..34d8dd8242 100644
--- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
+++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
@@ -7,7 +7,7 @@
# 56. 合并区间
-[力扣题目链接](https://leetcode-cn.com/problems/merge-intervals/)
+[力扣题目链接](https://leetcode.cn/problems/merge-intervals/)
给出一个区间的集合,请合并所有重叠的区间。
diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md"
index f59b7be8bc..02bcc2aeea 100644
--- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md"
+++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md"
@@ -6,7 +6,7 @@
# 62.不同路径
-[力扣题目链接](https://leetcode-cn.com/problems/unique-paths/)
+[力扣题目链接](https://leetcode.cn/problems/unique-paths/)
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。
diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
index 59c60156b8..ca34055a60 100644
--- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
+++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
@@ -6,7 +6,7 @@
# 63. 不同路径 II
-[力扣题目链接](https://leetcode-cn.com/problems/unique-paths-ii/)
+[力扣题目链接](https://leetcode.cn/problems/unique-paths-ii/)
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md"
index 34d414413a..c92c581c6e 100644
--- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md"
+++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md"
@@ -5,7 +5,7 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
# 70. 爬楼梯
-[力扣题目链接](https://leetcode-cn.com/problems/climbing-stairs/)
+[力扣题目链接](https://leetcode.cn/problems/climbing-stairs/)
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md"
index 0f482bb7cb..ec019e5759 100644
--- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md"
+++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md"
@@ -11,7 +11,7 @@
## 70. 爬楼梯
-[力扣题目链接](https://leetcode-cn.com/problems/climbing-stairs/)
+[力扣题目链接](https://leetcode.cn/problems/climbing-stairs/)
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md"
index 530774eefc..641d31284d 100644
--- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md"
+++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md"
@@ -6,7 +6,7 @@
## 72. 编辑距离
-[力扣题目链接](https://leetcode-cn.com/problems/edit-distance/)
+[力扣题目链接](https://leetcode.cn/problems/edit-distance/)
给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。
diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md"
index 9e0398aba1..8d22d0187f 100644
--- "a/problems/0077.\347\273\204\345\220\210.md"
+++ "b/problems/0077.\347\273\204\345\220\210.md"
@@ -9,7 +9,7 @@
# 第77题. 组合
-[力扣题目链接](https://leetcode-cn.com/problems/combinations/ )
+[力扣题目链接](https://leetcode.cn/problems/combinations/ )
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。
diff --git "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md"
index 94608ec18f..a6767047f4 100644
--- "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md"
+++ "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md"
@@ -14,7 +14,7 @@
文中的回溯法是可以剪枝优化的,本篇我们继续来看一下题目77. 组合。
-链接:https://leetcode-cn.com/problems/combinations/
+链接:https://leetcode.cn/problems/combinations/
**看本篇之前,需要先看[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)**。
diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md"
index e1c52b5b81..2c7c19748a 100644
--- "a/problems/0078.\345\255\220\351\233\206.md"
+++ "b/problems/0078.\345\255\220\351\233\206.md"
@@ -7,7 +7,7 @@
# 78.子集
-[力扣题目链接](https://leetcode-cn.com/problems/subsets/)
+[力扣题目链接](https://leetcode.cn/problems/subsets/)
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md"
index 439a3bc5b7..8463d8d05e 100644
--- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md"
+++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md"
@@ -7,7 +7,7 @@
# 84.柱状图中最大的矩形
-[力扣题目链接](https://leetcode-cn.com/problems/largest-rectangle-in-histogram/)
+[力扣题目链接](https://leetcode.cn/problems/largest-rectangle-in-histogram/)
给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md"
index 74ce000b0b..b5bae53f7b 100644
--- "a/problems/0090.\345\255\220\351\233\206II.md"
+++ "b/problems/0090.\345\255\220\351\233\206II.md"
@@ -8,7 +8,7 @@
## 90.子集II
-[力扣题目链接](https://leetcode-cn.com/problems/subsets-ii/)
+[力扣题目链接](https://leetcode.cn/problems/subsets-ii/)
给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
index 6401824b47..e4e16a227a 100644
--- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
+++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
@@ -8,7 +8,7 @@
# 93.复原IP地址
-[力扣题目链接](https://leetcode-cn.com/problems/restore-ip-addresses/)
+[力扣题目链接](https://leetcode.cn/problems/restore-ip-addresses/)
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
diff --git "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
index 25561b506a..9d98c62dbc 100644
--- "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
+++ "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
@@ -6,7 +6,7 @@
# 96.不同的二叉搜索树
-[力扣题目链接](https://leetcode-cn.com/problems/unique-binary-search-trees/)
+[力扣题目链接](https://leetcode.cn/problems/unique-binary-search-trees/)
给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种?
diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
index a8f3c32402..61dd542766 100644
--- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
+++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
@@ -7,7 +7,7 @@
# 98.验证二叉搜索树
-[力扣题目链接](https://leetcode-cn.com/problems/validate-binary-search-tree/)
+[力扣题目链接](https://leetcode.cn/problems/validate-binary-search-tree/)
给定一个二叉树,判断其是否是一个有效的二叉搜索树。
diff --git "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md"
index 5e805d0111..5288779b7c 100644
--- "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md"
+++ "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md"
@@ -8,7 +8,7 @@
# 100. 相同的树
-[力扣题目链接](https://leetcode-cn.com/problems/same-tree/)
+[力扣题目链接](https://leetcode.cn/problems/same-tree/)
给定两个二叉树,编写一个函数来检验它们是否相同。
diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md"
index c79fde0ec1..40249bb78d 100644
--- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md"
@@ -7,7 +7,7 @@
# 101. 对称二叉树
-[力扣题目链接](https://leetcode-cn.com/problems/symmetric-tree/)
+[力扣题目链接](https://leetcode.cn/problems/symmetric-tree/)
给定一个二叉树,检查它是否是镜像对称的。
diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
index 3aa176991c..a2717d09ba 100644
--- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
+++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md"
@@ -26,7 +26,7 @@
# 102.二叉树的层序遍历
-[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/)
+[力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal/)
给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
@@ -379,7 +379,7 @@ pub fn level_order(root: Option>>) -> Vec> {
# 107.二叉树的层次遍历 II
-[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/)
+[力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/)
给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
@@ -660,7 +660,7 @@ pub fn level_order(root: Option>>) -> Vec> {
# 199.二叉树的右视图
-[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-right-side-view/)
+[力扣题目链接](https://leetcode.cn/problems/binary-tree-right-side-view/)
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
@@ -907,7 +907,7 @@ object Solution {
# 637.二叉树的层平均值
-[力扣题目链接](https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/)
+[力扣题目链接](https://leetcode.cn/problems/average-of-levels-in-binary-tree/)
给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。
@@ -1163,7 +1163,7 @@ object Solution {
# 429.N叉树的层序遍历
-[力扣题目链接](https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/)
+[力扣题目链接](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/)
给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。
@@ -1434,7 +1434,7 @@ object Solution {
# 515.在每个树行中找最大值
-[力扣题目链接](https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/)
+[力扣题目链接](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/)
您需要在二叉树的每一行中找到最大的值。
@@ -1668,7 +1668,7 @@ object Solution {
# 116.填充每个节点的下一个右侧节点指针
-[力扣题目链接](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/)
+[力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/)
给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:
@@ -1956,7 +1956,7 @@ object Solution {
```
# 117.填充每个节点的下一个右侧节点指针II
-[力扣题目链接](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/)
+[力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/)
思路:
@@ -2236,7 +2236,7 @@ object Solution {
```
# 104.二叉树的最大深度
-[力扣题目链接](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/)
+[力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/)
给定一个二叉树,找出其最大深度。
@@ -2477,7 +2477,7 @@ object Solution {
# 111.二叉树的最小深度
-[力扣题目链接](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/)
+[力扣题目链接](https://leetcode.cn/problems/minimum-depth-of-binary-tree/)
相对于 104.二叉树的最大深度 ,本题还也可以使用层序遍历的方式来解决,思路是一样的。
diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md"
index 65a155fbda..defe8e06cb 100644
--- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md"
+++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md"
@@ -12,7 +12,7 @@
# 104.二叉树的最大深度
-[力扣题目链接](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/)
+[力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/)
给定一个二叉树,找出其最大深度。
@@ -223,7 +223,7 @@ impl Solution {
# 559.n叉树的最大深度
-[力扣题目链接](https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/)
+[力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/)
给定一个 n 叉树,找到其最大深度。
diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md"
index 188ad3cb44..8cbe5eb609 100644
--- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md"
@@ -12,7 +12,7 @@
# 106.从中序与后序遍历序列构造二叉树
-[力扣题目链接](https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)
+[力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)
根据一棵树的中序遍历与后序遍历构造二叉树。
@@ -394,7 +394,7 @@ public:
# 105.从前序与中序遍历序列构造二叉树
-[力扣题目链接](https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)
+[力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)
根据一棵树的前序遍历与中序遍历构造二叉树。
diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
index 6ee3947bec..c9c1a69335 100644
--- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
+++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
@@ -9,7 +9,7 @@
# 108.将有序数组转换为二叉搜索树
-[力扣题目链接](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/)
+[力扣题目链接](https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/)
将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。
diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md"
index b759836551..3aa815ab07 100644
--- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md"
@@ -9,7 +9,7 @@
# 110.平衡二叉树
-[力扣题目链接](https://leetcode-cn.com/problems/balanced-binary-tree/)
+[力扣题目链接](https://leetcode.cn/problems/balanced-binary-tree/)
给定一个二叉树,判断它是否是高度平衡的二叉树。
diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md"
index b13316591c..9f5ef4c8ea 100644
--- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md"
+++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md"
@@ -9,7 +9,7 @@
# 111.二叉树的最小深度
-[力扣题目链接](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/)
+[力扣题目链接](https://leetcode.cn/problems/minimum-depth-of-binary-tree/)
给定一个二叉树,找出其最小深度。
diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md"
index d3eec16b9e..681cd1a068 100644
--- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md"
+++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md"
@@ -16,7 +16,7 @@
# 112. 路径总和
-[力扣题目链接](https://leetcode-cn.com/problems/path-sum/)
+[力扣题目链接](https://leetcode.cn/problems/path-sum/)
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
@@ -216,7 +216,7 @@ public:
# 113. 路径总和ii
-[力扣题目链接](https://leetcode-cn.com/problems/path-sum-ii/)
+[力扣题目链接](https://leetcode.cn/problems/path-sum-ii/)
给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。
diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md"
index ca66e20d2a..9ae0413974 100644
--- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md"
@@ -6,7 +6,7 @@
## 115.不同的子序列
-[力扣题目链接](https://leetcode-cn.com/problems/distinct-subsequences/)
+[力扣题目链接](https://leetcode.cn/problems/distinct-subsequences/)
给定一个字符串 s 和一个字符串 t ,计算在 s 的子序列中 t 出现的个数。
diff --git "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md"
index 2c443de52a..ed8ce59217 100644
--- "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md"
+++ "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md"
@@ -7,7 +7,7 @@
# 116. 填充每个节点的下一个右侧节点指针
-[力扣题目链接](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/)
+[力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/)
给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:
diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md"
index a2498bb6b6..a577f1dda2 100644
--- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md"
+++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md"
@@ -6,7 +6,7 @@
## 121. 买卖股票的最佳时机
-[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/)
+[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/)
给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
index 1e7b77d80a..b9fa8386ea 100644
--- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
+++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
@@ -7,7 +7,7 @@
# 122.买卖股票的最佳时机II
-[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/)
+[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/)
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
index 12b21fde30..fa9f8842fd 100644
--- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
+++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
@@ -6,7 +6,7 @@
## 122.买卖股票的最佳时机II
-[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/)
+[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/)
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md"
index 67c9949719..c15aaee806 100644
--- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md"
+++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md"
@@ -6,7 +6,7 @@
## 123.买卖股票的最佳时机III
-[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/)
+[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iii/)
给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。
diff --git "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md"
index 584bcb2a15..f1c6f1828a 100644
--- "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md"
+++ "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md"
@@ -7,7 +7,7 @@
# 127. 单词接龙
-[力扣题目链接](https://leetcode-cn.com/problems/word-ladder/)
+[力扣题目链接](https://leetcode.cn/problems/word-ladder/)
字典 wordList 中从单词 beginWord 和 endWord 的 转换序列 是一个按下述规格形成的序列:
* 序列中第一个单词是 beginWord 。
diff --git "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md"
index b271ca7de5..ea3845b741 100644
--- "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md"
+++ "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md"
@@ -5,7 +5,7 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
# 129. 求根节点到叶节点数字之和
-[力扣题目链接](https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/)
+[力扣题目链接](https://leetcode.cn/problems/sum-root-to-leaf-numbers/)
# 思路
diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
index 7a702898dc..a70af603b5 100644
--- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
+++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
@@ -9,7 +9,7 @@
# 131.分割回文串
-[力扣题目链接](https://leetcode-cn.com/problems/palindrome-partitioning/)
+[力扣题目链接](https://leetcode.cn/problems/palindrome-partitioning/)
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。
diff --git "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md"
index 87d3e4b434..92f9b60238 100644
--- "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md"
+++ "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md"
@@ -8,7 +8,7 @@
# 132. 分割回文串 II
-[力扣题目链接](https://leetcode-cn.com/problems/palindrome-partitioning-ii/)
+[力扣题目链接](https://leetcode.cn/problems/palindrome-partitioning-ii/)
给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是回文。
diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md"
index a88f677d34..e6dec44be4 100644
--- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md"
+++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md"
@@ -7,7 +7,7 @@
# 134. 加油站
-[力扣题目链接](https://leetcode-cn.com/problems/gas-station/)
+[力扣题目链接](https://leetcode.cn/problems/gas-station/)
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。
diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md"
index 3456a04c29..55c2efc77c 100644
--- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md"
+++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md"
@@ -7,7 +7,7 @@
# 135. 分发糖果
-[力扣题目链接](https://leetcode-cn.com/problems/candy/)
+[力扣题目链接](https://leetcode.cn/problems/candy/)
老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。
diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md"
index 5b4e92b9aa..7ff13f72a9 100644
--- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md"
+++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md"
@@ -8,7 +8,7 @@
## 139.单词拆分
-[力扣题目链接](https://leetcode-cn.com/problems/word-break/)
+[力扣题目链接](https://leetcode.cn/problems/word-break/)
给定一个非空字符串 s 和一个包含非空单词的列表 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。
diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md"
index f8e62d4572..6b7c7e661f 100644
--- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md"
+++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md"
@@ -11,7 +11,7 @@
## 142.环形链表II
-[力扣题目链接](https://leetcode-cn.com/problems/linked-list-cycle-ii/)
+[力扣题目链接](https://leetcode.cn/problems/linked-list-cycle-ii/)
题意:
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
index 5452c30415..05916135be 100644
--- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
+++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
@@ -11,7 +11,7 @@
# 150. 逆波兰表达式求值
-[力扣题目链接](https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/)
+[力扣题目链接](https://leetcode.cn/problems/evaluate-reverse-polish-notation/)
根据 逆波兰表示法,求表达式的值。
diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
index 0e25fc4d8c..1bcf9888d1 100644
--- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
+++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
@@ -10,7 +10,7 @@
# 151.翻转字符串里的单词
-[力扣题目链接](https://leetcode-cn.com/problems/reverse-words-in-a-string/)
+[力扣题目链接](https://leetcode.cn/problems/reverse-words-in-a-string/)
给定一个字符串,逐个翻转字符串中的每个单词。
@@ -234,7 +234,7 @@ public:
}
void removeExtraSpaces(string& s) {//去除所有空格并在相邻单词之间添加空格, 快慢指针。
- int slow = 0; //整体思想参考Leetcode: 27. 移除元素:https://leetcode-cn.com/problems/remove-element/
+ int slow = 0; //整体思想参考Leetcode: 27. 移除元素:https://leetcode.cn/problems/remove-element/
for (int i = 0; i < s.size(); ++i) { //
if (s[i] != ' ') { //遇到非空格就处理,即删除所有空格。
if (slow != 0) s[slow++] = ' '; //手动控制空格,给单词之间添加空格。slow != 0说明不是第一个单词,需要在单词前添加空格。
diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md"
index 27eb38c365..7ff198527b 100644
--- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md"
+++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md"
@@ -6,7 +6,7 @@
## 188.买卖股票的最佳时机IV
-[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv/)
+[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iv/)
给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。
diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md"
index a828b9a9d6..ad660f2790 100644
--- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md"
+++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md"
@@ -6,7 +6,7 @@
## 198.打家劫舍
-[力扣题目链接](https://leetcode-cn.com/problems/house-robber/)
+[力扣题目链接](https://leetcode.cn/problems/house-robber/)
你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md"
index 7738b2f695..0bea0c7233 100644
--- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md"
+++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md"
@@ -10,7 +10,7 @@
# 第202题. 快乐数
-[力扣题目链接](https://leetcode-cn.com/problems/happy-number/)
+[力扣题目链接](https://leetcode.cn/problems/happy-number/)
编写一个算法来判断一个数 n 是不是快乐数。
diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
index 975ca42926..b79d29a50d 100644
--- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
+++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
@@ -9,7 +9,7 @@
# 203.移除链表元素
-[力扣题目链接](https://leetcode-cn.com/problems/remove-linked-list-elements/)
+[力扣题目链接](https://leetcode.cn/problems/remove-linked-list-elements/)
题意:删除链表中等于给定值 val 的所有节点。
diff --git "a/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md"
index d4b71c59c4..284e7fd944 100644
--- "a/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md"
@@ -7,7 +7,7 @@
# 205. 同构字符串
-[力扣题目链接](https://leetcode-cn.com/problems/isomorphic-strings/)
+[力扣题目链接](https://leetcode.cn/problems/isomorphic-strings/)
给定两个字符串 s 和 t,判断它们是否是同构的。
diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md"
index 25b16907e4..d8a4eddbe2 100644
--- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md"
+++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md"
@@ -9,7 +9,7 @@
# 206.反转链表
-[力扣题目链接](https://leetcode-cn.com/problems/reverse-linked-list/)
+[力扣题目链接](https://leetcode.cn/problems/reverse-linked-list/)
题意:反转一个单链表。
diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
index 69e0da4f1d..e2eb378e7a 100644
--- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
+++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
@@ -7,7 +7,7 @@
# 209.长度最小的子数组
-[力扣题目链接](https://leetcode-cn.com/problems/minimum-size-subarray-sum/)
+[力扣题目链接](https://leetcode.cn/problems/minimum-size-subarray-sum/)
给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的 连续 子数组,并返回其长度。如果不存在符合条件的子数组,返回 0。
@@ -133,8 +133,8 @@ public:
## 相关题目推荐
-* [904.水果成篮](https://leetcode-cn.com/problems/fruit-into-baskets/)
-* [76.最小覆盖子串](https://leetcode-cn.com/problems/minimum-window-substring/)
+* [904.水果成篮](https://leetcode.cn/problems/fruit-into-baskets/)
+* [76.最小覆盖子串](https://leetcode.cn/problems/minimum-window-substring/)
diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md"
index 9e698d01c0..d6b53a24cf 100644
--- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md"
+++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md"
@@ -6,7 +6,7 @@
## 213.打家劫舍II
-[力扣题目链接](https://leetcode-cn.com/problems/house-robber-ii/)
+[力扣题目链接](https://leetcode.cn/problems/house-robber-ii/)
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都 围成一圈 ,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警 。
diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
index 32b1347e56..cb9e379fdd 100644
--- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
+++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
@@ -11,7 +11,7 @@
# 216.组合总和III
-[力扣题目链接](https://leetcode-cn.com/problems/combination-sum-iii/)
+[力扣题目链接](https://leetcode.cn/problems/combination-sum-iii/)
找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。
diff --git "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md"
index ba7acc5a49..82d748a712 100644
--- "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md"
+++ "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md"
@@ -7,7 +7,7 @@
# 222.完全二叉树的节点个数
-[力扣题目链接](https://leetcode-cn.com/problems/count-complete-tree-nodes/)
+[力扣题目链接](https://leetcode.cn/problems/count-complete-tree-nodes/)
给出一个完全二叉树,求出该树的节点个数。
diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md"
index 3c134870ae..994d18752b 100644
--- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md"
+++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md"
@@ -10,7 +10,7 @@
# 225. 用队列实现栈
-[力扣题目链接](https://leetcode-cn.com/problems/implement-stack-using-queues/)
+[力扣题目链接](https://leetcode.cn/problems/implement-stack-using-queues/)
使用队列实现栈的下列操作:
diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md"
index 9b5cfbaef7..8e35fc9d33 100644
--- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md"
@@ -7,7 +7,7 @@
# 226.翻转二叉树
-[力扣题目链接](https://leetcode-cn.com/problems/invert-binary-tree/)
+[力扣题目链接](https://leetcode.cn/problems/invert-binary-tree/)
翻转一棵二叉树。
diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md"
index d9ba8e2632..47993fa1a8 100644
--- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md"
+++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md"
@@ -9,7 +9,7 @@
# 232.用栈实现队列
-[力扣题目链接](https://leetcode-cn.com/problems/implement-queue-using-stacks/)
+[力扣题目链接](https://leetcode.cn/problems/implement-queue-using-stacks/)
使用栈实现队列的下列操作:
diff --git "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md"
index db910d4ee0..f591fcef2b 100644
--- "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md"
+++ "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md"
@@ -7,7 +7,7 @@
# 234.回文链表
-[力扣题目链接](https://leetcode-cn.com/problems/palindrome-linked-list/)
+[力扣题目链接](https://leetcode.cn/problems/palindrome-linked-list/)
请判断一个链表是否为回文链表。
diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md"
index f7f1427aad..9ff7e2938f 100644
--- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md"
+++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md"
@@ -7,7 +7,7 @@
# 235. 二叉搜索树的最近公共祖先
-[力扣题目链接](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)
+[力扣题目链接](https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/)
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md"
index 69a6d0d6a7..23695b11a9 100644
--- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md"
+++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md"
@@ -9,7 +9,7 @@
# 236. 二叉树的最近公共祖先
-[力扣题目链接](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/)
+[力扣题目链接](https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/)
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md"
index eb32fdd279..23e79c8096 100644
--- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md"
+++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md"
@@ -10,7 +10,7 @@
# 239. 滑动窗口最大值
-[力扣题目链接](https://leetcode-cn.com/problems/sliding-window-maximum/)
+[力扣题目链接](https://leetcode.cn/problems/sliding-window-maximum/)
给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。
diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md"
index 8fd9c6048d..8f4b5ae2b7 100644
--- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md"
+++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md"
@@ -9,7 +9,7 @@
## 242.有效的字母异位词
-[力扣题目链接](https://leetcode-cn.com/problems/valid-anagram/)
+[力扣题目链接](https://leetcode.cn/problems/valid-anagram/)
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md"
index 1362897c89..dc76a4326d 100644
--- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md"
+++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md"
@@ -9,7 +9,7 @@
# 257. 二叉树的所有路径
-[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-paths/)
+[力扣题目链接](https://leetcode.cn/problems/binary-tree-paths/)
给定一个二叉树,返回所有从根节点到叶子节点的路径。
diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md"
index 5b15639c06..e8ec98c6c7 100644
--- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md"
+++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md"
@@ -7,7 +7,7 @@
## 279.完全平方数
-[力扣题目链接](https://leetcode-cn.com/problems/perfect-squares/)
+[力扣题目链接](https://leetcode.cn/problems/perfect-squares/)
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。
diff --git "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" "b/problems/0283.\347\247\273\345\212\250\351\233\266.md"
index ed59d2c4aa..9600edd345 100644
--- "a/problems/0283.\347\247\273\345\212\250\351\233\266.md"
+++ "b/problems/0283.\347\247\273\345\212\250\351\233\266.md"
@@ -8,7 +8,7 @@
# 283. 移动零
-[力扣题目链接](https://leetcode-cn.com/problems/move-zeroes/)
+[力扣题目链接](https://leetcode.cn/problems/move-zeroes/)
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md"
index ffa66c0258..41c6a7cef4 100644
--- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md"
@@ -6,7 +6,7 @@
## 300.最长递增子序列
-[力扣题目链接](https://leetcode-cn.com/problems/longest-increasing-subsequence/)
+[力扣题目链接](https://leetcode.cn/problems/longest-increasing-subsequence/)
给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。
diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md"
index f037fe8558..229fc636d1 100644
--- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md"
+++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md"
@@ -6,7 +6,7 @@
## 309.最佳买卖股票时机含冷冻期
-[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)
+[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-cooldown/)
给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。
diff --git "a/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" "b/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md"
index fc0490c8df..03c6a4e228 100644
--- "a/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md"
+++ "b/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md"
@@ -7,7 +7,7 @@
## 322. 零钱兑换
-[力扣题目链接](https://leetcode-cn.com/problems/coin-change/)
+[力扣题目链接](https://leetcode.cn/problems/coin-change/)
给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。
diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md"
index c71b2a9357..71942c799d 100644
--- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md"
+++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md"
@@ -9,7 +9,7 @@
# 332.重新安排行程
-[力扣题目链接](https://leetcode-cn.com/problems/reconstruct-itinerary/)
+[力扣题目链接](https://leetcode.cn/problems/reconstruct-itinerary/)
给定一个机票的字符串二维数组 [from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序。所有这些机票都属于一个从 JFK(肯尼迪国际机场)出发的先生,所以该行程必须从 JFK 开始。
diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md"
index 6f50723d8f..d2add23256 100644
--- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md"
+++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md"
@@ -7,7 +7,7 @@
## 337.打家劫舍 III
-[力扣题目链接](https://leetcode-cn.com/problems/house-robber-iii/)
+[力扣题目链接](https://leetcode.cn/problems/house-robber-iii/)
在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区。这个地区只有一个入口,我们称之为“根”。 除了“根”之外,每栋房子有且只有一个“父“房子与之相连。一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”。 如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警。
diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md"
index 279f1d7182..dd03937f3e 100644
--- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md"
+++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md"
@@ -6,7 +6,7 @@
# 343. 整数拆分
-[力扣题目链接](https://leetcode-cn.com/problems/integer-break/)
+[力扣题目链接](https://leetcode.cn/problems/integer-break/)
给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。
diff --git "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md"
index e1f27bd7c5..1217be15ce 100644
--- "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md"
@@ -10,7 +10,7 @@
# 344.反转字符串
-[力扣题目链接](https://leetcode-cn.com/problems/reverse-string/)
+[力扣题目链接](https://leetcode.cn/problems/reverse-string/)
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。
diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md"
index 20932b2814..a04041ccdf 100644
--- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md"
+++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md"
@@ -11,7 +11,7 @@
# 347.前 K 个高频元素
-[力扣题目链接](https://leetcode-cn.com/problems/top-k-frequent-elements/)
+[力扣题目链接](https://leetcode.cn/problems/top-k-frequent-elements/)
给定一个非空的整数数组,返回其中出现频率前 k 高的元素。
diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
index 4fbdd41460..9851864750 100644
--- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
+++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
@@ -12,7 +12,7 @@
## 349. 两个数组的交集
-[力扣题目链接](https://leetcode-cn.com/problems/intersection-of-two-arrays/)
+[力扣题目链接](https://leetcode.cn/problems/intersection-of-two-arrays/)
题意:给定两个数组,编写一个函数来计算它们的交集。
diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
index 6822896e73..00f8f70cf9 100644
--- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
+++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
@@ -9,7 +9,7 @@
# 376. 摆动序列
-[力扣题目链接](https://leetcode-cn.com/problems/wiggle-subsequence/)
+[力扣题目链接](https://leetcode.cn/problems/wiggle-subsequence/)
如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列。第一个差(如果存在的话)可能是正数或负数。少于两个元素的序列也是摆动序列。
diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md"
index 1d808a3a0a..f8e544a927 100644
--- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md"
+++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md"
@@ -8,7 +8,7 @@
## 377. 组合总和 Ⅳ
-[力扣题目链接](https://leetcode-cn.com/problems/combination-sum-iv/)
+[力扣题目链接](https://leetcode.cn/problems/combination-sum-iv/)
难度:中等
diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md"
index 9c3dda8c4c..3cde547260 100644
--- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md"
+++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md"
@@ -10,7 +10,7 @@
# 383. 赎金信
-[力扣题目链接](https://leetcode-cn.com/problems/ransom-note/)
+[力扣题目链接](https://leetcode.cn/problems/ransom-note/)
给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成,返回 true ;否则返回 false。
diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md"
index 3f7eb11d8d..9a26d63900 100644
--- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md"
@@ -7,7 +7,7 @@
## 392.判断子序列
-[力扣题目链接](https://leetcode-cn.com/problems/is-subsequence/)
+[力扣题目链接](https://leetcode.cn/problems/is-subsequence/)
给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md"
index 4ef68d3b4e..6a1d80c973 100644
--- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md"
+++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md"
@@ -7,7 +7,7 @@
# 404.左叶子之和
-[力扣题目链接](https://leetcode-cn.com/problems/sum-of-left-leaves/)
+[力扣题目链接](https://leetcode.cn/problems/sum-of-left-leaves/)
计算给定二叉树的所有左叶子之和。
diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md"
index 641086a93c..879820eae9 100644
--- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md"
+++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md"
@@ -7,7 +7,7 @@
# 406.根据身高重建队列
-[力扣题目链接](https://leetcode-cn.com/problems/queue-reconstruction-by-height/)
+[力扣题目链接](https://leetcode.cn/problems/queue-reconstruction-by-height/)
假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序)。每个 people[i] = [hi, ki] 表示第 i 个人的身高为 hi ,前面 正好 有 ki 个身高大于或等于 hi 的人。
diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
index eb6601e184..b9fa78d235 100644
--- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
+++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md"
@@ -6,7 +6,7 @@
## 416. 分割等和子集
-[力扣题目链接](https://leetcode-cn.com/problems/partition-equal-subset-sum/)
+[力扣题目链接](https://leetcode.cn/problems/partition-equal-subset-sum/)
题目难易:中等
diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md"
index 66aa124475..f1e259aefa 100644
--- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md"
+++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md"
@@ -7,7 +7,7 @@
# 435. 无重叠区间
-[力扣题目链接](https://leetcode-cn.com/problems/non-overlapping-intervals/)
+[力扣题目链接](https://leetcode.cn/problems/non-overlapping-intervals/)
给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠。
diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md"
index e8f7e54c4b..aca9084fa6 100644
--- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md"
+++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md"
@@ -9,7 +9,7 @@
# 450.删除二叉搜索树中的节点
-[力扣题目链接]( https://leetcode-cn.com/problems/delete-node-in-a-bst/)
+[力扣题目链接]( https://leetcode.cn/problems/delete-node-in-a-bst/)
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md"
index d4bbe961ee..e593b87672 100644
--- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md"
+++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md"
@@ -7,7 +7,7 @@
# 452. 用最少数量的箭引爆气球
-[力扣题目链接](https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/)
+[力扣题目链接](https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons/)
在二维空间中有许多球形的气球。对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标。由于它是水平的,所以纵坐标并不重要,因此只要知道开始和结束的横坐标就足够了。开始坐标总是小于结束坐标。
diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md"
index 726fdb1569..d22e23359d 100644
--- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md"
+++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md"
@@ -9,7 +9,7 @@
# 第454题.四数相加II
-[力扣题目链接](https://leetcode-cn.com/problems/4sum-ii/)
+[力扣题目链接](https://leetcode.cn/problems/4sum-ii/)
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。
diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md"
index 17db4a859e..b787dee6be 100644
--- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md"
+++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md"
@@ -7,7 +7,7 @@
# 455.分发饼干
-[力扣题目链接](https://leetcode-cn.com/problems/assign-cookies/)
+[力扣题目链接](https://leetcode.cn/problems/assign-cookies/)
假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。
diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
index a51c68eea7..bb55bd7ce0 100644
--- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
@@ -11,7 +11,7 @@
# 459.重复的子字符串
-[力扣题目链接](https://leetcode-cn.com/problems/repeated-substring-pattern/)
+[力扣题目链接](https://leetcode.cn/problems/repeated-substring-pattern/)
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。
diff --git "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md"
index 9911dfe5a3..ea0381402d 100644
--- "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md"
+++ "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md"
@@ -5,7 +5,7 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
# 463. 岛屿的周长
-[力扣题目链接](https://leetcode-cn.com/problems/island-perimeter/)
+[力扣题目链接](https://leetcode.cn/problems/island-perimeter/)
## 思路
diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md"
index d38ce03f89..a5018baf21 100644
--- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md"
+++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md"
@@ -7,7 +7,7 @@
## 474.一和零
-[力扣题目链接](https://leetcode-cn.com/problems/ones-and-zeroes/)
+[力扣题目链接](https://leetcode.cn/problems/ones-and-zeroes/)
给你一个二进制字符串数组 strs 和两个整数 m 和 n 。
diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md"
index 3ea2382b1f..291a19bd5e 100644
--- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md"
@@ -9,7 +9,7 @@
# 491.递增子序列
-[力扣题目链接](https://leetcode-cn.com/problems/increasing-subsequences/)
+[力扣题目链接](https://leetcode.cn/problems/increasing-subsequences/)
给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是2。
diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md"
index 8ce1f6f1ae..540de7f4f4 100644
--- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md"
+++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md"
@@ -7,7 +7,7 @@
## 494. 目标和
-[力扣题目链接](https://leetcode-cn.com/problems/target-sum/)
+[力扣题目链接](https://leetcode.cn/problems/target-sum/)
难度:中等
diff --git "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md"
index 02339677bc..aa5277f8d5 100644
--- "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md"
+++ "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md"
@@ -6,7 +6,7 @@
# 496.下一个更大元素 I
-[力扣题目链接](https://leetcode-cn.com/problems/next-greater-element-i/)
+[力扣题目链接](https://leetcode.cn/problems/next-greater-element-i/)
给你两个 没有重复元素 的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集。
diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md"
index 9cb5d0713a..f22f1eb3ed 100644
--- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md"
+++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md"
@@ -9,7 +9,7 @@
# 501.二叉搜索树中的众数
-[力扣题目链接](https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/solution/)
+[力扣题目链接](https://leetcode.cn/problems/find-mode-in-binary-search-tree/solution/)
给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。
diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md"
index ace4d40bd4..67c2c9bc71 100644
--- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md"
+++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md"
@@ -6,7 +6,7 @@
# 503.下一个更大元素II
-[力扣题目链接](https://leetcode-cn.com/problems/next-greater-element-ii/)
+[力扣题目链接](https://leetcode.cn/problems/next-greater-element-ii/)
给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素。数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1。
diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
index 1d17784d43..71647a0a41 100644
--- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
+++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md"
@@ -6,7 +6,7 @@
# 509. 斐波那契数
-[力扣题目链接](https://leetcode-cn.com/problems/fibonacci-number/)
+[力扣题目链接](https://leetcode.cn/problems/fibonacci-number/)
斐波那契数,通常用 F(n) 表示,形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:
F(0) = 0,F(1) = 1
diff --git "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md"
index 12c62c7068..6230ea51ee 100644
--- "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md"
+++ "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md"
@@ -7,7 +7,7 @@
# 513.找树左下角的值
-[力扣题目链接](https://leetcode-cn.com/problems/find-bottom-left-tree-value/)
+[力扣题目链接](https://leetcode.cn/problems/find-bottom-left-tree-value/)
给定一个二叉树,在树的最后一行找到最左边的值。
diff --git "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md"
index 63120b141b..ba71b240d2 100644
--- "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md"
@@ -5,7 +5,7 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
## 516.最长回文子序列
-[力扣题目链接](https://leetcode-cn.com/problems/longest-palindromic-subsequence/)
+[力扣题目链接](https://leetcode.cn/problems/longest-palindromic-subsequence/)
给定一个字符串 s ,找到其中最长的回文子序列,并返回该序列的长度。可以假设 s 的最大长度为 1000 。
diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md"
index b6593438c8..222a10d759 100644
--- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md"
+++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md"
@@ -7,7 +7,7 @@
## 518. 零钱兑换 II
-[力扣题目链接](https://leetcode-cn.com/problems/coin-change-2/)
+[力扣题目链接](https://leetcode.cn/problems/coin-change-2/)
难度:中等
diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md"
index 77699c9f4e..cbf8138c82 100644
--- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md"
+++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md"
@@ -9,7 +9,7 @@
# 530.二叉搜索树的最小绝对差
-[力扣题目链接](https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/)
+[力扣题目链接](https://leetcode.cn/problems/minimum-absolute-difference-in-bst/)
给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值。
diff --git "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md"
index 37eb7d0f2c..853cca6f37 100644
--- "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md"
+++ "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md"
@@ -7,7 +7,7 @@
# 538.把二叉搜索树转换为累加树
-[力扣题目链接](https://leetcode-cn.com/problems/convert-bst-to-greater-tree/)
+[力扣题目链接](https://leetcode.cn/problems/convert-bst-to-greater-tree/)
给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。
diff --git "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md"
index 99d6ebe04e..2a4bd3b324 100644
--- "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md"
+++ "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md"
@@ -10,7 +10,7 @@
# 541. 反转字符串II
-[力扣题目链接](https://leetcode-cn.com/problems/reverse-string-ii/)
+[力扣题目链接](https://leetcode.cn/problems/reverse-string-ii/)
给定一个字符串 s 和一个整数 k,你需要对从字符串开头算起的每隔 2k 个字符的前 k 个字符进行反转。
diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md"
index 00f1170009..fc7e6f3940 100644
--- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md"
+++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md"
@@ -6,7 +6,7 @@
## 583. 两个字符串的删除操作
-[力扣题目链接](https://leetcode-cn.com/problems/delete-operation-for-two-strings/)
+[力扣题目链接](https://leetcode.cn/problems/delete-operation-for-two-strings/)
给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符。
diff --git "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md"
index 55786ea988..40aa98c3c0 100644
--- "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md"
@@ -7,7 +7,7 @@
# 617.合并二叉树
-[力扣题目链接](https://leetcode-cn.com/problems/merge-two-binary-trees/)
+[力扣题目链接](https://leetcode.cn/problems/merge-two-binary-trees/)
给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。
diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md"
index 913aec655a..dd0b8d51c0 100644
--- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md"
+++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md"
@@ -6,7 +6,7 @@
## 647. 回文子串
-[力扣题目链接](https://leetcode-cn.com/problems/palindromic-substrings/)
+[力扣题目链接](https://leetcode.cn/problems/palindromic-substrings/)
给定一个字符串,你的任务是计算这个字符串中有多少个回文子串。
diff --git "a/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" "b/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md"
index 6e84c9fde2..f1b3be11c3 100644
--- "a/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md"
+++ "b/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md"
@@ -8,7 +8,7 @@
# 649. Dota2 参议院
-[力扣题目链接](https://leetcode-cn.com/problems/dota2-senate/)
+[力扣题目链接](https://leetcode.cn/problems/dota2-senate/)
Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇)
diff --git "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md"
index 1c73354b00..38d2a9ec86 100644
--- "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md"
@@ -7,7 +7,7 @@
# 654.最大二叉树
-[力扣题目地址](https://leetcode-cn.com/problems/maximum-binary-tree/)
+[力扣题目地址](https://leetcode.cn/problems/maximum-binary-tree/)
给定一个不含重复元素的整数数组。一个以此数组构建的最大二叉树定义如下:
diff --git "a/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" "b/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md"
index 4eb69a6ce3..3d91a5c313 100644
--- "a/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md"
+++ "b/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md"
@@ -7,7 +7,7 @@
# 657. 机器人能否返回原点
-[力扣题目链接](https://leetcode-cn.com/problems/robot-return-to-origin/)
+[力扣题目链接](https://leetcode.cn/problems/robot-return-to-origin/)
在二维平面上,有一个机器人从原点 (0, 0) 开始。给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束。
diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
index 385b226863..154ba5a9a6 100644
--- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
+++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md"
@@ -10,7 +10,7 @@
# 669. 修剪二叉搜索树
-[力扣题目链接](https://leetcode-cn.com/problems/trim-a-binary-search-tree/)
+[力扣题目链接](https://leetcode.cn/problems/trim-a-binary-search-tree/)
给定一个二叉搜索树,同时给定最小边界L 和最大边界 R。通过修剪二叉搜索树,使得所有节点的值在[L, R]中 (R>=L) 。你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根节点。
diff --git "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md"
index 9a2c5db29d..8a6a2d4658 100644
--- "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md"
+++ "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md"
@@ -8,7 +8,7 @@
# 673.最长递增子序列的个数
-[力扣题目链接](https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/)
+[力扣题目链接](https://leetcode.cn/problems/number-of-longest-increasing-subsequence/)
给定一个未排序的整数数组,找到最长递增子序列的个数。
diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
index b1ed80c3e9..5865a68dac 100644
--- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
+++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
@@ -6,7 +6,7 @@
## 674. 最长连续递增序列
-[力扣题目链接](https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/)
+[力扣题目链接](https://leetcode.cn/problems/longest-continuous-increasing-subsequence/)
给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。
diff --git "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md"
index a16833bc8e..3928e05106 100644
--- "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md"
+++ "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md"
@@ -9,7 +9,7 @@
# 684.冗余连接
-[力扣题目链接](https://leetcode-cn.com/problems/redundant-connection/)
+[力扣题目链接](https://leetcode.cn/problems/redundant-connection/)
树可以看成是一个连通且 无环 的 无向 图。
diff --git "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md"
index d96d4912fe..a8da112449 100644
--- "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md"
+++ "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md"
@@ -7,7 +7,7 @@
# 685.冗余连接II
-[力扣题目链接](https://leetcode-cn.com/problems/redundant-connection-ii/)
+[力扣题目链接](https://leetcode.cn/problems/redundant-connection-ii/)
在本问题中,有根树指满足以下条件的 有向 图。该树只有一个根节点,所有其他节点都是该根节点的后继。该树除了根节点之外的每一个节点都有且只有一个父节点,而根节点没有父节点。
diff --git "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md"
index 40cf4ea1fc..bda400c285 100644
--- "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md"
+++ "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md"
@@ -7,7 +7,7 @@
# 700.二叉搜索树中的搜索
-[力扣题目地址](https://leetcode-cn.com/problems/search-in-a-binary-search-tree/)
+[力扣题目地址](https://leetcode.cn/problems/search-in-a-binary-search-tree/)
给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。
diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md"
index 50e39ade5e..102f091e9c 100644
--- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md"
+++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md"
@@ -7,7 +7,7 @@
# 701.二叉搜索树中的插入操作
-[力扣题目链接](https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/)
+[力扣题目链接](https://leetcode.cn/problems/insert-into-a-binary-search-tree/)
给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据保证,新值和原始二叉搜索树中的任意节点值都不同。
diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
index 50f01226f0..6a37e4d107 100644
--- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
+++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
@@ -7,7 +7,7 @@
## 704. 二分查找
-[力扣题目链接](https://leetcode-cn.com/problems/binary-search/)
+[力扣题目链接](https://leetcode.cn/problems/binary-search/)
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
index dcdb53f44e..6ee11eefbf 100644
--- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
+++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
@@ -9,7 +9,7 @@
# 707.设计链表
-[力扣题目链接](https://leetcode-cn.com/problems/design-linked-list/)
+[力扣题目链接](https://leetcode.cn/problems/design-linked-list/)
题意:
diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md"
index b27631c68c..7600c52cc0 100644
--- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md"
+++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md"
@@ -7,7 +7,7 @@
# 714. 买卖股票的最佳时机含手续费
-[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)
+[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)
给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。
diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
index 5625604b9d..7734450ec1 100644
--- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
+++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md"
@@ -6,7 +6,7 @@
## 714.买卖股票的最佳时机含手续费
-[力扣题目链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)
+[力扣题目链接](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)
给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。
diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md"
index 0b7b519958..18007b700b 100644
--- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md"
+++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md"
@@ -6,7 +6,7 @@
## 718. 最长重复子数组
-[力扣题目链接](https://leetcode-cn.com/problems/maximum-length-of-repeated-subarray/)
+[力扣题目链接](https://leetcode.cn/problems/maximum-length-of-repeated-subarray/)
给两个整数数组 A 和 B ,返回两个数组中公共的、长度最长的子数组的长度。
diff --git "a/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md" "b/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md"
index 14dcd2c08a..2fc4200941 100644
--- "a/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md"
+++ "b/problems/0724.\345\257\273\346\211\276\346\225\260\347\273\204\347\232\204\344\270\255\345\277\203\347\264\242\345\274\225.md"
@@ -7,7 +7,7 @@
# 724.寻找数组的中心下标
-[力扣题目链接](https://leetcode-cn.com/problems/find-pivot-index/)
+[力扣题目链接](https://leetcode.cn/problems/find-pivot-index/)
给你一个整数数组 nums ,请计算数组的 中心下标 。
diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md"
index 4e4079a76e..d2f041f5fb 100644
--- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md"
+++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md"
@@ -6,7 +6,7 @@
# 738.单调递增的数字
-[力扣题目链接](https://leetcode-cn.com/problems/monotone-increasing-digits/)
+[力扣题目链接](https://leetcode.cn/problems/monotone-increasing-digits/)
给定一个非负整数 N,找出小于或等于 N 的最大的整数,同时这个整数需要满足其各个位数上的数字是单调递增。
diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
index 987ce27e34..dbe9afe081 100644
--- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
+++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md"
@@ -7,7 +7,7 @@
# 739. 每日温度
-[力扣题目链接](https://leetcode-cn.com/problems/daily-temperatures/)
+[力扣题目链接](https://leetcode.cn/problems/daily-temperatures/)
请根据每日 气温 列表,重新生成一个列表。对应位置的输出为:要想观测到更高的气温,至少需要等待的天数。如果气温在这之后都不会升高,请在该位置用 0 来代替。
diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md"
index 5931fc8ac8..c2c737150e 100644
--- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md"
+++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md"
@@ -6,7 +6,7 @@
# 746. 使用最小花费爬楼梯
-[力扣题目链接](https://leetcode-cn.com/problems/min-cost-climbing-stairs/)
+[力扣题目链接](https://leetcode.cn/problems/min-cost-climbing-stairs/)
数组的每个下标作为一个阶梯,第 i 个阶梯对应着一个非负数的体力花费值 cost[i](下标从 0 开始)。
diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
index 2f4d1b4861..eb21e42f97 100644
--- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
+++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md"
@@ -7,7 +7,7 @@
# 763.划分字母区间
-[力扣题目链接](https://leetcode-cn.com/problems/partition-labels/)
+[力扣题目链接](https://leetcode.cn/problems/partition-labels/)
字符串 S 由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。返回一个表示每个字符串片段的长度的列表。
diff --git "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md"
index 1cd130653b..6f51b4addb 100644
--- "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md"
+++ "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md"
@@ -8,7 +8,7 @@
# 841.钥匙和房间
-[力扣题目链接](https://leetcode-cn.com/problems/keys-and-rooms/)
+[力扣题目链接](https://leetcode.cn/problems/keys-and-rooms/)
有 N 个房间,开始时你位于 0 号房间。每个房间有不同的号码:0,1,2,...,N-1,并且房间里可能有一些钥匙能使你进入下一个房间。
diff --git "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md"
index dccc5404cc..c0773653d0 100644
--- "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md"
@@ -7,7 +7,7 @@
# 844.比较含退格的字符串
-[力扣题目链接](https://leetcode-cn.com/problems/backspace-string-compare/)
+[力扣题目链接](https://leetcode.cn/problems/backspace-string-compare/)
给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果。 # 代表退格字符。
diff --git "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md"
index aa09e1c67d..f5785a912d 100644
--- "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md"
+++ "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md"
@@ -7,7 +7,7 @@
# 860.柠檬水找零
-[力扣题目链接](https://leetcode-cn.com/problems/lemonade-change/)
+[力扣题目链接](https://leetcode.cn/problems/lemonade-change/)
在柠檬水摊上,每一杯柠檬水的售价为 5 美元。
diff --git "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md"
index cb564fb683..8ca46db9b5 100644
--- "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md"
+++ "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md"
@@ -9,7 +9,7 @@
# 922. 按奇偶排序数组II
-[力扣题目链接](https://leetcode-cn.com/problems/sort-array-by-parity-ii/)
+[力扣题目链接](https://leetcode.cn/problems/sort-array-by-parity-ii/)
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。
diff --git "a/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md" "b/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md"
index 0ef5a3d7f3..bb6aeff257 100644
--- "a/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md"
+++ "b/problems/0925.\351\225\277\346\214\211\351\224\256\345\205\245.md"
@@ -6,7 +6,7 @@
# 925.长按键入
-[力扣题目链接](https://leetcode-cn.com/problems/long-pressed-name/)
+[力扣题目链接](https://leetcode.cn/problems/long-pressed-name/)
你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次。
diff --git "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md"
index 4b7a978c0e..7c98aeeaee 100644
--- "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md"
+++ "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md"
@@ -7,7 +7,7 @@
# 941.有效的山脉数组
-[力扣题目链接](https://leetcode-cn.com/problems/valid-mountain-array/)
+[力扣题目链接](https://leetcode.cn/problems/valid-mountain-array/)
给定一个整数数组 arr,如果它是有效的山脉数组就返回 true,否则返回 false。
diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md"
index 9a510a1b79..0aa04a0297 100644
--- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md"
@@ -7,7 +7,7 @@
# 968.监控二叉树
-[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-cameras/)
+[力扣题目链接](https://leetcode.cn/problems/binary-tree-cameras/)
给定一个二叉树,我们在树的节点上安装摄像头。
diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
index 20bdf7b040..4052c57027 100644
--- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
+++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
@@ -8,7 +8,7 @@
# 977.有序数组的平方
-[力扣题目链接](https://leetcode-cn.com/problems/squares-of-a-sorted-array/)
+[力扣题目链接](https://leetcode.cn/problems/squares-of-a-sorted-array/)
给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。
diff --git "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md"
index 075b5ef1a6..6f54c0986a 100644
--- "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md"
+++ "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md"
@@ -8,7 +8,7 @@
# 1002. 查找常用字符
-[力扣题目链接](https://leetcode-cn.com/problems/find-common-characters/)
+[力扣题目链接](https://leetcode.cn/problems/find-common-characters/)
给你一个字符串数组 words ,请你找出所有在 words 的每个字符串中都出现的共用字符( 包括重复字符),并以数组形式返回。你可以按 任意顺序 返回答案。
diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md"
index 202534da8c..8e161594fc 100644
--- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md"
+++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md"
@@ -7,7 +7,7 @@
# 1005.K次取反后最大化的数组和
-[力扣题目链接](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/)
+[力扣题目链接](https://leetcode.cn/problems/maximize-sum-of-array-after-k-negations/)
给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次。(我们可以多次选择同一个索引 i。)
diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md"
index 4463c5f713..83ccb08c10 100644
--- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md"
+++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md"
@@ -6,7 +6,7 @@
## 1035.不相交的线
-[力扣题目链接](https://leetcode-cn.com/problems/uncrossed-lines/)
+[力扣题目链接](https://leetcode.cn/problems/uncrossed-lines/)
我们在两条独立的水平线上按给定的顺序写下 A 和 B 中的整数。
diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md"
index a92a3911e1..7d16017243 100644
--- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md"
+++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md"
@@ -11,7 +11,7 @@
# 1047. 删除字符串中的所有相邻重复项
-[力扣题目链接](https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/)
+[力扣题目链接](https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/)
给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。
diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
index 3d256c3dd1..c0cd6d5966 100644
--- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
+++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md"
@@ -7,7 +7,7 @@
## 1049. 最后一块石头的重量 II
-[力扣题目链接](https://leetcode-cn.com/problems/last-stone-weight-ii/)
+[力扣题目链接](https://leetcode.cn/problems/last-stone-weight-ii/)
题目难度:中等
diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md"
index d58330ec11..ad9825b818 100644
--- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md"
@@ -6,7 +6,7 @@
## 1143.最长公共子序列
-[力扣题目链接](https://leetcode-cn.com/problems/longest-common-subsequence/)
+[力扣题目链接](https://leetcode.cn/problems/longest-common-subsequence/)
给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度。
diff --git "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md"
index ba92552ab8..e88f909c9b 100644
--- "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md"
+++ "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md"
@@ -6,7 +6,7 @@
# 1207.独一无二的出现次数
-[力扣题目链接](https://leetcode-cn.com/problems/unique-number-of-occurrences/)
+[力扣题目链接](https://leetcode.cn/problems/unique-number-of-occurrences/)
给你一个整数数组 arr,请你帮忙统计数组中每个数的出现次数。
diff --git "a/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" "b/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md"
index 1a9b34a29e..08d4fee719 100644
--- "a/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md"
@@ -6,7 +6,7 @@
# 1221. 分割平衡字符串
-[力扣题目链接](https://leetcode-cn.com/problems/split-a-string-in-balanced-strings/)
+[力扣题目链接](https://leetcode.cn/problems/split-a-string-in-balanced-strings/)
在一个 平衡字符串 中,'L' 和 'R' 字符的数量是相同的。
diff --git "a/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" "b/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md"
index 838c3a9600..5ca73607c0 100644
--- "a/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md"
+++ "b/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md"
@@ -8,9 +8,9 @@
# 1356. 根据数字二进制下 1 的数目排序
-[力扣题目链接](https://leetcode-cn.com/problems/sort-integers-by-the-number-of-1-bits/)
+[力扣题目链接](https://leetcode.cn/problems/sort-integers-by-the-number-of-1-bits/)
-题目链接:https://leetcode-cn.com/problems/sort-integers-by-the-number-of-1-bits/
+题目链接:https://leetcode.cn/problems/sort-integers-by-the-number-of-1-bits/
给你一个整数数组 arr 。请你将数组中的元素按照其二进制表示中数字 1 的数目升序排序。
diff --git "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md"
index 78fa84c094..1d1f86b44c 100644
--- "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md"
+++ "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md"
@@ -8,7 +8,7 @@
# 1365.有多少小于当前数字的数字
-[力扣题目链接](https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number/)
+[力扣题目链接](https://leetcode.cn/problems/how-many-numbers-are-smaller-than-the-current-number/)
给你一个数组 nums,对于其中每个元素 nums[i],请你统计数组中比它小的所有数字的数目。
diff --git "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md"
index 57231ec46b..d944ac3016 100644
--- "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md"
+++ "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md"
@@ -7,7 +7,7 @@
# 1382.将二叉搜索树变平衡
-[力扣题目链接](https://leetcode-cn.com/problems/balance-a-binary-search-tree/)
+[力扣题目链接](https://leetcode.cn/problems/balance-a-binary-search-tree/)
给你一棵二叉搜索树,请你返回一棵 平衡后 的二叉搜索树,新生成的树应该与原来的树有着相同的节点值。
diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md"
index fac30f9989..dc8e812c1c 100644
--- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md"
+++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md"
@@ -11,9 +11,9 @@
看完本篇大家可以使用迭代法,再重新解决如下三道leetcode上的题目:
-* [144.二叉树的前序遍历](https://leetcode-cn.com/problems/binary-tree-preorder-traversal/)
-* [94.二叉树的中序遍历](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/)
-* [145.二叉树的后序遍历](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/)
+* [144.二叉树的前序遍历](https://leetcode.cn/problems/binary-tree-preorder-traversal/)
+* [94.二叉树的中序遍历](https://leetcode.cn/problems/binary-tree-inorder-traversal/)
+* [145.二叉树的后序遍历](https://leetcode.cn/problems/binary-tree-postorder-traversal/)
为什么可以用迭代法(非递归的方式)来实现二叉树的前后中序遍历呢?
diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md"
index 29c0cfdaa4..1cce2a0db9 100644
--- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md"
+++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md"
@@ -99,9 +99,9 @@ void traversal(TreeNode* cur, vector& vec) {
此时大家可以做一做leetcode上三道题目,分别是:
-* [144.二叉树的前序遍历](https://leetcode-cn.com/problems/binary-tree-preorder-traversal/)
-* [145.二叉树的后序遍历](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/)
-* [94.二叉树的中序遍历](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/)
+* [144.二叉树的前序遍历](https://leetcode.cn/problems/binary-tree-preorder-traversal/)
+* [145.二叉树的后序遍历](https://leetcode.cn/problems/binary-tree-postorder-traversal/)
+* [94.二叉树的中序遍历](https://leetcode.cn/problems/binary-tree-inorder-traversal/)
可能有同学感觉前后中序遍历的递归太简单了,要打迭代法(非递归),别急,我们明天打迭代法,打个通透!
diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md"
index eecd7f0c56..a4a8c77752 100644
--- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md"
+++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md"
@@ -7,7 +7,7 @@
# 题目:剑指Offer 05.替换空格
-[力扣题目链接](https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/)
+[力扣题目链接](https://leetcode.cn/problems/ti-huan-kong-ge-lcof/)
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md"
index 7c39ed69ae..4674c14170 100644
--- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md"
@@ -9,7 +9,7 @@
# 题目:剑指Offer58-II.左旋转字符串
-[力扣题目链接](https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/)
+[力扣题目链接](https://leetcode.cn/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/)
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md"
index 0a38cc3309..1ae01061ad 100644
--- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md"
+++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md"
@@ -7,7 +7,7 @@
# 面试题 02.07. 链表相交
-[力扣题目链接](https://leetcode-cn.com/problems/intersection-of-two-linked-lists-lcci/)
+[力扣题目链接](https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/)
给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。
From 8bc026fed035ee9c0f2f8b0452f7e0a5f5531281 Mon Sep 17 00:00:00 2001
From: UndeadSheep
Date: Mon, 27 Jun 2022 15:28:47 +0800
Subject: [PATCH 433/551] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E6=A0=88=E4=B8=8E?=
=?UTF-8?q?=E9=98=9F=E5=88=97=E9=83=A8=E5=88=86=E7=9A=84=20C#=E7=89=88?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...10\347\232\204\346\213\254\345\217\267.md" | 31 ++++++++++++++
...76\345\274\217\346\261\202\345\200\274.md" | 34 +++++++++++++++
...27\345\256\236\347\216\260\346\240\210.md" | 35 ++++++++++++++++
...36\347\216\260\351\230\237\345\210\227.md" | 41 +++++++++++++++++++
...43\346\234\200\345\244\247\345\200\274.md" | 41 +++++++++++++++++++
...30\351\242\221\345\205\203\347\264\240.md" | 37 ++++++++++++++++-
...73\351\207\215\345\244\215\351\241\271.md" | 17 ++++++++
7 files changed, 235 insertions(+), 1 deletion(-)
diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md"
index 7bb7f7463a..cdca70f9a9 100644
--- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md"
+++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md"
@@ -401,5 +401,36 @@ bool isValid(char * s){
}
```
+C#:
+```csharp
+public class Solution {
+ public bool IsValid(string s) {
+ var len = s.Length;
+ if(len % 2 == 1) return false; // 字符串长度为单数,直接返回 false
+ // 初始化栈
+ var stack = new Stack();
+ // 遍历字符串
+ for(int i = 0; i < len; i++){
+ // 当字符串为左括号时,进栈对应的右括号
+ if(s[i] == '('){
+ stack.Push(')');
+ }else if(s[i] == '['){
+ stack.Push(']');
+ }else if(s[i] == '{'){
+ stack.Push('}');
+ }
+ // 当字符串为右括号时,当栈为空(无左括号) 或者 出栈字符不是当前的字符
+ else if(stack.Count == 0 || stack.Pop() != s[i])
+ return false;
+ }
+ // 如果栈不为空,例如“((()”,右括号少于左括号,返回false
+ if (stack.Count > 0)
+ return false;
+ // 上面的校验都满足,则返回true
+ else
+ return true;
+ }
+}
+```
-----------------------
diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
index f4dad8231a..8dafa460e3 100644
--- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
+++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md"
@@ -326,5 +326,39 @@ func evalRPN(_ tokens: [String]) -> Int {
}
```
+C#:
+```csharp
+public int EvalRPN(string[] tokens) {
+ int num;
+ Stack stack = new Stack();
+ foreach(string s in tokens){
+ if(int.TryParse(s, out num)){
+ stack.Push(num);
+ }else{
+ int num1 = stack.Pop();
+ int num2 = stack.Pop();
+ switch (s)
+ {
+ case "+":
+ stack.Push(num1 + num2);
+ break;
+ case "-":
+ stack.Push(num2 - num1);
+ break;
+ case "*":
+ stack.Push(num1 * num2);
+ break;
+ case "/":
+ stack.Push(num2 / num1);
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ return stack.Pop();
+ }
+```
+
-----------------------
diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md"
index 3457c4b368..5d902fb1f9 100644
--- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md"
+++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md"
@@ -816,5 +816,40 @@ class MyStack {
}
```
+C#:
+```csharp
+public class MyStack {
+ Queue queue1;
+ Queue queue2;
+ public MyStack() {
+ queue1 = new Queue();
+ queue2 = new Queue();
+ }
+
+ public void Push(int x) {
+ queue2.Enqueue(x);
+ while(queue1.Count != 0){
+ queue2.Enqueue(queue1.Dequeue());
+ }
+ Queue queueTemp;
+ queueTemp = queue1;
+ queue1 = queue2;
+ queue2 = queueTemp;
+ }
+
+ public int Pop() {
+ return queue1.Count > 0 ? queue1.Dequeue() : -1;
+ }
+
+ public int Top() {
+ return queue1.Count > 0 ? queue1.Peek() : -1;
+ }
+
+ public bool Empty() {
+ return queue1.Count == 0;
+ }
+}
+
+```
-----------------------
diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md"
index 1a56d9f386..d58dc55f68 100644
--- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md"
+++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md"
@@ -496,5 +496,46 @@ void myQueueFree(MyQueue* obj) {
}
```
+C#:
+```csharp
+public class MyQueue {
+ Stack inStack;
+ Stack outStack;
+
+ public MyQueue() {
+ inStack = new Stack();// 负责进栈
+ outStack = new Stack();// 负责出栈
+ }
+
+ public void Push(int x) {
+ inStack.Push(x);
+ }
+
+ public int Pop() {
+ dumpstackIn();
+ return outStack.Pop();
+ }
+
+ public int Peek() {
+ dumpstackIn();
+ return outStack.Peek();
+ }
+
+ public bool Empty() {
+ return inStack.Count == 0 && outStack.Count == 0;
+ }
+
+ // 处理方法:
+ // 如果outStack为空,那么将inStack中的元素全部放到outStack中
+ private void dumpstackIn(){
+ if (outStack.Count != 0) return;
+ while(inStack.Count != 0){
+ outStack.Push(inStack.Pop());
+ }
+ }
+}
+
+```
+
-----------------------
diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md"
index f269450f3b..35ca1eed20 100644
--- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md"
+++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md"
@@ -631,5 +631,46 @@ func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] {
}
```
+C#:
+```csharp
+class myDequeue{
+ private LinkedList linkedList = new LinkedList();
+
+ public void Enqueue(int n){
+ while(linkedList.Count > 0 && linkedList.Last.Value < n){
+ linkedList.RemoveLast();
+ }
+ linkedList.AddLast(n);
+ }
+
+ public int Max(){
+ return linkedList.First.Value;
+ }
+
+ public void Dequeue(int n){
+ if(linkedList.First.Value == n){
+ linkedList.RemoveFirst();
+ }
+ }
+ }
+
+ myDequeue window = new myDequeue();
+ List res = new List();
+
+ public int[] MaxSlidingWindow(int[] nums, int k) {
+ for(int i = 0; i < k; i++){
+ window.Enqueue(nums[i]);
+ }
+ res.Add(window.Max());
+ for(int i = k; i < nums.Length; i++){
+ window.Dequeue(nums[i-k]);
+ window.Enqueue(nums[i]);
+ res.Add(window.Max());
+ }
+
+ return res.ToArray();
+ }
+```
+
-----------------------
diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md"
index 1d6a358bfc..c570672ffe 100644
--- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md"
+++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md"
@@ -374,7 +374,42 @@ function topKFrequent(nums: number[], k: number): number[] {
};
```
-
+C#:
+```csharp
+ public int[] TopKFrequent(int[] nums, int k) {
+ //哈希表-标权重
+ Dictionary dic = new();
+ for(int i = 0; i < nums.Length; i++){
+ if(dic.ContainsKey(nums[i])){
+ dic[nums[i]]++;
+ }else{
+ dic.Add(nums[i], 1);
+ }
+ }
+ //优先队列-从小到大排列
+ PriorityQueue pq = new();
+ foreach(var num in dic){
+ pq.Enqueue(num.Key, num.Value);
+ if(pq.Count > k){
+ pq.Dequeue();
+ }
+ }
+
+ // //Stack-倒置
+ // Stack res = new();
+ // while(pq.Count > 0){
+ // res.Push(pq.Dequeue());
+ // }
+ // return res.ToArray();
+
+ //数组倒装
+ int[] res = new int[k];
+ for(int i = k - 1; i >= 0; i--){
+ res[i] = pq.Dequeue();
+ }
+ return res;
+ }
+```
-----------------------
diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md"
index 638c8f4ed2..9237acdb90 100644
--- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md"
+++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md"
@@ -375,5 +375,22 @@ func removeDuplicates(_ s: String) -> String {
}
```
+C#:
+```csharp
+public string RemoveDuplicates(string s) {
+ //拿字符串直接作为栈,省去了栈还要转为字符串的操作
+ StringBuilder res = new StringBuilder();
+
+ foreach(char c in s){
+ if(res.Length > 0 && res[res.Length-1] == c){
+ res.Remove(res.Length-1, 1);
+ }else{
+ res.Append(c);
+ }
+ }
+
+ return res.ToString();
+ }
+```
-----------------------
From 16af63e22ad0d3522c19ccf5eb3dd4a0b4294a9e Mon Sep 17 00:00:00 2001
From: Shuo Zhang
Date: Mon, 27 Jun 2022 22:54:52 -0400
Subject: [PATCH 434/551] =?UTF-8?q?=E4=BF=AE=E6=94=B90349=20-=20=E7=94=A8?=
=?UTF-8?q?=20Java=20stream=20=E4=BB=A3=E6=9B=BF=20for=20loop=20(=E8=BF=99?=
=?UTF-8?q?=E6=A0=B7=E4=B8=BB=E9=A2=98=E6=9B=B4=E6=98=8E=E6=98=BE=EF=BC=89?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...260\347\273\204\347\232\204\344\272\244\351\233\206.md" | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)
diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
index 9851864750..2a8b2dae84 100644
--- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
+++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
@@ -104,13 +104,8 @@ class Solution {
resSet.add(i);
}
}
- int[] resArr = new int[resSet.size()];
- int index = 0;
//将结果几何转为数组
- for (int i : resSet) {
- resArr[index++] = i;
- }
- return resArr;
+ return resSet.stream().mapToInt(x -> x).toArray();
}
}
```
From c6db9037901601bd84ed907455cbe2adf9f2e9b9 Mon Sep 17 00:00:00 2001
From: White Dove <43168562+zhouweiwei18@users.noreply.github.com>
Date: Tue, 28 Jun 2022 16:24:27 +0800
Subject: [PATCH 435/551] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87?=
=?UTF-8?q?=E5=92=8C.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
细节
---
"problems/0494.\347\233\256\346\240\207\345\222\214.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md"
index 8f116fae8d..00e4bdfa4a 100644
--- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md"
+++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md"
@@ -156,7 +156,7 @@ dp[j] 表示:填满j(包括j)这么大容积的包,有dp[j]种方法
有哪些来源可以推出dp[j]呢?
-不考虑nums[i]的情况下,填满容量为j - nums[i]的背包,有dp[j - nums[i]]种方法。
+不考虑nums[i]的情况下,填满容量为j的背包,有dp[j]种方法。
那么只要搞到nums[i]的话,凑成dp[j]就有dp[j - nums[i]] 种方法。
From c70316d7aa59caffe1a0cec9b1c818fcc867a411 Mon Sep 17 00:00:00 2001
From: wzqwtt <1722249371@qq.com>
Date: Tue, 28 Jun 2022 21:30:49 +0800
Subject: [PATCH 436/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C=E5=8C=85?=
=?UTF-8?q?=E9=97=AE=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=AE=8C?=
=?UTF-8?q?=E5=85=A8=E8=83=8C=E5=8C=85.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...14\345\205\250\350\203\214\345\214\205.md" | 20 +++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md"
index 54e772e028..fc4609a69b 100644
--- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md"
+++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md"
@@ -359,7 +359,27 @@ function test_CompletePack(): void {
test_CompletePack();
```
+Scala:
+```scala
+// 先遍历物品,再遍历背包容量
+object Solution {
+ def test_CompletePack() {
+ var weight = Array[Int](1, 3, 4)
+ var value = Array[Int](15, 20, 30)
+ var baseweight = 4
+
+ var dp = new Array[Int](baseweight + 1)
+
+ for (i <- 0 until weight.length) {
+ for (j <- weight(i) to baseweight) {
+ dp(j) = math.max(dp(j), dp(j - weight(i)) + value(i))
+ }
+ }
+ dp(baseweight)
+ }
+}
+```
-----------------------
From bdd4c83b642dfa5b2c7c5a6b6fd214aa8e0488bb Mon Sep 17 00:00:00 2001
From: wzqwtt <1722249371@qq.com>
Date: Tue, 28 Jun 2022 21:56:42 +0800
Subject: [PATCH 437/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200518.=E9=9B=B6?=
=?UTF-8?q?=E9=92=B1=E5=85=91=E6=8D=A2II.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...66\351\222\261\345\205\221\346\215\242II.md" | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md"
index 222a10d759..3abb96015b 100644
--- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md"
+++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md"
@@ -289,7 +289,22 @@ function change(amount: number, coins: number[]): number {
};
```
-
+Scala:
+
+```scala
+object Solution {
+ def change(amount: Int, coins: Array[Int]): Int = {
+ var dp = new Array[Int](amount + 1)
+ dp(0) = 1
+ for (i <- 0 until coins.length) {
+ for (j <- coins(i) to amount) {
+ dp(j) += dp(j - coins(i))
+ }
+ }
+ dp(amount)
+ }
+}
+```
-----------------------
From 0cd4ffd9a470038f1b77207af082a47c00e8d430 Mon Sep 17 00:00:00 2001
From: azou
Date: Wed, 29 Jun 2022 23:32:29 +0800
Subject: [PATCH 438/551] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=EF=BC=9A=E7=A7=BB?=
=?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0TS=E7=89=88?=
=?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81=E9=94=99=E8=AF=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...351\223\276\350\241\250\345\205\203\347\264\240.md" | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
index b79d29a50d..0e461ce8f6 100644
--- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
+++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
@@ -397,18 +397,18 @@ function removeElements(head: ListNode | null, val: number): ListNode | null {
```typescript
function removeElements(head: ListNode | null, val: number): ListNode | null {
- let dummyHead = new ListNode(0, head);
- let pre: ListNode = dummyHead, cur: ListNode | null = dummyHead.next;
- // 删除非头部节点
+ // 添加虚拟节点
+ const data = new ListNode(0, head);
+ let pre = data, cur = data.next;
while (cur) {
if (cur.val === val) {
- pre.next = cur.next;
+ pre.next = cur.next
} else {
pre = cur;
}
cur = cur.next;
}
- return head.next;
+ return data.next;
};
```
From 55086c231acbb8794299d2f143444173de2b85e9 Mon Sep 17 00:00:00 2001
From: w2xi <43wangxi@gmail.com>
Date: Sat, 2 Jul 2022 11:14:57 +0800
Subject: [PATCH 439/551] =?UTF-8?q?Update=200226.=E7=BF=BB=E8=BD=AC?=
=?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91.md=20JavaScript=E9=80=92=E5=BD=92?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...54\344\272\214\345\217\211\346\240\221.md" | 27 ++++++-------------
1 file changed, 8 insertions(+), 19 deletions(-)
diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md"
index 8e35fc9d33..83d20df856 100644
--- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md"
@@ -470,25 +470,14 @@ func invertTree(root *TreeNode) *TreeNode {
使用递归版本的前序遍历
```javascript
var invertTree = function(root) {
- //1. 首先使用递归版本的前序遍历实现二叉树翻转
- //交换节点函数
- const inverNode=function(left,right){
- let temp=left;
- left=right;
- right=temp;
- //需要重新给root赋值一下
- root.left=left;
- root.right=right;
- }
- //确定递归函数的参数和返回值inverTree=function(root)
- //确定终止条件
- if(root===null){
- return root;
- }
- //确定节点处理逻辑 交换
- inverNode(root.left,root.right);
- invertTree(root.left);
- invertTree(root.right);
+ // 终止条件
+ if (!root) {
+ return null;
+ }
+ // 交换左右节点
+ const rightNode = root.right;
+ root.right = invertTree(root.left);
+ root.left = invertTree(rightNode);
return root;
};
```
From 889256a3781ebc8891f8a37778334f402acf362e Mon Sep 17 00:00:00 2001
From: chenwingsing <742474834@qq.com>
Date: Sat, 2 Jul 2022 21:58:17 +0800
Subject: [PATCH 440/551] =?UTF-8?q?=E6=9B=B4=E6=96=B00018JAVA=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
用例有一个是[1000000000,1000000000,1000000000,1000000000] -294967296
如果用int会越界,所以修改为long
---
.../0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
index 2146a11469..9961307087 100644
--- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
@@ -153,7 +153,7 @@ class Solution {
int left = j + 1;
int right = nums.length - 1;
while (right > left) {
- int sum = nums[i] + nums[j] + nums[left] + nums[right];
+ long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
if (sum > target) {
right--;
} else if (sum < target) {
From 1965ff69f70373fd48f31c590e77152444ceefe3 Mon Sep 17 00:00:00 2001
From: chenwingsing <742474834@qq.com>
Date: Sun, 3 Jul 2022 09:07:51 +0800
Subject: [PATCH 441/551] =?UTF-8?q?=E6=9B=B4=E6=96=B0[0015.=E4=B8=89?=
=?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C]=E8=A7=A3=E9=A2=98=E6=8F=8F?=
=?UTF-8?q?=E8=BF=B0=E9=83=A8=E5=88=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加逗号让解题更加清晰,避免歧义,我一开始就看成a = num[i] *b了
---
.../0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
index 4f1d711a7a..8bc8dd9104 100644
--- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
@@ -95,7 +95,7 @@ public:
拿这个nums数组来举例,首先将数组排序,然后有一层for循环,i从下标0的地方开始,同时定一个下标left 定义在i+1的位置上,定义下标right 在数组结尾的位置上。
-依然还是在数组中找到 abc 使得a + b +c =0,我们这里相当于 a = nums[i] b = nums[left] c = nums[right]。
+依然还是在数组中找到 abc 使得a + b +c =0,我们这里相当于 a = nums[i],b = nums[left],c = nums[right]。
接下来如何移动left 和right呢, 如果nums[i] + nums[left] + nums[right] > 0 就说明 此时三数之和大了,因为数组是排序后了,所以right下标就应该向左移动,这样才能让三数之和小一些。
From 8c54205566578bf331587de936e85b5bc4454e5a Mon Sep 17 00:00:00 2001
From: chenwingsing <742474834@qq.com>
Date: Sun, 3 Jul 2022 11:53:38 +0800
Subject: [PATCH 442/551] =?UTF-8?q?Update=200151.=E7=BF=BB=E8=BD=AC?=
=?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D?=
=?UTF-8?q?.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
修改错误字
---
...\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
index 38372f91e4..23f966e8a6 100644
--- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
+++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
@@ -79,7 +79,7 @@ void removeExtraSpaces(string& s) {
逻辑很简单,从前向后遍历,遇到空格了就erase。
-如果不仔细琢磨一下erase的时间复杂读,还以为以上的代码是O(n)的时间复杂度呢。
+如果不仔细琢磨一下erase的时间复杂度,还以为以上的代码是O(n)的时间复杂度呢。
想一下真正的时间复杂度是多少,一个erase本来就是O(n)的操作,erase实现原理题目:[数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html),最优的算法来移除元素也要O(n)。
From 11b701fdb6ba689bdc9f07362e497a4d2ab5eefd Mon Sep 17 00:00:00 2001
From: AronJudge <2286381138@qq.com>
Date: Mon, 4 Jul 2022 00:58:56 +0800
Subject: [PATCH 443/551] =?UTF-8?q?0018=20Java=E4=BB=A3=E7=A0=81=E9=83=A8?=
=?UTF-8?q?=E5=88=86=E5=A2=9E=E5=8A=A0=E5=89=AA=E6=9E=9D=E6=93=8D=E4=BD=9C?=
=?UTF-8?q?,=E4=B8=8D=E7=84=B6Leetcode=E4=B8=8D=E8=83=BD=E9=80=9A=E8=BF=87?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" | 5 +++++
1 file changed, 5 insertions(+)
diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
index 6cbd40c288..d0b7fc68a5 100644
--- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
@@ -140,6 +140,11 @@ class Solution {
for (int i = 0; i < nums.length; i++) {
+ // nums[i] > target 直接返回, 剪枝操作
+ if (nums[i] > 0 && nums[i] > target) {
+ return result;
+ }
+
if (i > 0 && nums[i - 1] == nums[i]) {
continue;
}
From ca00ec680557689d6b302fe27b3e66e479a0d421 Mon Sep 17 00:00:00 2001
From: xiaojun <13589818805@163.com>
Date: Mon, 4 Jul 2022 15:43:35 +0800
Subject: [PATCH 444/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0031.=E4=B8=8B?=
=?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=8E=92=E5=88=97.md)go=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...00\344\270\252\346\216\222\345\210\227.md" | 24 +++++++++++++++++--
1 file changed, 22 insertions(+), 2 deletions(-)
diff --git "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md"
index bce8adef88..1a3641b037 100644
--- "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md"
+++ "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md"
@@ -171,13 +171,13 @@ class Solution(object):
i = n-2
while i >= 0 and nums[i] >= nums[i+1]:
i -= 1
-
+
if i > -1: // i==-1,不存在下一个更大的排列
j = n-1
while j >= 0 and nums[j] <= nums[i]:
j -= 1
nums[i], nums[j] = nums[j], nums[i]
-
+
start, end = i+1, n-1
while start < end:
nums[start], nums[end] = nums[end], nums[start]
@@ -190,6 +190,26 @@ class Solution(object):
## Go
```go
+//卡尔的解法
+func nextPermutation(nums []int) {
+ for i:=len(nums)-1;i>=0;i--{
+ for j:=len(nums)-1;j>i;j--{
+ if nums[j]>nums[i]{
+ //交换
+ nums[j],nums[i]=nums[i],nums[j]
+ reverse(nums,0+i+1,len(nums)-1)
+ return
+ }
+ }
+ }
+ reverse(nums,0,len(nums)-1)
+}
+//对目标切片指定区间的反转方法
+func reverse(a []int,begin,end int){
+ for i,j:=begin,end;i
Date: Tue, 5 Jul 2022 15:36:27 +0800
Subject: [PATCH 445/551] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E7=AC=AC111?=
=?UTF-8?q?=E9=A2=98=E4=BA=8C=E5=8F=89=E6=A0=91=E6=9C=80=E5=B0=8F=E6=B7=B1?=
=?UTF-8?q?=E5=BA=A6js=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md"
index 0137bd15b1..6378300c0b 100644
--- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md"
+++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md"
@@ -372,7 +372,7 @@ var minDepth1 = function(root) {
// 到叶子节点 返回 1
if(!root.left && !root.right) return 1;
// 只有右节点时 递归右节点
- if(!root.left) return 1 + minDepth(root.right);、
+ if(!root.left) return 1 + minDepth(root.right);
// 只有左节点时 递归左节点
if(!root.right) return 1 + minDepth(root.left);
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
From 0e480c58f0f7cc66e77f09cac6231d049dc7f23f Mon Sep 17 00:00:00 2001
From: coderwei <916606569@qq.com>
Date: Tue, 5 Jul 2022 18:14:30 +0800
Subject: [PATCH 446/551] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E0234ts?=
=?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...36\346\226\207\351\223\276\350\241\250.md" | 68 +++++++++++++++++++
1 file changed, 68 insertions(+)
diff --git "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md"
index f591fcef2b..fa31dede3e 100644
--- "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md"
+++ "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md"
@@ -319,6 +319,74 @@ var isPalindrome = function(head) {
};
```
+```typescript
+function isPalindrome(head: ListNode | null): boolean {
+ // 方法1:利用栈结构 将所有元素添加到栈中
+ // 声明栈
+ let stack:number[] = []
+
+ // 保存一份头结点 后续需要移动头结点
+ let dummy:ListNode = head
+
+ // 遍历链表将所有元素添加到数栈中
+ while(dummy){
+ stack.push(dummy.val)
+ dummy = dummy.next
+ }
+
+ // 利用栈结构的先进先出后出的特性判断是否会文
+ while(head){
+ if(head.val!== stack.pop()) return false
+ head = head.next
+ }
+
+ return true
+
+ // 方法2:翻转后半部分链表
+ // 封装翻转链表函数
+ function reverseList(node:ListNode){
+ let pre:ListNode = null
+ while(node){
+ // 借助第三个变量 进行翻转 有疑问可以看链表篇(https://www.programmercarl.com/链表理论基础.html#单链表)
+ let temp:ListNode = node.next
+ node.next = pre
+ pre = node
+ node = temp
+ }
+ return pre
+ }
+
+ // 快慢指针 需要注意的我们需要保证slow指针指向在要分隔节点的上一个节点,因为是单链表,无法找到链表前面的节点(也可以使用js代码的方法,新增个变量用去记录前一个节点)
+ let fast:ListNode = head.next
+ let slow:ListNode = head
+ while(fast){
+ fast = fast.next
+ //只有fast有值的情况下才继续移动 不然代码会出现异常
+ if(fast) {
+ fast = fast.next
+ slow = slow.next
+ }
+ }
+
+ // 分隔链表
+ let list2Head:ListNode = slow.next
+ // 断开slow节点的next指针
+ slow.next = null
+ // 将后半部分进行翻转
+ let list2 = reverseList(list2Head)
+
+ // 最后进行迭代对比节点的值即可
+ while(head && list2){
+ if(head.val != list2.val) return false
+ head = head.next
+ list2 = list2.next
+ }
+
+ return true
+};
+
+```
+
-----------------------
From 0331eaa57f39c3a791603b94c1615e6118ac6737 Mon Sep 17 00:00:00 2001
From: wang2jun <91008685+wang2jun@users.noreply.github.com>
Date: Tue, 5 Jul 2022 22:43:35 +0800
Subject: [PATCH 447/551] =?UTF-8?q?Update=200674.=E6=9C=80=E9=95=BF?=
=?UTF-8?q?=E8=BF=9E=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
更正对构造函数的使用
---
...\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
index 5865a68dac..7e4d0c19e4 100644
--- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
+++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md"
@@ -300,7 +300,7 @@ Javascript:
> 动态规划:
```javascript
const findLengthOfLCIS = (nums) => {
- let dp = Array(nums.length).fill(1);
+ let dp = new Array(nums.length).fill(1);
for(let i = 0; i < nums.length - 1; i++) {
From 2b68bee8f596b41a6be0b8e458b9ef90b245edcb Mon Sep 17 00:00:00 2001
From: wang2jun <91008685+wang2jun@users.noreply.github.com>
Date: Wed, 6 Jul 2022 11:13:36 +0800
Subject: [PATCH 448/551] =?UTF-8?q?Update=200583.=E4=B8=A4=E4=B8=AA?=
=?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4=E6=93=8D?=
=?UTF-8?q?=E4=BD=9C.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
更正语法规范
---
...\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md"
index fc7e6f3940..0e02e721a8 100644
--- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md"
+++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md"
@@ -205,7 +205,7 @@ func min(a, b int) int {
Javascript:
```javascript
const minDistance = (word1, word2) => {
- let dp = Array.from(Array(word1.length + 1), () => Array(word2.length+1).fill(0));
+ let dp = Array.from(new Array(word1.length + 1), () => Array(word2.length+1).fill(0));
for(let i = 1; i <= word1.length; i++) {
dp[i][0] = i;
From c383c1effeacf26ddbbee78e1e2fb29c7034f946 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=97=A4=E9=9C=B2?= <080301087@163.com>
Date: Wed, 6 Jul 2022 17:38:33 +0800
Subject: [PATCH 449/551] =?UTF-8?q?Update=200450.=E5=88=A0=E9=99=A4?=
=?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84?=
=?UTF-8?q?=E8=8A=82=E7=82=B9.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
python中不用删除节点,这样逻辑更清晰简洁
---
...55\347\232\204\350\212\202\347\202\271.md" | 46 ++++++++-----------
1 file changed, 19 insertions(+), 27 deletions(-)
diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md"
index 3fa2a1c586..81da018f57 100644
--- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md"
+++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md"
@@ -322,33 +322,25 @@ class Solution {
```python
class Solution:
- def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
- if not root: return root #第一种情况:没找到删除的节点,遍历到空节点直接返回了
- if root.val == key:
- if not root.left and not root.right: #第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
- del root
- return None
- if not root.left and root.right: #第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点
- tmp = root
- root = root.right
- del tmp
- return root
- if root.left and not root.right: #第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
- tmp = root
- root = root.left
- del tmp
- return root
- else: #第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置
- v = root.right
- while v.left:
- v = v.left
- v.left = root.left
- tmp = root
- root = root.right
- del tmp
- return root
- if root.val > key: root.left = self.deleteNode(root.left,key) #左递归
- if root.val < key: root.right = self.deleteNode(root.right,key) #右递归
+ def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
+ if not root : return None # 节点为空,返回
+ if root.val < key :
+ root.right = self.deleteNode(root.right, key)
+ elif root.val > key :
+ root.left = self.deleteNode(root.left, key)
+ else:
+ # 当前节点的左子树为空,返回当前的右子树
+ if not root.left : return root.right
+ # 当前节点的右子树为空,返回当前的左子树
+ if not root.right: return root.left
+ # 左右子树都不为空,找到右孩子的最左节点 记为p
+ node = root.right
+ while node.left :
+ node = node.left
+ # 将当前节点的左子树挂在p的左孩子上
+ node.left = root.left
+ # 当前节点的右子树替换掉当前节点,完成当前节点的删除
+ root = root.right
return root
```
From 0ee18c392d33c87f86a2de97cb7cd3654736a551 Mon Sep 17 00:00:00 2001
From: w2xi <43wangxi@gmail.com>
Date: Wed, 6 Jul 2022 22:01:14 +0800
Subject: [PATCH 450/551] =?UTF-8?q?Update=200450.=E5=88=A0=E9=99=A4?=
=?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84?=
=?UTF-8?q?=E8=8A=82=E7=82=B9.md=20JavaScript=E9=80=92=E5=BD=92=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...55\347\232\204\350\212\202\347\202\271.md" | 51 +++++++++++--------
1 file changed, 31 insertions(+), 20 deletions(-)
diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md"
index 3fa2a1c586..3bc8369cc0 100644
--- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md"
+++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md"
@@ -456,31 +456,42 @@ func deleteNode(root *TreeNode, key int) *TreeNode {
* @param {number} key
* @return {TreeNode}
*/
-var deleteNode = function (root, key) {
- if (root === null)
+var deleteNode = function(root, key) {
+ if (!root) return null;
+ if (key > root.val) {
+ root.right = deleteNode(root.right, key);
return root;
- if (root.val === key) {
- if (!root.left)
- return root.right;
- else if (!root.right)
+ } else if (key < root.val) {
+ root.left = deleteNode(root.left, key);
+ return root;
+ } else {
+ // 场景1: 该节点是叶节点
+ if (!root.left && !root.right) {
+ return null
+ }
+ // 场景2: 有一个孩子节点不存在
+ if (root.left && !root.right) {
return root.left;
- else {
- let cur = root.right;
- while (cur.left) {
- cur = cur.left;
- }
- cur.left = root.left;
- root = root.right;
- delete root;
- return root;
+ } else if (root.right && !root.left) {
+ return root.right;
}
+ // 场景3: 左右节点都存在
+ const rightNode = root.right;
+ // 获取最小值节点
+ const minNode = getMinNode(rightNode);
+ // 将待删除节点的值替换为最小值节点值
+ root.val = minNode.val;
+ // 删除最小值节点
+ root.right = deleteNode(root.right, minNode.val);
+ return root;
}
- if (root.val > key)
- root.left = deleteNode(root.left, key);
- if (root.val < key)
- root.right = deleteNode(root.right, key);
- return root;
};
+function getMinNode(root) {
+ while (root.left) {
+ root = root.left;
+ }
+ return root;
+}
```
迭代
From 0c6b1db35cc3c5fc6d227026e4b5175628003318 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?=
Date: Thu, 7 Jul 2022 09:00:14 +0800
Subject: [PATCH 451/551] =?UTF-8?q?Update=200707.=E8=AE=BE=E8=AE=A1?=
=?UTF-8?q?=E9=93=BE=E8=A1=A8.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
index a788939b5b..31458d84c6 100644
--- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
+++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
@@ -106,12 +106,9 @@ public:
// 如果index大于链表的长度,则返回空
// 如果index小于0,则置为0,作为链表的新头节点。
void addAtIndex(int index, int val) {
- if (index > _size) {
+ if (index > _size || index < 0) {
return;
}
- else if (index < 0) {
- index = 0;
- }
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur = _dummyHead;
while(index--) {
From 0f3ffb3e282af527cd2a529d7921a16321c2561e Mon Sep 17 00:00:00 2001
From: BaoTaoqi <464115280@qq.com>
Date: Fri, 8 Jul 2022 10:58:53 +0800
Subject: [PATCH 452/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200746.=E4=BD=BF?=
=?UTF-8?q?=E7=94=A8=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC?=
=?UTF-8?q?=E6=A2=AF.md=20Rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0746.使用最小花费爬楼梯.md Rust版本
---
...271\347\210\254\346\245\274\346\242\257.md" | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md"
index c2c737150e..0006f7ac93 100644
--- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md"
+++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md"
@@ -288,6 +288,24 @@ function minCostClimbingStairs(cost: number[]): number {
};
```
+### Rust
+
+```Rust
+use std::cmp::min;
+impl Solution {
+ pub fn min_cost_climbing_stairs(cost: Vec) -> i32 {
+ let len = cost.len();
+ let mut dp = vec![0; len];
+ dp[0] = cost[0];
+ dp[1] = cost[1];
+ for i in 2..len {
+ dp[i] = min(dp[i-1], dp[i-2]) + cost[i];
+ }
+ min(dp[len-1], dp[len-2])
+ }
+}
+```
+
### C
```c
From 9b3468e3b3e7ff844bc1c8d1a5bbb141072d5689 Mon Sep 17 00:00:00 2001
From: Ezralin <10881430+ezralin@user.noreply.gitee.com>
Date: Fri, 8 Jul 2022 14:10:18 +0800
Subject: [PATCH 453/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A00034=20Kotlin?=
=?UTF-8?q?=E7=89=88,0203=20Kotlin=E7=89=88,0209=20Koltin=E7=89=88,=200977?=
=?UTF-8?q?=20Kotlin=E7=89=88?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...00\344\270\252\344\275\215\347\275\256.md" | 43 +++++++++++++++++++
...76\350\241\250\345\205\203\347\264\240.md" | 34 +++++++++++++++
...04\345\255\220\346\225\260\347\273\204.md" | 32 ++++++++++++++
...04\347\232\204\345\271\263\346\226\271.md" | 28 ++++++++++++
4 files changed, 137 insertions(+)
diff --git "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md"
index a81b36416a..6922c399dc 100644
--- "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md"
+++ "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md"
@@ -584,5 +584,48 @@ object Solution {
```
+### Kotlin
+```kotlin
+class Solution {
+ fun searchRange(nums: IntArray, target: Int): IntArray {
+ var index = binarySearch(nums, target)
+ // 没找到,返回[-1, -1]
+ if (index == -1) return intArrayOf(-1, -1)
+ var left = index
+ var right = index
+ // 寻找左边界
+ while (left - 1 >=0 && nums[left - 1] == target){
+ left--
+ }
+ // 寻找右边界
+ while (right + 1 target) {
+ right = middle - 1
+ }
+ else {
+ if (nums[middle] < target) {
+ left = middle + 1
+ }
+ else {
+ return middle
+ }
+ }
+ }
+ // 没找到,返回-1
+ return -1
+ }
+}
+```
+
-----------------------
diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
index b79d29a50d..ea936093f8 100644
--- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
+++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
@@ -532,5 +532,39 @@ object Solution {
}
}
```
+Kotlin:
+```kotlin
+/**
+ * Example:
+ * var li = ListNode(5)
+ * var v = li.`val`
+ * Definition for singly-linked list.
+ * class ListNode(var `val`: Int) {
+ * var next: ListNode? = null
+ * }
+ */
+class Solution {
+ fun removeElements(head: ListNode?, `val`: Int): ListNode? {
+ // 使用虚拟节点,令该节点指向head
+ var dummyNode = ListNode(-1)
+ dummyNode.next = head
+ // 使用cur遍历链表各个节点
+ var cur = dummyNode
+ // 判断下个节点是否为空
+ while (cur.next != null) {
+ // 符合条件,移除节点
+ if (cur.next.`val` == `val`) {
+ cur.next = cur.next.next
+ }
+ // 不符合条件,遍历下一节点
+ else {
+ cur = cur.next
+ }
+ }
+ // 注意:返回的不是虚拟节点
+ return dummyNode.next
+ }
+}
+```
-----------------------
diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
index 090e73f073..c1633def0e 100644
--- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
+++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
@@ -417,6 +417,38 @@ class Solution {
}
}
```
+滑动窗口
+```kotlin
+class Solution {
+ fun minSubArrayLen(target: Int, nums: IntArray): Int {
+ // 左边界 和 右边界
+ var left: Int = 0
+ var right: Int = 0
+ // sum 用来记录和
+ var sum: Int = 0
+ // result记录一个固定值,便于判断是否存在的这样的数组
+ var result: Int = Int.MAX_VALUE
+ // subLenth记录长度
+ var subLength = Int.MAX_VALUE
+
+
+ while (right < nums.size) {
+ // 从数组首元素开始逐次求和
+ sum += nums[right++]
+ // 判断
+ while (sum >= target) {
+ var temp = right - left
+ // 每次和上一次比较求出最小数组长度
+ subLength = if (subLength > temp) temp else subLength
+ // sum减少,左边界右移
+ sum -= nums[left++]
+ }
+ }
+ // 如果subLength为初始值,则说明长度为0,否则返回subLength
+ return if(subLength == result) 0 else subLength
+ }
+}
+```
Scala:
滑动窗口:
diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
index 458107dd2e..d46b525993 100644
--- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
+++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md"
@@ -362,6 +362,8 @@ class Solution {
```
Kotlin:
+
+双指针法
```kotlin
class Solution {
// 双指针法
@@ -383,6 +385,32 @@ class Solution {
}
}
```
+骚操作(暴力思路)
+```kotlin
+class Solution {
+ fun sortedSquares(nums: IntArray): IntArray {
+ // left 与 right 用来控制循环,类似于滑动窗口
+ var left: Int = 0;
+ var right: Int = nums.size - 1;
+ // 将每个数字的平方经过排序后加入result数值
+ var result: IntArray = IntArray(nums.size);
+ var k: Int = nums.size - 1;
+ while (left <= right) {
+ // 从大到小,从后向前填满数组
+ // [left, right] 控制循环
+ if (nums[left] * nums[left] > nums[right] * nums[right]) {
+ result[k--] = nums[left] * nums[left]
+ left++
+ }
+ else {
+ result[k--] = nums[right] * nums[right]
+ right--
+ }
+ }
+ return result
+ }
+}
+```
Scala:
From a8897483eade1262627d360544e7022951162243 Mon Sep 17 00:00:00 2001
From: BaoTaoqi <464115280@qq.com>
Date: Fri, 8 Jul 2022 14:40:26 +0800
Subject: [PATCH 454/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200062.=E4=B8=8D?=
=?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84.md=20Rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0062.不同路径.md Rust版本
---
...15\345\220\214\350\267\257\345\276\204.md" | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md"
index 02bcc2aeea..aa7c8ab823 100644
--- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md"
+++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md"
@@ -374,6 +374,30 @@ function uniquePaths(m: number, n: number): number {
};
```
+### Rust
+
+```Rust
+impl Solution {
+ pub fn unique_paths(m: i32, n: i32) -> i32 {
+ let m = m as usize;
+ let n = n as usize;
+ let mut dp = vec![vec![0; n]; m];
+ for i in 0..m {
+ dp[i][0] = 1;
+ }
+ for j in 0..n {
+ dp[0][j] = 1;
+ }
+ for i in 1..m {
+ for j in 1..n {
+ dp[i][j] = dp[i-1][j] + dp[i][j-1];
+ }
+ }
+ dp[m-1][n-1]
+ }
+}
+```
+
### C
```c
From 821cca116c6d42020ddac3bbb68c632eba7de132 Mon Sep 17 00:00:00 2001
From: BaoTaoqi <464115280@qq.com>
Date: Fri, 8 Jul 2022 15:11:49 +0800
Subject: [PATCH 455/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200063.=E4=B8=8D?=
=?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84II.md=20Rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0063.不同路径II.md Rust版本
---
...\345\220\214\350\267\257\345\276\204II.md" | 36 +++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
index ca34055a60..e3857db6c9 100644
--- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
+++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md"
@@ -384,6 +384,42 @@ function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
};
```
+### Rust
+
+```Rust
+impl Solution {
+ pub fn unique_paths_with_obstacles(obstacle_grid: Vec>) -> i32 {
+ let m: usize = obstacle_grid.len();
+ let n: usize = obstacle_grid[0].len();
+ if obstacle_grid[0][0] == 1 || obstacle_grid[m-1][n-1] == 1 {
+ return 0;
+ }
+ let mut dp = vec![vec![0; n]; m];
+ for i in 0..m {
+ if obstacle_grid[i][0] == 1 {
+ break;
+ }
+ else { dp[i][0] = 1; }
+ }
+ for j in 0..n {
+ if obstacle_grid[0][j] == 1 {
+ break;
+ }
+ else { dp[0][j] = 1; }
+ }
+ for i in 1..m {
+ for j in 1..n {
+ if obstacle_grid[i][j] == 1 {
+ continue;
+ }
+ dp[i][j] = dp[i-1][j] + dp[i][j-1];
+ }
+ }
+ dp[m-1][n-1]
+ }
+}
+```
+
### C
```c
From e2d16c5894b2b9abed99c42522977dac2b6e8654 Mon Sep 17 00:00:00 2001
From: BaoTaoqi <464115280@qq.com>
Date: Sun, 10 Jul 2022 09:09:02 +0800
Subject: [PATCH 456/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200017.=E7=94=B5?=
=?UTF-8?q?=E8=AF=9D=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84?=
=?UTF-8?q?=E5=90=88=200077.=E7=BB=84=E5=90=88=200077.=E7=BB=84=E5=90=88?=
=?UTF-8?q?=E4=BC=98=E5=8C=96=200216.=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8CI?=
=?UTF-8?q?II=20Rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0017.电话号码的字母组合 0077.组合 0077.组合优化 0216.组合总和III Rust版本
---
...27\346\257\215\347\273\204\345\220\210.md" | 43 ++++++++++++++++
"problems/0077.\347\273\204\345\220\210.md" | 50 +++++++++++++++++++
...04\345\220\210\344\274\230\345\214\226.md" | 26 ++++++++++
...345\220\210\346\200\273\345\222\214III.md" | 29 +++++++++++
4 files changed, 148 insertions(+)
diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md"
index e357a33b6f..5778e903c5 100644
--- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md"
+++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md"
@@ -454,6 +454,49 @@ function letterCombinations(digits: string): string[] {
};
```
+## Rust
+
+```Rust
+impl Solution {
+ fn backtracking(result: &mut Vec, s: &mut String, map: &[&str; 10], digits: &String, index: usize) {
+ let len = digits.len();
+ if len == index {
+ result.push(s.to_string());
+ return;
+ }
+ // 在保证不会越界的情况下使用unwrap()将Some()中的值提取出来
+ let digit= digits.chars().nth(index).unwrap().to_digit(10).unwrap() as usize;
+ let letters = map[digit];
+ for i in letters.chars() {
+ s.push(i);
+ Self::backtracking(result, s, &map, &digits, index+1);
+ s.pop();
+ }
+ }
+ pub fn letter_combinations(digits: String) -> Vec {
+ if digits.len() == 0 {
+ return vec![];
+ }
+ const MAP: [&str; 10] = [
+ "",
+ "",
+ "abc",
+ "def",
+ "ghi",
+ "jkl",
+ "mno",
+ "pqrs",
+ "tuv",
+ "wxyz"
+ ];
+ let mut result: Vec = Vec::new();
+ let mut s: String = String::new();
+ Self::backtracking(&mut result, &mut s, &MAP, &digits, 0);
+ result
+ }
+}
+```
+
## C
```c
diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md"
index fc72be150a..5a4811bab7 100644
--- "a/problems/0077.\347\273\204\345\220\210.md"
+++ "b/problems/0077.\347\273\204\345\220\210.md"
@@ -535,6 +535,56 @@ func backtrack(n,k,start int,track []int){
}
```
+### Rust
+
+```Rust
+impl Solution {
+ fn backtracking(result: &mut Vec>, path: &mut Vec, n: i32, k: i32, startIndex: i32) {
+ let len= path.len() as i32;
+ if len == k{
+ result.push(path.to_vec());
+ return;
+ }
+ for i in startIndex..= n {
+ path.push(i);
+ Self::backtracking(result, path, n, k, i+1);
+ path.pop();
+ }
+ }
+ pub fn combine(n: i32, k: i32) -> Vec> {
+ let mut result: Vec> = Vec::new();
+ let mut path: Vec = Vec::new();
+ Self::backtracking(&mut result, &mut path, n, k, 1);
+ result
+ }
+}
+```
+
+剪枝
+```Rust
+impl Solution {
+ fn backtracking(result: &mut Vec>, path: &mut Vec, n: i32, k: i32, startIndex: i32) {
+ let len= path.len() as i32;
+ if len == k{
+ result.push(path.to_vec());
+ return;
+ }
+ // 此处剪枝
+ for i in startIndex..= n - (k - len) + 1 {
+ path.push(i);
+ Self::backtracking(result, path, n, k, i+1);
+ path.pop();
+ }
+ }
+ pub fn combine(n: i32, k: i32) -> Vec> {
+ let mut result: Vec> = Vec::new();
+ let mut path: Vec = Vec::new();
+ Self::backtracking(&mut result, &mut path, n, k, 1);
+ result
+ }
+}
+```
+
### C
```c
int* path;
diff --git "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md"
index 8d74256602..e336fb7547 100644
--- "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md"
+++ "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md"
@@ -261,6 +261,32 @@ function combine(n: number, k: number): number[][] {
};
```
+Rust:
+
+```Rust
+impl Solution {
+ fn backtracking(result: &mut Vec>, path: &mut Vec, n: i32, k: i32, startIndex: i32) {
+ let len= path.len() as i32;
+ if len == k{
+ result.push(path.to_vec());
+ return;
+ }
+ // 此处剪枝
+ for i in startIndex..= n - (k - len) + 1 {
+ path.push(i);
+ Self::backtracking(result, path, n, k, i+1);
+ path.pop();
+ }
+ }
+ pub fn combine(n: i32, k: i32) -> Vec> {
+ let mut result: Vec> = Vec::new();
+ let mut path: Vec = Vec::new();
+ Self::backtracking(&mut result, &mut path, n, k, 1);
+ result
+ }
+}
+```
+
C:
```c
diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
index 2c1bf717e8..0a59e21643 100644
--- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
+++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
@@ -411,6 +411,35 @@ function combinationSum3(k: number, n: number): number[][] {
};
```
+## Rust
+
+```Rust
+impl Solution {
+ fn backtracking(result: &mut Vec>, path:&mut Vec, targetSum:i32, k: i32, mut sum: i32, startIndex: i32) {
+ let len = path.len() as i32;
+ if len == k {
+ if sum == targetSum {
+ result.push(path.to_vec());
+ }
+ return;
+ }
+ for i in startIndex..=9 {
+ sum += i;
+ path.push(i);
+ Self::backtracking(result, path, targetSum, k, sum, i+1);
+ sum -= i;
+ path.pop();
+ }
+ }
+ pub fn combination_sum3(k: i32, n: i32) -> Vec> {
+ let mut result: Vec> = Vec::new();
+ let mut path: Vec = Vec::new();
+ Self::backtracking(&mut result, &mut path, n, k, 0, 1);
+ result
+ }
+}
+```
+
## C
```c
From 85e0d6c85d593339152483a3fea33ab23cdcd5b3 Mon Sep 17 00:00:00 2001
From: AronJudge <2286381138@qq.com>
Date: Mon, 11 Jul 2022 11:06:51 +0800
Subject: [PATCH 457/551] =?UTF-8?q?=E6=9B=B4=E6=96=B00347=20=E5=89=8DK?=
=?UTF-8?q?=E4=B8=AA=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
由原来的构建小顶堆改为构建大顶堆,减少部分代码逻辑,并增加了注释
通过LeetCode提交代码,修改后的执行时间更快。
---
...252\351\253\230\351\242\221\345\205\203\347\264\240.md" | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md"
index a04041ccdf..0c978b2a6c 100644
--- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md"
+++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md"
@@ -141,13 +141,10 @@ class Solution {
}
Set> entries = map.entrySet();
- // 根据map的value值正序排,相当于一个小顶堆
- PriorityQueue> queue = new PriorityQueue<>((o1, o2) -> o1.getValue() - o2.getValue());
+ // 根据map的value值,构建于一个大顶堆(o1 - o2: 小顶堆, o2 - o1 : 大顶堆)
+ PriorityQueue> queue = new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue());
for (Map.Entry entry : entries) {
queue.offer(entry);
- if (queue.size() > k) {
- queue.poll();
- }
}
for (int i = k - 1; i >= 0; i--) {
result[i] = queue.poll().getKey();
From c265c9220c62a78109c1f6033d47f9bc8b3f47bd Mon Sep 17 00:00:00 2001
From: BaoTaoqi <464115280@qq.com>
Date: Mon, 11 Jul 2022 14:50:03 +0800
Subject: [PATCH 458/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200203.=E7=A7=BB?=
=?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0=200344.=E5=8F=8D?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=200376.=E6=91=86=E5=8A=A8?=
=?UTF-8?q?=E5=BA=8F=E5=88=97=200541.=E5=8F=8D=E8=BD=AC=E5=AD=97=E7=AC=A6?=
=?UTF-8?q?=E4=B8=B2II=20=E5=89=91=E6=8C=87Offer05.=E6=9B=BF=E6=8D=A2?=
=?UTF-8?q?=E7=A9=BA=E6=A0=BC=20Rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0203.移除链表元素 0344.反转字符串 0376.摆动序列 0541.反转字符串II 剑指Offer05.替换空格 Rust版本
---
...76\350\241\250\345\205\203\347\264\240.md" | 18 ++++++-----
...54\345\255\227\347\254\246\344\270\262.md" | 16 ++++++++++
...06\345\212\250\345\272\217\345\210\227.md" | 23 ++++++++++++++
...\345\255\227\347\254\246\344\270\262II.md" | 31 +++++++++++++++++++
...77\346\215\242\347\251\272\346\240\274.md" | 31 +++++++++++++++++++
5 files changed, 111 insertions(+), 8 deletions(-)
diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
index b79d29a50d..cb46dc703b 100644
--- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
+++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md"
@@ -487,17 +487,19 @@ RUST:
// }
impl Solution {
pub fn remove_elements(head: Option>, val: i32) -> Option> {
- let mut head = head;
- let mut dummy_head = ListNode::new(0);
- let mut cur = &mut dummy_head;
- while let Some(mut node) = head {
- head = std::mem::replace(&mut node.next, None);
- if node.val != val {
- cur.next = Some(node);
+ let mut dummyHead = Box::new(ListNode::new(0));
+ dummyHead.next = head;
+ let mut cur = dummyHead.as_mut();
+ // 使用take()替换std::men::replace(&mut node.next, None)达到相同的效果,并且更普遍易读
+ while let Some(nxt) = cur.next.take() {
+ if nxt.val == val {
+ cur.next = nxt.next;
+ } else {
+ cur.next = Some(nxt);
cur = cur.next.as_mut().unwrap();
}
}
- dummy_head.next
+ dummyHead.next
}
}
```
diff --git "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md"
index aba6e2f381..3138f60f8b 100644
--- "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md"
@@ -238,6 +238,22 @@ func reverseString(_ s: inout [Character]) {
```
+Rust:
+```Rust
+impl Solution {
+ pub fn reverse_string(s: &mut Vec) {
+ let (mut left, mut right) = (0, s.len()-1);
+ while left < right {
+ let temp = s[left];
+ s[left] = s[right];
+ s[right] = temp;
+ left += 1;
+ right -= 1;
+ }
+ }
+}
+```
+
C:
```c
void reverseString(char* s, int sSize){
diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
index 00f8f70cf9..33a5282d1c 100644
--- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
+++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
@@ -298,6 +298,29 @@ var wiggleMaxLength = function(nums) {
};
```
+### Rust
+**贪心**
+```Rust
+impl Solution {
+ pub fn wiggle_max_length(nums: Vec) -> i32 {
+ let len = nums.len() as usize;
+ if len <= 1 {
+ return len as i32;
+ }
+ let mut preDiff = 0;
+ let mut curDiff = 0;
+ let mut result = 1;
+ for i in 0..len-1 {
+ curDiff = nums[i+1] - nums[i];
+ if (preDiff <= 0 && curDiff > 0) || (preDiff >= 0 && curDiff < 0) {
+ result += 1;
+ preDiff = curDiff;
+ }
+ }
+ result
+ }
+}
+```
### C
**贪心**
diff --git "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md"
index 84061ef56b..7ef6463e0c 100644
--- "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md"
+++ "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md"
@@ -389,5 +389,36 @@ object Solution {
}
}
```
+
+Rust:
+
+```Rust
+impl Solution {
+ pub fn reverse(s: &mut Vec, mut begin: usize, mut end: usize){
+ while begin < end {
+ let temp = s[begin];
+ s[begin] = s[end];
+ s[end] = temp;
+ begin += 1;
+ end -= 1;
+ }
+ }
+ pub fn reverse_str(s: String, k: i32) -> String {
+ let len = s.len();
+ let k = k as usize;
+ let mut s = s.chars().collect::>();
+ for i in (0..len).step_by(2 * k) {
+ if i + k < len {
+ Self::reverse(&mut s, i, i + k - 1);
+ }
+ else {
+ Self::reverse(&mut s, i, len - 1);
+ }
+ }
+ s.iter().collect::()
+ }
+}
+```
+
-----------------------
diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md"
index 78b03b17be..e1ccc458a4 100644
--- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md"
+++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md"
@@ -506,6 +506,37 @@ function spaceLen($s){
}
```
+Rust
+
+```Rust
+impl Solution {
+ pub fn replace_space(s: String) -> String {
+ let mut len: usize = s.len();
+ let mut s = s.chars().collect::>();
+ let mut count = 0;
+ for i in &s {
+ if i.is_ascii_whitespace() {
+ count += 1;
+ }
+ }
+ let mut new_len = len + count * 2;
+ s.resize(new_len, ' ');
+ while len < new_len {
+ len -= 1;
+ new_len -= 1;
+ if s[len].is_ascii_whitespace() {
+ s[new_len] = '0';
+ s[new_len - 1] = '2';
+ s[new_len - 2] = '%';
+ new_len -= 2;
+ }
+ else { s[new_len] = s[len] }
+ }
+ s.iter().collect::()
+ }
+}
+```
+
-----------------------
From e0a422859904ddae865cc828bc30d6620ccb8137 Mon Sep 17 00:00:00 2001
From: Ezralin <10881430+ezralin@user.noreply.gitee.com>
Date: Mon, 11 Jul 2022 15:09:41 +0800
Subject: [PATCH 459/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A00206.=E7=BF=BB?=
=?UTF-8?q?=E8=BD=AC=E9=93=BE=E8=A1=A8=20Kotlin=E8=A7=A3=E6=9E=90=E7=89=88?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...73\350\275\254\351\223\276\350\241\250.md" | 35 +++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md"
index 24ec7b943a..bb9b62d52a 100644
--- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md"
+++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md"
@@ -420,6 +420,41 @@ fun reverseList(head: ListNode?): ListNode? {
return pre
}
```
+```kotlin
+/**
+ * Example:
+ * var li = ListNode(5)
+ * var v = li.`val`
+ * Definition for singly-linked list.
+ * class ListNode(var `val`: Int) {
+ * var next: ListNode? = null
+ * }
+ */
+class Solution {
+ fun reverseList(head: ListNode?): ListNode? {
+ // temp用来存储临时的节点
+ var temp: ListNode?
+ // cur用来遍历链表
+ var cur: ListNode? = head
+ // pre用来作为链表反转的工具
+ // pre是比pre前一位的节点
+ var pre: ListNode? = null
+ while (cur != null) {
+ // 临时存储原本cur的下一个节点
+ temp = cur.next
+ // 使cur下一节点地址为它之前的
+ cur.next = pre
+ // 之后随着cur的遍历移动pre
+ pre = cur;
+ // 移动cur遍历链表各个节点
+ cur = temp;
+ }
+ // 由于开头使用pre为null,所以cur等于链表本身长度+1,
+ // 此时pre在cur前一位,所以此时pre为头节点
+ return pre;
+ }
+}
+```
Swift:
```swift
From 4e77e911b397938419a41c2a06684318740fc907 Mon Sep 17 00:00:00 2001
From: cezarbbb <105843128+cezarbbb@users.noreply.github.com>
Date: Mon, 11 Jul 2022 17:42:21 +0800
Subject: [PATCH 460/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200151.=E7=BF=BB?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95?=
=?UTF-8?q?=E8=AF=8D=20Rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0151.翻转字符串里的单词 Rust版本
---
...14\347\232\204\345\215\225\350\257\215.md" | 53 ++++++++++++++++++-
1 file changed, 52 insertions(+), 1 deletion(-)
diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
index 38372f91e4..552584f5dc 100644
--- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
+++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md"
@@ -864,7 +864,58 @@ function reverseString(&$s, $start, $end) {
return ;
}
```
-
+Rust:
+
+```Rust
+// 根据C++版本二思路进行实现
+// 函数名根据Rust编译器建议由驼峰命名法改为蛇形命名法
+impl Solution {
+ pub fn reverse(s: &mut Vec, mut begin: usize, mut end: usize){
+ while begin < end {
+ let temp = s[begin];
+ s[begin] = s[end];
+ s[end] = temp;
+ begin += 1;
+ end -= 1;
+ }
+}
+pub fn remove_extra_spaces(s: &mut Vec) {
+ let mut slow: usize = 0;
+ let len = s.len();
+ // 注意这里不能用for循环,不然在里面那个while循环中对i的递增会失效
+ let mut i: usize = 0;
+ while i < len {
+ if !s[i].is_ascii_whitespace() {
+ if slow != 0 {
+ s[slow] = ' ';
+ slow += 1;
+ }
+ while i < len && !s[i].is_ascii_whitespace() {
+ s[slow] = s[i];
+ slow += 1;
+ i += 1;
+ }
+ }
+ i += 1;
+ }
+ s.resize(slow, ' ');
+ }
+ pub fn reverse_words(s: String) -> String {
+ let mut s = s.chars().collect::>();
+ Self::remove_extra_spaces(&mut s);
+ let len = s.len();
+ Self::reverse(&mut s, 0, len - 1);
+ let mut start = 0;
+ for i in 0..=len {
+ if i == len || s[i].is_ascii_whitespace() {
+ Self::reverse(&mut s, start, i - 1);
+ start = i + 1;
+ }
+ }
+ s.iter().collect::()
+ }
+}
+```
-----------------------
From ba081f84a85ff75134c4db551e0e631b86693081 Mon Sep 17 00:00:00 2001
From: AronJudge <2286381138@qq.com>
Date: Tue, 12 Jul 2022 11:36:26 +0800
Subject: [PATCH 461/551] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200104.=20=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
代码错误,更正代码,通过LeetCode代码检验。
---
...00\345\244\247\346\267\261\345\272\246.md" | 25 +++++++++----------
1 file changed, 12 insertions(+), 13 deletions(-)
diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md"
index ed27f95d11..559801897a 100644
--- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md"
+++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md"
@@ -294,14 +294,13 @@ class solution {
/**
* 递归法
*/
- public int maxdepth(treenode root) {
+ public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
- int leftdepth = maxdepth(root.left);
- int rightdepth = maxdepth(root.right);
- return math.max(leftdepth, rightdepth) + 1;
-
+ int leftDepth = maxDepth(root.left);
+ int rightDepth = maxDepth(root.right);
+ return Math.max(leftDepth, rightDepth) + 1;
}
}
```
@@ -311,23 +310,23 @@ class solution {
/**
* 迭代法,使用层序遍历
*/
- public int maxdepth(treenode root) {
+ public int maxDepth(TreeNode root) {
if(root == null) {
return 0;
}
- deque deque = new linkedlist<>();
+ Deque deque = new LinkedList<>();
deque.offer(root);
int depth = 0;
- while (!deque.isempty()) {
+ while (!deque.isEmpty()) {
int size = deque.size();
depth++;
for (int i = 0; i < size; i++) {
- treenode poll = deque.poll();
- if (poll.left != null) {
- deque.offer(poll.left);
+ TreeNode node = deque.poll();
+ if (node.left != null) {
+ deque.offer(node.left);
}
- if (poll.right != null) {
- deque.offer(poll.right);
+ if (node.right != null) {
+ deque.offer(node.right);
}
}
}
From a8e19d60bb371a44296134932b7470825c1fda6a Mon Sep 17 00:00:00 2001
From: cezarbbb <105843128+cezarbbb@users.noreply.github.com>
Date: Tue, 12 Jul 2022 14:08:50 +0800
Subject: [PATCH 462/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200202.=E5=BF=AB?=
=?UTF-8?q?=E4=B9=90=E6=95=B0=20Rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0202.快乐数 Rust版本
---
...2.\345\277\253\344\271\220\346\225\260.md" | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md"
index 0bea0c7233..2303765c2f 100644
--- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md"
+++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md"
@@ -315,6 +315,36 @@ class Solution {
}
```
+Rust:
+```Rust
+use std::collections::HashSet;
+impl Solution {
+ pub fn get_sum(mut n: i32) -> i32 {
+ let mut sum = 0;
+ while n > 0 {
+ sum += (n % 10) * (n % 10);
+ n /= 10;
+ }
+ sum
+ }
+
+ pub fn is_happy(n: i32) -> bool {
+ let mut n = n;
+ let mut set = HashSet::new();
+ loop {
+ let sum = Self::get_sum(n);
+ if sum == 1 {
+ return true;
+ }
+ if set.contains(&sum) {
+ return false;
+ } else { set.insert(sum); }
+ n = sum;
+ }
+ }
+}
+```
+
C:
```C
typedef struct HashNodeTag {
From eb3eb336dedfcbbcc0d90eab392e4799479d675e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=A1=9C=E5=B0=8F=E8=B7=AF=E4=B8=83=E8=91=89?=
<20304773@qq.com>
Date: Tue, 12 Jul 2022 14:09:05 +0800
Subject: [PATCH 463/551] =?UTF-8?q?Update=200059.=E8=9E=BA=E6=97=8B?=
=?UTF-8?q?=E7=9F=A9=E9=98=B5II.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...\346\227\213\347\237\251\351\230\265II.md" | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
index bf0a279e25..9690abb602 100644
--- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
+++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md"
@@ -598,5 +598,30 @@ object Solution {
}
}
```
+C#:
+```csharp
+public class Solution {
+ public int[][] GenerateMatrix(int n) {
+ int[][] answer = new int[n][];
+ for(int i = 0; i < n; i++)
+ answer[i] = new int[n];
+ int start = 0;
+ int end = n - 1;
+ int tmp = 1;
+ while(tmp < n * n)
+ {
+ for(int i = start; i < end; i++) answer[start][i] = tmp++;
+ for(int i = start; i < end; i++) answer[i][end] = tmp++;
+ for(int i = end; i > start; i--) answer[end][i] = tmp++;
+ for(int i = end; i > start; i--) answer[i][start] = tmp++;
+ start++;
+ end--;
+ }
+ if(n % 2 == 1) answer[n / 2][n / 2] = tmp;
+ return answer;
+ }
+}
+```
+
-----------------------
From 07ab44ede3fa043184754a638f59e6b76a7c3e0a Mon Sep 17 00:00:00 2001
From: cezarbbb <105843128+cezarbbb@users.noreply.github.com>
Date: Tue, 12 Jul 2022 15:26:07 +0800
Subject: [PATCH 464/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200015.=E4=B8=89?=
=?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=200018.=E5=9B=9B=E6=95=B0=E4=B9=8B?=
=?UTF-8?q?=E5=92=8C=20Rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0015.三数之和 0018.四数之和 Rust版本
---
...11\346\225\260\344\271\213\345\222\214.md" | 65 +++++++++++++++++++
...33\346\225\260\344\271\213\345\222\214.md" | 45 +++++++++++++
2 files changed, 110 insertions(+)
diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
index 4f1d711a7a..94885b0c6f 100644
--- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
@@ -554,6 +554,71 @@ func threeSum(_ nums: [Int]) -> [[Int]] {
}
```
+Rust:
+```Rust
+// 哈希解法
+use std::collections::HashSet;
+impl Solution {
+ pub fn three_sum(nums: Vec) -> Vec> {
+ let mut result: Vec> = Vec::new();
+ let mut nums = nums;
+ nums.sort();
+ let len = nums.len();
+ for i in 0..len {
+ if nums[i] > 0 { break; }
+ if i > 0 && nums[i] == nums[i - 1] { continue; }
+ let mut set = HashSet::new();
+ for j in (i + 1)..len {
+ if j > i + 2 && nums[j] == nums[j - 1] && nums[j] == nums[j - 2] { continue; }
+ let c = 0 - (nums[i] + nums[j]);
+ if set.contains(&c) {
+ result.push(vec![nums[i], nums[j], c]);
+ set.remove(&c);
+ } else { set.insert(nums[j]); }
+ }
+ }
+ result
+ }
+}
+```
+
+```Rust
+// 双指针法
+use std::collections::HashSet;
+impl Solution {
+ pub fn three_sum(nums: Vec) -> Vec> {
+ let mut result: Vec> = Vec::new();
+ let mut nums = nums;
+ nums.sort();
+ let len = nums.len();
+ for i in 0..len {
+ if nums[i] > 0 { return result; }
+ if i > 0 && nums[i] == nums[i - 1] { continue; }
+ let (mut left, mut right) = (i + 1, len - 1);
+ while left < right {
+ if nums[i] + nums[left] + nums[right] > 0 {
+ right -= 1;
+ // 去重
+ while left < right && nums[right] == nums[right + 1] { right -= 1; }
+ } else if nums[i] + nums[left] + nums[right] < 0 {
+ left += 1;
+ // 去重
+ while left < right && nums[left] == nums[left - 1] { left += 1; }
+ } else {
+ result.push(vec![nums[i], nums[left], nums[right]]);
+ // 去重
+ right -= 1;
+ left += 1;
+ while left < right && nums[right] == nums[right + 1] { right -= 1; }
+ while left < right && nums[left] == nums[left - 1] { left += 1; }
+ }
+ }
+ }
+ result
+ }
+}
+```
+
C:
```C
//qsort辅助cmp函数
diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
index 2146a11469..f6cfad29cc 100644
--- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
@@ -522,6 +522,51 @@ public class Solution
}
}
```
+
+Rust:
+```Rust
+impl Solution {
+ pub fn four_sum(nums: Vec, target: i32) -> Vec> {
+ let mut result: Vec> = Vec::new();
+ let mut nums = nums;
+ nums.sort();
+ let len = nums.len();
+ for k in 0..len {
+ // 剪枝
+ if nums[k] > target && (nums[k] > 0 || target > 0) { break; }
+ // 去重
+ if k > 0 && nums[k] == nums[k - 1] { continue; }
+ for i in (k + 1)..len {
+ // 剪枝
+ if nums[k] + nums[i] > target && (nums[k] + nums[i] >= 0 || target >= 0) { break; }
+ // 去重
+ if i > k + 1 && nums[i] == nums[i - 1] { continue; }
+ let (mut left, mut right) = (i + 1, len - 1);
+ while left < right {
+ if nums[k] + nums[i] > target - (nums[left] + nums[right]) {
+ right -= 1;
+ // 去重
+ while left < right && nums[right] == nums[right + 1] { right -= 1; }
+ } else if nums[k] + nums[i] < target - (nums[left] + nums[right]) {
+ left += 1;
+ // 去重
+ while left < right && nums[left] == nums[left - 1] { left += 1; }
+ } else {
+ result.push(vec![nums[k], nums[i], nums[left], nums[right]]);
+ // 去重
+ while left < right && nums[right] == nums[right - 1] { right -= 1; }
+ while left < right && nums[left] == nums[left + 1] { left += 1; }
+ left += 1;
+ right -= 1;
+ }
+ }
+ }
+ }
+ result
+ }
+}
+```
+
Scala:
```scala
object Solution {
From f5e1834439d64d09b2e8d8b4e5141510e205dbc3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=9D=A8=E6=9D=A8?= <2979742951@qq.com>
Date: Wed, 13 Jul 2022 16:15:12 +0800
Subject: [PATCH 465/551] =?UTF-8?q?=E5=8F=8D=E8=BD=AC=E5=AD=97=E7=AC=A6?=
=?UTF-8?q?=E4=B8=B2II=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=B8=80=E7=A7=8DC++?=
=?UTF-8?q?=E8=A7=A3=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...4\345\255\227\347\254\246\344\270\262II.md" | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md"
index 84061ef56b..d9b9466cd0 100644
--- "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md"
+++ "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md"
@@ -63,6 +63,24 @@ public:
};
```
+
+```
+class Solution {
+public:
+ string reverseStr(string s, int k) {
+ int n=s.size(),pos=0;
+ while(pos
Date: Wed, 13 Jul 2022 19:27:34 +0800
Subject: [PATCH 466/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=89=91=E6=8C=87?=
=?UTF-8?q?Offer=2058-II.=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97=E7=AC=A6?=
=?UTF-8?q?=E4=B8=B2=20Rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 剑指Offer 58-II.左旋转字符串 Rust版本
---
...54\345\255\227\347\254\246\344\270\262.md" | 25 ++++++++++++++++++-
1 file changed, 24 insertions(+), 1 deletion(-)
diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md"
index 4674c14170..de4a903000 100644
--- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md"
@@ -341,7 +341,30 @@ object Solution {
}
```
-
+Rust:
+
+```Rust
+impl Solution {
+ pub fn reverse(s: &mut Vec, mut begin: usize, mut end: usize){
+ while begin < end {
+ let temp = s[begin];
+ s[begin] = s[end];
+ s[end] = temp;
+ begin += 1;
+ end -= 1;
+ }
+ }
+ pub fn reverse_left_words(s: String, n: i32) -> String {
+ let len = s.len();
+ let mut s = s.chars().collect::>();
+ let n = n as usize;
+ Self::reverse(&mut s, 0, n - 1);
+ Self::reverse(&mut s, n, len - 1);
+ Self::reverse(&mut s, 0, len - 1);
+ s.iter().collect::()
+ }
+}
+```
From e15beb5f14f212e2ffe24bedba1e5ae30fdcaba7 Mon Sep 17 00:00:00 2001
From: jiecheney <89916583+JakeVander@users.noreply.github.com>
Date: Thu, 14 Jul 2022 10:41:34 +0800
Subject: [PATCH 467/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200090.=E5=AD=90?=
=?UTF-8?q?=E9=9B=86II.md=20Python3=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.idea/.gitignore | 8 +++
.idea/encodings.xml | 6 ++
.idea/inspectionProfiles/Project_Default.xml | 70 +++++++++++++++++++
.../inspectionProfiles/profiles_settings.xml | 6 ++
.idea/leetcode-master.iml | 8 +++
.idea/misc.xml | 11 +++
.idea/modules.xml | 8 +++
.idea/vagrant.xml | 7 ++
.idea/vcs.xml | 6 ++
"problems/0090.\345\255\220\351\233\206II.md" | 27 +++++++
10 files changed, 157 insertions(+)
create mode 100644 .idea/.gitignore
create mode 100644 .idea/encodings.xml
create mode 100644 .idea/inspectionProfiles/Project_Default.xml
create mode 100644 .idea/inspectionProfiles/profiles_settings.xml
create mode 100644 .idea/leetcode-master.iml
create mode 100644 .idea/misc.xml
create mode 100644 .idea/modules.xml
create mode 100644 .idea/vagrant.xml
create mode 100644 .idea/vcs.xml
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000000..35410cacdc
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
new file mode 100644
index 0000000000..c2bae49d78
--- /dev/null
+++ b/.idea/encodings.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
new file mode 100644
index 0000000000..3f19e2d85a
--- /dev/null
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -0,0 +1,70 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml
new file mode 100644
index 0000000000..105ce2da2d
--- /dev/null
+++ b/.idea/inspectionProfiles/profiles_settings.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/leetcode-master.iml b/.idea/leetcode-master.iml
new file mode 100644
index 0000000000..d0876a78d0
--- /dev/null
+++ b/.idea/leetcode-master.iml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000000..8ca8e3b1af
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000000..7c250acde5
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vagrant.xml b/.idea/vagrant.xml
new file mode 100644
index 0000000000..a5aa786803
--- /dev/null
+++ b/.idea/vagrant.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000000..94a25f7f4c
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md"
index e85ec66d3c..d503846ea6 100644
--- "a/problems/0090.\345\255\220\351\233\206II.md"
+++ "b/problems/0090.\345\255\220\351\233\206II.md"
@@ -261,6 +261,33 @@ class Solution:
self.path.pop()
```
+### Python3
+```python3
+class Solution:
+ def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
+ res = []
+ path = []
+ nums.sort() # 去重需要先对数组进行排序
+
+ def backtracking(nums, startIndex):
+ # 终止条件
+ res.append(path[:])
+ if startIndex == len(nums):
+ return
+
+ # for循环
+ for i in range(startIndex, len(nums)):
+ # 数层去重
+ if i > startIndex and nums[i] == nums[i-1]: # 去重
+ continue
+ path.append(nums[i])
+ backtracking(nums, i+1)
+ path.pop()
+
+ backtracking(nums, 0)
+ return res
+```
+
### Go
```Go
From acd71b8b9e134fdc1fbe0f73d78b5f80b7b03a59 Mon Sep 17 00:00:00 2001
From: cezarbbb <105843128+cezarbbb@users.noreply.github.com>
Date: Thu, 14 Jul 2022 15:46:41 +0800
Subject: [PATCH 468/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200028.=E5=AE=9E?=
=?UTF-8?q?=E7=8E=B0strStr=200459.=E9=87=8D=E5=A4=8D=E7=9A=84=E5=AD=90?=
=?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=20Rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0028.实现strStr 0459.重复的子字符串 Rust版本
---
.../0028.\345\256\236\347\216\260strStr.md" | 44 +++++++++++++++++++
...20\345\255\227\347\254\246\344\270\262.md" | 32 ++++++++++++++
2 files changed, 76 insertions(+)
diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md"
index a95a52f814..1887e91bd3 100644
--- "a/problems/0028.\345\256\236\347\216\260strStr.md"
+++ "b/problems/0028.\345\256\236\347\216\260strStr.md"
@@ -1241,5 +1241,49 @@ function getNext(&$next, $s){
}
}
```
+
+Rust:
+
+> 前缀表统一不减一
+```Rust
+impl Solution {
+ pub fn get_next(next: &mut Vec, s: &Vec) {
+ let len = s.len();
+ let mut j = 0;
+ for i in 1..len {
+ while j > 0 && s[i] != s[j] {
+ j = next[j - 1];
+ }
+ if s[i] == s[j] {
+ j += 1;
+ }
+ next[i] = j;
+ }
+ }
+
+ pub fn str_str(haystack: String, needle: String) -> i32 {
+ let (haystack_len, needle_len) = (haystack.len(), needle.len());
+ if haystack_len == 0 { return 0; }
+ if haystack_len < needle_len { return -1;}
+ let (haystack, needle) = (haystack.chars().collect::>(), needle.chars().collect::>());
+ let mut next: Vec = vec![0; haystack_len];
+ Self::get_next(&mut next, &needle);
+ let mut j = 0;
+ for i in 0..haystack_len {
+ while j > 0 && haystack[i] != needle[j] {
+ j = next[j - 1];
+ }
+ if haystack[i] == needle[j] {
+ j += 1;
+ }
+ if j == needle_len {
+ return (i - needle_len + 1) as i32;
+ }
+ }
+ return -1;
+ }
+}
+```
+
-----------------------
diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
index bb55bd7ce0..4f45f4d733 100644
--- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
+++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md"
@@ -505,5 +505,37 @@ Swift:
}
```
+Rust:
+
+>前缀表统一不减一
+```Rust
+impl Solution {
+ pub fn get_next(next: &mut Vec, s: &Vec) {
+ let len = s.len();
+ let mut j = 0;
+ for i in 1..len {
+ while j > 0 && s[i] != s[j] {
+ j = next[j - 1];
+ }
+ if s[i] == s[j] {
+ j += 1;
+ }
+ next[i] = j;
+ }
+ }
+
+ pub fn repeated_substring_pattern(s: String) -> bool {
+ let s = s.chars().collect::>();
+ let len = s.len();
+ if len == 0 { return false; };
+ let mut next = vec![0; len];
+ Self::get_next(&mut next, &s);
+ if next[len - 1] != 0 && len % (len - (next[len - 1] )) == 0 { return true; }
+ return false;
+ }
+}
+```
+
+
-----------------------
From 781076a73d7d9de9f0fe525cbc8404358ca59990 Mon Sep 17 00:00:00 2001
From: Tristone
Date: Thu, 14 Jul 2022 22:39:17 +0800
Subject: [PATCH 469/551] =?UTF-8?q?=E9=9C=80=E8=A6=81=E5=90=8C=E6=97=B6?=
=?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=BE=93=E5=85=A5=E6=95=B0=E7=BB=84nums?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" | 1 +
1 file changed, 1 insertion(+)
diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
index 9d687cfb16..fd24e8f8ec 100644
--- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
+++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md"
@@ -219,6 +219,7 @@ func removeElement(nums []int, val int) int {
res++
}
}
+ nums=nums[:res]
return res
}
```
From 57840efc1a647803b24b5c793f4622c7e150721f Mon Sep 17 00:00:00 2001
From: cezarbbb <105843128+cezarbbb@users.noreply.github.com>
Date: Fri, 15 Jul 2022 20:06:53 +0800
Subject: [PATCH 470/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200039.=E7=BB=84?=
=?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8C=200040.=E7=BB=84=E5=90=88=E6=80=BB?=
=?UTF-8?q?=E5=92=8CII=20Rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0039.组合总和 0040.组合总和II Rust版本
---
...04\345\220\210\346\200\273\345\222\214.md" | 29 +++++++++++++++
...\345\220\210\346\200\273\345\222\214II.md" | 35 +++++++++++++++++++
2 files changed, 64 insertions(+)
diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
index 564d13ea3e..0f9cbc23ac 100644
--- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
+++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
@@ -417,6 +417,35 @@ function combinationSum(candidates: number[], target: number): number[][] {
};
```
+## Rust
+
+```Rust
+impl Solution {
+ pub fn backtracking(result: &mut Vec>, path: &mut Vec, candidates: &Vec, target: i32, mut sum: i32, start_index: usize) {
+ if sum == target {
+ result.push(path.to_vec());
+ return;
+ }
+ for i in start_index..candidates.len() {
+ if sum + candidates[i] <= target {
+ sum += candidates[i];
+ path.push(candidates[i]);
+ Self::backtracking(result, path, candidates, target, sum, i);
+ sum -= candidates[i];
+ path.pop();
+ }
+ }
+ }
+
+ pub fn combination_sum(candidates: Vec, target: i32) -> Vec> {
+ let mut result: Vec> = Vec::new();
+ let mut path: Vec = Vec::new();
+ Self::backtracking(&mut result, &mut path, &candidates, target, 0, 0);
+ result
+ }
+}
+```
+
## C
```c
diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md"
index 557eb8557b..99577f0cdf 100644
--- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md"
+++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md"
@@ -599,6 +599,41 @@ function combinationSum2(candidates: number[], target: number): number[][] {
};
```
+## Rust
+
+```Rust
+impl Solution {
+ pub fn backtracking(result: &mut Vec>, path: &mut Vec, candidates: &Vec, target: i32, mut sum: i32, start_index: usize, used: &mut Vec) {
+ if sum == target {
+ result.push(path.to_vec());
+ return;
+ }
+ for i in start_index..candidates.len() {
+ if sum + candidates[i] <= target {
+ if i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false { continue; }
+ sum += candidates[i];
+ path.push(candidates[i]);
+ used[i] = true;
+ Self::backtracking(result, path, candidates, target, sum, i + 1, used);
+ used[i] = false;
+ sum -= candidates[i];
+ path.pop();
+ }
+ }
+ }
+
+ pub fn combination_sum2(candidates: Vec, target: i32) -> Vec> {
+ let mut result: Vec> = Vec::new();
+ let mut path: Vec = Vec::new();
+ let mut used: Vec = vec![false; candidates.len()];
+ let mut candidates = candidates;
+ candidates.sort();
+ Self::backtracking(&mut result, &mut path, &candidates, target, 0, 0, &mut used);
+ result
+ }
+}
+```
+
## C
```c
From 26f5e59cdaf5536e0b08b94d554e4ef4428732a7 Mon Sep 17 00:00:00 2001
From: leo <55868230+LIU-HONGYANG@users.noreply.github.com>
Date: Sat, 16 Jul 2022 11:56:18 +0800
Subject: [PATCH 471/551] =?UTF-8?q?Update=20=E9=9D=A2=E8=AF=95=E9=A2=9802.?=
=?UTF-8?q?07.=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...3002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md"
index f603925d4a..d799bc80a5 100644
--- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md"
+++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md"
@@ -208,7 +208,7 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode {
}
```
-递归版本:
+双指针
```go
func getIntersectionNode(headA, headB *ListNode) *ListNode {
From fb982fa79d643bb3e01960bb01897322cc424bf7 Mon Sep 17 00:00:00 2001
From: xiaojun <13589818805@163.com>
Date: Sat, 16 Jul 2022 15:00:22 +0800
Subject: [PATCH 472/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88234.?=
=?UTF-8?q?=E5=9B=9E=E6=96=87=E9=93=BE=E8=A1=A8=EF=BC=89=E7=9A=84go?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...36\346\226\207\351\223\276\350\241\250.md" | 68 +++++++++++++++++++
1 file changed, 68 insertions(+)
diff --git "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md"
index eeee6fa531..1ac8756a4d 100644
--- "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md"
+++ "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md"
@@ -276,7 +276,75 @@ class Solution:
### Go
```go
+/**
+ * Definition for singly-linked list.
+ * type ListNode struct {
+ * Val int
+ * Next *ListNode
+ * }
+ */
+//方法一,使用数组
+func isPalindrome(head *ListNode) bool{
+ //计算切片长度,避免切片频繁扩容
+ cur,ln:=head,0
+ for cur!=nil{
+ ln++
+ cur=cur.Next
+ }
+ nums:=make([]int,ln)
+ index:=0
+ for head!=nil{
+ nums[index]=head.Val
+ index++
+ head=head.Next
+ }
+ //比较回文切片
+ for i,j:=0,ln-1;i<=j;i,j=i+1,j-1{
+ if nums[i]!=nums[j]{return false}
+ }
+ return true
+}
+// 方法二,快慢指针
+func isPalindrome(head *ListNode) bool {
+ if head==nil&&head.Next==nil{return true}
+ //慢指针,找到链表中间分位置,作为分割
+ slow:=head
+ fast:=head
+ //记录慢指针的前一个节点,用来分割链表
+ pre:=head
+ for fast!=nil && fast.Next!=nil{
+ pre=slow
+ slow=slow.Next
+ fast=fast.Next.Next
+ }
+ //分割链表
+ pre.Next=nil
+ //前半部分
+ cur1:=head
+ //反转后半部分,总链表长度如果是奇数,cur2比cur1多一个节点
+ cur2:=ReverseList(slow)
+
+ //开始两个链表的比较
+ for cur1!=nil{
+ if cur1.Val!=cur2.Val{return false}
+ cur1=cur1.Next
+ cur2=cur2.Next
+ }
+ return true
+}
+//反转链表
+func ReverseList(head *ListNode) *ListNode{
+ var pre *ListNode
+ cur:=head
+ for cur!=nil{
+ tmp:=cur.Next
+ cur.Next=pre
+ pre=cur
+ cur=tmp
+ }
+ return pre
+}
```
### JavaScript
From 4f867df86c7ef1ba3ef2ae3858fc1f928025a070 Mon Sep 17 00:00:00 2001
From: cezarbbb <105843128+cezarbbb@users.noreply.github.com>
Date: Sat, 16 Jul 2022 15:24:05 +0800
Subject: [PATCH 473/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200131.=E5=88=86?=
=?UTF-8?q?=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2=20Rust=E5=9B=9E=E6=BA=AF+?=
=?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0131.分割回文串 Rust回溯+动态规划版本
---
...62\345\233\236\346\226\207\344\270\262.md" | 37 ++++++++++++++++++-
1 file changed, 36 insertions(+), 1 deletion(-)
diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
index 64d458533f..a54d65764d 100644
--- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
+++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md"
@@ -626,7 +626,8 @@ func partition(_ s: String) -> [[String]] {
## Rust
-```rust
+**回溯+函数判断回文串**
+```Rust
impl Solution {
pub fn partition(s: String) -> Vec> {
let mut ret = vec![];
@@ -676,6 +677,40 @@ impl Solution {
}
}
```
+**回溯+动态规划预处理判断回文串**
+```Rust
+impl Solution {
+ pub fn backtracking(is_palindrome: &Vec>, result: &mut Vec>, path: &mut Vec, s: &Vec, start_index: usize) {
+ let len = s.len();
+ if start_index >= len {
+ result.push(path.to_vec());
+ return;
+ }
+ for i in start_index..len {
+ if is_palindrome[start_index][i] { path.push(s[start_index..=i].iter().collect::()); } else { continue; }
+ Self::backtracking(is_palindrome, result, path, s, i + 1);
+ path.pop();
+ }
+ }
+
+ pub fn partition(s: String) -> Vec> {
+ let mut result: Vec> = Vec::new();
+ let mut path: Vec = Vec::new();
+ let s = s.chars().collect::>();
+ let len: usize = s.len();
+ // 使用动态规划预先打表
+ // 当且仅当其为空串(i>j),或其长度为1(i=j),或者首尾字符相同且(s[i+1..j−1])时为回文串
+ let mut is_palindrome = vec![vec![true; len]; len];
+ for i in (0..len).rev() {
+ for j in (i + 1)..len {
+ is_palindrome[i][j] = s[i] == s[j] && is_palindrome[i + 1][j - 1];
+ }
+ }
+ Self::backtracking(&is_palindrome, &mut result, &mut path, &s, 0);
+ result
+ }
+}
+```
## Scala
From 8b9b64d7d576a783a3bb8fecdc3d614a4f9a6f36 Mon Sep 17 00:00:00 2001
From: AronJudge <2286381138@qq.com>
Date: Sat, 16 Jul 2022 15:43:50 +0800
Subject: [PATCH 474/551] =?UTF-8?q?=E4=BF=AE=E6=94=B9:=200617=20=E5=90=88?=
=?UTF-8?q?=E5=B9=B6=E4=BA=8C=E5=8F=89=E6=A0=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
修改代码细节,优化性能.
用原有的Root1 代替 创建的NewRoot.
---
...10\345\271\266\344\272\214\345\217\211\346\240\221.md" | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md"
index acdcc0aa41..6a84376312 100644
--- "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md"
+++ "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md"
@@ -262,10 +262,10 @@ class Solution {
if (root1 == null) return root2;
if (root2 == null) return root1;
- TreeNode newRoot = new TreeNode(root1.val + root2.val);
- newRoot.left = mergeTrees(root1.left,root2.left);
- newRoot.right = mergeTrees(root1.right,root2.right);
- return newRoot;
+ root1.val += root2.val;
+ root1.left = mergeTrees(root1.left,root2.left);
+ root1.right = mergeTrees(root1.right,root2.right);
+ return root1;
}
}
```
From 26376b6417a102e883071452c9863982f1d77802 Mon Sep 17 00:00:00 2001
From: cezarbbb <105843128+cezarbbb@users.noreply.github.com>
Date: Sun, 17 Jul 2022 10:53:14 +0800
Subject: [PATCH 475/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200093.=E5=A4=8D?=
=?UTF-8?q?=E5=8E=9FIP=E5=9C=B0=E5=9D=80=20Rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0093.复原IP地址 Rust版本
---
...\345\216\237IP\345\234\260\345\235\200.md" | 47 +++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
index 11ca2d03b5..46ac1c8677 100644
--- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
+++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md"
@@ -536,6 +536,53 @@ func isNormalIp(s string,startIndex,end int)bool{
```
+## Rust
+
+```Rust
+impl Solution {
+ fn is_valid(s: &Vec, start: usize, end: usize) -> bool {
+ if start > end { return false; }
+ if s[start] == '0' && start != end { return false; }
+ let mut num = 0;
+ for i in start..=end {
+ if s[i] > '9' || s[i] < '0' { return false; }
+ if let Some(digit) = s[i].to_digit(10) { num = num * 10 + digit; }
+ if num > 255 { return false; }
+ }
+ true
+ }
+
+ fn backtracking(result: &mut Vec, s: &mut Vec, start_index: usize, mut point_num: usize) {
+ let len = s.len();
+ if point_num == 3 {
+ if Self::is_valid(s, start_index, len - 1) {
+ result.push(s.iter().collect::());
+ }
+ return;
+ }
+ for i in start_index..len {
+ if Self::is_valid(s, start_index, i) {
+ point_num += 1;
+ s.insert(i + 1, '.');
+ Self::backtracking(result, s, i + 2, point_num);
+ point_num -= 1;
+ s.remove(i + 1);
+ } else { break; }
+ }
+ }
+
+ pub fn restore_ip_addresses(s: String) -> Vec {
+ let mut result: Vec = Vec::new();
+ let len = s.len();
+ if len < 4 || len > 12 { return result; }
+ let mut s = s.chars().collect::>();
+ Self::backtracking(&mut result, &mut s, 0, 0);
+ result
+ }
+
+}
+```
+
## C
```c
//记录结果
From 6f3dc1e6f80c5527d266c125b1a57366d314787a Mon Sep 17 00:00:00 2001
From: DarrenRuan <31378679+DarrenRuan@users.noreply.github.com>
Date: Sat, 16 Jul 2022 23:57:33 -0400
Subject: [PATCH 476/551] =?UTF-8?q?Update=200039.=E7=BB=84=E5=90=88?=
=?UTF-8?q?=E6=80=BB=E5=92=8C.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
这里python代码有个注释似乎写错了。原来为`不是i-1`,应该是`不是i+1`.
---
.../0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
index 564d13ea3e..54e9f2e5c1 100644
--- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
+++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md"
@@ -291,7 +291,7 @@ class Solution:
for i in range(start_index, len(candidates)):
sum_ += candidates[i]
self.path.append(candidates[i])
- self.backtracking(candidates, target, sum_, i) # 因为无限制重复选取,所以不是i-1
+ self.backtracking(candidates, target, sum_, i) # 因为无限制重复选取,所以不是i+1
sum_ -= candidates[i] # 回溯
self.path.pop() # 回溯
```
From aaac4825ec0f638b07e6772829bc25a26f677863 Mon Sep 17 00:00:00 2001
From: cezarbbb <105843128+cezarbbb@users.noreply.github.com>
Date: Mon, 18 Jul 2022 09:46:46 +0800
Subject: [PATCH 477/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200078.=E5=AD=90?=
=?UTF-8?q?=E9=9B=86=20Rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0078.子集 Rust版本
---
"problems/0078.\345\255\220\351\233\206.md" | 24 +++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md"
index e6cc668bdf..3e98311ed0 100644
--- "a/problems/0078.\345\255\220\351\233\206.md"
+++ "b/problems/0078.\345\255\220\351\233\206.md"
@@ -292,6 +292,30 @@ function subsets(nums: number[]): number[][] {
};
```
+## Rust
+
+```Rust
+impl Solution {
+ fn backtracking(result: &mut Vec>, path: &mut Vec, nums: &Vec, start_index: usize) {
+ result.push(path.clone());
+ let len = nums.len();
+ // if start_index >= len { return; }
+ for i in start_index..len {
+ path.push(nums[i]);
+ Self::backtracking(result, path, nums, i + 1);
+ path.pop();
+ }
+ }
+
+ pub fn subsets(nums: Vec) -> Vec> {
+ let mut result: Vec> = Vec::new();
+ let mut path: Vec = Vec::new();
+ Self::backtracking(&mut result, &mut path, &nums, 0);
+ result
+ }
+}
+```
+
## C
```c
From ac4bdf21015d0bdcd72522b4190158b36deea391 Mon Sep 17 00:00:00 2001
From: cezarbbb <105843128+cezarbbb@users.noreply.github.com>
Date: Tue, 19 Jul 2022 09:40:59 +0800
Subject: [PATCH 478/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200090.=E5=AD=90?=
=?UTF-8?q?=E9=9B=86II=20Rust=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0090.子集II Rust版本
---
"problems/0090.\345\255\220\351\233\206II.md" | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md"
index e85ec66d3c..7ab6afb683 100644
--- "a/problems/0090.\345\255\220\351\233\206II.md"
+++ "b/problems/0090.\345\255\220\351\233\206II.md"
@@ -340,6 +340,36 @@ function subsetsWithDup(nums: number[]): number[][] {
};
```
+### Rust
+
+```Rust
+impl Solution {
+ fn backtracking(result: &mut Vec>, path: &mut Vec, nums: &Vec, start_index: usize, used: &mut Vec) {
+ result.push(path.clone());
+ let len = nums.len();
+ // if start_index >= len { return; }
+ for i in start_index..len {
+ if i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false { continue; }
+ path.push(nums[i]);
+ used[i] = true;
+ Self::backtracking(result, path, nums, i + 1, used);
+ used[i] = false;
+ path.pop();
+ }
+ }
+
+ pub fn subsets_with_dup(nums: Vec) -> Vec> {
+ let mut result: Vec> = Vec::new();
+ let mut path: Vec = Vec::new();
+ let mut used = vec![false; nums.len()];
+ let mut nums = nums;
+ nums.sort();
+ Self::backtracking(&mut result, &mut path, &nums, 0, &mut used);
+ result
+ }
+}
+```
+
### C
```c
From 50b9753b647ace27caa0a1a31bd91975672db4d7 Mon Sep 17 00:00:00 2001
From: dcj_hp <294487055@qq.com>
Date: Wed, 20 Jul 2022 01:11:00 +0800
Subject: [PATCH 479/551] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200376.=E6=91=86?=
=?UTF-8?q?=E5=8A=A8=E5=BA=8F=E5=88=97=20=20=E5=8A=A8=E6=80=81=E8=A7=84?=
=?UTF-8?q?=E5=88=92=E7=89=88=E6=9C=AC(Python,=20C)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...06\345\212\250\345\272\217\345\210\227.md" | 64 +++++++++++++++++++
1 file changed, 64 insertions(+)
diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
index 70a6214570..714dba57e0 100644
--- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
+++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
@@ -228,6 +228,8 @@ class Solution {
### Python
+**贪心**
+
```python
class Solution:
def wiggleMaxLength(self, nums: List[int]) -> int:
@@ -240,7 +242,30 @@ class Solution:
return res
```
+**动态规划**
+
+```python
+class Solution:
+ def wiggleMaxLength(self, nums: List[int]) -> int:
+ # 0 i 作为波峰的最大长度
+ # 1 i 作为波谷的最大长度
+ # dp是一个列表,列表中每个元素是长度为 2 的列表
+ dp = []
+ for i in range(len(nums)):
+ # 初始为[1, 1]
+ dp.append([1, 1])
+ for j in range(i):
+ # nums[i] 为波谷
+ if nums[j] > nums[i]:
+ dp[i][1] = max(dp[i][1], dp[j][0] + 1)
+ # nums[i] 为波峰
+ if nums[j] < nums[i]:
+ dp[i][0] = max(dp[i][0], dp[j][1] + 1)
+ return max(dp[-1][0], dp[-1][1])
+```
+
### Go
+
```golang
func wiggleMaxLength(nums []int) int {
var count,preDiff,curDiff int
@@ -324,6 +349,7 @@ impl Solution {
### C
**贪心**
+
```c
int wiggleMaxLength(int* nums, int numsSize){
if(numsSize <= 1)
@@ -349,6 +375,44 @@ int wiggleMaxLength(int* nums, int numsSize){
}
```
+**动态规划**
+
+```c
+int max(int left, int right)
+{
+ return left > right ? left : right;
+}
+int wiggleMaxLength(int* nums, int numsSize){
+ if(numsSize <= 1)
+ {
+ return numsSize;
+ }
+ // 0 i 作为波峰的最大长度
+ // 1 i 作为波谷的最大长度
+ int dp[numsSize][2];
+ for(int i = 0; i < numsSize; i++)
+ {
+ dp[i][0] = 1;
+ dp[i][1] = 1;
+ for(int j = 0; j < i; j++)
+ {
+ // nums[i] 为山谷
+ if(nums[j] > nums[i])
+ {
+ dp[i][1] = max(dp[i][1], dp[j][0] + 1);
+ }
+ // nums[i] 为山峰
+ if(nums[j] < nums[i])
+ {
+ dp[i][0] = max(dp[i][0], dp[j][1] + 1);
+ }
+ }
+ }
+ return max(dp[numsSize - 1][0], dp[numsSize - 1][1]);
+}
+```
+
+
### TypeScript
From d4f89d1b0eec92115d0270630c77a09803b45703 Mon Sep 17 00:00:00 2001
From: programmercarl <826123027@qq.com>
Date: Wed, 20 Jul 2022 09:45:11 +0800
Subject: [PATCH 480/551] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8E=9F=E9=A2=98?=
=?UTF-8?q?=E8=A7=A3=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=83=A8=E5=88=86=E9=A2=98?=
=?UTF-8?q?=E7=9B=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...44\346\225\260\344\271\213\345\222\214.md" | 86 +++--
...11\346\225\260\344\271\213\345\222\214.md" | 96 +++++-
...33\346\225\260\344\271\213\345\222\214.md" | 37 +--
...55\347\232\204\350\212\202\347\202\271.md" | 6 +-
...00\344\270\252\344\275\215\347\275\256.md" | 2 +
...10\345\271\266\345\214\272\351\227\264.md" | 3 -
...\344\275\263\346\227\266\346\234\272II.md" | 4 +-
...\345\275\242\351\223\276\350\241\250II.md" | 2 +
...2.\345\277\253\344\271\220\346\225\260.md" | 47 ---
...73\350\275\254\351\223\276\350\241\250.md" | 2 +
...04\345\255\220\346\225\260\347\273\204.md" | 2 +-
...345\220\210\346\200\273\345\222\214III.md" | 2 +-
...15\345\274\202\344\275\215\350\257\215.md" | 2 +
...04\347\232\204\344\272\244\351\233\206.md" | 37 ++-
...06\345\212\250\345\272\217\345\210\227.md" | 12 +-
...55\345\255\220\345\272\217\345\210\227.md" | 6 +-
...\346\225\260\347\233\270\345\212\240II.md" | 13 +-
...36\346\226\207\345\255\220\344\270\262.md" | 10 +-
...14\345\210\206\346\237\245\346\211\276.md" | 2 +-
...76\350\256\241\351\223\276\350\241\250.md" | 2 +
...15\345\255\220\346\225\260\347\273\204.md" | 2 +-
...75\347\232\204\350\267\257\345\276\204.md" | 296 ++++++++++++++++++
...31\345\222\214\346\210\277\351\227\264.md" | 229 ++++++++++----
...55\345\277\203\350\212\202\347\202\271.md" | 73 +++++
...30\345\234\250\350\267\257\345\276\204.md" | 123 ++++++++
...22\346\200\273\347\273\223\347\257\207.md" | 2 +-
...76\350\241\250\347\233\270\344\272\244.md" | 4 +-
27 files changed, 896 insertions(+), 206 deletions(-)
create mode 100644 "problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md"
create mode 100644 "problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md"
create mode 100644 "problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md"
diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md"
index fb3c1d4560..055f89402c 100644
--- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md"
@@ -24,7 +24,9 @@
## 思路
-很明显暴力的解法是两层for循环查找,时间复杂度是$O(n^2)$。
+建议看一下我录的这期视频:[梦开始的地方,Leetcode:1.两数之和](https://www.bilibili.com/video/BV1aT41177mK),结合本题解来学习,事半功倍。
+
+很明显暴力的解法是两层for循环查找,时间复杂度是O(n^2)。
建议大家做这道题目之前,先做一下这两道
* [242. 有效的字母异位词](https://www.programmercarl.com/0242.有效的字母异位词.html)
@@ -32,7 +34,16 @@
[242. 有效的字母异位词](https://www.programmercarl.com/0242.有效的字母异位词.html) 这道题目是用数组作为哈希表来解决哈希问题,[349. 两个数组的交集](https://www.programmercarl.com/0349.两个数组的交集.html)这道题目是通过set作为哈希表来解决哈希问题。
-本题呢,则要使用map,那么来看一下使用数组和set来做哈希法的局限。
+
+首先我在强调一下 **什么时候使用哈希法**,当我们需要查询一个元素是否出现过,或者一个元素是否在集合里的时候,就要第一时间想到哈希法。
+
+本题呢,我就需要一个集合来存放我们遍历过的元素,然后在遍历数组的时候去询问这个集合,某元素是否遍历过,也就是 是否出现在这个集合。
+
+那么我们就应该想到使用哈希法了。
+
+因为本地,我们不仅要知道元素有没有遍历过,还有知道这个元素对应的下标,**需要使用 key value结构来存放,key来存元素,value来存下标,那么使用map正合适**。
+
+再来看一下使用数组和set来做哈希法的局限。
* 数组的大小是受限制的,而且如果元素很少,而哈希值太大会造成内存空间的浪费。
* set是一个集合,里面放的元素只能是一个key,而两数之和这道题目,不仅要判断y是否存在而且还要记录y的下标位置,因为要返回x 和 y的下标。所以set 也不能用。
@@ -43,20 +54,38 @@ C++中map,有三种类型:
|映射 |底层实现 | 是否有序 |数值是否可以重复 | 能否更改数值|查询效率 |增删效率|
|---|---| --- |---| --- | --- | ---|
-|std::map |红黑树 |key有序 |key不可重复 |key不可修改 | $O(\log n)$|$O(\log n)$ |
-|std::multimap | 红黑树|key有序 | key可重复 | key不可修改|$O(\log n)$ |$O(\log n)$ |
-|std::unordered_map |哈希表 | key无序 |key不可重复 |key不可修改 |$O(1)$ | $O(1)$|
+|std::map |红黑树 |key有序 |key不可重复 |key不可修改 | O(log n)|O(log n) |
+|std::multimap | 红黑树|key有序 | key可重复 | key不可修改|O(log n) |O(log n) |
+|std::unordered_map |哈希表 | key无序 |key不可重复 |key不可修改 |O(1) | O(1)|
std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底层实现是红黑树。
同理,std::map 和std::multimap 的key也是有序的(这个问题也经常作为面试题,考察对语言容器底层的理解)。 更多哈希表的理论知识请看[关于哈希表,你该了解这些!](https://www.programmercarl.com/哈希表理论基础.html)。
-**这道题目中并不需要key有序,选择std::unordered_map 效率更高!**
+**这道题目中并不需要key有序,选择std::unordered_map 效率更高!** 使用其他语言的录友注意了解一下自己所用语言的数据结构就行。
+
+接下来需要明确两点:
+
+* **map用来做什么**
+* **map中key和value分别表示什么**
+
+map目的用来存放我们访问过的元素,因为遍历数组的时候,需要记录我们之前遍历过哪些元素和对应的下表,这样才能找到与当前元素相匹配的(也就是相加等于target)
+
+接下来是map中key和value分别表示什么。
+
+这道题 我们需要 给出一个元素,判断这个元素是否出现过,如果出现过,返回这个元素的下标。
+
+那么判断元素是否出现,这个元素就要作为key,所以数组中的元素作为key,有key对应的就是value,value用来存下标。
+
+所以 map中的存储结构为 {key:数据元素,value:数组元素对应的下表}。
-解题思路动画如下:
+在遍历数组的时候,只需要向map去查询是否有和目前遍历元素比配的数值,如果有,就找到的匹配对,如果没有,就把目前遍历的元素放进map中,因为map存放的就是我们访问过的元素。
-
+过程如下:
+
+
+
C++代码:
@@ -66,18 +95,31 @@ public:
vector twoSum(vector& nums, int target) {
std::unordered_map map;
for(int i = 0; i < nums.size(); i++) {
- auto iter = map.find(target - nums[i]);
+ // 遍历当前元素,并在map中寻找是否有匹配的key
+ auto iter = map.find(target - nums[i]);
if(iter != map.end()) {
return {iter->second, i};
}
- map.insert(pair(nums[i], i));
+ // 如果没找到匹配对,就把访问过的元素和下标加入到map中
+ map.insert(pair(nums[i], i));
}
return {};
}
};
```
+## 总结
+
+本题其实有四个重点:
+
+* 为什么会想到用哈希表
+* 哈希表为什么用map
+* 本题map是用来存什么的
+* map中的key和value用来存什么的
+把这四点想清楚了,本题才算是理解透彻了。
+
+很多录友把这道题目 通过了,但都没想清楚map是用来做什么的,以至于对代码的理解其实是 一知半解的。
## 其他语言版本
@@ -250,30 +292,6 @@ func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
}
```
-PHP:
-```php
-class Solution {
- /**
- * @param Integer[] $nums
- * @param Integer $target
- * @return Integer[]
- */
- function twoSum($nums, $target) {
- if (count($nums) == 0) {
- return [];
- }
- $table = [];
- for ($i = 0; $i < count($nums); $i++) {
- $temp = $target - $nums[$i];
- if (isset($table[$temp])) {
- return [$table[$temp], $i];
- }
- $table[$nums[$i]] = $i;
- }
- return [];
- }
-}
-```
Scala:
```scala
diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
index e6dc82ddd7..5a4168d128 100644
--- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md"
@@ -39,7 +39,7 @@
去重的过程不好处理,有很多小细节,如果在面试中很难想到位。
-时间复杂度可以做到$O(n^2)$,但还是比较费时的,因为不好做剪枝操作。
+时间复杂度可以做到O(n^2),但还是比较费时的,因为不好做剪枝操作。
大家可以尝试使用哈希法写一写,就知道其困难的程度了。
@@ -85,7 +85,7 @@ public:
**其实这道题目使用哈希法并不十分合适**,因为在去重的操作中有很多细节需要注意,在面试中很难直接写出没有bug的代码。
-而且使用哈希法 在使用两层for循环的时候,能做的剪枝操作很有限,虽然时间复杂度是$O(n^2)$,也是可以在leetcode上通过,但是程序的执行时间依然比较长 。
+而且使用哈希法 在使用两层for循环的时候,能做的剪枝操作很有限,虽然时间复杂度是O(n^2),也是可以在leetcode上通过,但是程序的执行时间依然比较长 。
接下来我来介绍另一个解法:双指针法,**这道题目使用双指针法 要比哈希法高效一些**,那么来讲解一下具体实现的思路。
@@ -101,7 +101,7 @@ public:
如果 nums[i] + nums[left] + nums[right] < 0 说明 此时 三数之和小了,left 就向右移动,才能让三数之和大一些,直到left与right相遇为止。
-时间复杂度:$O(n^2)$。
+时间复杂度:O(n^2)。
C++代码代码如下:
@@ -118,13 +118,13 @@ public:
if (nums[i] > 0) {
return result;
}
- // 错误去重方法,将会漏掉-1,-1,2 这种情况
+ // 错误去重a方法,将会漏掉-1,-1,2 这种情况
/*
if (nums[i] == nums[i + 1]) {
continue;
}
*/
- // 正确去重方法
+ // 正确去重a方法
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
@@ -136,17 +136,11 @@ public:
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
*/
- if (nums[i] + nums[left] + nums[right] > 0) {
- right--;
- // 当前元素不合适了,可以去重
- while (left < right && nums[right] == nums[right + 1]) right--;
- } else if (nums[i] + nums[left] + nums[right] < 0) {
- left++;
- // 不合适,去重
- while (left < right && nums[left] == nums[left - 1]) left++;
- } else {
+ if (nums[i] + nums[left] + nums[right] > 0) right--;
+ else if (nums[i] + nums[left] + nums[right] < 0) left++;
+ else {
result.push_back(vector{nums[i], nums[left], nums[right]});
- // 去重逻辑应该放在找到一个三元组之后
+ // 去重逻辑应该放在找到一个三元组之后,对b 和 c去重
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
@@ -162,6 +156,78 @@ public:
};
```
+## 去重逻辑的思考
+
+### a的去重
+
+说道去重,其实主要考虑三个数的去重。 a, b ,c, 对应的就是 nums[i],nums[left],nums[right]
+
+a 如果重复了怎么办,a是nums里遍历的元素,那么应该直接跳过去。
+
+但这里有一个问题,是判断 nums[i] 与 nums[i + 1]是否相同,还是判断 nums[i] 与 nums[i-1] 是否相同。
+
+有同学可能想,这不都一样吗。
+
+其实不一样!
+
+都是和 nums[i]进行比较,是比较它的前一个,还是比较他的后一个。
+
+如果我们的写法是 这样:
+
+```C++
+if (nums[i] == nums[i + 1]) { // 去重操作
+ continue;
+}
+```
+
+那就我们就把 三元组中出现重复元素的情况直接pass掉了。 例如{-1, -1 ,2} 这组数据,当遍历到第一个-1 的时候,判断 下一个也是-1,那这组数据就pass了。
+
+**我们要做的是 不能有重复的三元组,但三元组内的元素是可以重复的!**
+
+所以这里是有两个重复的维度。
+
+那么应该这么写:
+
+```C++
+if (i > 0 && nums[i] == nums[i - 1]) {
+ continue;
+}
+```
+
+这么写就是当前使用 nums[i],我们判断前一位是不是一样的元素,在看 {-1, -1 ,2} 这组数据,当遍历到 第一个 -1 的时候,只要前一位没有-1,那么 {-1, -1 ,2} 这组数据一样可以收录到 结果集里。
+
+这是一个非常细节的思考过程。
+
+### b与c的去重
+
+很多同学写本题的时候,去重的逻辑多加了 对right 和left 的去重:(代码中注释部分)
+
+```C++
+while (right > left) {
+ if (nums[i] + nums[left] + nums[right] > 0) {
+ right--;
+ // 去重 right
+ while (left < right && nums[right] == nums[right + 1]) right--;
+ } else if (nums[i] + nums[left] + nums[right] < 0) {
+ left++;
+ // 去重 left
+ while (left < right && nums[left] == nums[left - 1]) left++;
+ } else {
+ }
+}
+```
+
+但细想一下,这种去重其实对提升程序运行效率是没有帮助的。
+
+拿right去重为例,即使不加这个去重逻辑,依然根据 `while (right > left) ` 和 `if (nums[i] + nums[left] + nums[right] > 0)` 去完成right-- 的操作。
+
+多加了 ` while (left < right && nums[right] == nums[right + 1]) right--;` 这一行代码,其实就是把 需要执行的逻辑提前执行了,但并没有减少 判断的逻辑。
+
+最直白的思考过程,就是right还是一个数一个数的减下去的,所以在哪里减的都是一样的。
+
+所以这种去重 是可以不加的。 仅仅是 把去重的逻辑提前了而已。
+
+
# 思考题
diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
index 2146a11469..7989ad8fff 100644
--- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
+++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md"
@@ -35,11 +35,11 @@
[15.三数之和](https://programmercarl.com/0015.三数之和.html)的双指针解法是一层for循环num[i]为确定值,然后循环内有left和right下标作为双指针,找到nums[i] + nums[left] + nums[right] == 0。
-四数之和的双指针解法是两层for循环nums[k] + nums[i]为确定值,依然是循环内有left和right下标作为双指针,找出nums[k] + nums[i] + nums[left] + nums[right] == target的情况,三数之和的时间复杂度是$O(n^2)$,四数之和的时间复杂度是$O(n^3)$ 。
+四数之和的双指针解法是两层for循环nums[k] + nums[i]为确定值,依然是循环内有left和right下标作为双指针,找出nums[k] + nums[i] + nums[left] + nums[right] == target的情况,三数之和的时间复杂度是O(n^2),四数之和的时间复杂度是O(n^3) 。
那么一样的道理,五数之和、六数之和等等都采用这种解法。
-对于[15.三数之和](https://programmercarl.com/0015.三数之和.html)双指针法就是将原本暴力$O(n^3)$的解法,降为$O(n^2)$的解法,四数之和的双指针解法就是将原本暴力$O(n^4)$的解法,降为$O(n^3)$的解法。
+对于[15.三数之和](https://programmercarl.com/0015.三数之和.html)双指针法就是将原本暴力O(n^3)的解法,降为O(n^2)的解法,四数之和的双指针解法就是将原本暴力O(n^4)的解法,降为O(n^3)的解法。
之前我们讲过哈希表的经典题目:[454.四数相加II](https://programmercarl.com/0454.四数相加II.html),相对于本题简单很多,因为本题是要求在一个集合中找出四个数相加等于target,同时四元组不能重复。
@@ -47,14 +47,13 @@
我们来回顾一下,几道题目使用了双指针法。
-双指针法将时间复杂度:$O(n^2)$的解法优化为 $O(n)$的解法。也就是降一个数量级,题目如下:
+双指针法将时间复杂度:O(n^2)的解法优化为 O(n)的解法。也就是降一个数量级,题目如下:
* [27.移除元素](https://programmercarl.com/0027.移除元素.html)
* [15.三数之和](https://programmercarl.com/0015.三数之和.html)
* [18.四数之和](https://programmercarl.com/0018.四数之和.html)
-
-操作链表:
+链表相关双指针题目:
* [206.反转链表](https://programmercarl.com/0206.翻转链表.html)
* [19.删除链表的倒数第N个节点](https://programmercarl.com/0019.删除链表的倒数第N个节点.html)
@@ -72,21 +71,21 @@ public:
vector> result;
sort(nums.begin(), nums.end());
for (int k = 0; k < nums.size(); k++) {
- // 剪枝处理
- if (nums[k] > target && (nums[k] >= 0 || target >= 0)) {
+ // 剪枝处理
+ if (nums[k] > target && nums[k] >= 0 && target >= 0) {
break; // 这里使用break,统一通过最后的return返回
}
- // 去重
+ // 对nums[k]去重
if (k > 0 && nums[k] == nums[k - 1]) {
continue;
}
for (int i = k + 1; i < nums.size(); i++) {
- // 2级剪枝处理
- if (nums[k] + nums[i] > target && (nums[k] + nums[i] >= 0 || target >= 0)) {
- break;
- }
-
- // 正确去重方法
+ // 2级剪枝处理
+ if (nums[k] + nums[i] > target && nums[k] + nums[i] >= 0 && target >= 0) {
+ break;
+ }
+
+ // 对nums[i]去重
if (i > k + 1 && nums[i] == nums[i - 1]) {
continue;
}
@@ -94,18 +93,14 @@ public:
int right = nums.size() - 1;
while (right > left) {
// nums[k] + nums[i] + nums[left] + nums[right] > target 会溢出
- if (nums[k] + nums[i] > target - (nums[left] + nums[right])) {
+ if ((long) nums[k] + nums[i] + nums[left] + nums[right] > target) {
right--;
- // 当前元素不合适了,可以去重
- while (left < right && nums[right] == nums[right + 1]) right--;
// nums[k] + nums[i] + nums[left] + nums[right] < target 会溢出
- } else if (nums[k] + nums[i] < target - (nums[left] + nums[right])) {
+ } else if ((long) nums[k] + nums[i] + nums[left] + nums[right] < target) {
left++;
- // 不合适,去重
- while (left < right && nums[left] == nums[left - 1]) left++;
} else {
result.push_back(vector{nums[k], nums[i], nums[left], nums[right]});
- // 去重逻辑应该放在找到一个四元组之后
+ // 对nums[left]和nums[right]去重
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md"
index e636bfff58..550a886ef6 100644
--- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md"
+++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md"
@@ -18,6 +18,8 @@
## 思路
+针对本题重点难点,我录制了B站讲解视频,[帮你把链表细节学清楚! | LeetCode:24. 两两交换链表中的节点](https://www.bilibili.com/video/BV1YT411g7br),相信结合视频在看本篇题解,更有助于大家对链表的理解。
+
这道题目正常模拟就可以了。
建议使用虚拟头结点,这样会方便很多,要不然每次针对头结点(没有前一个指针指向头结点),还要单独处理。
@@ -63,8 +65,8 @@ public:
};
```
-* 时间复杂度:$O(n)$
-* 空间复杂度:$O(1)$
+* 时间复杂度:O(n)
+* 空间复杂度:O(1)
## 拓展
diff --git "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md"
index 260462c23b..bf9493c942 100644
--- "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md"
+++ "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md"
@@ -7,6 +7,8 @@
# 34. 在排序数组中查找元素的第一个和最后一个位置
+[题目链接](https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/)
+
给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。
如果数组中不存在目标值 target,返回 [-1, -1]。
diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
index 34d8dd8242..757896c1df 100644
--- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
+++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md"
@@ -22,9 +22,6 @@
* 解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。
* 注意:输入类型已于2019年4月15日更改。 请重置默认代码定义以获取新方法签名。
-提示:
-
-* intervals[i][0] <= intervals[i][1]
## 思路
diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
index b9fa8386ea..ae9744d173 100644
--- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
+++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md"
@@ -91,8 +91,8 @@ public:
};
```
-* 时间复杂度:$O(n)$
-* 空间复杂度:$O(1)$
+* 时间复杂度:O(n)
+* 空间复杂度:O(1)
### 动态规划
diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md"
index 6b7c7e661f..254f9db027 100644
--- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md"
+++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md"
@@ -24,6 +24,8 @@
## 思路
+为了易于大家理解,我录制讲解视频:[B站:把环形链表讲清楚! ](https://www.bilibili.com/video/BV1if4y1d7ob)。结合视频在看本篇题解,事半功倍。
+
这道题目,不仅考察对链表的操作,而且还需要一些数学运算。
主要考察两知识点:
diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md"
index 0bea0c7233..0054582ca8 100644
--- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md"
+++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md"
@@ -338,53 +338,6 @@ static inline int calcSquareSum(int num) {
return sum;
}
-#define HASH_TABLE_SIZE (32)
-
-bool isHappy(int n){
- int sum = n;
- int index = 0;
- bool bHappy = false;
- bool bExit = false;
- /* allocate the memory for hash table with chaining method*/
- HashNode ** hashTable = (HashNode **)calloc(HASH_TABLE_SIZE, sizeof(HashNode));
-
- while(bExit == false) {
- /* check if n has been calculated */
- index = hash(n, HASH_TABLE_SIZE);
-
- HashNode ** p = hashTable + index;
-
- while((*p) && (bExit == false)) {
- /* Check if this num was calculated, if yes, this will be endless loop */
- if((*p)->key == n) {
- bHappy = false;
- bExit = true;
- }
- /* move to next node of the same index */
- p = &((*p)->next);
- }
-
- /* put n intot hash table */
- HashNode * newNode = (HashNode *)malloc(sizeof(HashNode));
- newNode->key = n;
- newNode->next = NULL;
-
- *p = newNode;
-
- sum = calcSquareSum(n);
- if(sum == 1) {
- bHappy = true;
- bExit = true;
- }
- else {
- n = sum;
-
- }
- }
-
- return bHappy;
-}
-```
Scala:
```scala
diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md"
index 24ec7b943a..c412e840f1 100644
--- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md"
+++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md"
@@ -19,6 +19,8 @@
# 思路
+本题我录制了B站视频,[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频在看本篇题解,更有助于大家对链表的理解。
+
如果再定义一个新的链表,实现链表元素的反转,其实这是对内存空间的浪费。
其实只需要改变链表的next指针的指向,直接将链表反转 ,而不用重新定义一个新的链表,如图所示:
diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
index e2eb378e7a..b374aaec92 100644
--- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
+++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md"
@@ -19,7 +19,7 @@
# 思路
-为了易于大家理解,我特意录制了[拿下滑动窗口! | LeetCode 209 长度最小的子数组](https://www.bilibili.com/video/BV1tZ4y1q7XE)
+为了易于大家理解,我特意录制了B站视频[拿下滑动窗口! | LeetCode 209 长度最小的子数组](https://www.bilibili.com/video/BV1tZ4y1q7XE)
## 暴力解法
diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
index 0ace0fd5af..a8a6567d59 100644
--- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
+++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md"
@@ -212,7 +212,7 @@ public:
# 总结
-开篇就介绍了本题与[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)的区别,相对来说加了元素总和的限制,如果做完[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)再做本题在合适不过。
+开篇就介绍了本题与[77.组合](https://programmercarl.com/0077.组合.html)的区别,相对来说加了元素总和的限制,如果做完[77.组合](https://programmercarl.com/0077.组合.html)再做本题在合适不过。
分析完区别,依然把问题抽象为树形结构,按照回溯三部曲进行讲解,最后给出剪枝的优化。
diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md"
index 8f4b5ae2b7..0437330bbf 100644
--- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md"
+++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md"
@@ -27,6 +27,8 @@
## 思路
+本题B站视频讲解版:[学透哈希表,数组使用有技巧!Leetcode:242.有效的字母异位词](https://www.bilibili.com/video/BV1YG411p7BA)
+
先看暴力的解法,两层for循环,同时还要记录字符是否重复出现,很明显时间复杂度是 O(n^2)。
暴力的方法这里就不做介绍了,直接看一下有没有更优的方式。
diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
index 9851864750..c1d3515bea 100644
--- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
+++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md"
@@ -24,6 +24,8 @@
## 思路
+关于本题,我录制了讲解视频:[学透哈希表,set使用有技巧!Leetcode:349. 两个数组的交集](https://www.bilibili.com/video/BV1ba411S7wu),看视频配合题解,事半功倍。
+
这道题目,主要要学会使用一种哈希数据结构:unordered_set,这个数据结构可以解决很多类似的问题。
注意题目特意说明:**输出结果中的每个元素一定是唯一的,也就是说输出的结果的去重的, 同时可以不考虑输出结果的顺序**
@@ -48,7 +50,8 @@ std::set和std::multiset底层实现都是红黑树,std::unordered_set的底
思路如图所示:
-
+
+
C++代码如下:
@@ -56,7 +59,7 @@ C++代码如下:
class Solution {
public:
vector intersection(vector& nums1, vector& nums2) {
- unordered_set result_set; // 存放结果
+ unordered_set result_set; // 存放结果,之所以用set是为了给结果集去重
unordered_set nums_set(nums1.begin(), nums1.end());
for (int num : nums2) {
// 发现nums2的元素 在nums_set里又出现过
@@ -77,6 +80,36 @@ public:
不要小瞧 这个耗时,在数据量大的情况,差距是很明显的。
+## 后记
+
+本题后面 力扣改了 题目描述 和 后台测试数据,增添了 数值范围:
+
+* 1 <= nums1.length, nums2.length <= 1000
+* 0 <= nums1[i], nums2[i] <= 1000
+
+所以就可以 使用数组来做哈希表了, 因为数组都是 1000以内的。
+
+对应C++代码如下:
+
+```c++
+class Solution {
+public:
+ vector intersection(vector& nums1, vector& nums2) {
+ unordered_set result_set; // 存放结果,之所以用set是为了给结果集去重
+ int hash[1005] = {0}; // 默认数值为0
+ for (int num : nums1) { // nums1中出现的字母在hash数组中做记录
+ hash[num] = 1;
+ }
+ for (int num : nums2) { // nums2中出现话,result记录
+ if (hash[num] == 1) {
+ result_set.insert(num);
+ }
+ }
+ return vector(result_set.begin(), result_set.end());
+ }
+};
+```
+
## 其他语言版本
diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
index 00f8f70cf9..e8db980d63 100644
--- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
+++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md"
@@ -88,8 +88,8 @@ public:
};
```
-* 时间复杂度:$O(n)$
-* 空间复杂度:$O(1)$
+* 时间复杂度:O(n)
+* 空间复杂度:O(1)
## 思路2(动态规划)
@@ -138,8 +138,8 @@ public:
};
```
-* 时间复杂度:$O(n^2)$
-* 空间复杂度:$O(n)$
+* 时间复杂度:O(n^2)
+* 空间复杂度:O(n)
**进阶**
@@ -149,9 +149,9 @@ public:
* 每次更新`dp[i][1]`,则在`tree2`的`nums[i]`位置值更新为`dp[i][1]`
* 则dp转移方程中就没有必要j从0遍历到i-1,可以直接在线段树中查询指定区间的值即可。
-时间复杂度:$O(n\log n)$
+时间复杂度:O(nlog n)
-空间复杂度:$O(n)$
+空间复杂度:O(n)
## 总结
diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md"
index 9a26d63900..dedeb0ad53 100644
--- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md"
+++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md"
@@ -31,7 +31,7 @@
## 思路
-(这道题可以用双指针的思路来实现,时间复杂度就是$O(n)$)
+(这道题可以用双指针的思路来实现,时间复杂度就是O(n))
这道题应该算是编辑距离的入门题目,因为从题意中我们也可以发现,只需要计算删除的情况,不用考虑增加和替换的情况。
@@ -122,8 +122,8 @@ public:
};
```
-* 时间复杂度:$O(n × m)$
-* 空间复杂度:$O(n × m)$
+* 时间复杂度:O(n × m)
+* 空间复杂度:O(n × m)
## 总结
diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md"
index d22e23359d..b735c106a8 100644
--- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md"
+++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md"
@@ -18,14 +18,19 @@
**例如:**
输入:
-A = [ 1, 2]
-B = [-2,-1]
-C = [-1, 2]
-D = [ 0, 2]
+* A = [ 1, 2]
+* B = [-2,-1]
+* C = [-1, 2]
+* D = [ 0, 2]
+
输出:
+
2
+
**解释:**
+
两个元组如下:
+
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md"
index c0b34e8a8b..fa9bf7b804 100644
--- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md"
+++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md"
@@ -32,7 +32,7 @@
两层for循环,遍历区间起始位置和终止位置,然后判断这个区间是不是回文。
-时间复杂度:$O(n^3)$
+时间复杂度:O(n^3)
## 动态规划
@@ -171,8 +171,8 @@ public:
};
```
-* 时间复杂度:$O(n^2)$
-* 空间复杂度:$O(n^2)$
+* 时间复杂度:O(n^2)
+* 空间复杂度:O(n^2)
## 双指针法
@@ -213,8 +213,8 @@ public:
};
```
-* 时间复杂度:$O(n^2)$
-* 空间复杂度:$O(1)$
+* 时间复杂度:O(n^2)
+* 空间复杂度:O(1)
## 其他语言版本
diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
index 6a37e4d107..432305e2dd 100644
--- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
+++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md"
@@ -36,7 +36,7 @@
## 思路
-为了易于大家理解,我还录制了视频,可以看这里:[手把手带你撕出正确的二分法](https://www.bilibili.com/video/BV1fA4y1o715)
+为了易于大家理解,我还录制了视频,可以看这里:[B站:手把手带你撕出正确的二分法](https://www.bilibili.com/video/BV1fA4y1o715)
**这道题目的前提是数组为有序数组**,同时题目还强调**数组中无重复元素**,因为一旦有重复元素,使用二分查找法返回的元素下标可能不是唯一的,这些都是使用二分法的前提条件,当大家看到题目描述满足如上条件的时候,可要想一想是不是可以用二分法了。
diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
index 6ee11eefbf..b0385ce167 100644
--- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
+++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md"
@@ -26,6 +26,8 @@
# 思路
+为了方便大家理解,我特意录制了视频:[帮你把链表操作学个通透!LeetCode:707.设计链表](https://www.bilibili.com/video/BV1FU4y1X7WD),结合视频在看本题解,事半功倍。
+
如果对链表的基础知识还不太懂,可以看这篇文章:[关于链表,你该了解这些!](https://programmercarl.com/链表理论基础.html)
如果对链表的虚拟头结点不清楚,可以看这篇文章:[链表:听说用虚拟头节点会方便很多?](https://programmercarl.com/0203.移除链表元素.html)
diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md"
index 18007b700b..6de8b80f43 100644
--- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md"
+++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md"
@@ -31,7 +31,7 @@ B: [3,2,1,4,7]
1. 确定dp数组(dp table)以及下标的含义
-dp[i][j] :以下标i - 1为结尾的A,和以下标j - 1为结尾的B,最长重复子数组长度为dp[i][j]。
+dp[i][j] :以下标i - 1为结尾的A,和以下标j - 1为结尾的B,最长重复子数组长度为dp[i][j]。 (**特别注意**: “以下标i - 1为结尾的A” 标明一定是 以A[i-1]为结尾的字符串 )
此时细心的同学应该发现,那dp[0][0]是什么含义呢?总不能是以下标-1为结尾的A数组吧。
diff --git "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md"
new file mode 100644
index 0000000000..b990053e10
--- /dev/null
+++ "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md"
@@ -0,0 +1,296 @@
+
+看一下 算法4,深搜是怎么讲的
+
+# 797.所有可能的路径
+
+本题是一道 原汁原味的 深度优先搜索(dfs)模板题,那么用这道题目 来讲解 深搜最合适不过了。
+
+接下来给大家详细讲解dfs:
+
+## dfs 与 bfs 区别
+
+先来了解dfs的过程,很多录友可能对dfs(深度优先搜索),bfs(广度优先搜索)分不清。
+
+先给大家说一下两者大概的区别:
+
+* dfs是可一个方向去搜,不到黄河不回头,直到遇到绝境了,搜不下去了,在换方向(换方向的过程就涉及到了回溯)。
+* bfs是先把本节点所连接的所有节点遍历一遍,走到下一个节点的时候,再把连接节点的所有节点遍历一遍,搜索方向更像是广度,四面八方的搜索过程。
+
+当然以上讲的是,大体可以这么理解,接下来 我们详细讲解dfs,(bfs在用单独一篇文章详细讲解)
+
+## dfs 搜索过程
+
+上面说道dfs是可一个方向搜,不到黄河不回头。 那么我们来举一个例子。
+
+如图一,是一个无向图,我们要搜索从节点1到节点6的所有路径。
+
+
+
+那么dfs搜索的第一条路径是这样的: (假设第一次延默认方向,就找到了节点6),图二
+
+
+
+此时我们找到了节点6,(遇到黄河了,是不是应该回头了),那么应该再去搜索其他方向了。 如图三:
+
+
+
+路径2撤销了,改变了方向,走路径3(红色线), 接着也找到终点6。 那么撤销路径2,改为路径3,在dfs中其实就是回溯的过程(这一点很重要,很多录友,都不理解dfs代码中回溯是用来干什么的)
+
+又找到了一条从节点1到节点6的路径,又到黄河了,此时再回头,下图图四中,路径4撤销(回溯的过程),改为路径5。
+
+
+
+又找到了一条从节点1到节点6的路径,又到黄河了,此时再回头,下图图五,路径6撤销(回溯的过程),改为路径7,路径8 和 路径7,路径9, 结果发现死路一条,都走到了自己走过的节点。
+
+
+
+那么节点2所连接路径和节点3所链接的路径 都走过了,撤销路径只能向上回退,去选择撤销当初节点4的选择,也就是撤销路径5,改为路径10 。 如图图六:
+
+
+
+
+上图演示中,其实我并没有把 所有的 从节点1 到节点6的dfs(深度优先搜索)的过程都画出来,那样太冗余了,但 已经把dfs 关键的地方都涉及到了,关键就两点:
+
+* 搜索方向,是认准一个方向搜,直到碰壁之后在换方向
+* 换方向是撤销原路径,改为节点链接的下一个路径,回溯的过程。
+
+
+## 代码框架
+
+正式因为dfs搜索可一个方向,并需要回溯,所以用递归的方式来实现是最方便的。
+
+很多录友对回溯很陌生,建议先看看码随想录,[回溯算法章节](https://programmercarl.com/回溯算法理论基础.html)。
+
+有递归的地方就有回溯,那么回溯在哪里呢?
+
+就地递归函数的下面,例如如下代码:
+```
+void dfs(参数) {
+ 处理节点
+ dfs(图,选择的节点); // 递归
+ 回溯,撤销处理结果
+}
+```
+
+可以看到回溯操作就在递归函数的下面,递归和回溯是相辅相成的。
+
+在讲解[二叉树章节](https://programmercarl.com/二叉树理论基础.html)的时候,二叉树的递归法其实就是dfs,而二叉树的迭代法,就是bfs(广度优先搜索)
+
+所以**dfs,bfs其实是基础搜索算法,也广泛应用与其他数据结构与算法中**。
+
+我们在回顾一下[回溯法](https://programmercarl.com/回溯算法理论基础.html)的代码框架:
+
+```
+void backtracking(参数) {
+ if (终止条件) {
+ 存放结果;
+ return;
+ }
+ for (选择:本层集合中元素(树中节点孩子的数量就是集合的大小)) {
+ 处理节点;
+ backtracking(路径,选择列表); // 递归
+ 回溯,撤销处理结果
+ }
+}
+
+```
+
+回溯算法,其实就是dfs的过程,这里给出dfs的代码框架:
+
+```
+void dfs(参数) {
+ if (终止条件) {
+ 存放结果;
+ return;
+ }
+
+ for (选择:本节点所连接的其他节点) {
+ 处理节点;
+ dfs(图,选择的节点); // 递归
+ 回溯,撤销处理结果
+ }
+}
+
+```
+
+可以发现dfs的代码框架和回溯算法的代码框架是差不多的。
+
+下面我在用 深搜三部曲,来解读 dfs的代码框架。
+
+## 深搜三部曲
+
+在 [二叉树递归讲解](https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%80%92%E5%BD%92%E9%81%8D%E5%8E%86.html)中,给出了递归三部曲。
+
+[回溯算法](https://programmercarl.com/回溯算法理论基础.html)讲解中,给出了 回溯三部曲。
+
+其实深搜也是一样的,深搜三部曲如下:
+
+1. 确认递归函数,参数
+
+```
+void dfs(参数)
+```
+
+通常我们递归的时候,我们递归搜索需要了解哪些参数,其实也可以在写递归函数的时候,发现需要什么参数,再去补充就可以。
+
+一般情况,深搜需要 二维数组数组结构保存所有路径,需要一维数组保存单一路径,这种保存结果的数组,我们可以定义一个全局遍历,避免让我们的函数参数过多。
+
+例如这样:
+
+```
+vector> result; // 保存符合条件的所有路径
+vector path; // 起点到终点的路径
+void dfs (图,目前搜索的节点)
+```
+
+但这种写法看个人习惯,不强求。
+
+2. 确认终止条件
+
+终止条件很重要,很多同学写dfs的时候,之所以容易死循环,栈溢出等等这些问题,都是因为终止条件没有想清楚。
+
+```
+if (终止条件) {
+ 存放结果;
+ return;
+}
+```
+
+终止添加不仅是结束本层递归,同时也是我们收获结果的时候。
+
+3. 处理目前搜索节点出发的路径
+
+一般这里就是一个for循环的操作,去遍历 目前搜索节点 所能到的所有节点。
+
+```
+for (选择:本节点所连接的其他节点) {
+ 处理节点;
+ dfs(图,选择的节点); // 递归
+ 回溯,撤销处理结果
+}
+```
+
+不少录友疑惑的地方,都是 dfs代码框架中for循环里分明已经处理节点了,那么 dfs函数下面 为什么还要撤销的呢。
+
+如图七所示, 路径2 已经走到了 目的地节点6,那么 路径2 是如何撤销,然后改为 路径3呢? 其实这就是 回溯的过程,撤销路径2,走换下一个方向。
+
+
+
+
+## 总结
+
+我们讲解了,dfs 和 bfs的大体区别(bfs详细过程下篇来讲),dfs的搜索过程以及代码框架。
+
+最后还有 深搜三部曲来解读这份代码框架。
+
+以上如果大家都能理解了,其实搜索的代码就很好写,具体题目套用具体场景就可以了。
+
+## 797. 所有可能的路径
+
+### 思路
+
+1. 确认递归函数,参数
+
+首先我们dfs函数一定要存一个图,用来遍历的,还要存一个目前我们遍历的节点,定义为x
+
+至于 单一路径,和路径集合可以放在全局变量,那么代码是这样的:
+
+```c++
+vector> result; // 收集符合条件的路径
+vector path; // 0节点到终点的路径
+// x:目前遍历的节点
+// graph:存当前的图
+void dfs (vector>& graph, int x)
+```
+
+2. 确认终止条件
+
+什么时候我们就找到一条路径了?
+
+当目前遍历的节点 为 最后一个节点的时候,就找到了一条,从 出发点到终止点的路径。
+
+当前遍历的节点,我们定义为x,最后一点节点,就是 graph.size() - 1。
+
+所以 但 x 等于 graph.size() - 1 的时候就找到一条有效路径。 代码如下:
+
+
+```c++
+// 要求从节点 0 到节点 n-1 的路径并输出,所以是 graph.size() - 1
+if (x == graph.size() - 1) { // 找到符合条件的一条路径
+ result.push_back(path); // 收集有效路径
+ return;
+}
+```
+
+3. 处理目前搜索节点出发的路径
+
+接下来是走 当前遍历节点x的下一个节点。
+
+首先是要找到 x节点链接了哪些节点呢? 遍历方式是这样的:
+
+```c++
+for (int i = 0; i < graph[x].size(); i++) { // 遍历节点n链接的所有节点
+```
+
+接下来就是将 选中的x所连接的节点,加入到 单一路劲来。
+
+```C++
+path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来
+
+```
+
+当前遍历的节点就是 `graph[x][i]` 了,所以进入下一层递归
+
+```C++
+dfs(graph, graph[x][i]); // 进入下一层递归
+```
+
+最后就是回溯的过程,撤销本次添加节点的操作。 该过程整体代码:
+
+```C++
+for (int i = 0; i < graph[x].size(); i++) { // 遍历节点n链接的所有节点
+ path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来
+ dfs(graph, graph[x][i]); // 进入下一层递归
+ path.pop_back(); // 回溯,撤销本节点
+}
+```
+
+
+### 本题代码
+
+```c++
+class Solution {
+private:
+ vector> result; // 收集符合条件的路径
+ vector path; // 0节点到终点的路径
+ // x:目前遍历的节点
+ // graph:存当前的图
+ void dfs (vector>& graph, int x) {
+ // 要求从节点 0 到节点 n-1 的路径并输出,所以是 graph.size() - 1
+ if (x == graph.size() - 1) { // 找到符合条件的一条路径
+ result.push_back(path);
+ return;
+ }
+ for (int i = 0; i < graph[x].size(); i++) { // 遍历节点n链接的所有节点
+ path.push_back(graph[x][i]); // 遍历到的节点加入到路径中来
+ dfs(graph, graph[x][i]); // 进入下一层递归
+ path.pop_back(); // 回溯,撤销本节点
+ }
+ }
+public:
+ vector> allPathsSourceTarget(vector>& graph) {
+ path.push_back(0); // 无论什么路径已经是从0节点出发
+ dfs(graph, 0); // 开始遍历
+ return result;
+ }
+};
+
+```
+
+## 其他语言版本
+
+### Java
+
+### Python
+
+### Go
diff --git "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md"
index 6f51b4addb..58765a8fae 100644
--- "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md"
+++ "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md"
@@ -31,21 +31,182 @@
* 解释:我们不能进入 2 号房间。
-## 思
+## 思路
-其实这道题的本质就是判断各个房间所连成的有向图,说明不用访问所有的房间。
+本题其实给我们是一个有向图, 意识到这是有向图很重要!
-如图所示:
+图中给我的两个示例: `[[1],[2],[3],[]]` `[[1,3],[3,0,1],[2],[0]]`,画成对应的图如下:
-
+
-示例1就可以访问所有的房间,因为通过房间里的key将房间连在了一起。
+我们可以看出图1的所有节点都是链接的,而图二中,节点2 是孤立的。
-示例2中,就不能访问所有房间,从图中就可以看出,房间2是一个孤岛,我们从0出发,无论怎么遍历,都访问不到房间2。
+这就很容易让我们想起岛屿问题,只要发现独立的岛,就是不能进入所有房间。
-认清本质问题之后,**使用 广度优先搜索(BFS) 还是 深度优先搜索(DFS) 都是可以的。**
+此时也容易想到用并查集的方式去解决。
-BFS C++代码代码如下:
+**但本题是有向图**,在有向图中,即使所有节点都是链接的,但依然不可能从0出发遍历所有边。
+给大家举一个例子:
+
+图3:[[5], [], [1, 3], [5]] ,如图:
+
+
+
+在图3中,大家可以发现,节点0只能到节点5,然后就哪也去不了了。
+
+所以本题是一个有向图搜索全路径的问题。 只能用深搜(BFS)或者广搜(DFS)来搜。
+
+关于DFS的理论,如果大家有困惑,可以先看我这篇题解: [DFS理论基础](https://leetcode.cn/problems/all-paths-from-source-to-target/solution/by-carlsun-2-66pf)
+
+**以下dfs分析 大家一定要仔细看,本题有两种dfs的解法,很多题解没有讲清楚**。 看完之后 相信你对dfs会有更深的理解。
+
+深搜三部曲:
+
+1. 确认递归函数,参数
+
+需要传入二维数组rooms来遍历地图,需要知道当前我们拿到的key,以至于去下一个房间。
+
+同时还需要一个数组,用来记录我们都走过了哪些房间,这样好知道最后有没有把所有房间都遍历的,可以定义一个一维数组。
+
+所以 递归函数参数如下:
+
+```C++
+// key 当前得到的可以
+// visited 记录访问过的房间
+void dfs(const vector>& rooms, int key, vector& visited) {
+```
+
+2. 确认终止条件
+
+遍历的时候,什么时候终止呢?
+
+这里有一个很重要的逻辑,就是在递归中,**我们是处理当前访问的节点,还是处理下一个要访问的节点**。
+
+这决定 终止条件怎么写。
+
+首先明确,本题中什么叫做处理,就是 visited数组来记录访问过的节点,那么把该节点默认 数组里元素都是false,把元素标记为true就是处理 本节点了。
+
+如果我们是处理当前访问的节点,当前访问的节点如果是 true ,说明是访问过的节点,那就终止本层递归,如果不是true,我们就把它赋值为true,因为我们处理本层递归的节点。
+
+代码就是这样:
+
+```C++
+// 写法一:处理当前访问的节点
+void dfs(const vector>& rooms, int key, vector& visited) {
+ if (visited[key]) { // 本层递归是true,说明访问过,立刻返回
+ return;
+ }
+ visited[key] = true; // 给当前遍历的节点赋值true
+ vector keys = rooms[key];
+ for (int key : keys) {
+ // 深度优先搜索遍历
+ dfs(rooms, key, visited);
+ }
+}
+```
+
+如果我们是处理下一层访问的节点,而不是当前层。那么就要在 深搜三部曲中第三步:处理目前搜索节点出发的路径 的时候对 节点进行处理。
+
+这样的话,就不需要终止条件,而是在 搜索下一个节点的时候,直接判断 下一个节点是否是我们要搜的节点。
+
+代码就是这样的:
+
+```C++
+// 写法二:处理下一个要访问的节点
+void dfs(const vector>& rooms, int key, vector& visited) {
+ // 这里 没有终止条件,而是在 处理下一层节点的时候来判断
+ vector keys = rooms[key];
+ for (int key : keys) {
+ if (visited[key] == false) { // 处理下一层节点,判断是否要进行递归
+ visited[key] = true;
+ dfs(rooms, key, visited);
+ }
+ }
+}
+```
+
+可以看出,如果看待 我们要访问的节点,直接决定了两种不一样的写法,很多同学对这一块很模糊,其实做过这道题,也没有思考到这个维度上。
+
+
+3. 处理目前搜索节点出发的路径
+
+其实在上面,深搜三部曲 第二部,就已经讲了,因为终止条件的两种写法, 直接决定了两种不一样的递归写法。
+
+这里还有细节:
+
+看上面两个版本的写法中, 好像没有发现回溯的逻辑。
+
+我们都知道,有递归就有回溯,回溯就在递归函数的下面, 那么之前我们做的dfs题目,都需要回溯操作,例如:[797.所有可能的路径](https://leetcode.cn/problems/all-paths-from-source-to-target/solution/by-carlsun-2-66pf/), **为什么本题就没有回溯呢?**
+
+代码中可以看到dfs函数下面并没有回溯的操作。
+
+此时就要在思考本题的要求了,本题是需要判断 0节点是否能到所有节点,那么我们就没有必要回溯去撤销操作了,只要遍历过的节点一律都标记上。
+
+**那什么时候需要回溯操作呢?**
+
+当我们需要搜索一条可行路径的时候,就需要回溯操作了,因为没有回溯,就没法“调头”, 如果不理解的话,去看我写的 [797.所有可能的路径](https://leetcode.cn/problems/all-paths-from-source-to-target/solution/by-carlsun-2-66pf/) 的题解。
+
+
+以上分析完毕,DFS整体实现C++代码如下:
+
+```CPP
+// 写法一:处理当前访问的节点
+class Solution {
+private:
+ void dfs(const vector>& rooms, int key, vector& visited) {
+ if (visited[key]) {
+ return;
+ }
+ visited[key] = true;
+ vector keys = rooms[key];
+ for (int key : keys) {
+ // 深度优先搜索遍历
+ dfs(rooms, key, visited);
+ }
+ }
+public:
+ bool canVisitAllRooms(vector>& rooms) {
+ vector