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 a530ae4

Browse files
Merge branch 'youngyangyang04:master' into master
2 parents 30526c1 + 312e7d8 commit a530ae4

7 files changed

+343
-2
lines changed

‎problems/0112.路径总和.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,92 @@ class Solution:
486486

487487
Go:
488488

489+
> 112. 路径总和
490+
491+
```go
492+
//递归法
493+
/**
494+
* Definition for a binary tree node.
495+
* type TreeNode struct {
496+
* Val int
497+
* Left *TreeNode
498+
* Right *TreeNode
499+
* }
500+
*/
501+
func hasPathSum(root *TreeNode, targetSum int) bool {
502+
var flage bool //找没找到的标志
503+
if root==nil{
504+
return flage
505+
}
506+
pathSum(root,0,targetSum,&flage)
507+
return flage
508+
}
509+
func pathSum(root *TreeNode, sum int,targetSum int,flage *bool){
510+
sum+=root.Val
511+
if root.Left==nil&&root.Right==nil&&sum==targetSum{
512+
*flage=true
513+
return
514+
}
515+
if root.Left!=nil&&!(*flage){//左节点不为空且还没找到
516+
pathSum(root.Left,sum,targetSum,flage)
517+
}
518+
if root.Right!=nil&&!(*flage){//右节点不为空且没找到
519+
pathSum(root.Right,sum,targetSum,flage)
520+
}
521+
}
522+
```
523+
524+
525+
526+
> 113 递归法
527+
528+
```go
529+
/**
530+
* Definition for a binary tree node.
531+
* type TreeNode struct {
532+
* Val int
533+
* Left *TreeNode
534+
* Right *TreeNode
535+
* }
536+
*/
537+
func pathSum(root *TreeNode, targetSum int) [][]int {
538+
var result [][]int//最终结果
539+
if root==nil{
540+
return result
541+
}
542+
var sumNodes []int//经过路径的节点集合
543+
hasPathSum(root,&sumNodes,targetSum,&result)
544+
return result
545+
}
546+
func hasPathSum(root *TreeNode,sumNodes *[]int,targetSum int,result *[][]int){
547+
*sumNodes=append(*sumNodes,root.Val)
548+
if root.Left==nil&&root.Right==nil{//叶子节点
549+
fmt.Println(*sumNodes)
550+
var sum int
551+
var number int
552+
for k,v:=range *sumNodes{//求该路径节点的和
553+
sum+=v
554+
number=k
555+
}
556+
tempNodes:=make([]int,number+1)//新的nodes接受指针里的值,防止最终指针里的值发生变动,导致最后的结果都是最后一个sumNodes的值
557+
for k,v:=range *sumNodes{
558+
tempNodes[k]=v
559+
}
560+
if sum==targetSum{
561+
*result=append(*result,tempNodes)
562+
}
563+
}
564+
if root.Left!=nil{
565+
hasPathSum(root.Left,sumNodes,targetSum,result)
566+
*sumNodes=(*sumNodes)[:len(*sumNodes)-1]//回溯
567+
}
568+
if root.Right!=nil{
569+
hasPathSum(root.Right,sumNodes,targetSum,result)
570+
*sumNodes=(*sumNodes)[:len(*sumNodes)-1]//回溯
571+
}
572+
}
573+
```
574+
489575
JavaScript:
490576

491577
0112.路径总和

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,66 @@ func maxSlidingWindow(nums []int, k int) []int {
332332

333333
```
334334

335+
```go
336+
// 封装单调队列的方式解题
337+
type MyQueue struct {
338+
queue []int
339+
}
340+
341+
func NewMyQueue() *MyQueue {
342+
return &MyQueue{
343+
queue: make([]int, 0),
344+
}
345+
}
346+
347+
func (m *MyQueue) Front() int {
348+
return m.queue[0]
349+
}
350+
351+
func (m *MyQueue) Back() int {
352+
return m.queue[len(m.queue)-1]
353+
}
354+
355+
func (m *MyQueue) Empty() bool {
356+
return len(m.queue) == 0
357+
}
358+
359+
func (m *MyQueue) Push(val int) {
360+
for !m.Empty() && val > m.Back() {
361+
m.queue = m.queue[:len(m.queue)-1]
362+
}
363+
m.queue = append(m.queue, val)
364+
}
365+
366+
func (m *MyQueue) Pop(val int) {
367+
if !m.Empty() && val == m.Front() {
368+
m.queue = m.queue[1:]
369+
}
370+
}
371+
372+
func maxSlidingWindow(nums []int, k int) []int {
373+
queue := NewMyQueue()
374+
length := len(nums)
375+
res := make([]int, 0)
376+
// 先将前k个元素放入队列
377+
for i := 0; i < k; i++ {
378+
queue.Push(nums[i])
379+
}
380+
// 记录前k个元素的最大值
381+
res = append(res, queue.Front())
382+
383+
for i := k; i < length; i++ {
384+
// 滑动窗口移除最前面的元素
385+
queue.Pop(nums[i-k])
386+
// 滑动窗口添加最后面的元素
387+
queue.Push(nums[i])
388+
// 记录最大值
389+
res = append(res, queue.Front())
390+
}
391+
return res
392+
}
393+
```
394+
335395
Javascript:
336396
```javascript
337397
var maxSlidingWindow = function (nums, k) {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,27 @@ class Solution:
130130
return True
131131
```
132132

133+
Python写法二(没有使用数组作为哈希表,只是介绍defaultdict这样一种解题思路):
134+
135+
```python
136+
class Solution:
137+
def isAnagram(self, s: str, t: str) -> bool:
138+
from collections import defaultdict
139+
140+
s_dict = defaultdict(int)
141+
t_dict = defaultdict(int)
142+
143+
for x in s:
144+
s_dict[x] += 1
145+
146+
for x in t:
147+
t_dict[x] += 1
148+
149+
return s_dict == t_dict
150+
```
151+
133152
Go:
153+
134154
```go
135155
func isAnagram(s string, t string) bool {
136156
if len(s)!=len(t){

‎problems/0343.整数拆分.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class Solution:
218218
# 假设对正整数 i 拆分出的第一个正整数是 j(1 <= j < i),则有以下两种方案:
219219
# 1) 将 i 拆分成 j 和 i−j 的和,且 i−j 不再拆分成多个正整数,此时的乘积是 j * (i-j)
220220
# 2) 将 i 拆分成 j 和 i−j 的和,且 i−j 继续拆分成多个正整数,此时的乘积是 j * dp[i-j]
221-
for j in range(1, i):
221+
for j in range(1, i-1):
222222
dp[i] = max(dp[i], max(j * (i - j), j * dp[i - j]))
223223
return dp[n]
224224
```

‎problems/0404.左叶子之和.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,71 @@ class Solution:
226226
```
227227
Go:
228228

229+
> 递归法
230+
231+
```go
232+
/**
233+
* Definition for a binary tree node.
234+
* type TreeNode struct {
235+
* Val int
236+
* Left *TreeNode
237+
* Right *TreeNode
238+
* }
239+
*/
240+
func sumOfLeftLeaves(root *TreeNode) int {
241+
var res int
242+
findLeft(root,&res)
243+
return res
244+
}
245+
func findLeft(root *TreeNode,res *int){
246+
//左节点
247+
if root.Left!=nil&&root.Left.Left==nil&&root.Left.Right==nil{
248+
*res=*res+root.Left.Val
249+
}
250+
if root.Left!=nil{
251+
findLeft(root.Left,res)
252+
}
253+
if root.Right!=nil{
254+
findLeft(root.Right,res)
255+
}
256+
}
257+
```
258+
259+
> 迭代法
260+
261+
```go
262+
/**
263+
* Definition for a binary tree node.
264+
* type TreeNode struct {
265+
* Val int
266+
* Left *TreeNode
267+
* Right *TreeNode
268+
* }
269+
*/
270+
func sumOfLeftLeaves(root *TreeNode) int {
271+
var res int
272+
queue:=list.New()
273+
queue.PushBack(root)
274+
for queue.Len()>0{
275+
length:=queue.Len()
276+
for i:=0;i<length;i++{
277+
node:=queue.Remove(queue.Front()).(*TreeNode)
278+
if node.Left!=nil&&node.Left.Left==nil&&node.Left.Right==nil{
279+
res=res+node.Left.Val
280+
}
281+
if node.Left!=nil{
282+
queue.PushBack(node.Left)
283+
}
284+
if node.Right!=nil{
285+
queue.PushBack(node.Right)
286+
}
287+
}
288+
}
289+
return res
290+
}
291+
```
292+
293+
229294
JavaScript:
230295
递归版本
231296
```javascript
@@ -275,6 +340,7 @@ var sumOfLeftLeaves = function(root) {
275340

276341

277342

343+
278344
-----------------------
279345
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
280346
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

‎problems/0513.找树左下角的值.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,80 @@ class Solution:
298298
```
299299
Go:
300300

301+
> 递归法
302+
303+
```go
304+
/**
305+
* Definition for a binary tree node.
306+
* type TreeNode struct {
307+
* Val int
308+
* Left *TreeNode
309+
* Right *TreeNode
310+
* }
311+
*/
312+
var maxDeep int // 全局变量 深度
313+
var value int //全局变量 最终值
314+
func findBottomLeftValue(root *TreeNode) int {
315+
if root.Left==nil&&root.Right==nil{//需要提前判断一下(不要这个if的话提交结果会出错,但执行代码不会。防止这种情况出现,故先判断是否只有一个节点)
316+
return root.Val
317+
}
318+
findLeftValue (root,maxDeep)
319+
return value
320+
}
321+
func findLeftValue (root *TreeNode,deep int){
322+
//最左边的值在左边
323+
if root.Left==nil&&root.Right==nil{
324+
if deep>maxDeep{
325+
value=root.Val
326+
maxDeep=deep
327+
}
328+
}
329+
//递归
330+
if root.Left!=nil{
331+
deep++
332+
findLeftValue(root.Left,deep)
333+
deep--//回溯
334+
}
335+
if root.Right!=nil{
336+
deep++
337+
findLeftValue(root.Right,deep)
338+
deep--//回溯
339+
}
340+
}
341+
```
342+
343+
> 迭代法
344+
345+
```go
346+
/**
347+
* Definition for a binary tree node.
348+
* type TreeNode struct {
349+
* Val int
350+
* Left *TreeNode
351+
* Right *TreeNode
352+
* }
353+
*/
354+
func findBottomLeftValue(root *TreeNode) int {
355+
queue:=list.New()
356+
var gradation int
357+
queue.PushBack(root)
358+
for queue.Len()>0{
359+
length:=queue.Len()
360+
for i:=0;i<length;i++{
361+
node:=queue.Remove(queue.Front()).(*TreeNode)
362+
if i==0{gradation=node.Val}
363+
if node.Left!=nil{
364+
queue.PushBack(node.Left)
365+
}
366+
if node.Right!=nil{
367+
queue.PushBack(node.Right)
368+
}
369+
}
370+
}
371+
return gradation
372+
}
373+
```
374+
301375
JavaScript:
302376
1. 递归版本
303377
```javascript

0 commit comments

Comments
(0)

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