Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit aac5e5e

Browse files
Merge branch 'youngyangyang04:master' into master
2 parents fa309c9 + 6e2407a commit aac5e5e

12 files changed

+264
-74
lines changed

‎problems/0027.移除元素.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,28 @@ class Solution {
144144

145145
Python:
146146

147-
```python
147+
```python3
148148
class Solution:
149-
def removeElement(self, nums: List[int], val: int) -> int:
150-
i,n = 0,len(nums)
151-
for j in range(n):
152-
if nums[j] != val:
153-
nums[i] = nums[j]
154-
i += 1
155-
return i
149+
"""双指针法
150+
时间复杂度:O(n)
151+
空间复杂度:O(1)
152+
"""
153+
154+
@classmethod
155+
def removeElement(cls, nums: List[int], val: int) -> int:
156+
fast = slow = 0
157+
158+
while fast < len(nums):
159+
160+
if nums[fast] != val:
161+
nums[slow] = nums[fast]
162+
slow += 1
163+
164+
# 当 fast 指针遇到要删除的元素时停止赋值
165+
# slow 指针停止移动, fast 指针继续前进
166+
fast += 1
167+
168+
return slow
156169
```
157170

158171

‎problems/0104.二叉树的最大深度.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,35 @@ class solution {
311311
}
312312
```
313313

314+
### 559.n叉树的最大深度
315+
```java
316+
class solution {
317+
/**
318+
* 迭代法,使用层序遍历
319+
*/
320+
public int maxDepth(Node root) {
321+
if (root == null) return 0;
322+
int depth = 0;
323+
Queue<Node> que = new LinkedList<>();
324+
que.offer(root);
325+
while (!que.isEmpty())
326+
{
327+
depth ++;
328+
int len = que.size();
329+
while (len > 0)
330+
{
331+
Node node = que.poll();
332+
for (int i = 0; i < node.children.size(); i++)
333+
if (node.children.get(i) != null)
334+
que.offer(node.children.get(i));
335+
len--;
336+
}
337+
}
338+
return depth;
339+
}
340+
}
341+
```
342+
314343
## python
315344

316345
### 104.二叉树的最大深度

‎problems/0242.有效的字母异位词.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,29 @@ var isAnagram = function(s, t) {
198198
};
199199
```
200200

201+
Swift:
202+
```Swift
203+
func isAnagram(_ s: String, _ t: String) -> Bool {
204+
if s.count != t.count {
205+
return false
206+
}
207+
var record = Array(repeating: 0, count: 26)
208+
let aUnicodeScalar = "a".unicodeScalars.first!.value
209+
for c in s.unicodeScalars {
210+
record[Int(c.value - aUnicodeScalar)] += 1
211+
}
212+
for c in t.unicodeScalars {
213+
record[Int(c.value - aUnicodeScalar)] -= 1
214+
}
215+
for value in record {
216+
if value != 0 {
217+
return false
218+
}
219+
}
220+
return true
221+
}
222+
```
223+
201224
## 相关题目
202225

203226
* 383.赎金信

‎problems/0454.四数相加II.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,9 @@ class Solution(object):
161161
# count=0
162162
# for x3 in nums3:
163163
# for x4 in nums4:
164-
# key = -x3-x4
165-
# value = hashmap.get(key)
166-
167-
# dict的get方法会返回None(key不存在)或者key对应的value
168-
# 所以如果value==0,就会继续执行or,count+0,否则就会直接加value
169-
# 这样就不用去写if判断了
170-
171-
# count += value or 0
172-
164+
# key = 0 - x3 - x4
165+
# value = hashmap[key] # 若差值(key)不存在,则value被赋值0
166+
# count += value
173167
# return count
174168

175169
```

‎problems/0649.Dota2参议院.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
# 649. Dota2 参议院
1212

13+
[力扣题目链接](https://leetcode-cn.com/problems/dota2-senate/)
14+
15+
1316
Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇)
1417

1518
Dota2 参议院由来自两派的参议员组成。现在参议院希望对一个 Dota2 游戏里的改变作出决定。他们以一个基于轮为过程的投票进行。在每一轮中,每一位参议员都可以行使两项权利中的一项:
@@ -115,16 +118,102 @@ public:
115118
## Java
116119
117120
```java
121+
class Solution {
122+
public String predictPartyVictory(String senateStr) {
123+
// R = true表示本轮循环结束后,字符串里依然有R。D同理
124+
Boolean R = true, D = true;
125+
// 当flag大于0时,R在D前出现,R可以消灭D。当flag小于0时,D在R前出现,D可以消灭R
126+
int flag = 0;
127+
byte[] senate = senateStr.getBytes();
128+
while (R && D) { // 一旦R或者D为false,就结束循环,说明本轮结束后只剩下R或者D了
129+
R = false;
130+
D = false;
131+
for (int i = 0; i < senate.length; i++) {
132+
if (senate[i] == 'R') {
133+
if (flag < 0) senate[i] = 0; // 消灭R,R此时为false
134+
else R = true; // 如果没被消灭,本轮循环结束有R
135+
flag++;
136+
}
137+
if (senate[i] == 'D') {
138+
if (flag > 0) senate[i] = 0;
139+
else D = true;
140+
flag--;
141+
}
142+
}
143+
}
144+
// 循环结束之后,R和D只能有一个为true
145+
return R == true ? "Radiant" : "Dire";
146+
}
147+
}
118148
```
119149

120150
## Python
121151

122152
```python
153+
class Solution:
154+
def predictPartyVictory(self, senate: str) -> str:
155+
# R = true表示本轮循环结束后,字符串里依然有R。D同理
156+
R , D = True, True
157+
158+
# 当flag大于0时,R在D前出现,R可以消灭D。当flag小于0时,D在R前出现,D可以消灭R
159+
flag = 0
160+
161+
senate = list(senate)
162+
while R and D: # 一旦R或者D为false,就结束循环,说明本轮结束后只剩下R或者D了
163+
R = False
164+
D = False
165+
for i in range(len(senate)) :
166+
if senate[i] == 'R' :
167+
if flag < 0: senate[i] = '0' # 消灭R,R此时为false
168+
else: R = True # 如果没被消灭,本轮循环结束有R
169+
flag += 1
170+
if senate[i] == 'D':
171+
if flag > 0: senate[i] = '0'
172+
else: D = True
173+
flag -= 1
174+
# 循环结束之后,R和D只能有一个为true
175+
return "Radiant" if R else "Dire"
123176
```
124177

125178
## Go
126179

127180
```go
181+
182+
func predictPartyVictory(senateStr string) string {
183+
// R = true表示本轮循环结束后,字符串里依然有R。D同理
184+
R, D := true, true
185+
// 当flag大于0时,R在D前出现,R可以消灭D。当flag小于0时,D在R前出现,D可以消灭R
186+
flag := 0
187+
188+
senate := []byte(senateStr)
189+
for R && D { // 一旦R或者D为false,就结束循环,说明本轮结束后只剩下R或者D了
190+
R = false
191+
D = false
192+
for i := 0; i < len(senate); i++ {
193+
if senate[i] == 'R' {
194+
if flag < 0 {
195+
senate[i] = 0 // 消灭R,R此时为false
196+
} else {
197+
R = true // 如果没被消灭,本轮循环结束有R
198+
}
199+
flag++;
200+
}
201+
if (senate[i] == 'D') {
202+
if flag > 0 {
203+
senate[i] = 0
204+
} else {
205+
D = true
206+
}
207+
flag--
208+
}
209+
}
210+
}
211+
// 循环结束之后,R和D只能有一个为true
212+
if R {
213+
return "Radiant"
214+
}
215+
return "Dire";
216+
}
128217
```
129218

130219
## JavaScript

‎problems/0704.二分查找.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,49 @@ func search(nums: [Int], target: Int) -> Int {
406406

407407
```
408408

409+
**Rust:**
410+
411+
```rust
412+
# (版本一)左闭右闭区间
413+
414+
impl Solution {
415+
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
416+
let mut left:usize = 0;
417+
let mut right:usize = nums.len() - 1;
418+
while left as i32 <= right as i32{
419+
let mid = (left + right) / 2;
420+
if nums[mid] < target {
421+
left = mid + 1;
422+
} else if nums[mid] > target {
423+
right = mid - 1;
424+
} else {
425+
return mid as i32;
426+
}
427+
}
428+
-1
429+
}
430+
}
431+
432+
# (版本二)左闭右开区间
433+
434+
impl Solution {
435+
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
436+
let mut left:usize = 0;
437+
let mut right:usize = nums.len();
438+
while left < right {
439+
let mid = (left + right) / 2;
440+
if nums[mid] < target {
441+
left = mid + 1;
442+
} else if nums[mid] > target {
443+
right = mid;
444+
} else {
445+
return mid as i32;
446+
}
447+
}
448+
-1
449+
}
450+
}
451+
```
409452

410453

411454
-----------------------

‎problems/为了绝杀编辑距离,卡尔做了三步铺垫.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
## 判断子序列
1616

17-
[动态规划:392.判断子序列](https://mp.weixin.qq.com/s/2pjT4B4fjfOx5iB6N6xyng) 给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
17+
[动态规划:392.判断子序列](https://programmercarl.com/0392.判断子序列.html) 给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
1818

1919

2020
这道题目 其实是可以用双指针或者贪心的的,但是我在开篇的时候就说了这是编辑距离的入门题目,因为从题意中我们也可以发现,只需要计算删除的情况,不用考虑增加和替换的情况。
@@ -33,9 +33,9 @@ else dp[i][j] = dp[i][j - 1];
3333

3434
## 不同的子序列
3535

36-
[动态规划:115.不同的子序列](https://mp.weixin.qq.com/s/1SULY2XVSROtk_hsoVLu8A) 给定一个字符串 s 和一个字符串 t ,计算在 s 的子序列中 t 出现的个数。
36+
[动态规划:115.不同的子序列](https://programmercarl.com/0115.不同的子序列.html) 给定一个字符串 s 和一个字符串 t ,计算在 s 的子序列中 t 出现的个数。
3737

38-
本题虽然也只有删除操作,不用考虑替换增加之类的,但相对于[动态规划:392.判断子序列](https://mp.weixin.qq.com/s/2pjT4B4fjfOx5iB6N6xyng)就有难度了,这道题目双指针法可就做不了。
38+
本题虽然也只有删除操作,不用考虑替换增加之类的,但相对于[动态规划:392.判断子序列](https://programmercarl.com/0392.判断子序列.html)就有难度了,这道题目双指针法可就做不了。
3939

4040

4141
当s[i - 1] 与 t[j - 1]相等时,dp[i][j]可以有两部分组成。
@@ -68,9 +68,9 @@ if (s[i - 1] == t[j - 1]) {
6868

6969
## 两个字符串的删除操作
7070

71-
[动态规划:583.两个字符串的删除操作](https://mp.weixin.qq.com/s/a8BerpqSf76DCqkPDJrpYg)给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符。
71+
[动态规划:583.两个字符串的删除操作](https://programmercarl.com/0583.两个字符串的删除操作.html)给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符。
7272

73-
本题和[动态规划:115.不同的子序列](https://mp.weixin.qq.com/s/1SULY2XVSROtk_hsoVLu8A)相比,其实就是两个字符串可以都可以删除了,情况虽说复杂一些,但整体思路是不变的。
73+
本题和[动态规划:115.不同的子序列](https://programmercarl.com/0115.不同的子序列.html)相比,其实就是两个字符串可以都可以删除了,情况虽说复杂一些,但整体思路是不变的。
7474

7575

7676
* 当word1[i - 1] 与 word2[j - 1]相同的时候
@@ -100,10 +100,10 @@ if (word1[i - 1] == word2[j - 1]) {
100100

101101
## 编辑距离
102102

103-
[动态规划:72.编辑距离](https://mp.weixin.qq.com/s/8aG71XjSgZG6kZbiAdkJnQ) 给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。
103+
[动态规划:72.编辑距离](https://programmercarl.com/0072.编辑距离.html) 给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。
104104

105105

106-
编辑距离终于来了,**有了前面三道题目的铺垫,应该有思路了**,本题是两个字符串可以增删改,比 [动态规划:判断子序列](https://mp.weixin.qq.com/s/2pjT4B4fjfOx5iB6N6xyng),[动态规划:不同的子序列](https://mp.weixin.qq.com/s/1SULY2XVSROtk_hsoVLu8A),[动态规划:两个字符串的删除操作](https://mp.weixin.qq.com/s/a8BerpqSf76DCqkPDJrpYg)都要复杂的多。
106+
编辑距离终于来了,**有了前面三道题目的铺垫,应该有思路了**,本题是两个字符串可以增删改,比 [动态规划:判断子序列](https://programmercarl.com/0392.判断子序列.html),[动态规划:不同的子序列](https://programmercarl.com/0115.不同的子序列.html),[动态规划:两个字符串的删除操作](https://programmercarl.com/0583.两个字符串的删除操作.html)都要复杂的多。
107107

108108

109109
在确定递推公式的时候,首先要考虑清楚编辑的几种操作,整理如下:
@@ -161,7 +161,7 @@ else {
161161

162162
## 总结
163163

164-
心思的录友应该会发现我用了三道题做铺垫,才最后引出了[动态规划:72.编辑距离](https://mp.weixin.qq.com/s/8aG71XjSgZG6kZbiAdkJnQ) ,Carl的良苦用心呀,你们体会到了嘛!
164+
心思的录友应该会发现我用了三道题做铺垫,才最后引出了[动态规划:72.编辑距离](https://programmercarl.com/0072.编辑距离.html) ,Carl的良苦用心呀,你们体会到了嘛!
165165

166166
## 其他语言版本
167167

‎problems/前序/北京互联网公司总结.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105

106106
可能是我写总结写习惯了,什么文章都要有一个总结,哈哈,那么我就总结一下。
107107

108-
北京的互联网氛围绝对是最好的(暂不讨论户口和房价问题),大家如果看了[深圳原来有这么多互联网公司,你都知道么?](https://mp.weixin.qq.com/s/3VJHF2zNohBwDBxARFIn-Q)这篇之后,**会发现北京互联网外企和二线互联网公司数量多的优势,在深圳的互联网公司断档比较严重,如果去不了为数不多的一线公司,可选择的余地就非常少了,而北京选择的余地就很多!**
108+
北京的互联网氛围绝对是最好的(暂不讨论户口和房价问题),大家如果看了[深圳原来有这么多互联网公司,你都知道么?](https://programmercarl.com/前序/深圳互联网公司总结.html)这篇之后,**会发现北京互联网外企和二线互联网公司数量多的优势,在深圳的互联网公司断档比较严重,如果去不了为数不多的一线公司,可选择的余地就非常少了,而北京选择的余地就很多!**
109109

110110
相对来说,深圳的硬件企业更多一些,因为珠三角制造业配套比较完善。而大多数互联网公司其实就是媒体公司,当然要靠近政治文化中心,这也是有原因的。
111111

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /