From 11115137a8e1b51958954eba3899f052f380353e Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Tue, 15 Jun 2021 22:20:45 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A00071.0501.=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=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加0071.0501.二叉搜索树中的众数 go版本 --- problems/0501.二叉搜索树中的众数.md | 93 ++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index dfd589ce..3ca7d892 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -428,7 +428,100 @@ class Solution: return self.res ``` Go: +暴力法(非BSL) +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func findMode(root *TreeNode) []int { + var history map[int]int + var maxValue int + var maxIndex int + var result []int + history=make(map[int]int) + traversal(root,history) + for k,value:=range history{ + if value>maxValue{ + maxValue=value + maxIndex=k + } + } + for k,value:=range history{ + if value==history[maxIndex]{ + result=append(result,k) + } + } + return result +} +func traversal(root *TreeNode,history map[int]int){ + if root.Left!=nil{ + traversal(root.Left,history) + } + if value,ok:=history[root.Val];ok{ + history[root.Val]=value+1 + }else{ + history[root.Val]=1 + } + if root.Right!=nil{ + traversal(root.Right,history) + } +} +``` + +计数法BSL(此代码在执行代码里能执行,但提交后报错,不知为何,思路是对的) + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + var count,maxCount int //统计计数 +func findMode(root *TreeNode) []int { + var result []int + var pre *TreeNode //前指针 + if root.Left==nil&&root.Right==nil{ + result=append(result,root.Val) + return result + } + traversal(root,&result,pre) + return result +} +func traversal(root *TreeNode,result *[]int,pre *TreeNode){//遍历统计 + //如果BSL中序遍历相邻的两个节点值相同,则统计频率;如果不相同,依据BSL中序遍历排好序的性质,重新计数 + if pre==nil{ + count=1 + }else if pre.Val==root.Val{ + count++ + }else { + count=1 + } + //如果统计的频率等于最大频率,则加入结果集;如果统计的频率大于最大频率,更新最大频率且重新将结果加入新的结果集中 + if count==maxCount{ + *result=append(*result,root.Val) + }else if count>maxCount{ + maxCount=count//重新赋值maxCount + *result=[]int{}//清空result中的内容 + *result=append(*result,root.Val) + } + pre=root//保存上一个的节点 + if root.Left!=nil{ + traversal(root.Left,result,pre) + } + if root.Right!=nil{ + traversal(root.Right,result,pre) + } +} +``` From 7221797cca0a206f469732c3dc5a06c3ef9d3885 Mon Sep 17 00:00:00 2001 From: yangxk201396 <54167260+yangxk201396@users.noreply.github.com> Date: Wed, 16 Jun 2021 02:06:23 +0800 Subject: [PATCH 2/4] =?UTF-8?q?Update=201005.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.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 1005.K次取反后最大化的数组和.md Golang版本 --- ...1005.K次取反后最大化的数组和.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index 387de147..c3e99f7e 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -138,6 +138,30 @@ class Solution: ``` Go: +```Go +func largestSumAfterKNegations(nums []int, K int) int { + sort.Slice(nums, func(i, j int) bool { + return math.Abs(float64(nums[i])) > math.Abs(float64(nums[j])) + }) + + for i := 0; i < len(nums); i++ { + if K > 0 && nums[i] < 0 { + nums[i] = -nums[i] + K-- + } + } + + if K%2 == 1 { + nums[len(nums)-1] = -nums[len(nums)-1] + } + + result := 0 + for i := 0; i < len(nums); i++ { + result += nums[i] + } + return result +} +``` Javascript: From 381c4e1bdf8c2b894e88817a79ffd388144c164c Mon Sep 17 00:00:00 2001 From: yangxk201396 <54167260+yangxk201396@users.noreply.github.com> Date: Wed, 16 Jun 2021 10:43:50 +0800 Subject: [PATCH 3/4] =?UTF-8?q?Update=200134.=E5=8A=A0=E6=B2=B9=E7=AB=99.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0134.加油站.md Golang 版本 --- problems/0134.加油站.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index dfed2d96..9b660ea0 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -240,6 +240,25 @@ class Solution: ``` Go: +```go +func canCompleteCircuit(gas []int, cost []int) int { + curSum := 0 + totalSum := 0 + start := 0 + for i := 0; i < len(gas); i++ { + curSum += gas[i] - cost[i] + totalSum += gas[i] - cost[i] + if curSum < 0 { + start = i+1 + curSum = 0 + } + } + if totalSum < 0 { + return -1 + } + return start +} +``` Javascript: ```Javascript From 2f514cd212303f53e8f30da93498b66e2df9577c Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Wed, 16 Jun 2021 23:11:56 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A00235.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC?= =?UTF-8?q?=E5=85=B1=E7=A5=96=E5=85=88=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加0235.二叉搜索树的最近公共祖先 go版本 --- ...35.二叉搜索树的最近公共祖先.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index 15ff7af4..d78db42a 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -265,6 +265,54 @@ class Solution: else: return root ``` Go: +> BSL法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +//利用BSL的性质(前序遍历有序) +func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { + if root==nil{return nil} + if root.Val>p.Val&&root.Val>q.Val{//当前节点的值大于给定的值,则说明满足条件的在左边 + return lowestCommonAncestor(root.Left,p,q) + }else if root.Val