Skip to content

Commit 10a2089

Browse files
committed
Merge branch 'youngyangyang04:master' into master
2 parents 37515f2 + ff2ec86 commit 10a2089

15 files changed

+519
-17
lines changed

problems/0001.两数之和.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ func twoSum(nums []int, target int) []int {
136136
}
137137
```
138138

139+
```go
140+
// 使用map方式解题,降低时间复杂度
141+
func twoSum(nums []int, target int) []int {
142+
m := make(map[int]int)
143+
for index, val := range nums {
144+
if preIndex, ok := m[target-val]; ok {
145+
return []int{preIndex, index}
146+
} else {
147+
m[val] = index
148+
}
149+
}
150+
return []int{}
151+
}
152+
```
153+
139154
Rust
140155

141156
```rust

problems/0056.合并区间.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,7 @@ Java:
141141
class Solution {
142142
public int[][] merge(int[][] intervals) {
143143
List<int[]> res = new LinkedList<>();
144-
Arrays.sort(intervals, new Comparator<int[]>() {
145-
@Override
146-
public int compare(int[] o1, int[] o2) {
147-
if (o1[0] != o2[0]) {
148-
return Integer.compare(o1[0],o2[0]);
149-
} else {
150-
return Integer.compare(o1[1],o2[1]);
151-
}
152-
}
153-
});
144+
Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[0], o2[0]));
154145

155146
int start = intervals[0][0];
156147
for (int i = 1; i < intervals.length; i++) {

problems/0101.对称二叉树.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,54 @@ Python:
363363

364364
Go:
365365

366+
```go
367+
/**
368+
* Definition for a binary tree node.
369+
* type TreeNode struct {
370+
* Val int
371+
* Left *TreeNode
372+
* Right *TreeNode
373+
* }
374+
*/
375+
// 递归
376+
func defs(left *TreeNode, right *TreeNode) bool {
377+
if left == nil && right == nil {
378+
return true;
379+
};
380+
if left == nil || right == nil {
381+
return false;
382+
};
383+
if left.Val != right.Val {
384+
return false;
385+
}
386+
return defs(left.Left, right.Right) && defs(right.Left, left.Right);
387+
}
388+
func isSymmetric(root *TreeNode) bool {
389+
return defs(root.Left, root.Right);
390+
}
391+
392+
// 迭代
393+
func isSymmetric(root *TreeNode) bool {
394+
var queue []*TreeNode;
395+
if root != nil {
396+
queue = append(queue, root.Left, root.Right);
397+
}
398+
for len(queue) > 0 {
399+
left := queue[0];
400+
right := queue[1];
401+
queue = queue[2:];
402+
if left == nil && right == nil {
403+
continue;
404+
}
405+
if left == nil || right == nil || left.Val != right.Val {
406+
return false;
407+
};
408+
queue = append(queue, left.Left, right.Right, right.Left, left.Right);
409+
}
410+
return true;
411+
}
412+
```
413+
366414

367415
JavaScript
368416
```javascript

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,55 @@ Python:
284284

285285
Go:
286286

287+
```go
288+
/**
289+
* Definition for a binary tree node.
290+
* type TreeNode struct {
291+
* Val int
292+
* Left *TreeNode
293+
* Right *TreeNode
294+
* }
295+
*/
296+
func max (a, b int) int {
297+
if a > b {
298+
return a;
299+
}
300+
return b;
301+
}
302+
// 递归
303+
func maxDepth(root *TreeNode) int {
304+
if root == nil {
305+
return 0;
306+
}
307+
return max(maxDepth(root.Left), maxDepth(root.Right)) + 1;
308+
}
309+
310+
// 遍历
311+
func maxDepth(root *TreeNode) int {
312+
levl := 0;
313+
queue := make([]*TreeNode, 0);
314+
if root != nil {
315+
queue = append(queue, root);
316+
}
317+
for l := len(queue); l > 0; {
318+
for ;l > 0;l-- {
319+
node := queue[0];
320+
if node.Left != nil {
321+
queue = append(queue, node.Left);
322+
}
323+
if node.Right != nil {
324+
queue = append(queue, node.Right);
325+
}
326+
queue = queue[1:];
327+
}
328+
levl++;
329+
l = len(queue);
330+
}
331+
return levl;
332+
}
333+
334+
```
335+
287336

288337
JavaScript
289338
```javascript

problems/0111.二叉树的最小深度.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,64 @@ class Solution:
301301

302302
Go:
303303

304+
```go
305+
/**
306+
* Definition for a binary tree node.
307+
* type TreeNode struct {
308+
* Val int
309+
* Left *TreeNode
310+
* Right *TreeNode
311+
* }
312+
*/
313+
func min(a, b int) int {
314+
if a < b {
315+
return a;
316+
}
317+
return b;
318+
}
319+
// 递归
320+
func minDepth(root *TreeNode) int {
321+
if root == nil {
322+
return 0;
323+
}
324+
if root.Left == nil && root.Right != nil {
325+
return 1 + minDepth(root.Right);
326+
}
327+
if root.Right == nil && root.Left != nil {
328+
return 1 + minDepth(root.Left);
329+
}
330+
return min(minDepth(root.Left), minDepth(root.Right)) + 1;
331+
}
332+
333+
// 迭代
334+
335+
func minDepth(root *TreeNode) int {
336+
dep := 0;
337+
queue := make([]*TreeNode, 0);
338+
if root != nil {
339+
queue = append(queue, root);
340+
}
341+
for l := len(queue); l > 0; {
342+
dep++;
343+
for ; l > 0; l-- {
344+
node := queue[0];
345+
if node.Left == nil && node.Right == nil {
346+
return dep;
347+
}
348+
if node.Left != nil {
349+
queue = append(queue, node.Left);
350+
}
351+
if node.Right != nil {
352+
queue = append(queue, node.Right);
353+
}
354+
queue = queue[1:];
355+
}
356+
l = len(queue);
357+
}
358+
return dep;
359+
}
360+
```
361+
304362

305363
JavaScript:
306364

problems/0151.翻转字符串里的单词.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,66 @@ class Solution {
318318

319319
Python:
320320

321-
322321
Go:
323322

323+
```go
324+
import (
325+
"fmt"
326+
)
327+
328+
func reverseWords(s string) string {
329+
//1.使用双指针删除冗余的空格
330+
slowIndex, fastIndex := 0, 0
331+
b := []byte(s)
332+
//删除头部冗余空格
333+
for len(b) > 0 && fastIndex < len(b) && b[fastIndex] == ' ' {
334+
fastIndex++
335+
}
336+
//删除单词间冗余空格
337+
for ; fastIndex < len(b); fastIndex++ {
338+
if fastIndex-1 > 0 && b[fastIndex-1] == b[fastIndex] && b[fastIndex] == ' ' {
339+
continue
340+
}
341+
b[slowIndex] = b[fastIndex]
342+
slowIndex++
343+
}
344+
//删除尾部冗余空格
345+
if slowIndex-1 > 0 && b[slowIndex-1] == ' ' {
346+
b = b[:slowIndex-1]
347+
} else {
348+
b = b[:slowIndex]
349+
}
350+
//2.反转整个字符串
351+
reverse(&b, 0, len(b)-1)
352+
//3.反转单个单词 i单词开始位置,j单词结束位置
353+
i := 0
354+
for i < len(b) {
355+
j := i
356+
for ; j < len(b) && b[j] != ' '; j++ {
357+
}
358+
reverse(&b, i, j-1)
359+
i = j
360+
i++
361+
}
362+
return string(b)
363+
}
364+
365+
func reverse(b *[]byte, left, right int) {
366+
for left < right {
367+
(*b)[left], (*b)[right] = (*b)[right], (*b)[left]
368+
left++
369+
right--
370+
}
371+
}
372+
```
373+
374+
375+
324376

325377

326378

327379
-----------------------
328380
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
329381
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
330382
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
331-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
383+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/0239.滑动窗口最大值.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,41 @@ class Solution {
263263
```
264264

265265
Python:
266+
```python
267+
class MyQueue: #单调队列(从大到小
268+
def __init__(self):
269+
self.queue = [] #使用list来实现单调队列
270+
271+
#每次弹出的时候,比较当前要弹出的数值是否等于队列出口元素的数值,如果相等则弹出。
272+
#同时pop之前判断队列当前是否为空。
273+
def pop(self, value):
274+
if self.queue and value == self.queue[0]:
275+
self.queue.pop(0)#list.pop()时间复杂度为O(n),这里可以使用collections.deque()
276+
277+
#如果push的数值大于入口元素的数值,那么就将队列后端的数值弹出,直到push的数值小于等于队列入口元素的数值为止。
278+
#这样就保持了队列里的数值是单调从大到小的了。
279+
def push(self, value):
280+
while self.queue and value > self.queue[-1]:
281+
self.queue.pop()
282+
self.queue.append(value)
283+
284+
#查询当前队列里的最大值 直接返回队列前端也就是front就可以了。
285+
def front(self):
286+
return self.queue[0]
287+
288+
class Solution:
289+
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
290+
que = MyQueue()
291+
result = []
292+
for i in range(k): #先将前k的元素放进队列
293+
que.push(nums[i])
294+
result.append(que.front()) #result 记录前k的元素的最大值
295+
for i in range(k, len(nums)):
296+
que.pop(nums[i - k]) #滑动窗口移除最前面元素
297+
que.push(nums[i]) #滑动窗口前加入最后面的元素
298+
result.append(que.front()) #记录对应的最大值
299+
return result
300+
```
266301

267302

268303
Go:

problems/0347.前K个高频元素.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,33 @@ class Solution {
162162

163163

164164
Python:
165-
165+
```python
166+
#时间复杂度:O(nlogk)
167+
#空间复杂度:O(n)
168+
import heapq
169+
class Solution:
170+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
171+
#要统计元素出现频率
172+
map_ = {} #nums[i]:对应出现的次数
173+
for i in range(len(nums)):
174+
map_[nums[i]] = map_.get(nums[i], 0) + 1
175+
176+
#对频率排序
177+
#定义一个小顶堆,大小为k
178+
pri_que = [] #小顶堆
179+
180+
#用固定大小为k的小顶堆,扫面所有频率的数值
181+
for key, freq in map_.items():
182+
heapq.heappush(pri_que, (freq, key))
183+
if len(pri_que) > k: #如果堆的大小大于了K,则队列弹出,保证堆的大小一直为k
184+
heapq.heappop(pri_que)
185+
186+
#找出前K个高频元素,因为小顶堆先弹出的是最小的,所以倒叙来输出到数组
187+
result = [0] * k
188+
for i in range(k-1, -1, -1):
189+
result[i] = heapq.heappop(pri_que)[1]
190+
return result
191+
```
166192

167193
Go:
168194

problems/0383.赎金信.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ class Solution(object):
166166
```
167167

168168
Go:
169+
```go
170+
func canConstruct(ransomNote string, magazine string) bool {
171+
record := make([]int, 26)
172+
for _, v := range magazine {
173+
record[v-'a']++
174+
}
175+
for _, v := range ransomNote {
176+
record[v-'a']--
177+
if record[v-'a'] < 0 {
178+
return false
179+
}
180+
}
181+
return true
182+
}
183+
```
169184

170185
javaScript:
171186

0 commit comments

Comments
 (0)