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 1/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880925.=E9=95=BF?=
=?UTF-8?q?=E6=8C=89=E9=94=AE=E5=85=A5.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
---
...77346円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 2/7] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91.md=20=20python=20code=20via=20iterate?=
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.
---
...41344円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 3/7] =?UTF-8?q?Update=200101.=E5=AF=B9=E7=A7=B0=E4=BA=8C?=
=?UTF-8?q?=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.
---
...60344円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 beb6805c1d7fd77b66ba0870870fd12228d5a4cf Mon Sep 17 00:00:00 2001
From: unknown
Date: 2022年6月23日 10:29:24 +0100
Subject: [PATCH 4/7] =?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円217円II.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円217円II.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円217円II.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円217円II.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円217円II.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 16af63e22ad0d3522c19ccf5eb3dd4a0b4294a9e Mon Sep 17 00:00:00 2001
From: Shuo Zhang
Date: 2022年6月27日 22:54:52 -0400
Subject: [PATCH 5/7] =?UTF-8?q?=E4=BF=AE=E6=94=B90349=20-=20=E7=94=A8=20Ja?=
=?UTF-8?q?va=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
---
...260347円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 0cd4ffd9a470038f1b77207af082a47c00e8d430 Mon Sep 17 00:00:00 2001
From: azou
Date: 2022年6月29日 23:32:29 +0800
Subject: [PATCH 6/7] =?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
---
...351223円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 0c1c77c23412b3a8ebba93fde9017ca8d820936e Mon Sep 17 00:00:00 2001
From: cezarbbb <105843128+cezarbbb@users.noreply.github.com>
Date: 2022年7月13日 19:27:34 +0800
Subject: [PATCH 7/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=89=91=E6=8C=87Offe?=
=?UTF-8?q?r=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版本
---
...54345円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円207円Offer58-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円207円Offer58-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円207円Offer58-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円207円Offer58-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::()
+ }
+}
+```