Merge pull request #56 from QuinnDK/添加0198打家劫舍Go版本

添加0226.翻转二叉树,0019.删除链表的倒数第N个节点,0015.三数之和,0198打家劫舍 Go版本
This commit is contained in:
Carl Sun
2021-05-13 17:09:04 +08:00
committed by GitHub
4 changed files with 96 additions and 1 deletions

View File

@ -221,6 +221,40 @@ Python
Go Go
```Go
func threeSum(nums []int)[][]int{
sort.Ints(nums)
res:=[][]int{}
for i:=0;i<len(nums)-2;i++{
n1:=nums[i]
if n1>0{
break
}
if i>0&&n1==nums[i-1]{
continue
}
l,r:=i+1,len(nums)-1
for l<r{
n2,n3:=nums[l],nums[r]
if n1+n2+n3==0{
res=append(res,[]int{n1,n2,n3})
for l<r&&nums[l]==n2{
l++
}
for l<r&&nums[r]==n3{
r--
}
}else if n1+n2+n3<0{
l++
}else {
r--
}
}
}
return res
}
```

View File

@ -112,7 +112,28 @@ class Solution {
} }
} }
``` ```
Go:
```Go
func removeNthFromEnd(head *ListNode, n int) *ListNode {
result:=&ListNode{}
result.Next=head
var pre *ListNode
cur:=result
i:=1
for head!=nil{
if i>=n{
pre=cur
cur=cur.Next
}
head=head.Next
i++
}
pre.Next=pre.Next.Next
return result.Next
}
```
----------------------- -----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

View File

@ -117,6 +117,33 @@ Python
Go Go
```Go
func rob(nums []int) int {
if len(nums)<1{
return 0
}
if len(nums)==1{
return nums[0]
}
if len(nums)==2{
return max(nums[0],nums[1])
}
dp :=make([]int,len(nums))
dp[0]=nums[0]
dp[1]=max(nums[0],nums[1])
for i:=2;i<len(nums);i++{
dp[i]=max(dp[i-2]+nums[i],dp[i-1])
}
return dp[len(dp)-1]
}
func max(a, b int) int {
if a>b{
return a
}
return b
}
```

View File

@ -208,8 +208,21 @@ Java
Python Python
Go ```Go
func invertTree(root *TreeNode) *TreeNode {
if root ==nil{
return nil
}
temp:=root.Left
root.Left=root.Right
root.Right=temp
invertTree(root.Left)
invertTree(root.Right)
return root
}
```