<|start_filename|>42lian-xu-zi-shu-zu-de-zui-da-he-lcof/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
)

func maxSubArray(nums []int) int {
	max :=math.MinInt64
	for i:=0;i<len(nums);i++{
		 for j:=i+1;j<=len(nums);j++{
			r := SubArraySum(nums[i:j])
			if max<r {
				 max = r
			}
		 }
	 }
	 return max
}
func SubArraySum(nums[]int)int{
	var res int
	for i:=0;i<len(nums);i++{
		res +=nums[i]
	}
	return res
}


func maxSubArray1(nums []int) int {
    if len(nums)==0{
    	return 0
	}
	max :=nums[0]
	res :=make([][]int,len(nums))
	for i:=0;i<len(nums);i++{
		res[i]= make([]int,len(nums)+1)
		res[i][i]=nums[i]
		if max<res[i][i] {
			max = res[i][i]
		}
		for j:=i+1;j<len(nums);j++{
			res[i][j]= res[i][j-1]+nums[j]
			if max<res[i][j] {
				max = res[i][j]
			}
		}
	}
	return max
}





func main() {
	//fmt.Println(maxSubArray1([]int{-2,1,-3,4,-1,2,1,-5,4}))
	//fmt.Println(maxSubArray1([]int{1}))
	fmt.Println(maxSubArray1([]int{-2,1}))
	//fmt.Println(maxSubArray([]int{-2,1}))
}


<|start_filename|>68er-cha-sou-suo-shu-de-zui-jin-gong-gong-zu-xian-lcof/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	v int
	left *TreeNode
	right *TreeNode
}

func lowestCommonAncestor(root,left,right *TreeNode)int  {
	for root!=nil{
		if left.v>root.v&&right.v>root.v {
			root=root.right
		}else if left.v<root.v&&right.v<root.v {
			root = right.left
		}else {
			break
		}
	}
	return root.v
}

func main() {
	a :=TreeNode{v:6}
	b :=TreeNode{v:2}
	c :=TreeNode{v:8}
	a.left= &b
	a.right =&c
	d :=TreeNode{v:0}
	f :=TreeNode{v:4}
	b.left = &d
	b.right = &f
	g :=TreeNode{v:3}
	h :=TreeNode{v:5}
	f.left = &g
	f.right = &h
	i :=TreeNode{v:7}
	j :=TreeNode{v:9}
	c.left = &i
	c.right = &j
	fmt.Println(lowestCommonAncestor(&a,&b,&c))
	fmt.Println(lowestCommonAncestor(&a,&b,&f))
}


<|start_filename|>443string-compression/main.go<|end_filename|>
package main

import (
	"fmt"
	"strconv"
)

func compress(chars []byte) int {
	var anchor = 0
	var write = 0
	for read := 0; read < len(chars); read++ {
		if read+1 == len(chars) || chars[read+1] != chars[read] {
			chars[write] = chars[anchor]
			write++
			if read > anchor {
				char := strconv.Itoa(read - anchor + 1)
				for i := 0; i < len(char); i++ {
					chars[write] = char[i]
					write++
				}
			}
			anchor = read + 1
		}
	}
	return write
}

func main() {
	fmt.Println(compress([]byte{'a', 'a', 'a', 'b', 'b'}))
	fmt.Println(compress([]byte{'a','a','b','b','c','c','c'}))
	fmt.Println(compress([]byte{'a'}))
	fmt.Println(compress([]byte{'a','b','b','b','b','b','b','b','b','b','b','b','b'}))
}


<|start_filename|>111minimum-depth-of-binary-tree/main.go<|end_filename|>
package main

import (
	"fmt"
)

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

var min = 1<<63 - 1

func minDepth(root *TreeNode) int {
	if root == nil {
		return 0
	}
	dfs(root, 1) //深度递归
	return min
}

func dfs(node *TreeNode, depth int) {
	if node.Right == nil && node.Left == nil { //递归结束条件
		if depth < min {
			min = depth
		}
		return
	}
	if node.Left != nil { // 遍历左节点
		dfs(node.Left, depth+1)
	}

	if node.Right != nil { // 遍历右节点
		dfs(node.Right, depth+1)
	}
}

func main() {
	a := &TreeNode{
		Val: 3,
	}
	b := &TreeNode{
		Val: 9,
	}
	a.Left = b
	c := &TreeNode{
		Val: 20,
	}
	a.Right = c
	d := &TreeNode{
		Val: 15,
	}
	c.Left = d
	e := &TreeNode{
		Val: 7,
	}
	c.Right = e
	fmt.Println(minDepth(a))
}


<|start_filename|>1394find-lucky-integer-in-an-array/main.go<|end_filename|>
package main

import (
	"fmt"
)

func findLucky(arr []int) int {
      m :=make(map[int]int)
      for _,v:=range arr{
      	   m[v]++
	  }
	  max := -1
	  for k,v:=range m{
	  	  if k==v&&max<k{
	  	  	 max =k
		  }
	  }
	  return max
}

func main() {
	fmt.Println(findLucky([]int{2,2,3,4}))
	fmt.Println(findLucky([]int{1,2,2,3,3,3}))
	fmt.Println(findLucky([]int{2,2,2,3,3}))
	fmt.Println(findLucky([]int{5}))
	fmt.Println(findLucky([]int{7,7,7,7,7,7,7}))
}


<|start_filename|>45jump-game-ii/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
)

func jump(nums []int) int {

	//记录能够到达的最远距离
	maxPosition :=0
	steps :=0
	//记录上一步 step 跳的最远距离也就是边界
	end :=0
	//        //这里有个小细节，因为是起跳的时候就 + 1 了，如果最后一次跳跃刚好到达了最后一个位置，那么遍历到最后一个位置的时候就会再次起跳，这是不允许的，因此不能遍历最后一个位置
	for i:=0;i<len(nums)-1;i++{
		//更新最大距离
		maxPosition = int(math.Max(float64(maxPosition),float64(nums[i]+i)))
		/*
		   我们什么时候需要步数 + 1？
		   当到达上一步的最远距离的时候，那么意味着我们需要进行一次新的起跳，那么步数 + 1
		   并且更新最远距离
		*/
		////第一次起跳 或 到达跳跃的边界
		if i==end{
			end =maxPosition
			steps++
		}
	}
	return steps
}


func main() {
	fmt.Println(jump([]int{1,2}))
	fmt.Println(jump([]int{1,2,3}))
	fmt.Println(jump([]int{2,3,1,1,4}))
}


<|start_filename|>657robot-return-to-origin/main.go<|end_filename|>
package main

import "fmt"

func judgeCircle1(moves string) bool {
     m :=make(map[byte]int)
     for i:=range moves{
     	m[moves[i]]++
	 }
	if m['U']==m['D']&&m['R']==m['L'] {
		return true
	}
	return false
}

func judgeCircle(moves string) bool {
	m :=make([]int,4)
	for _,v:=range moves{
		if v=='U'{
			m[0]++
		}else if v=='D'{
			m[1]++
		}else if v=='R'{
			m[2]++
		}else {
			m[3]++
		}
	}
	if m[0]==m[1]&&m[2]==m[3] {
		return true
	}
	return false
}



func main() {
	fmt.Println(judgeCircle("UD"))
	fmt.Println(judgeCircle("LL"))
}


<|start_filename|>1025divisor-game/main.go<|end_filename|>
package main
func divisorGame(N int) bool {
	if N%2==0{
		return true
	}
	return false
}
func main() {
	
}


<|start_filename|>2.01remove-duplicate-node-lcci/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

func removeDuplicateNodes1(head *ListNode) *ListNode {
    m :=make(map[int]struct{})
    dummyHead :=&ListNode{Val:-1}
	pre :=dummyHead
    for head!=nil{
    	if _,ok:=m[head.Val];!ok{
			m[head.Val] = struct{}{}
			pre.Next = head
			pre = head
		}else {
			pre.Next = head.Next
		}
		head = head.Next
	}
	return  dummyHead.Next
}


func removeDuplicateNodes(head *ListNode) *ListNode {
	m :=make(map[int]struct{})
	dummyHead :=&ListNode{Next:head}
	curr :=dummyHead
	for curr!=nil&&curr.Next!=nil{
		val :=curr.Next.Val
		if _,ok:=m[val];ok{
			curr.Next = curr.Next.Next
		}else {
			m[val] = struct{}{}
			curr = curr.Next
		}
	}
	return  dummyHead.Next
}
func main() {
	n1 :=&ListNode{Val:1}
	n2 :=&ListNode{Val:2}
	n1.Next = n2
	n3 :=&ListNode{Val:3}
	n2.Next = n3
	n4 :=&ListNode{Val:3}
	n3.Next = n4
	n5 :=&ListNode{Val:2}
	n4.Next = n5
	n6:=&ListNode{Val:1}
	n5.Next = n6
	h :=removeDuplicateNodes1(n1)
	for h!=nil{
		fmt.Println(h.Val)
		h=h.Next
	}
}


<|start_filename|>893groups-of-special-equivalent-strings/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func numSpecialEquivGroups(A []string) int {
	var res int
	m:=make(map[string]struct{})
	for _,word:=range A{
		var even []byte
		var odd []byte
		for i:=range word{
			if i%2==0{
				even=append(even,word[i])
			}else{
				odd=append(odd,word[i])
			}
		}
		sort.Slice(even, func(i, j int) bool {
			return even[i]<even[j]
		})
		sort.Slice(odd, func(i, j int) bool {
			return odd[i]<odd[j]
		})
		s:=string(even)+string(odd)
		if _,ok:=m[s];!ok{
			res++
			m[s]= struct{}{}
		}
	}
	return res
}

func main() {
	fmt.Println(numSpecialEquivGroups([]string{"aa","bb","ab","ba"}))
	fmt.Println(numSpecialEquivGroups([]string{"abcd","cdab","adcb","cbad"}))
}


<|start_filename|>25reverse-nodes-in-k-group/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
	     Val int
	     Next *ListNode
}
func revert(head *ListNode)*ListNode{
	var pre *ListNode
	curr := head
	for curr!= nil{
		pre,curr,curr.Next = curr,curr.Next,pre
	}
	return pre
}

func reverseKGroup2(head *ListNode, k int) *ListNode {
	dummy := &ListNode{Val:  -1,}
	dummy.Next = head
	pre,curr,end := dummy,dummy,dummy
	for end!=nil{
		// 获取当前节点
		curr = pre.Next
		// 获取结束结点
		for i:=0;i<k;i++{
			end = end.Next
			if end == nil{
				return dummy.Next
			}
		}
		// 暂存后节点
		next := end.Next
		end.Next = nil// 至空
		pre.Next = revert(curr)
		// 反转结束后当前子链表的头结点
		pre,end = curr,curr// 都是当前值
		curr.Next = next//需要把链表串起来
	}
	return dummy.Next
}

func reverseKGroup1(head *ListNode, k int) *ListNode {
	dummy := &ListNode{Val:  -1,}
	dummy.Next = head
	pre := dummy
	stack := make([]*ListNode,0)
	n := k
	for pre.Next!=nil{
		tmp := pre.Next
		for tmp!= nil&&n>0{
			stack = append(stack,tmp)
			tmp = tmp.Next
			n--
		}
		nextNode := stack[len(stack)-1].Next
		if n==0{
			for len(stack)>0{
				node :=stack[len(stack)-1]
				stack = stack[:len(stack)-1]
				pre.Next = node
				pre = pre.Next
			}
			//需要保证节点的连续行
			pre.Next = nextNode
			n = k
		}else {
			return dummy.Next
		}
	}
	return dummy.Next
}
// 栈处理
func reverseKGroup(head *ListNode, k int) *ListNode {
	if head==nil{
		return nil
	}
	dummyHead :=&ListNode{}
	preNode := dummyHead
	stack := make([]*ListNode,0)
	for head!=nil{
		count :=0
		for head!=nil&&count<k{
			stack = append(stack,head)
			head = head.Next
			count++
		}
		l := len(stack)
		if l<k{
			preNode.Next = stack[0]
			break
		}
		for i:=l-1;i>=0;i--{
			preNode.Next = stack[i]
			preNode = stack[i]
		}
		preNode.Next =nil
		stack =stack[l:]
	}
	return dummyHead.Next
}


func main() {
	a := &ListNode{Val:  1}
	b :=&ListNode{Val:  2}
	a.Next =b
	c :=&ListNode{Val:  3}
	b.Next = c
	d :=&ListNode{Val:  4}
	c.Next = d
	e :=&ListNode{Val:  5}
	d.Next = e
	//f :=&ListNode{Val:  6}
	//e.Next = f
	//r :=a
	r := reverseKGroup(a,3)
	for r!=nil{
		fmt.Println(r.Val)
		r = r.Next
	}
}


<|start_filename|>105construct-binary-tree-from-preorder-and-inorder-traversal/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func buildTree(preorder []int, inorder []int) *TreeNode {
	if len(preorder) == 0 {
		return nil
	}
	rootVal := preorder[0]
	i := 0
	// 查找根节点位置
	for ; i < len(inorder); i++ {
		if rootVal == inorder[i] {
			break
		}
	}
	root := &TreeNode{Val: rootVal}
	root.Left = buildTree(preorder[1:i+1], inorder[:i])
	root.Right = buildTree(preorder[i+1:], inorder[i+1:])
	return root
}

func main() {
	r := buildTree([]int{3, 9, 20, 15, 7}, []int{9, 3, 15, 20, 7})
	printPreorder(r)
	fmt.Println()
	printInorder(r)

}
func printPreorder(r *TreeNode) {
	if r == nil {
		return
	}
	fmt.Print(r.Val)
	printPreorder(r.Left)
	printPreorder(r.Right)
}

func printInorder(r *TreeNode) {
	if r == nil {
		return
	}

	printInorder(r.Left)
	fmt.Print(r.Val)
	printInorder(r.Right)
}


<|start_filename|>1370increasing-decreasing-string/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
	"strings"
)

type Node struct {
	Val byte
	Count int
}


func sortString(s string) string {
	if len(s)<=1{
		return s
	}
	m := make(map[byte]int)
	for i :=range s{
		m[s[i]]++
	}
	var nodeList []*Node
	for k,v:=range m{
		nodeList = append(nodeList,&Node{Val: k, Count: v,})
	}
	sort.Slice(nodeList, func(i, j int) bool {
		   return nodeList[i].Val<nodeList[j].Val
	})

	var res strings.Builder
	for len(res.String())<len(s){
		for i:=0;i<len(nodeList);i++{
			 n := nodeList[i]
			if  n.Count>0{
				res.WriteByte(n.Val)
				n.Count--
			}
		}
		for i:=len(nodeList)-1;i>=0;i--{
			n := nodeList[i]
			if  n.Count>0{
				res.WriteByte(n.Val)
				n.Count--
			}
		}
	}
	return res.String()
}

func main() {
	fmt.Println(sortString("aaaabbbbcccc"))
	fmt.Println(sortString("rat"))
}


<|start_filename|>122best-time-to-buy-and-sell-stock-ii/main.go<|end_filename|>
package main

import "fmt"

func maxProfit(prices []int) int {
	var res int
	for i:=0;i<len(prices)-1;i++{
        p := prices[i+1]-prices[i]
        if p>0{
        	res+=p
		}
	}
	return res
}


func main() {
	fmt.Println(maxProfit([]int{7,1,5,3,6,4}))
	fmt.Println(maxProfit([]int{1,2,3,4,5}))
}


<|start_filename|>350intersection-of-two-arrays-ii/main.go<|end_filename|>
package main

import "fmt"

func intersect(nums1 []int, nums2 []int) []int {
	m1 :=make(map[int]int)
	for _,v:=range nums1{
		m1[v]++
	}
	m2 :=make(map[int]int)
	for _,v:=range nums2{
		m2[v]++
	}
	var res[]int
	for k,v :=range m1{
		r := m2[k]
		r = min(v,r)
		for i:=0;i<r;i++{
			res =append(res,k)
		}
	}
	return res
}

func min(a,b int)int  {
	if a<b{
		return a
	}
	return b
}

func main() {
	fmt.Println(intersect([]int{1,2,2,1},[]int{2,2}))
	fmt.Println(intersect([]int{4,9,5},[]int{9,4,9,8,4}))
}


<|start_filename|>3shu-zu-zhong-zhong-fu-de-shu-zi-lcof/main.go<|end_filename|>
package main

import "fmt"

func findRepeatNumber(nums []int) int {
	m := make([]int,len(nums))
    for _,v:=range nums{
    	if m[v]>0{
    		return v
		}else{
			m[v]++
		}
	}
	return -1
}
func main() {
	fmt.Println(findRepeatNumber([]int{2, 3, 1, 0, 2, 5, 3}))
}


<|start_filename|>27remove-element/main.go<|end_filename|>
package main

import "fmt"

//2n
func removeElement(nums []int, val int) int {
	var l []int
    for _,v :=range nums{
		if val!=v{
			l = append(l,v)
		}
	}
	for i,v:=range l{
		nums[i]=v
	}
	return len(l)
}

func removeElement2(nums []int, val int) int {
	var index = 0
	for _,v :=range nums{
		if val!=v{
			nums[index]= v
			index++
		}
	}
	return index
}


func removeElement3(nums []int, val int) int {
	slow := 0
	quick :=0
	for quick<len(nums){
		if nums[quick]!=val {
			nums[slow]=nums[quick]
			slow++
		}
		quick++
	}
	return slow
}


func main() {
	fmt.Println(removeElement3([]int{3,2,2,3},3))//2
	fmt.Println(removeElement3([]int{0,1,2,2,3,0,4,2},2))//5
	fmt.Println(removeElement3([]int{2},3))//1
	fmt.Println(removeElement3([]int{2},2))//0
}


<|start_filename|>532next-greater-element-ii/main.go<|end_filename|>
package main

import (
	"fmt"
)

func findPairs(nums []int, k int) int {
	numsHas := make(map[int]bool)
	diffHas := make(map[int]bool)
   for _,num :=range nums{
	   if  numsHas[num-k]{
		   diffHas[num - k] = true
	   }
	   if numsHas[num+k] {
		   diffHas[num] = true
	   }
	   numsHas[num] = true
   }
   return len(diffHas)
}

func main() {
	fmt.Println(findPairs([]int{3, 1, 4, 1, 5},2))
	fmt.Println(findPairs([]int{1, 2, 3, 4, 5},1))
	fmt.Println(findPairs([]int{1,2,3,4,5},3))
}


<|start_filename|>92reverse-linked-list-ii/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
	     Val int
	     Next *ListNode
}

func reverseBetween1(head *ListNode, m int, n int) *ListNode {
	dummyNode := &ListNode{}
	dummyNode.Next = head
	begin := dummyNode
	for i:=0;i<m-1;i++{
		begin = begin.Next
	}
	var pre *ListNode
	curr := begin.Next
	for i:=0;i<n-m+1;i++{
		tmp := curr.Next
		curr.Next = pre
		pre = curr
		curr = tmp
	}
	begin.Next.Next = curr// 链接后节点
	begin.Next = pre // 开始反转的最后节点
	return dummyNode.Next
}

func reverseBetween(head *ListNode, m int, n int) *ListNode {
	dummyHead :=&ListNode{
		Val:  -1,
		Next: head,
	}
	curr :=head
	i := 1
	pre :=dummyHead
	var next *ListNode
	var start *ListNode
	for curr!=nil{
		if i==m {
			start = curr
		}
		if i==n{
			next = curr.Next
			curr.Next = nil
			pre.Next = revertNode(start)
			start.Next = next
			return dummyHead.Next
		}
		if i<=m-1{
			pre = curr //获取前一个节点
		}
		curr = curr.Next
		i++
	}

	return dummyHead.Next
}

func revertNode(head*ListNode)*ListNode{
	curr :=head
	var pre *ListNode
	for curr!=nil{
		pre,curr,curr.Next = curr,curr.Next,pre
	}
	return pre
}
func revertNode1(head*ListNode)*ListNode{
	  var pre *ListNode
	  curr := head
	  for curr!=nil{
		  tmp :=curr.Next
		  curr.Next = pre
		  pre = curr
	  	  curr= tmp
	  }
	return pre
}

func makeLinkedList(list []int)*ListNode{
	dummyNode := &ListNode{} // 创建哑结点 指向头节点
	curr := dummyNode
	for i:=0;i<len(list);i++{
		curr.Next= &ListNode{Val:list[i]}
		curr = curr.Next
	}
	return dummyNode.Next
}
func main() {
     head := makeLinkedList([]int{1,2,3,4,5})
    // head = revertNode(head)
     head = reverseBetween(head,1,2)
     for head!=nil{
     	fmt.Println(head.Val)
     	head = head.Next
	 }
}


<|start_filename|>136single-number/main.go<|end_filename|>
package main

import "fmt"

//func singleNumber(nums []int) int {
//        n :=map[int]int{}
//        for _,v :=range nums{
//        	n[v]++
//		}
//        for k,v :=range n{
//        	if v ==1{
//        		return k
//			}
//		}
//        return 0
//}

// 异或运算
//任何数和 00 做异或运算，结果仍然是原来的数
// 任何数和其自身做异或运算，结果是 00
func singleNumber(nums []int) int {
	var ans = 0
	for _,n :=range nums {
		ans ^= n
	}
	return ans
}

func main() {
	fmt.Println(singleNumber([]int{2,2,1}))
	fmt.Println(singleNumber([]int{4,1,2,1,2}))
	fmt.Println(singleNumber([]int{-1}))
}


<|start_filename|>598range-addition-ii/main.go<|end_filename|>
package main

import "fmt"

func maxCount1(m int, n int, ops [][]int) int {
	if len(ops) == 0 {
		return m * n
	}
	matrix := make([][]int, m)
	for i := range matrix {
		matrix[i] = make([]int, n)
	}

	for i := 0; i < len(ops); i++ {
		for j := 0; j < ops[i][0]; j++ {
			for k := 0; k < ops[i][1]; k++ {
				matrix[j][k]++
			}
		}
	}
	max := matrix[0][0]
	var res int
	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			if max == matrix[i][j] {
				res++
			}
		}
	}
	return res
}

func maxCount(m int, n int, ops [][]int) int {
	if len(ops) == 0 {
		return m * n
	}
	r := ops[0][0]
	c := ops[0][1]
	for i := 1; i < len(ops); i++ {
		if r > ops[i][0] {
			r = ops[i][0]
		}
		if c > ops[i][1] {
			c = ops[i][1]
		}
	}
	return r * c
}

func main() {
	fmt.Println(maxCount1(3, 3, [][]int{{2, 2}, {3, 3}}))
}


<|start_filename|>144binary-tree-preorder-traversal/main.go<|end_filename|>
package main

import "fmt"

// 前序遍历
type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}
// 递归方法
func preorderTraversal1(root *TreeNode) []int{
	if root==nil {
		return nil
	}
	fmt.Println(root.Val)
	preorderTraversal1(root.Left)
	preorderTraversal1(root.Right)
	return nil
}

 //中序遍历
func preorderTraversal2(root *TreeNode) []int {
	var l []int
	//如果是空直接返回
	if root == nil {
		return nil
	}
	// 创建栈 递归从根源就是栈结构
	stack := make([]*TreeNode, 0)
	 curr := root
	// 只要当前节点不为空，或者栈不为空，就一直循环查找左节点
	for curr != nil || len(stack) > 0 {
		//查找左节点
		for (curr != nil){
			stack = append(stack,curr)
			fmt.Println(curr.Val)
			l = append(l,curr.Val)
			curr = curr.Left
		}
		curr = stack[len(stack)-1]
		stack = stack[:len(stack)-1]
		curr = curr.Right
	}
	return l
}

//前序遍历  中左右
func preorderTraversal(root *TreeNode) []int {
	var l []int
	//如果是空直接返回
	if root == nil {
		return nil
	}
	// 创建栈 递归从根源就是栈结构
	stack := make([]*TreeNode, 0)
	stack = append(stack,root)
	// 只要栈不为空
	for  len(stack) > 0 {
		//直接出栈
		 curr := stack[len(stack)-1]
		 stack = stack[:len(stack)-1]
		 l = append(l,curr.Val)
		 //先压入右节点
		if curr.Right != nil {
			stack = append(stack,curr.Right)
		}
		 //先压入左节点
		if curr.Left != nil {
			stack = append(stack,curr.Left)
		}
	}
	return l
}


//https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
func main() {
	a := &TreeNode{
		Val: 1,
	}
	b := &TreeNode{
		Val: 2,
	}
	a.Left = b
	c := &TreeNode{
		Val: 3,
	}
	a.Right = c
	d := &TreeNode{
		Val: 4,
	}
	b.Left = d
	e := &TreeNode{
		Val: 5,
	}
	b.Right = e
	f := &TreeNode{
		Val: 6,
	}
	c.Left = f
	// 1 2 4 5 3 6
	preorderTraversal(a)
}


<|start_filename|>59hua-dong-chuang-kou-de-zui-da-zhi-lcof/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
)

func Max(a [] int)int{
    var	max = math.MinInt64
	for _,v :=range a{
		if v>max {
			max = v
		}
	}
	return max
}
func maxSlidingWindow(nums []int, k int) []int {
	if len(nums)==0 {
		return nil
	}
	l :=0
	res :=make([]int,0)
	for i:=0;i<=len(nums)-k;i++{
		res =append(res,Max(nums[l:l+k]))
		l++
	}
	return res
}

func main() {
	fmt.Println(maxSlidingWindow([]int{1,3,-1,-3,5,3,6,7},3))
}


<|start_filename|>3longest-substring-without-repeating-characters/main.go<|end_filename|>
package main

import (
	"fmt"
)

func lengthOfLongestSubstring(s string) int {
	if len(s)<=1{
		return len(s)
	}
	m :=make(map[byte]struct{})
	m[s[0]]= struct{}{}
	var res = 0
	start,end := 0,1
	for start<len(s)&&end<len(s){
		if _,ok:=m[s[end]];!ok{
			m[s[end]]= struct{}{}
			res = max(res,end-start+1)
			end++
		}else{
			delete(m,s[start])
			start++
		}
	}
	return res
}

func max(i,j int)int{
	if i>j{
		return i
	}else{
		return j
	}
}

func lengthOfLongestSubstring2(s string) int{
	m :=make(map[byte]int) //定义map
	max :=0
    start :=0
	for i :=range s{
		v,ok := m[s[i]]
		if ok {
			j := start
			for ;j<=v;j++{
				delete(m,s[j])
			}
			start =j
		}
		m[s[i]] = i
		if max<i-start+1 {
			max = i-start+1
		}
	}
	return max
}
func lengthOfLongestSubstring3(s string) int{
	m := make(map[byte]int)
	var r []byte //窗口数据
	l :=0
	for i :=range s{
		if j,ok:=m[s[i]];ok {
			if i-j>len(r) {
				r =r[i-j:]
			}
		}else {
			r = append(r,s[i])
		}
		m[s[i]] = i
		l = max(l,len(r))
	}
	return l
}


func lengthOfLongestSubstring4(s string) int{
	if len(s)==0 {
		return 0
	}
	ans := 0 // 默认就是1了
	m := map[byte]int{}
	start := -1
	for i:=0 ;i<len(s);i++{// 遍历字符串
		if index,ok := m[s[i]];ok {//存在
			start = max(start,index)//但是要保证起始索引是递增的
		}
		ans = max(ans,i-start)
		m[s[i]]=i
	}
	return ans
}

func main() {
	fmt.Println(lengthOfLongestSubstring(""))//0
	fmt.Println(lengthOfLongestSubstring(" "))//1
	fmt.Println(lengthOfLongestSubstring("au"))//2
	fmt.Println(lengthOfLongestSubstring("abcabcbb"))//3
	fmt.Println(lengthOfLongestSubstring("bbbb"))//1
	fmt.Println(lengthOfLongestSubstring("pwwkew"))//3
	fmt.Println(lengthOfLongestSubstring("abba"))//2
	fmt.Println(lengthOfLongestSubstring("bpfbhmipx"))//7
    fmt.Println(lengthOfLongestSubstring("eeydgwdykpv"))//7
}


<|start_filename|>146lru-cache/main.go<|end_filename|>
package main

import "fmt"

type Node struct {
	key int
	value int
	pre *Node
	next *Node
}

type LRUCache struct {
     m map[int] *Node
     head *Node
     tail *Node
	 capacity int
}


func Constructor(capacity int) LRUCache {
    cache := LRUCache{
		  m:        make(map[int]*Node),
		  head:     &Node{},
		  tail:     &Node{},
		  capacity: capacity,
	  }
	  cache.head.next = cache.tail
	  cache.tail.pre = cache.head
	  return cache
}
// 头部添加元素
func (this *LRUCache)AddHead(n *Node){
	 tmp := this.head.next
	 this.head.next = n
	 n.next = tmp
	 n.pre = this.head
	 tmp.pre = n
}
func (this *LRUCache)RemoveNode(n *Node){
	pre := n.pre
	next := n.next
	pre.next = next
	next.pre = pre
	n.pre = nil
	n.next = nil
}


func (this *LRUCache) Get(key int) int {
	node ,ok:= this.m[key]
	if ok {
		this.RemoveNode(node)
		this.AddHead(node)
		return node.value
	}
	return -1
}


func (this *LRUCache) Put(key int, value int)  {
	node ,ok:= this.m[key]
	if ok {// 存在
		if node.value!= value {
			node.value = value
		}
		this.RemoveNode(node)
		this.AddHead(node)
	}else {
		n := &Node{
			key:key,
			value: value,
		}
		if this.capacity==len(this.m) {// 删除
			delete(this.m,this.tail.pre.key)
			this.RemoveNode(this.tail.pre)
		}
		this.AddHead(n)
		this.m[key] = n
	}
}
func main() {
	cache := Constructor(2)
	cache.Put(1,1)
	cache.Put(2,2)
	fmt.Println(cache.Get(1))//1
	cache.Put(3,3)
	fmt.Println(cache.Get(2))//-1
	cache.Put(4,4)
	fmt.Println(cache.Get(1))//-1
	fmt.Println(cache.Get(3))//3
	fmt.Println(cache.Get(4))//4
}


<|start_filename|>10fei-bo-na-qi-shu-lie-lcof/dp.go<|end_filename|>
package main

import "fmt"

func fib(n int) int {
	if n<2 {
		return n
	}
	dp := make([]int,n+1)
	dp[0] =0
	dp[1] =1
	for i:=2;i<=n;i++{
		dp[i] = (dp[i-1]+dp[i-2])%1000000007
	}
   return dp[n]
}


func main() {
	fmt.Println(fib(5))
	fmt.Println(fib(43))
}


<|start_filename|>292nim-game/main.go<|end_filename|>
package main

import "fmt"

func canWinNim(n int) bool {
   return n%4!=0
}

func main() {
	fmt.Println(canWinNim(3))
}


<|start_filename|>56shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/main.go<|end_filename|>
package main

import (
	"fmt"
)

func singleNumbers(nums []int) []int {
	var res int
   for _,v:=range nums{
   	   res= res^v
   	   fmt.Println(v,res)
   }
   return nil
}

func main() {
	fmt.Println(singleNumbers([]int{4,1,4,6}))
	fmt.Println(singleNumbers([]int{1,2,10,4,1,4,3,3}))
}


<|start_filename|>58fan-zhuan-dan-ci-shun-xu-lcof/main.go<|end_filename|>
package main

import (
	"fmt"
	"strings"
)

func reverseWords(s string) string {
	strList := strings.Split(s," ")
	var res []string
	for i :=len(strList)-1;i>=0;i--{
		str := strings.TrimSpace(strList[i])
		if  len(str)>0 {
			res = append(res,strList[i])
		}
	}
	return strings.Join(res," ")
}

func main() {
	fmt.Println(reverseWords("the sky is blue"))
	fmt.Println(reverseWords("  hello world!  "))
	//
	fmt.Println(reverseWords("a good   example"))

}


<|start_filename|>239sliding-window-maximum/main.go<|end_filename|>
package main

import (
	"fmt"
)

// 判断需要移动的次数
func maxSlidingWindow(nums []int, k int) []int {
	//len-k+1
	p :=len(nums)-k+1
	slide:=make([]int,p)
	for i:=0;i<p;i++{
		max := nums[i]
		for j:=i+1;j<i+k;j++{
			if max<nums[j] {
				max =nums[j]
			}
		}
		slide[i] = max
	}
	return slide
}

type Dequeue struct {
	arr []int
}
func NewDequeue (size int)*Dequeue{
	return &Dequeue{arr:make([]int,size)}
}
//
func(d *Dequeue)AddFirst(v int){
	d.arr = append([]int{v},d.arr...)
}

func(d *Dequeue)RemoveFirst(){
	if len(d.arr)==0 {
		return
	}
	d.arr =d.arr[1:]
}
func(d *Dequeue)isEmpty()bool{
	return len(d.arr)==0
}

func(d *Dequeue)GetFirst()int{
	if len(d.arr)==0 {
		return 0
	}
	return d.arr[0]
}
func(d *Dequeue)AddLast(v int){
	d.arr = append(d.arr,v)
}
func(d *Dequeue)RemoveLast(){
	if len(d.arr)==0 {
		return
	}
	d.arr =d.arr[:len(d.arr)-1]
}
func(d *Dequeue)GetLast()int{
	if len(d.arr)==0 {
		return 0
	}
	return d.arr[len(d.arr)-1]
}
func maxSlidingWindow1(nums []int, k int )[]int{
	numLens :=len(nums)
	if numLens<=0{
		return nil
	}
	queues := NewDequeue(numLens)
	var result []int
	for i,v :=range nums{
		for !queues.isEmpty()&&v>=queues.GetLast(){
			queues.RemoveLast()
		}
		queues.AddLast(nums[i])
		if i>=k&&nums[i-k]==queues.GetFirst() {// 窗口最大值已经不在超过了窗口里 需要移开
			queues.RemoveFirst()
		}
		if i>=k-1 {//需要大于等于窗口才把最大元素放入
			// 放入结果数组
			result = append(result,queues.GetFirst())
		}

	}
   return result
}

func maxSlidingWindow2(nums []int, k int )[]int{
	numLens :=len(nums)
	if numLens<=0{
		return nil
	}
	var window []int //存储坐标点
	var result []int // 窗口最大值
	for i :=range nums{
		if len(window)>0&&window[0]==i-k {//需要保证最大值的索引值在当前窗口访问内
			window = window[1:]
		}
		for len(window)>0&&(nums[window[len(window)-1]]<=nums[i] ) {// 最后值小于所求当前值
			window = window[:len(window)-1] // 保证最左边最大值
		}
		window =append(window,i)
		if i>=k-1{
			result = append(result,nums[window[0]])
		}
	}
	return result
}


// 滑动窗口最大值
func main() {
	// [3 3 5 5 6 7]
	fmt.Println(maxSlidingWindow2([]int{1,3,-1,-3,5,3,6,7},3))
	// [1,-1]
	//fmt.Println(maxSlidingWindow2([]int{1,-1},1))
}


<|start_filename|>671second-minimum-node-in-a-binary-tree/main.go<|end_filename|>
package main
type TreeNode struct {
	Val int
	Left *TreeNode
	Right *TreeNode
}

func findSecondMinimumValue(root *TreeNode) int {
	return dfs(root, root.Val)
}

func dfs(root *TreeNode, s int) int {

	// If root is nil, that means we can't find anything,
	// we return -1
	if root == nil {
		return -1
	}

	// If the current tree value is bigger than the smallest
	// element we hat, that means we have found the second smallest
	// one in the tree, since all subtrees is greater or equal to this
	// value.
	if root.Val > s {
		return root.Val
	}

	sLeft := dfs(root.Left, s)
	sRight := dfs(root.Right, s)

	if sLeft == -1 {
		return sRight
	}

	if sRight == -1 {
		return sLeft
	}

	return min(sLeft, sRight)

}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func main() {
	a := &TreeNode{
		Val: 4,
	}
	b := &TreeNode{
		Val: 2,
	}
	a.Left = b
	c := &TreeNode{
		Val: 7,
	}
	a.Right = c
	d := &TreeNode{
		Val: 1,
	}
	b.Left = d
	e := &TreeNode{
		Val: 3,
	}
	b.Right = e
	f := &TreeNode{
		Val: 6,
	}
	c.Left = f
	g := &TreeNode{
		Val: 9,
	}
	c.Right = g
	// 9 7 64 3 2 1
	findSecondMinimumValue(a)
}


<|start_filename|>500keyboard-row/main.go<|end_filename|>
package main

import "fmt"

func findWords(words []string) []string {
     m :=make([]int,123)
     for _,v :=range "qwertyuiopQWERTYUIOP"{
     	m[v]=0
	 }
	for _,v :=range "asdfghjklASDFGHJKL"{
		m[v]=1
	}
	for _,v :=range "zxcvbnmZXCVBNM"{
		m[v]=2
	}
	var res []string
	for _,word:=range words{
		level :=m[word[0]]
		isLevel :=true
		for i:=1;i<len(word);i++{
			if level!=m[word[i]]{
				isLevel = false
				break
			}
		}
		if isLevel{
			res = append(res,word)
		}
	}
	return res
}


func main() {
	fmt.Println(findWords([]string{"Hello", "Alaska", "Dad", "Peace"}))
}


<|start_filename|>7zhong-jian-er-cha-shu-lcof/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func buildTree(preorder []int, inorder []int) *TreeNode {
	if len(preorder) == 0 {
		return nil
	}
	rootIdx := 0
	for i := range inorder {
		if inorder[i] == preorder[0] {
			rootIdx = i
			break
		}
	}
	root := &TreeNode{Val: preorder[0]}
	root.Left = buildTree(preorder[1:rootIdx+1], inorder[:rootIdx])
	root.Right = buildTree(preorder[rootIdx+1:], inorder[rootIdx+1:])
	return root
}
func printTree(root *TreeNode) {
	if root == nil {
		return
	}
	fmt.Println(root.Val)
	printTree(root.Left)
	printTree(root.Right)
}

func main() {
	r := buildTree([]int{3, 9, 20, 15, 7}, []int{9, 3, 15, 20, 7})
	printTree(r)
}


<|start_filename|>17da-yin-cong-1dao-zui-da-de-nwei-shu-lcof/main.go<|end_filename|>
package main

import (
	"math"
	"fmt"
)

func printNumbers(n int) []int {
    var res []int
    var max int
    for n>0{
    	max =max*10+9
    	n--
	}
    for i:=1;i<=max;i++{
    	res = append(res,i)
	}
	return res
}

func printNumbers1(n int) []int {
	var res []int
	for i:=1;i<int(math.Pow10(n));i++{
		res = append(res,i)
	}
	return res
}

func main() {
	//fmt.Println(printNumbers1(1))
	fmt.Println(printNumbers1(2))
	//fmt.Println(printNumbers(3))
}


<|start_filename|>64minimum-path-sum/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
)

func minPathSum(grid [][]int) int {
    return cal(grid,0,0)
}

func cal(grid [][]int,i,j int)int{
	if i==len(grid)||j==len(grid[0]){
		return math.MaxInt64
	}
	if i ==len(grid)-1&&j==len(grid[0])-1{
		return grid[i][j]
	}
	return grid[i][j]+min(cal(grid,i+1,j),cal(grid,i,j+1))
}
func min(a,b int)int{
	if a>b{
		return b
	}
	return a
}

func main() {
	fmt.Println(minPathSum([][]int{{1,3,1}, {1,5,1}, {4,2,1}}))

	//fmt.Println(minPathSum([][]int{
	//	{1,4,8,6,2,2,1,7},
	//	{4,7,3,1,4,5,5,1},
	//	{8,8,2,1,1,8,0,1},
	//	{8,9,2,9,8,0,8,9},
	//	{5,7,5,7,1,8,5,5},
	//	{7,0,9,4,5,6,5,6},
	//	{4,9,9,7,9,1,9,0}},
	//))
}


<|start_filename|>1413minimum-value-to-get-positive-step-by-step-sum/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
)
// 需要一个数 和所有数组相加和都保持不小于1
func minStartValue(nums []int) int {
	 var sum int
	 var res =1
     for _,v:=range nums{
     	sum +=v
     	if sum<1{
     		res = int(math.Max(float64(res),float64(1-sum)))
		}
	 }
	 return res
}

func main() {
	fmt.Println(minStartValue([]int{-3,2,-3,4,2}))
	fmt.Println(minStartValue([]int{1,2}))
	fmt.Println(minStartValue([]int{1,-2,-3}))
}


<|start_filename|>25he-bing-liang-ge-pai-xu-de-lian-biao-lcof/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
    dummyHead :=&ListNode{Val:  -1,}
    curr :=dummyHead
    for l1!=nil||l2!=nil{
    	if l1!=nil&&l2!=nil{
    		if l1.Val<l2.Val{
				curr.Next = l1
				curr = l1
    			l1 =l1.Next
			}else {
				curr.Next = l2
				curr = l2
				l2 =l2.Next
			}
		}else if l1!=nil{
			curr.Next = l1
			break
		}else {
			curr.Next = l2
			break
		}

	}
	return dummyHead.Next
}

//面试题25. 合并两个排序的链表
func main() {
  l1:=&ListNode{Val:  1,}
	two:=&ListNode{Val:  2,}
	l1.Next=two
	four:=&ListNode{Val:  4,}
	two.Next=four


	l2:=&ListNode{Val:  1,}
	three:=&ListNode{Val:  3,}
	l2.Next=three
	f:=&ListNode{Val:  4,}
	three.Next=f

	h := mergeTwoLists(l1,l2)
	for h!=nil{
		fmt.Println(h.Val)
		h =h.Next
	}
}


<|start_filename|>16sub-sort-lcci/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func subSort(array []int) []int {
     tmp := make([]int,len(array))
     copy(tmp,array)
     sort.Slice(tmp,func(i ,j int)bool{
		 return tmp[i]<tmp[j]
	 })
     start :=-1
     end := -1
     for i :=0;i<len(array);i++{
		 if array[i]!=tmp[i] {
			 if start<0 {
				start= i
			 }
			 if end<i {
				 end=i
			 }
		 }
	 }
     return []int{start,end}
}
func main() {
	fmt.Println(subSort([]int{1,2,4,7,10,11,7,12,6,7,16,18,19}))
}


<|start_filename|>4.02minimum-height-tree-lcci/main.go<|end_filename|>
package main

 type TreeNode struct {
	     Val int
	     Left *TreeNode
	     Right *TreeNode
 }

func sortedArrayToBST(nums []int) *TreeNode {
       if len(nums)==0{
       	return nil
	   }
	   mid := len(nums)/2
	   root :=&TreeNode{Val:nums[mid]}
	   root.Left = sortedArrayToBST(nums[:mid])
	   root.Right = sortedArrayToBST(nums[mid+1:])
	   return root
}

//面试题 04.02. 最小高度树
func main() {
	
}


<|start_filename|>647palindromic-substrings/main.go<|end_filename|>
package main

import "fmt"

func countSubstrings(s string) int {
	var res int
	for i := 0; i < len(s); i++ {
		for j := i + 1; j <= len(s); j++ {
			if isPalindrome(s[i:j]) {
				res++
			}
		}
	}
	return res
}

func countSubstrings1(s string) int {

	var res int
	flag := make([][]bool,len(s))
	for i:=range flag{
		flag[i]=  make([]bool,len(s))
	}
	for j := 0; j < len(s); j++ {
		for i := j ; i >=0; i-- {
			if s[i] == s[j] && (j - i < 2 || flag[i + 1][j - 1]) {
				flag[i][j] = true
				res++
			}
		}
	}
	return res
}

func isPalindrome(s string) bool {
	fmt.Println(s)
	l := 0
	r := len(s) - 1
	for l < r {
		if s[l] != s[r] {
			return false
		}
		l++
		r--
	}
	return true
}

func main() {
	fmt.Println(countSubstrings1("abba"))
	//fmt.Println(countSubstrings1("aaa"))
}


<|start_filename|>38count-and-say/main.go<|end_filename|>
package main

import (
	"fmt"
	"strconv"
	"strings"
)

func countAndSay(n int) string {
	str :="1"
    for i:=2;i<=n;i++{
    	var tmp strings.Builder
    	preByte := str[0]
    	count := 1
    	for j:=1;j<len(str);j++{
    		if str[j]==preByte{
    			count++
			}else {
				tmp.WriteString(strconv.Itoa(count))
				tmp.WriteByte(preByte)
				preByte =str[j]
				count=1
			}
		}
		tmp.WriteString(strconv.Itoa(count))
		tmp.WriteByte(preByte)
    	str = tmp.String()
	}
	return str
}

func main() {
	fmt.Println(countAndSay(4))
}


<|start_filename|>1.01is-unique-lcci/main.go<|end_filename|>
package main

import "fmt"

func isUnique(astr string) bool {
      m := make([]byte,128)
      for i :=range astr{
      	  if m[astr[i]]>0{
      	  	return false
		  }
		  m[astr[i]]++
	  }
	  return true
}

//面试题 01.01. 判定字符是否唯一 https://leetcode-cn.com/problems/is-unique-lcci/
func main() {
	fmt.Println(isUnique("leetcode"))
	fmt.Println(isUnique("abc"))
}


<|start_filename|>442find-all-duplicates-in-an-array/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func findDuplicates(nums []int) []int {
    m := make(map[int]int)
    for _,v:=range nums{
		if i,ok:=m[v];ok {
			m[v]=i+1
		}else {
			m[v]=1
		}
	}
	var res []int
    for k,v:=range m{
		if v==2 {
			res = append(res,k)
		}
	}
	sort.Ints(res)
	return res
}
func main() {
	fmt.Println(findDuplicates([]int{4,3,2,7,8,2,3,1}))
}


<|start_filename|>234palindrome-linked-list/main.go<|end_filename|>
package main

import (
	"fmt"
)

type ListNode struct {
	     Val int
	     Next *ListNode
}
func isPalindrome(head *ListNode) bool {
	if head==nil {
		return  true
	}
	var list []*ListNode
      for head!= nil{
      	list = append(list,head)
      	head = head.Next
	  }
	  i,j:= 0,len(list)-1
	  for i<j{
		  if list[i].Val!=list[j].Val {
			  return false
		  }
		  i++
		  j--
	  }
	  return true
}

func isPalindrome1(head *ListNode) bool {
	if head==nil {
		return  true
	}
	mid := findMid(head)
    rev := revertNode(mid)
	for head!=nil&&rev!= nil{
		if head.Val!= rev.Val {
			return false
		}
		head= head.Next
		rev = rev.Next
	}
	return true
}

func findMid(head *ListNode)*ListNode{
	low,fast := head,head
	for fast!= nil&&fast.Next!=nil{
		low = low.Next
		fast = fast.Next.Next
	}
	return low
}

func revertNode1(head *ListNode)*ListNode{
	var pre *ListNode
	curr := head
	for curr!= nil{
		pre,curr,curr.Next = curr,curr.Next,pre
	}
  return pre
}
func revertNode(head *ListNode)*ListNode{
	var pre *ListNode
	curr := head
	for curr!= nil{
		next := curr.Next
		curr.Next = pre
		pre = curr
		curr = next
		//pre,curr,curr.Next = curr,curr.Next,pre
	}
	return pre
}

func main() {
	a :=&ListNode{
		Val:  1,
	}
	b :=&ListNode{
		Val:  2,
	}
	a.Next = b
	fmt.Println(isPalindrome1(a))

	a1 :=&ListNode{
		Val:  1,
	}
	b1 :=&ListNode{
		Val:  2,
	}
	a1.Next = b1
	c1 :=&ListNode{
		Val:  2,
	}
	b1.Next = c1
	d2 :=&ListNode{
		Val:  1,
	}
	c1.Next = d2
	fmt.Println(isPalindrome1(a1))
}


<|start_filename|>59.02dui-lie-de-zui-da-zhi-lcof/main.go<|end_filename|>
package main

type MaxQueue struct {
    List []int
    Max  int
}

func Constructor() MaxQueue {
    return MaxQueue{List: make([]int,0),Max: -1}
}

func (this *MaxQueue) Max_value() int {
    return this.Max
}

func (this *MaxQueue) Push_back(value int)  {
     if this.Max<value{
     	this.Max = value
	 }
	 this.List = append(this.List,value)
}

func (this *MaxQueue) Pop_front() int {
	if len(this.List)==0{
		return -1
	}
	currVal := this.List[0]
	this.List = this.List[1:]
	if currVal==this.Max{
		this.Max = -1
		for i:=0;i<len(this.List);i++{
			if this.Max<this.List[i]{
				this.Max=this.List[i]
			}
		}
	}
	return currVal
}


//面试题59 - II. 队列的最大值
func main() {
	
}


<|start_filename|>147insertion-sort-list/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

func insertionSortList(head *ListNode) *ListNode {
    dummyHead :=&ListNode{Val:  -1}
    var pre *ListNode
    curr := head
	for curr!=nil{
		pre = dummyHead
		next := curr.Next
		// 查找插入位置，从头结点开始遍历，知道当前节点的数据大于插入值停止
		for pre.Next!= nil&&pre.Next.Val<curr.Val{
			pre = pre.Next
		}
		// pre 节点就是需要插入节点位置的前节点
		// 插入一个新的节点
		curr.Next = pre.Next
		pre.Next = curr
		curr = next
	}
	return dummyHead.Next
}


func main() {
	four :=&ListNode{Val:  4}
	two :=&ListNode{Val:  2}
	four.Next = two
	one :=&ListNode{Val:  1}
	two.Next = one
	three :=&ListNode{Val:  3}
	one.Next= three
	h:= insertionSortList(four)
	for h!=nil{
		fmt.Println(h.Val)
		h =h.Next
	}
	fmt.Println("------")

	 head :=&ListNode{Val:  -1}
	 five:=&ListNode{Val:  5}
	 head.Next = five
	 five.Next = three
	 three.Next= four
	 zero :=&ListNode{Val:  0}
	 four.Next= zero
	 h = insertionSortList(head)
	 for h!=nil{
		fmt.Println(h.Val)
		h =h.Next
	 }
}


<|start_filename|>704binary-search/main.go<|end_filename|>
package main

import "fmt"

func search(nums []int, target int) int {
	left := 0
	right := len(nums) - 1
	for left <= right {
		mid := left + (right-left)>>1
		if nums[mid] == target {
			return mid
		} else if nums[mid] > target {
			right = mid - 1
		} else {
			left = mid + 1
		}
	}
	return -1
}

func main() {
	fmt.Println(search([]int{-1, 0, 3, 5, 9, 12}, 9))
}


<|start_filename|>556next-greater-element-iii/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
	"sort"
)

func nextGreaterElement(n int) int {
     // 第一步需要拆解成数组
	  var nums []int
	  for n>0{
	  	  r := n %10
	  	  n = n/10
	  	  nums = append(nums,r)
	  }
	  idx :=-1
      //[2,3,4,1]
	  for i:=0;i<len(nums)-1;i++{
		  if nums[i]>nums[i+1] {
		  	  idx = i+1
			  break
		  }
	  }
	 if idx==-1 {
		return -1
	 }
	//[2,3,4,1]
	 for i:=0;i<idx;i++{
	 	 if nums[i]>nums[idx]{
	 	 	nums[i],nums[idx] =nums[idx],nums[i]
	 	 	break
		 }
	 }
     // [1,3,4,2]
	 sort.Slice(nums[:idx], func(i, j int) bool {
		   return  nums[i]>nums[j]
	 })
	 //[4,3,1,2]
	 res :=0
	 for i:=len(nums)-1;i>=0;i--{
	 	  res =res*10+nums[i]
		 if res>math.MaxInt32 {
			 return -1
		 }
	  }
	 return res
}
func main() {
	fmt.Println(nextGreaterElement(1432))
	fmt.Println(nextGreaterElement(1342))
	fmt.Println(nextGreaterElement(123))
	fmt.Println(nextGreaterElement(12))
	fmt.Println(nextGreaterElement(21))
}


<|start_filename|>219contains-duplicate-ii/main.go<|end_filename|>
package main

import (
	"fmt"
)

func containsNearbyDuplicate(nums []int, k int) bool {
	m := make(map[int]int)
	for i, v := range nums {
		if r, ok := m[v]; ok {
			if i-r <= k {
				return true
			}
		}
		m[v] = i
	}
	return false
}

func main() {
	fmt.Println(containsNearbyDuplicate([]int{1,2,3,1},3))
	fmt.Println(containsNearbyDuplicate([]int{1,0,1,1},1))
	fmt.Println(containsNearbyDuplicate([]int{1,2,3,1,2,3},2))
}


<|start_filename|>80remove-duplicates-from-sorted-array-ii/main.go<|end_filename|>
package main

import "fmt"

func removeDuplicates(nums []int) int {
    slow :=2
    for fast :=2;fast<len(nums);fast++{
    	if nums[fast]!=nums[slow-2]{
			nums[slow]=nums[fast]
    		slow++
		}
	}
	return slow
}
func main() {
	fmt.Println(removeDuplicates([]int{1,1,1,2,2,3}))
}


<|start_filename|>1021remove-outermost-parentheses/main.go<|end_filename|>
package main

import (
	"fmt"
	"strings"
)

func removeOuterParentheses(S string) string {
	  if len(S)==0{
	  	return ""
	  }
	  stack := []byte{S[0]}
	  start :=1
	  var res strings.Builder
      for i:=1;i<len(S);i++{
		  if len(stack)>0&&S[i]==')'&&stack[len(stack)-1]=='(' {
		  	  stack = stack[:len(stack)-1]
		  	  if len(stack)==0{
		  	  	  res.WriteString(S[start:i])
				  start = i+2
			  }
		  }else {
		  	 stack = append(stack,S[i])
		  }
	  }
	  return res.String()
}

func main() {
	fmt.Println(removeOuterParentheses("(()())(())"))
	fmt.Println(removeOuterParentheses("(()())(())(()(()))"))
	fmt.Println(removeOuterParentheses("()()"))
}


<|start_filename|>606construct-string-from-binary-tree/main.go<|end_filename|>
package main

import (
	"fmt"
	"strings"
)

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func tree2str(t *TreeNode) string {
	var str strings.Builder
	if t == nil {
		return ""
	}
	stack := []interface{}{t}
	for len(stack) > 0 {
		node := stack[len(stack)-1]
		stack = stack[:len(stack)-1]
		if n, ok := node.(*TreeNode); ok {
			str.WriteString(fmt.Sprintf("%d", n.Val))
			if n.Right != nil {
				stack = append(stack, ")")
				stack = append(stack, n.Right)
				stack = append(stack, "(")
			}
			if n.Right != nil && n.Left == nil {
				stack = append(stack, "()")
			}
			if n.Left != nil {
				stack = append(stack, ")")
				stack = append(stack, n.Left)
				stack = append(stack, "(")
			}
		} else {
			s := node.(string)
			str.WriteString(s)
		}
	}
	return str.String()
}
func main() {
	one := &TreeNode{Val: 1}
	two := &TreeNode{Val: 2}
	one.Left = two
	three := &TreeNode{Val: 3}
	one.Right = three
	four := &TreeNode{Val: 4}
	two.Right = four
	fmt.Println(tree2str(one))

	//one1 :=&TreeNode{Val: 1}
	//two1 :=&TreeNode{Val: 2}
	//one1.Left = two1
	//three1 :=&TreeNode{Val: 3}
	//one1.Right = three1
	//four1 :=&TreeNode{Val: 4}
	//two1.Right = four1
	//fmt.Println(tree2str(one1))

}


<|start_filename|>101symmetric-tree/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	Val int
	Left *TreeNode
	Right *TreeNode
}

func isSymmetric(root *TreeNode) bool {
	return isMirror(root.Left,root.Right)
}

func isMirror(t1 *TreeNode,t2 *TreeNode) bool{
	if t1==nil&&t2==nil {
		return true
	}
	if t1==nil||t2==nil{
		return false
	}
	return (t1.Val == t2.Val)&&isMirror(t1.Left,t2.Right)&&isMirror(t1.Right,t2.Left)
}

func isSymmetric1(root *TreeNode) bool {
	if root == nil {
		return  true // []认为是true
	}
	queue := make([]*TreeNode,0)
	queue = append(queue,root.Left)
	queue = append(queue,root.Right)
	for len(queue)>=2{
		 left := queue[0]
		 right := queue[1]
		queue = queue[2:]//出栈操作
		if left==nil&&right==nil {
			continue
		}
		if left==nil||right==nil{
			return false
		}
		if left.Val != right.Val {
			return false
		}

		queue = append(queue,left.Left)
		queue = append(queue,right.Right)
		queue = append(queue,left.Right)
		queue = append(queue,right.Left)
	}
	return true
}
func main() {
	a := &TreeNode{
		Val: 1,
	}
	b := &TreeNode{
		Val: 2,
	}
	a.Left = b
	c := &TreeNode{
		Val: 2,
	}
	a.Right = c
	d := &TreeNode{
		Val: 3,
	}
	b.Left = d
	e := &TreeNode{
		Val: 4,
	}
	b.Right = e
	f := &TreeNode{
		Val: 4,
	}
	c.Left = f
	g := &TreeNode{
		Val: 3,
	}
	c.Right = g
	// 4,2,5,1,6,3
	fmt.Println(isSymmetric1(a))
}


<|start_filename|>1342number-of-steps-to-reduce-a-number-to-zero/main.go<|end_filename|>
package main

import "fmt"

func numberOfSteps (num int) int {
	var res int
	for num !=0{
		r := num%2
		if r ==0{//偶数
			num=num/2
		}else {
			num= num-1
		}
		res++
	}
	return res
}


func main() {
	fmt.Println(numberOfSteps(14))
}


<|start_filename|>572subtree-of-another-tree/main.go<|end_filename|>
package main

import (
	"fmt"
	"strings"
)

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func isSubtree(s *TreeNode, t *TreeNode) bool{
    if s==nil{
    	return false
	}
	return helper(s,t)||isSubtree(s.Left,t)||isSubtree(s.Right,t)
}

func helper(a,b *TreeNode)bool{
	if a==nil&&b==nil{
		return true
	}
	if a==nil||b==nil{
		return false
	}
	if a.Val==b.Val{
		return helper(a.Left,b.Left)&&helper(a.Right,b.Right)
	}
	return false
}

func isSubtree1(s *TreeNode, t *TreeNode) bool{
	var s1 strings.Builder
	preOrder(s,&s1,"")
	var s2 strings.Builder
	preOrder(t,&s2,"")
	fmt.Println(s1.String())
	fmt.Println(s2.String())
	return strings.Contains(s1.String(),s2.String())
}
func preOrder(root *TreeNode, res *strings.Builder,tag string){
     if root ==nil{
		 res.WriteString(tag)
		 return
	 }
	res.WriteString(fmt.Sprintf(" %d ",root.Val))
	preOrder(root.Left,res,"L")
	preOrder(root.Right,res,"R")
}

func main() {
	three := &TreeNode{Val: 3}
	four := &TreeNode{Val: 4}
	three.Left = four
	five := &TreeNode{Val: 5}
	three.Right = five
	one := &TreeNode{Val: 1}
	four.Left = one
	two := &TreeNode{Val: 2}
	four.Right = two
	//zero := &TreeNode{Val: 0}
	//two.Left = zero

	four1 := &TreeNode{Val: 4}
	one1 := &TreeNode{Val: 1}
	four1.Left = one1
	two2 := &TreeNode{Val: 2}
	four1.Right = two2


	fmt.Println(isSubtree1(&TreeNode{Val:12},&TreeNode{Val:2}))
    fmt.Println(strings.Contains("(12)","(2)"))


}


<|start_filename|>503next-greater-element-ii/main.go<|end_filename|>
package main

import "fmt"

func nextGreaterElements(nums []int) []int {
	 res :=make([]int,len(nums))
	 flag :=false
     for i:=0;i<len(nums);i++{
		 flag = false
		 for j:=1;j<len(nums);j++{
		 	 idx :=(j+i)%len(nums)
			 if nums[i]<nums[idx] {
			 	 res[i] = nums[idx]
			 	 flag = true
				break
			 }
		 }
		 if !flag {
			 res[i] = -1
		 }
	 }
	 return res
}

func main() {
	fmt.Println(nextGreaterElements([]int{1,2,1}))
	fmt.Println(nextGreaterElements([]int{5,4,3,2,1}))
}


<|start_filename|>445add-two-numbers-ii/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	l1 = revertNode(l1)
	l2 = revertNode(l2)
	dummyHead := &ListNode{Val: -1,}
	var carry int
	for l1 != nil || l2 != nil {
		var a int
		if l1 != nil {
			a = l1.Val
			l1 = l1.Next
		}
		var b int
		if l2 != nil {
			b = l2.Val
			l2 = l2.Next
		}
		sum := a + b + carry
		carry = sum / 10
		node := &ListNode{
			Val: sum % 10,
		}
		head := dummyHead.Next
		node.Next = head
		dummyHead.Next = node
	}
	if carry > 0 {
		node := &ListNode{
			Val: carry,
		}
		head := dummyHead.Next
		node.Next = head
		dummyHead.Next = node
	}
	return dummyHead.Next
}

func revertNode(l *ListNode) *ListNode {
	var pre *ListNode
	curr := l
	for curr != nil {
		next := curr.Next
		curr.Next = pre
		pre = curr
		curr = next
	}
	return pre
}

func main() {
    a1 :=  &ListNode{
		 Val:  8,
	 }
	b1 :=  &ListNode{
		Val:  0,
	}
	a1.Next = b1
	c1 :=  &ListNode{
		Val:  6,
	}
	b1.Next = c1


	a2 :=  &ListNode{
		Val:  1,
	}
	b2 :=  &ListNode{
		Val:  2,
	}
	a2.Next = b2
	c2 :=  &ListNode{
		Val:  3,
	}
	b2.Next = c2

	r := addTwoNumbers(a1,a2)
	for r != nil{
		fmt.Println(r.Val)
		r = r.Next
	}
}


<|start_filename|>32cong-shang-dao-xia-da-yin-er-cha-shu-lcof/main.go<|end_filename|>
package main

 type TreeNode struct {
	     Val int
	     Left *TreeNode
	     Right *TreeNode
	 }

func levelOrder(root *TreeNode) []int {
	var res []int
	if root==nil{
		return res
	}
	queue := []*TreeNode{root}
	for len(queue)>0{
		size :=len(queue)
		for i:=0;i<size;i++{
			node :=queue[i]
			if node.Left!=nil{
              queue = append(queue,node.Left)
			}
			if node.Right!=nil{
				queue = append(queue,node.Right)
			}
			res = append(res,node.Val)
		}
		queue = queue[size:]
	}
	return res
}

//面试题32 - I. 从上到下打印二叉树
func main() {
	
}


<|start_filename|>54er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func kthLargest(root *TreeNode, k int) int {
	stack := make([]*TreeNode, 0)
	curr := root
	var s []int
	for curr != nil || len(stack) > 0 {
		if curr != nil {
			stack = append(stack, curr)
			curr = curr.Left
		} else {
			v := stack[len(stack)-1]     // 获取栈顶元素
			stack = stack[:len(stack)-1] // 出栈
			s = append(s, v.Val)
			curr = v.Right
		}
	}
	return s[len(s)-k]
}

func kthLargest1(root *TreeNode, k int) int {
	stack := make([]int, 0)
	inorder(root, &stack)
	return stack[k-1]
}
func inorder(root *TreeNode, a *[]int) {
	if root == nil {
		return
	}
	inorder(root.Right, a)
	*a = append(*a, root.Val)
	inorder(root.Left, a)
}

// 面试题54. 二叉搜索树的第k大节点 https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/
func main() {
	a := &TreeNode{Val: 3}
	b := &TreeNode{Val: 1}
	c := &TreeNode{Val: 4}
	d := &TreeNode{Val: 2}
	a.Left = b
	a.Right = c
	b.Right = d
	fmt.Println(kthLargest1(a, 1))
}


<|start_filename|>86partition-list/main.go<|end_filename|>
package main

type ListNode struct {
	Val  int
	Next *ListNode
}

func partition(head *ListNode, x int) *ListNode {
	greatDummy := &ListNode{}
	lessDummy := &ListNode{}
	currGreat := greatDummy
	currLess := lessDummy
	for head != nil {
		if head.Val < x {
			currLess.Next = head
			currLess = head
		} else {
			currGreat.Next = head
			currGreat = head
		}
		head = head.Next
	}
	currGreat.Next = nil
	currLess.Next = greatDummy.Next
	return lessDummy.Next
}

func main() {

}


<|start_filename|>51shu-zu-zhong-de-ni-xu-dui-lcof/main.go<|end_filename|>
package main

import "fmt"
// leetcode 会超时
func reversePairs(nums []int) int {
	num :=0
    for i:=0;i<len(nums);i++{
    	for j:=i+1;j<len(nums);j++{
			if nums[i]>nums[j] {
				num++
			}
		}
	}
	return num
}
func main() {
	fmt.Println(reversePairs([]int{7,5,6,4}))
}


<|start_filename|>125valid-palindrome/main.go<|end_filename|>
package main

import (
	"fmt"
	"unicode"
)

func isValid(i rune)bool{
	return unicode.IsDigit(i)||unicode.IsLetter(i)
}

func isPalindrome1(s string) bool {
	if len(s)<2{
		return true
	}
	l :=0
	r :=len(s)-1
	for l<r{
		vl, vr := rune(s[l]), rune(s[r])
		if !isValid(vl)&&!isValid(vr){
			l++
			r--
		}else if !isValid(vl){
			l++
		}else if !isValid(vr){
			r--
		}else if unicode.ToUpper(vl) != unicode.ToUpper(vr){
			return false
		}else{
			l++
			r--
		}
	}
	return true
}
func isPalindrome(s string) bool {
	// 判断是否是需要比较的合法回文字符,
	// 题目说空格是合法的回文字符很误导人, 其实这个地方需要排除空格
	isValid := func(v rune) bool {
		return unicode.IsDigit(v) || unicode.IsLetter(v)
	}
	i, j := 0, len(s)-1
	for i < j {
         for i<len(s)&&!isValid(rune(s[i])){
         	i++
		 }
		for j>=0&&!isValid(rune(s[j])){
			j--
		}
	     if i<len(s)&&j>0&&(unicode.ToUpper(rune(s[i])) != unicode.ToUpper(rune(s[j]))) {
			return false // 如果都合法如果不相等则直接返回
		} else { // 如果相等则移动两个指针
			i++
			j--
		}
	}
	return true
}

func main() {
	//fmt.Println(isPalindrome("A man, a plan, a canal: Panama"))
	//fmt.Println(isPalindrome("race a car"))
	fmt.Println(isPalindrome("a."))
	fmt.Println(isPalindrome(".,"))
}


<|start_filename|>119pascals-triangle-ii/main.go<|end_filename|>
package main

import "fmt"

func getRow(rowIndex int) []int {
	res :=[]int{1}
	for i:=1;i<=rowIndex;i++{
		res =append([]int{0},res...)
		for j:=0;j<i;j++{
			res[j]= res[j]+res[j+1]
		}
	}
	return res
}

func main() {
	fmt.Println(getRow(0))
}


<|start_filename|>1389create-target-array-in-the-given-order/main.go<|end_filename|>
package main

import "fmt"

func createTargetArray(nums []int, index []int) []int {
	res := make([]int, len(index))
	sortedNum := 0 //当前已经排列总数
	for i := 0; i < len(nums); i++ {
		v := index[i]
		if i == v {
			res[i] = nums[i]
		} else {
			for n := sortedNum; n > v; n-- { //需要把数字往后移动
				res[n] = res[n-1]
			}
			res[v] = nums[i]
		}
		sortedNum++
	}
	return res
}

func main() {
	fmt.Println(createTargetArray([]int{0, 1, 2, 3, 4}, []int{0, 1, 2, 2, 1}))
	fmt.Println(createTargetArray([]int{1, 2, 3, 4, 0}, []int{0, 1, 2, 3, 0}))
	fmt.Println(createTargetArray([]int{1}, []int{0}))
}


<|start_filename|>118pascals-triangle/main.go<|end_filename|>
package main

import "fmt"

func generate(numRows int) [][]int {
	 var res [][]int
	 if numRows==0{
	 	return res
	 }
	 res = append(res,[]int{1})
     for i:=1;i<numRows;i++{
     	m :=[]int{0}
     	m =append(m,res[i-1]...)
     	for j:=0;j<len(m)-1;j++{
     		m[j]=m[j]+m[j+1]
		}
		res = append(res,m)
	 }
	 return res
}

func main() {
	fmt.Println(generate(5))
}


<|start_filename|>501find-mode-in-binary-search-tree/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	     Val int
	     Left *TreeNode
	    Right *TreeNode
}

func findMode(root *TreeNode) []int {
	stack := make([]*TreeNode, 0)
	var max int
	m := make(map[int]int)
	var res []int
	for root != nil || len(stack) > 0 {
		for root != nil {
			stack = append(stack, root)
			root = root.Left
		}
		node := stack[len(stack)-1]
		stack = stack[:len(stack)-1]
		m[node.Val]++
		if max < m[node.Val] {
			max = m[node.Val]
			res = nil
			res = append(res,node.Val)
		}else if max == m[node.Val]{
			res = append(res,node.Val)
		}
		root = node.Right
	}
	return res
}


func main() {
	one := &TreeNode{Val:1}
	two := &TreeNode{Val:2}
	one.Right = two
	three :=&TreeNode{Val:2}
	two.Left = three
	fmt.Println(findMode(one))
}


<|start_filename|>148sort-list/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

type ListNode struct {
	Val int
	Next *ListNode
}

//归并排序链表
func sortList(head *ListNode) *ListNode {
	var res []*ListNode
	for head !=nil{
		res = append(res,head)
		head = head.Next
	}
	sort.Slice(res, func(i, j int) bool {
		 return res[i].Val<res[j].Val
	})
    dummyHead :=&ListNode{}
    preNode := dummyHead
	for i:=0;i<len(res);i++{
		preNode.Next = res[i]
		preNode = res[i]
	}
    preNode.Next = nil
	return dummyHead.Next
}


func main() {
	four :=&ListNode{Val:4}
	two :=&ListNode{Val:2}
	four.Next = two
	one :=&ListNode{Val:1}
	two.Next = one
	three :=&ListNode{Val:3}
	one.Next = three

	r := sortList(four)
	for r!=nil{
		fmt.Println(r.Val)
		r = r.Next
	}


}


<|start_filename|>160intersection-of-two-linked-lists/main.go<|end_filename|>
package main

import "unsafe"

type ListNode struct {
	     Val int
	     Next *ListNode
 }

func getIntersectionNode(headA, headB *ListNode) *ListNode {
	m :=make(map[uintptr]struct{})
	for headA!=nil||headB!=nil{
		if headA!=nil {
			if _,ok:=m[uintptr(unsafe.Pointer(headA))];ok{
				return  headA
			}else {
				m[uintptr(unsafe.Pointer(headA))] = struct{}{}
			}
			headA = headA.Next
		}
		if headB!=nil {
			if _,ok:=m[uintptr(unsafe.Pointer(headB))];ok{
				return  headB
			}else {
				m[uintptr(unsafe.Pointer(headB))] = struct{}{}
			}
			headB= headB.Next
		}
	}
	return nil
}

func main() {
	
}


<|start_filename|>226invert-binary-tree/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	     Val int
	     Left *TreeNode
	     Right *TreeNode
}


func invertTree3(root *TreeNode) *TreeNode {
	if root==nil {// 递归结束条件
		return nil
	}
	right:= invertTree3(root.Right)// 返回已经翻转的右节点
	left := invertTree3(root.Left) // 返回已经翻转的左节点
	// 当前需要一级需要做的是把 左节点指向已经翻转的右节点 右节点指向已经翻转的左节点
	root.Left = right
	root.Right = left
	return root
}
func invertTree1(root *TreeNode) *TreeNode {
	if root==nil {// 递归结束条件
		return nil
	}

	// 当前需要一级需要做的是把 左节点指向右节点 右节点指向已经翻转的左节点
	temp := root.Left
	root.Left = root.Right
	root.Right = temp

	invertTree1(root.Left) // 翻转的左节点
	invertTree1(root.Right)// 返翻转的右节点

	return root
}


func invertTree(root *TreeNode) *TreeNode {
       if root==nil{
       	  return root
	   }
	   queue :=[]*TreeNode{root}
	   for len(queue)>0{
	   	  size  :=len(queue)
	   	  for i:=0;i<size;i++{
	   	  	  node := queue[i]
	   	  	  node.Left,node.Right = node.Right,node.Left
	   	  	  if node.Left!=nil{
				  queue = append(queue,node.Left)
			  }
			  if node.Right!=nil{
				  queue = append(queue,node.Right)
			  }
		  }
		  queue =queue[size:]
	   }
	   return root
}


func main() {
		a := &TreeNode{
			Val: 4,
		}
		b := &TreeNode{
			Val: 2,
		}
		a.Left = b
		c := &TreeNode{
			Val: 7,
		}
		a.Right = c
		d := &TreeNode{
			Val: 1,
		}
		b.Left = d
		e := &TreeNode{
			Val: 3,
		}
		b.Right = e
		//f := &TreeNode{
		//	Val: 6,
		//}
	    //c.Left = f
	    g := &TreeNode{
		Val: 9,
	    }
		c.Right = g
		// 9 7 64 3 2 1
	    r :=invertTree(a)
		printOrder(r)
}

func printOrder(root *TreeNode){
	if root== nil {
		return
	}
	fmt.Println(root.Val)
	printOrder(root.Left)
	printOrder(root.Right)
	return
}

<|start_filename|>832flipping-an-image/main.go<|end_filename|>
package main

import "fmt"

func flipAndInvertImage(A [][]int) [][]int {
    for i:=0;i<len(A);i++{
    	l :=0
    	r :=len(A[i])-1
    	for l<=r{
    		if l<r{
				A[i][l],A[i][r]= A[i][r],A[i][l]
				A[i][l]=A[i][l]^1
				A[i][r]=A[i][r]^1
			}else {
				A[i][l]=A[i][l]^1
			}
    		l++
    		r--
		}
	}
	return A
}

func main() {
	//
	fmt.Println(flipAndInvertImage([][]int{{1,1,0},{1,0,1},{0,0,0}}))
}


<|start_filename|>371sum-of-two-integers/main.go<|end_filename|>
package main

func getSum(a int, b int) int {
	for b != 0 {
		sum := a ^ b
		carry := (a & b) << 1
		a = sum
		b = carry
	}
	return a
}


func main() {
	
}


<|start_filename|>4median-of-two-sorted-arrays/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func findMedianSortedArrays1(nums1 []int, nums2 []int) float64 {
	nums1 = append(nums1, nums2...)
	sort.Ints(nums1)
	mid := len(nums1) / 2
	if len(nums1)%2 != 0 {
		return float64(nums1[mid])
	}
	return float64(nums1[mid]+nums1[mid-1]) / 2.0
}

func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
	nums1 = append(nums1, nums2...)
	sort.Ints(nums1)
	mid := len(nums1) / 2
	if len(nums1)%2 != 0 {
		return float64(nums1[mid])
	}
	return float64(nums1[mid]+nums1[mid-1]) / 2.0
}




func main() {
	//fmt.Println(findMedianSortedArrays([]int{1,3},[]int{2}))
	fmt.Println(findMedianSortedArrays([]int{1, 2}, []int{3, 4}))
}


<|start_filename|>230kth-smallest-element-in-a-bst/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	Val int
	Left *TreeNode
	Right *TreeNode
}


// 中序遍历
func kthSmallest(root *TreeNode, k int) int {
     if root==nil{
     	return 0
	 }
	 stack := make([]*TreeNode,0)
	 i :=0
	 for root!=nil||len(stack)>0{
		 if root!= nil {
			stack = append(stack,root)
			root = root.Left
		 }else {
		 	curr :=stack[len(stack)-1]
		 	stack = stack[:len(stack)-1]
		 	// 数据出站
			 if i==k-1  {
				 return curr.Val
			 }
			 i++
		 	root = curr.Right
		 }
	 }
	 return 0
}


func kthSmallest1(root *TreeNode, k int) int{
	//index = k
	dfs(root,k)
	return res

}
var res int
var index int
func dfs(root *TreeNode,k int){
	if root==nil {
		return
	}
	dfs(root.Left,k)
	index++
	if index==k {
		res = root.Val
		return
	}
	dfs(root.Right,k)
}

func main() {
	a := &TreeNode{
		Val: 3,
	}
	b := &TreeNode{
		Val: 1,
	}
	a.Left = b
	c := &TreeNode{
		Val: 4,
	}
	a.Right = c
	d := &TreeNode{
		Val: 2,
	}
	b.Right = d
	fmt.Println(kthSmallest1(a,1))
}


<|start_filename|>220contains-duplicate-iii/main.go<|end_filename|>
package main

import "fmt"

func Abs(a, b int) int {
	r := a - b
	if r < 0 {
		return -r
	}
	return r
}

func containsNearbyAlmostDuplicate(nums []int, k int, t int) bool {
	for i := 0; i < len(nums); i++ {
		for j := i + 1; j < len(nums); j++ {
			if Abs(nums[j], nums[i]) <= t && Abs(j, i) <= k {
				return true
			}
		}
	}
	return false
}
func main() {
	fmt.Println(containsNearbyAlmostDuplicate([]int{1, 2, 3, 1}, 3, 0))
	fmt.Println(containsNearbyAlmostDuplicate([]int{1, 0, 1, 1}, 1, 2))
	fmt.Println(containsNearbyAlmostDuplicate([]int{1, 5, 9, 1, 5, 9}, 2, 3))

}


<|start_filename|>20valid-parentheses/main.go<|end_filename|>
package main

import "fmt"

func isValid(s string) bool {
    m :=map[string]string{")":"(","}":"{","]":"["}
    stack := make([]string,0)
    for i :=range s{
		x := string(s[i])
		if x=="("||x=="{"||x=="[" {
			stack = append(stack,x)
		}else if x==")" || x=="}" || x=="]" {
			if len(stack)>0&&stack[len(stack)-1]==m[x] {
				stack = stack[:len(stack)-1]
			}else {
				return false
			}

		}
	}
	return len(stack)==0
}

func main() {
	//fmt.Println(isValid("()"))
	//fmt.Println(isValid("([)]"))
	fmt.Println(isValid("()[]{}"))
	fmt.Println(isValid("]"))
}


<|start_filename|>1103distribute-candies-to-people/main.go<|end_filename|>
package main

import "fmt"

func distributeCandies(candies int, num_people int) []int {
	ans :=make([]int,num_people)
	count :=0
	index :=0
	for candies>0{
		count++
		i := index%num_people
		if candies>=count{
			ans[i] +=count
		}else {
			ans[i] +=candies
		}
		index++
		candies-=count
	}
	return ans
}


func main() {
	fmt.Println(distributeCandies(7,4))
	fmt.Println(distributeCandies(10,3))
}


<|start_filename|>110balanced-binary-tree/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
)

type TreeNode struct {
	     Val int
	     Left *TreeNode
	     Right *TreeNode
	 }

func isBalanced(root *TreeNode) bool {
	if root ==nil {
		return true
	}
	l := dfs(root.Left)
	r := dfs(root.Right)
	return math.Abs(float64(l)-float64(r)) <=1&&isBalanced(root.Right)&&isBalanced(root.Left)
}
func dfs(root *TreeNode)int{
	if root ==nil {
		return 0
	}
	return max(dfs(root.Left),dfs(root.Right))+1
}
func max(a ,b int)int{
	if a>b {
		return a
	}
	return b
}

func main() {
	a := &TreeNode{
		Val: 3,
	}
	b := &TreeNode{
		Val: 9,
	}
	a.Left = b
	c := &TreeNode{
		Val: 20,
	}
	a.Right = c
	d := &TreeNode{
		Val: 15,
	}
	c.Left = d
	e := &TreeNode{
		Val: 7,
	}
	c.Right = e

	fmt.Println(isBalanced(a))
	// 4,2,5,1,6,3


	a1 := &TreeNode{
		Val: 1,
	}
	b1 := &TreeNode{
		Val: 2,
	}
	a1.Left = b1
	c1 := &TreeNode{
		Val: 2,
	}
	a1.Right = c1
	d1 := &TreeNode{
		Val: 3,
	}
	c1.Left = d1
	e1 := &TreeNode{
		Val: 3,
	}
	c1.Right = e1
	f1 := &TreeNode{
		Val: 4,
	}
	e1.Left = f1
	g1 := &TreeNode{
		Val: 4,
	}
	e1.Right = g1
	fmt.Println(isBalanced(a1))
}


<|start_filename|>138copy-list-with-random-pointer/main.go<|end_filename|>
package main

import "unsafe"

type Node struct {
	Val    int
	Next   *Node
	Random *Node
}


func copyRandomList(head *Node) *Node {
    curr := head
    m := make(map[uintptr]*Node)
    for curr!=nil{
    	node :=&Node{Val:curr.Val}
    	m[uintptr(unsafe.Pointer(curr))]=node
    	curr = curr.Next
	}
	curr = head
	for curr!=nil{
		node := m[uintptr(unsafe.Pointer(curr))]
		node.Next = m[uintptr(unsafe.Pointer(curr.Next))]
		node.Random	=m[uintptr(unsafe.Pointer(curr.Random))]
		curr = curr.Next
	}
	return m[uintptr(unsafe.Pointer(head))]
}


func main() {
	
}


<|start_filename|>0utils/tree/binarytree.go<|end_filename|>
package tree

import (
	"strconv"
	"strings"
)
type TreeNode struct {
	Val int
	Left *TreeNode
	Right *TreeNode
}
func BuildBinaryTree(src string)*TreeNode{
	src =src[1:len(src)-1]
	strList := strings.Split(src, ",")
	var currNode *TreeNode
	var root *TreeNode
	var queue []*TreeNode
	for i :=0;i<len(strList);i+=2{
		if i==0 {
			v,_:=strconv.Atoi(strList[i])
			currNode =&TreeNode{Val:v}
			root =currNode
			queue = append(queue,currNode)
		}
		if len(queue)>0 {
			currNode =queue[0]
			queue =queue[1:]
		}else {
			break
		}
		if i+1<len(strList)&&strList[i+1]!="null"{
			v,_:=strconv.Atoi(strList[i+1])
			currNode.Left = &TreeNode{Val:v}
			queue = append(queue,currNode.Left)
		}
		if i+2<len(strList)&&strList[i+2]!="null"{
			v,_:=strconv.Atoi(strList[i+2])
			currNode.Right = &TreeNode{Val:v}
			queue = append(queue,currNode.Right)
		}
	}
	return root
}

<|start_filename|>23merge-k-sorted-lists/main.go<|end_filename|>
package main

import (
	"container/heap"
	"fmt"
	"sort"
)

type ListNode struct {
	Val  int
	Next *ListNode
}
type NodeSlice []*ListNode

func (p NodeSlice) Len() int           { return len(p) }
func (p NodeSlice) Less(i, j int) bool { return p[i].Val < p[j].Val }
func (p NodeSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

func (p *NodeSlice) Push(x interface{}) {
	node := x.(*ListNode)
	*p = append(*p, node)
}
func (p *NodeSlice) Pop() interface{} {
	old := *p
	n := len(old)
	item := old[n-1]
	*p = old[0 : n-1]
	return item
}
func mergeKLists(lists []*ListNode) *ListNode {
	dummyHead := &ListNode{
		Val:  -1,
		Next: nil,
	}
	curr := dummyHead
	nodeList := make(NodeSlice, 0)
	for i := range lists {
		if lists[i] != nil {
			nodeList = append(nodeList, lists[i])
		}
	}
	heap.Init(&nodeList)
	for len(nodeList) > 0 {
		item := heap.Pop(&nodeList).(*ListNode)// 最小元素
		next := item.Next
		item.Next = nil
		curr.Next = item
		curr = item
		if next != nil {
			heap.Push(&nodeList, next)
		}
	}
	return dummyHead.Next
}

func mergeKLists1(lists []*ListNode) *ListNode {
	dummyHead := &ListNode{
		Val:  -1,
		Next: nil,
	}
	nodeList := make(NodeSlice, 0)
	for i := range lists {
		h :=lists[i]
		for h!= nil {
			nodeList = append(nodeList, h)
			h=h.Next
		}
	}
	sort.Sort(nodeList)
	curr := dummyHead
	for _,v :=range nodeList{
		curr.Next = v
		curr = v
	}
	return dummyHead.Next
}



func main() {
    a1 :=ListNode{Val:  1,}
	b1 :=&ListNode{Val: 4,}
	a1.Next = b1
	c1 :=ListNode{Val:  5,}
	b1.Next =&c1
	a2 :=ListNode{Val:  1,}
	b2 :=ListNode{Val:  3,}
	a2.Next = &b2
	c2 :=ListNode{Val:  4,}
	b2.Next =&c2
	a3 :=ListNode{Val:  2,}
	b3 :=ListNode{Val:  6,}
	a3.Next =&b3
	r := mergeKLists([]*ListNode{&a1,&a2,&a3,nil})
	for r!= nil{
		fmt.Println(r.Val)
		r = r.Next
	}
}


<|start_filename|>509fibonacci-number/main.go<|end_filename|>
package main

import "fmt"

func fib1(N int) int {
    if N<2{
    	return N
	}
	return fib1(N-1)+fib1(N-2)
}

func fib2(N int) int {
	if N<2{
		return N
	}
	var v1 =0
	var v2 =1
	for i:=2;i<=N;i++{
	 	tmp := v1+v2
		v1 = v2
		v2 = tmp
	}
	return v2
}
func fib3(N int) int {
	if N<2{
		return N
	}
	dp := make([]int,N+1)
	dp[0] = 0
	dp[1]= 1
	for i:=2;i<=N;i++{
		dp[i] = dp[i-1]+dp[i-2]
	}
	return dp[N]
}
func fib(N int) int {
	if N<2{
		return N
	}
	pre := 0
	curr := 1
	for i:=2;i<=N;i++{
		sum :=pre+curr
		pre = curr
		curr = sum
	}
	return curr
}
func main() {
	fmt.Println(fib(5))
}


<|start_filename|>215kth-largest-element-in-an-array/main.go<|end_filename|>
package main

import (
	"container/heap"
	"fmt"
)

type TopList []int

func (t TopList)Len()int{
	return len(t)
}
func (t TopList)Swap(i,j int){
	  t[i],t[j] = t[j],t[i]
}
func (t TopList)Less(i,j int)bool{
	return  t[i]<t[j]
}
func (t *TopList)Push(x interface{}){
	*t = append(*t,x.(int))
}
func (t *TopList)Pop()interface{}{
	x := (*t)[len(*t)-1]
	*t= (*t)[:len(*t)-1]
	return x
}


func findKthLargest(nums []int, k int) int {
	m :=make(TopList,0)
	size :=0
	for i:=range nums{
		if size<k{
			heap.Push(&m,nums[i])
			size++
		}else {
			if nums[i]>m[0]{//小顶堆 堆顶元素小于当前元素
				heap.Pop(&m)
				heap.Push(&m,nums[i])
			}
		}
	}
	return m[0]
}

func main() {
	fmt.Println(findKthLargest([]int{3,2,1,5,6,4},2))
	fmt.Println(findKthLargest([]int{3,2,3,1,2,4,5,5,6},4))
}


<|start_filename|>206reverse-linked-list/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
	 Val int
	 Next *ListNode
 }


func reverseList1(head *ListNode) *ListNode {
	if head == nil||head.Next==nil{
		return head
	}
	var pre *ListNode
	cur := head
	for cur != nil{
		next := cur.Next
		cur.Next = pre
		pre = cur
		cur = next
	}
	return pre
}

func reverseList2(head *ListNode) *ListNode {
	if head == nil||head.Next==nil{
		return head
	}
	dummyNode := &ListNode{}// 哑结点
	cur := head
	for cur != nil{
		next := cur.Next // 为了遍历，临时变量存储当前节点的下一个节点
		head :=  dummyNode.Next // 临时变量存储当前哑结点的指向的头结点
		dummyNode.Next = cur // 往头部插入数据,当前节点变成了头结点了
		cur.Next = head // 为了维持链条 需要把之前的头结点变成下一个节点
		cur = next // 遍历移动
	}
	return dummyNode.Next // 返回头结点
}


func reverseList(head *ListNode) *ListNode {
	var pre *ListNode
	curr := head
	for curr!= nil{//当为空表示迭代结束
		//前一个值 ，当前值，后一个值
		pre,curr,curr.Next = curr,curr.Next,pre
	}
	return pre
}

func makeLinkedList(list []int)*ListNode{
	dummyNode := &ListNode{} // 创建哑结点 指向头节点
	curr := dummyNode
	for i:=0;i<len(list);i++{
		curr.Next= &ListNode{Val:list[i]}
		curr = curr.Next
	}
	return dummyNode.Next
}



func main() {
	head := makeLinkedList([]int{1,2,3,4,5})
	head = reverseList(head)
	for head!=nil{
		fmt.Println(head.Val)
		head = head.Next
	}
}


<|start_filename|>2add-two-numbers/main.go<|end_filename|>
package main

import (
	"fmt"
	"strings"
)

type ListNode struct {
	Val int
	Next *ListNode
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	dummy := &ListNode{}
	p := l1
	q := l2
	currentHead := dummy
	var sum,carry int // carry 进位值
	for true  {
		if q != nil || p != nil {
			var x,y int
			if p!=nil {
				x=p.Val
				p = p.Next
			}
			if q!=nil {
				y =q.Val
				q = q.Next
			}

			sum = carry + x + y
			carry = sum / 10
		} else {
			break
		}
		currentHead.Val = sum % 10
		if p!=nil||q!=nil {
			currentHead.Next = &ListNode{}
			currentHead = currentHead.Next
		}

	}
	if carry != 0 {
		currentHead.Next = &ListNode{carry, nil}
	}
	return dummy
}
func addTwoNumbers1(l1 *ListNode, l2 *ListNode) *ListNode {
	dummyHead := &ListNode{
		Val:  0,
		Next: nil,
	}
	p :=l1
	q := l2
	curr := dummyHead
	carry :=0
	for p!=nil||q!=nil{
		var x,y int
		if p!=nil {
			x = p.Val
			p = p.Next
		}
		if q!= nil {
			y = q.Val
			q = q.Next
		}
		sum := carry+x+y
		carry = sum/10
		curr.Next =&ListNode{
			Val:  sum%10,
			Next: nil,
		}
		curr = curr.Next
	}
	if carry>0{
		curr.Next = &ListNode{
			Val:  carry,
			Next: nil,
		}
	}
	return dummyHead.Next
}
func main() {
	l1:= &ListNode{Val:2,Next:&ListNode{Val:4,Next:&ListNode{Val:3}}}
	l2:= &ListNode{Val:5,Next:&ListNode{Val:6,Next:&ListNode{Val:4}}}
	r := addTwoNumbers1(l1,l2)
	curr := r
	var str strings.Builder
	for curr!=nil{
		str.WriteString(fmt.Sprintf("%d -> ",curr.Val))
		curr = curr.Next
	}
	fmt.Println(strings.TrimRight(str.String()," -> "))
	ll1:= &ListNode{Val:0,Next:nil}
	ll2:= &ListNode{Val:0,Next:nil}
	r = addTwoNumbers1(ll1,ll2)
	curr = r
	var str1 strings.Builder
	for curr!=nil{
		str1.WriteString(fmt.Sprintf("%d -> ",curr.Val))
		curr = curr.Next
	}
	fmt.Println(strings.TrimRight(str1.String()," -> "))
}



<|start_filename|>141linked-list-cycle/main.go<|end_filename|>
package main

import (
	"fmt"
	"unsafe"
)

type ListNode struct {
 	Val int
 	Next *ListNode
}
func hasCycle(head *ListNode) bool {
	if head==nil||head.Next==nil {
		return false
	}
    slow :=head
    fast := head.Next
    for fast!= nil&&fast.Next!=nil{
		if slow==fast {
			return true
		}
		slow = slow.Next// 走一步
		fast = fast.Next.Next //走2部
	}
	return false
}

func hasCycle1(head *ListNode) bool {
	if head==nil||head.Next==nil {
		return false
	}
	for head!=nil{
		if head.Val==1>>63 {
			return true
		}
		head.Val = 1>>63
		head = head.Next
	}
	return false
}
func hasCycle2(head *ListNode) bool {
	if head==nil||head.Next==nil {
		return false
	}
	m :=make(map[uintptr]struct{})
	for head!=nil{
		if _,ok:=m[uintptr(unsafe.Pointer(head))];ok {
			return true
		}
		m[uintptr(unsafe.Pointer(head))] = struct{}{}
		head = head.Next
	}
	return false
}

func main() {
	aa := ListNode{Val:  1}
	//b := ListNode{Val:  2}
	//c := ListNode{Val:  0}
	//d := ListNode{Val:  -4}
	//a.Next = &b
	//b.Next = &c
	//c.Next = &d
	//d.Next = &b
	fmt.Println(hasCycle2(&aa))
	a := ListNode{Val:  1}
	b := ListNode{Val:  2}
	c := ListNode{Val:  0}
	d := ListNode{Val:  -4}
	a.Next = &b
	b.Next = &c
	c.Next = &d
	d.Next = &b
	fmt.Println(hasCycle2(&a))
}


<|start_filename|>46permutations/main.go<|end_filename|>
package main

import "fmt"

var mark []int
var n int
func permute(nums []int) [][]int {
    n =len(nums)
    res:= make([][]int,0)
    if n<=0{
    	return res
	}
	mark = make([]int,n)
	path :=make([]int,0)
	dfs(nums,path,&res)
	return res
}

func dfs(nums[]int,path[]int,res*[][]int)  {
	if len(path)==n {
		tmp := make([]int,n)
		copy(tmp,path)
		*res =append(*res,tmp)
		return
	}
	for i :=0;i<n;i++{
		if mark[i]==1 {//减掉枝叶
			continue
		}
		path = append(path,nums[i])
		mark[i]=1
		dfs(nums,path,res)
		path = path[:len(path)-1]
		mark[i]=0
	}
}

func main() {
	fmt.Println(permute([]int{1,2}))
}


<|start_filename|>673number-of-longest-increasing-subsequence/main.go<|end_filename|>
package main

import (
	"fmt"
)

func findNumberOfLIS(nums []int) int {
     dp := make([]int,len(nums))
	 count := make([]int,len(nums))
	maxLength := 1
     for i :=0;i<len(nums);i++{
     	dp[i] = 1
     	count[i] = 1
     	for j:=0;j<i;j++{
			if nums[j]<nums[i] {
				if dp[j]+1>dp[i] {
					dp[i] = dp[j]+1
					count[i] = count[j]
				}else if dp[j]+1==dp[i] {
					count[i] += count[j]
				}
				
			}
		}
		 if maxLength < dp[i]{
			 maxLength = dp[i]
		 }
	 }
	var maxNum int
	for i := 0; i <= len(dp) - 1; i += 1{
		if dp[i] == maxLength{
			maxNum += count[i]
		}
	}
	return maxNum
}

func main() {
	fmt.Println(findNumberOfLIS([]int{1,3,5,4,7}))
	fmt.Println(findNumberOfLIS([]int{2,2,2,2,2}))
}


<|start_filename|>155min-stack/main.go<|end_filename|>
package main

import "fmt"

type MinStack struct {
    s []Node
}
type Node struct {
	d int //data
	m int //current min
}


/** initialize your data structure here. */
func Constructor() MinStack {
   return MinStack{}
}


func (this *MinStack) Push(x int)  {
	d := Node{d:x,m:x}
	if len(this.s)>0 &&this.s[len(this.s)-1].m< x{
		d.m=this.s[len(this.s)-1].m
	}
     this.s = append(this.s,d)

}


func (this *MinStack) Pop()  {
	this.s = this.s[:len(this.s)-1]
}


func (this *MinStack) Top() int {
	return this.s[len(this.s)-1].d
}


func (this *MinStack) GetMin() int {
	return this.s[len(this.s)-1].m
}

func main() {
	m := MinStack{}
	m.Push(-10)
	m.Push(14)
	fmt.Println(m.GetMin())
	fmt.Println(m.GetMin())
	m.Push(-20)
	fmt.Println(m.GetMin())
	fmt.Println(m.GetMin())
	fmt.Println(m.Top())
	fmt.Println(m.GetMin())
	m.Pop()
	m.Push(10)
	m.Push(-7)
	fmt.Println(m.GetMin())
}


<|start_filename|>198house-robber/main.go<|end_filename|>
package main

import "fmt"

func max(a,b int)int{
	if a>b {
		return a
	}
	return b
}

func rob(nums []int) int {
	if len(nums)==0 {
		return 0
	}
	if len(nums)==1 {
		return nums[0]
	}
     dp := make([]int,len(nums))
     dp[0] = nums[0]
     dp[1] = max(nums[0],nums[1])
     maxVal := dp[1]
     for i:=2;i<len(nums);i++{
     	dp[i] = max(dp[i-1],dp[i-2]+nums[i])
     	maxVal = max(maxVal,dp[i])
	 }
	 return maxVal
}

func main() {
	fmt.Println(rob([]int{1,2,3,1}))
	fmt.Println(rob([]int{2,7,9,3,1}))
}


<|start_filename|>11xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/main.go<|end_filename|>
package main

import "fmt"

func minArray(numbers []int) int {
    left :=0
    right :=len(numbers)-1
    for left<right{
    	mid :=left+(right-left)>>1
    	if numbers[mid]>numbers[right]{
    		left = mid+1
		}else if numbers[mid]<=numbers[right] {
			right = right-1
		}
	}
	return numbers[left]
}

func main() {
	fmt.Println(minArray([]int{5,1,2,3,4}))//1
	fmt.Println(minArray([]int{3,4,5,6,7,1,2}))//1
	fmt.Println(minArray([]int{2,3,4,5,6,1}))//1
	fmt.Println(minArray([]int{1,3,5}))
}


<|start_filename|>2.06palindrome-linked-list-lcci/main.go<|end_filename|>
package main

import (
	"fmt"
)

type ListNode struct {
	Val  int
	Next *ListNode
}

func isPalindrome1(head *ListNode) bool {
	var str string
	var revertStr string
	for head != nil {
		str = fmt.Sprintf("%s%d", str, head.Val)
		revertStr = fmt.Sprintf("%d%s", head.Val, revertStr)
		head = head.Next
	}
	return str == revertStr
}

func isPalindrome(head *ListNode) bool {
	if head == nil {
		return true
	}
	// 查找中间节点
	midNode := findMidNode(head)
	// 翻转以中间节点的链表
	currTail := revertLinkedNode(midNode)
	// 判断头节点和尾节点值是否一直
	for head != nil && currTail != nil {
		if head.Val != currTail.Val {
			return false
		}
		head = head.Next
		currTail = currTail.Next
	}
	return true
}

func isPalindrome2(head *ListNode) bool {
	if head == nil {
		return true
	}
	var res []*ListNode
	for head != nil {
		res = append(res, head)
		head = head.Next
	}
	left := 0
	right := len(res) - 1
	for left < right {
		if res[left].Val != res[right].Val {
			return false
		}
		left++
		right--
	}
	return true
}

func findMidNode(head *ListNode) *ListNode {

	slow := head
	fast := head.Next
	for fast != nil && fast.Next != nil {
		slow = slow.Next
		fast = fast.Next.Next
	}
	return slow
}

func revertLinkedNode(head *ListNode) *ListNode {
	dummyHead := &ListNode{}
	for head != nil {
		next := head.Next
		proNode := dummyHead.Next
		dummyHead.Next = head
		head.Next = proNode
		head = next
	}
	return dummyHead.Next
}

func revertLinkedNode1(head *ListNode) *ListNode {
	if head == nil || head.Next == nil {
		return head
	}
	next := head.Next
	node := revertLinkedNode1(next)
	next.Next = head
	head.Next = nil
	return node
}

func main() {
	t1 := ListNode{Val: 1}
	t2 := ListNode{Val: 2}
	t1.Next = &t2
	t3 := ListNode{Val: 1}
	t2.Next = &t3
	t4 := ListNode{Val: 4}
	t3.Next = &t4
	t5 := ListNode{Val: 5}
	t4.Next = &t5

	fmt.Println(isPalindrome(&t1))

}


<|start_filename|>1160find-words-that-can-be-formed-by-characters/main.go<|end_filename|>
package main

import "fmt"

func countCharacters(words []string, chars string) int {
	var res int
	m := make(map[byte]int)
	for i :=range chars{
		m[chars[i]]++
	}
	for _,w:=range words{
		if isContainer(w,m) {
			res +=len(w)
		}
	}
	return res
}

func isContainer(w string,m map[byte]int)bool{
	charMap :=make(map[byte]int)
	for i:=range w{
		charMap[w[i]]++
	}

	for k,v :=range charMap{
		if m[k]<v{// 词汇表字母数量小于了当前单词数量 肯定是失败的
			return false
		}
	}
	return true
}

func isContainer1(w string,m map[byte]int)bool{
      charMap :=make(map[byte]int)
      for k,v:=range m{
      	charMap[k]=v
	  }
	for i :=range w{
		if charMap[w[i]]<1{
			return false
		}else {
			charMap[w[i]]--
		}
	}
	return true
}
func main() {
	fmt.Println(countCharacters([]string{"cat","bt","hat","tree"},"atach"))

	fmt.Println(countCharacters([]string{"hello","world","leetcode"},"welldonehoneyr"))

}


<|start_filename|>16.25lru-cache-lcci/main.go<|end_filename|>
package main

import (
	"container/list"
	"fmt"
)

type Node struct {
	key int
	value int
}

type LRUCache struct {
    m map[int] *list.Element
    l  *list.List
	capacity  int
}


func Constructor(capacity int) LRUCache {
    return LRUCache{
		m:        make(map[int]*list.Element),
		l:        list.New(),
		capacity: capacity,
	}
}

func (this *LRUCache) Get(key int) int {
	if v,ok:=this.m[key];ok {
		this.l.MoveToFront(v)
		r := v.Value.(*Node)
		return r.value
	}
	return -1
}

func (this *LRUCache) Put(key int, value int)  {
	if v,ok:=this.m[key];ok {
		 n := v.Value.(*Node)
		 n.value = value
		this.l.MoveToFront(v)
	}else {
		if this.capacity==len(this.m) {
			node := this.l.Back()
			n := node.Value.(*Node)
			delete(this.m,n.key)
			this.l.Remove(node)
		}
		n :=&Node{
			key:   key,
			value: value,
		}
		e := this.l.PushFront(n)
		this.m[key] = e
	}
}


func main() {
	 c := Constructor(2)
	 p :=&c
	 p.Put(2,1)
	 p.Put(2,2)
	 fmt.Println(p.Get(2))
	 p.Put(1,1)
	 p.Put(4,1)
	 fmt.Println(p.Get(2))

}


<|start_filename|>83remove-duplicates-from-sorted-list/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
     Val int
     Next *ListNode
}

func deleteDuplicates(head *ListNode) *ListNode {
	m :=make(map[int]struct{})
	curr := head
	var pre *ListNode
	for curr!=nil{
		tmp := curr.Next
		if _,ok:=m[curr.Val];ok{
			pre.Next = tmp
			curr.Next = nil
		}else {
			pre = curr
			m[curr.Val] = struct{}{}
		}
		curr = tmp
	}
	return head
}
func main() {
	a :=ListNode{
		Val:  1,
	}
	b :=&ListNode{
		Val:  1,
	}
	a.Next =b
	c :=&ListNode{
		Val:  2,
	}
	b.Next = c
	d :=&ListNode{
		Val:  3,
	}
	c.Next = d
	f :=&ListNode{
		Val:  3,
	}
	d.Next =f
	r := deleteDuplicates(&a)
	for r!=nil{
		fmt.Println(r.Val)
		r = r.Next
	}
}


<|start_filename|>61rotate-list/main.go<|end_filename|>
package main

import "fmt"
type ListNode struct {
	     Val int
	     Next *ListNode
 }



func rotateRight(head *ListNode, k int) *ListNode {
	if head==nil||head.Next==nil||k==0 {
		return head
	}
	count :=1
	cursor := head
	for cursor.Next!=nil{
		count++
		cursor =cursor.Next
	}
	//// 相等不需要移动
	k =k%count
	if k==0 {
		return head
	}
	//当前是尾结点 // 头尾相连  循环链表
	cursor.Next = head
	// 找到需要分割的节点
	/*
	   1 2 3 4 第0次
	   4 1 2 3 第1次
	   3 4 1 2 第2次
	*/
	l :=count-(k%count)
	pre := head
	for i:=0;i<l-1;i++{
		pre = pre.Next

	}
	newHead := pre.Next
	pre.Next = nil
	return newHead
}
func main() {
	one :=&ListNode{
		Val:    1,
	}
	two :=&ListNode{
		Val:    2,
	}
	three :=&ListNode{
		Val:    3,
	}
	four :=&ListNode{
		Val:    4,
	}
	five :=&ListNode{
		Val:    5,
	}
	one.Next = two
	two.Next = three
	three.Next= four
	four.Next = five
	 h:=rotateRight(one,2)
	 for h!=nil{
	 	fmt.Println(h.Val)
	 	h =h.Next
	 }
}


<|start_filename|>1.01guess-numbers/main.go<|end_filename|>
package main

import "fmt"

func game(guess []int, answer []int) int {
	var res int
     for i,v:=range guess{
     	 if answer[i]==v{
     	 	res++
		 }
	 }
	 return res
}

func main() {
	fmt.Println(game([]int{1,2,3},[]int{1,2,3}))
}


<|start_filename|>76minimum-window-substring/main.go<|end_filename|>
package main

import (
	"fmt"
)

func minWindow1(s string, t string) string {
	need := make(map[byte]int)
	for i :=range t{
		need[t[i]]++
	}

	window :=make(map[byte]int)
	var left,right,match int
	var minLen = 0
	var resL int//窗口最小左边索引
	var resR int//窗口最小右边索引
	for right<len(s){
		if _,ok:=need[s[right]];ok{
			window[s[right]]++
			if window[s[right]]==need[s[right]] {
				match++
			}
		}
		for match ==len(need){
			l := right-left+1
			//fmt.Println(s[left:right+1])
			if minLen==0||l<minLen {
				resL = left
				resR = right
				minLen = l
			}
			if v,ok:=need[s[left]];ok{
				window[s[left]]--
				if window[s[left]]<v{
					match--
				}
			}
			left++
		}
		right++
	}
	if minLen==0 {
		return ""
	}
	return s[resL:resR+1]
}

func isContainer(strMap map[byte]int,m map[byte]int)bool{
	for k,v:=range m{
		if strMap[k]<v{
			return false
		}
	}
	return true
}



func minWindow(s string, t string) string {
	need :=make(map[byte]int)
	for i:=0;i<len(t);i++{
		need[t[i]]++
	}
	var left,right,match,minLen,resL,resR int
	window := make(map[byte]int)
	for right<=len(s)-1{
		if _,ok:=need[s[right]];ok{
			window[s[right]]++
			if window[s[right]]==need[s[right]] {
				match++
			}
		}
		for match ==len(need){//窗口匹配
			l := right-left+1
			if minLen==0||l<minLen {
				resL = left
				resR = right
                minLen = l
			}
			if v,ok:=need[s[left]];ok{
				window[s[left]]--
				if window[s[left]]<v{
					match--
				}
			}
			left++
		}
		right++
	}
	if minLen==0 {
		return ""
	}
   return s[resL:resR+1]
}
func main() {
	//fmt.Println(minWindow1("BANC","ABC"))
	fmt.Println(minWindow1("ADOBECODEBANC","ABC"))
}


<|start_filename|>530minimum-absolute-difference-in-bst/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
	"strconv"
	"strings"
)

type TreeNode struct {
	Val int
	Left *TreeNode
	Right *TreeNode
}
func BuildBinaryTree(src string)*TreeNode{
	src =src[1:len(src)-1]
	strList := strings.Split(src, ",")
	var currNode *TreeNode
	var root *TreeNode
	var queue []*TreeNode
	for i :=0;i<len(strList);i+=2{
		if i==0 {
			v,_:=strconv.Atoi(strList[i])
			currNode =&TreeNode{Val:v}
			root =currNode
			queue = append(queue,currNode)
		}
		if len(queue)>0 {
			currNode =queue[0]
			queue =queue[1:]
		}else {
			break
		}
		if i+1<len(strList)&&strList[i+1]!="null"{
			v,_:=strconv.Atoi(strList[i+1])
			currNode.Left = &TreeNode{Val:v}
			queue = append(queue,currNode.Left)
		}
		if i+2<len(strList)&&strList[i+2]!="null"{
			v,_:=strconv.Atoi(strList[i+2])
			currNode.Right = &TreeNode{Val:v}
			queue = append(queue,currNode.Right)
		}
	}
	return root
}
var pre *TreeNode
var res int
func dfs(root *TreeNode){
	if root==nil{
		return
	}
	dfs(root.Left)
	if pre!=nil {
		res = int(math.Min(math.Abs(float64(root.Val-pre.Val)),float64(res)))
	}
	pre = root
	dfs(root.Right)
}
func getMinimumDifference(root *TreeNode) int {
	res = math.MaxInt32
    dfs(root)
    return res
}
func main() {
	root :=&TreeNode{Val:   1}
	two :=&TreeNode{Val:   2}
	three :=&TreeNode{Val:   3}
	root.Right = three
	three.Left = two
	fmt.Println(getMinimumDifference(root))
}


<|start_filename|>50di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof/main.go<|end_filename|>
package main

func firstUniqChar(s string) byte {


	m := make(map[byte]int)
	keys :=make([]byte,0)
	for i:=range s{
		if _,ok:=m[s[i]];!ok{
			keys = append(keys,s[i])
		}
		m[s[i]]++
	}
	for _,v:=range keys{
		if m[v]==1{
			return v
		}
	}
	var res byte = ' '
	return res
}

func main() {
	
}


<|start_filename|>448find-all-numbers-disappeared-in-an-array/main.go<|end_filename|>
package main

import "fmt"

func findDisappearedNumbers(nums []int) []int {
	r :=make([]int,len(nums))
	for _,v:=range nums {
		r[v-1] =r[v-1]+1
	}
	var res []int
	for i,v:=range r{
		if v==0 {
			res =append(res,i+1)
		}
	}
	return res
}
func main() {
	fmt.Println(findDisappearedNumbers([]int{4,3,2,7,8,2,3,1}))
}


<|start_filename|>1108defanging-an-ip-address/main.go<|end_filename|>
package main

import (
	"fmt"
	"strings"
)

func defangIPaddr(address string) string {
	var s strings.Builder
	for  i :=range address{
		if address[i]=='.' {
			s.WriteString("[.]")
		}else {
			s.WriteByte(address[i])
		}
	}
	return s.String()
}
func main() {
	fmt.Println(defangIPaddr("1.1.1.1"))
}


<|start_filename|>491increasing-subsequences/main.go<|end_filename|>
package main

import "fmt"

func findSubsequences(nums []int) [][]int {
    for i:=0;i<len(nums);i++{
    	 l :=[]int {nums[i]}
    	for j:=i+1;j<len(nums);j++{
			if nums[j]>=nums[i] {

				l = append(l,nums[j])
			}else {
				l =[]int {nums[i]}
			}
			if len(l)>=2{
				fmt.Println(l)
			}

		}
	}
	return nil
}

func main() {
	fmt.Println(findSubsequences([]int{4, 6, 7, 7}))
}


<|start_filename|>1189maximum-number-of-balloons/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
)

func maxNumberOfBalloons(text string) int {
	l :=[26]int{}
	for _,b :=range text{
		l[b-'a']++
	}
	l['l'-'a']= l['l'-'a']/2
	l['o'-'a']= l['o'-'a']/2
	var min = math.MaxInt16
	for _,b :=range "balon"{
		  v :=l[b-'a']
          if v==0{
          	return 0
		  }

		if v<min {
			min = v
		}
	}
	return min
}

func main() {
	fmt.Println(maxNumberOfBalloons("leetcode"))

}


<|start_filename|>496next-greater-element-i/main.go<|end_filename|>
package main

import (
	"fmt"
)

func nextGreaterElement(nums1 []int, nums2 []int) []int {
    m := make(map[int]int)
	for i,v:=range nums2{
		m[v]= i
	}
     var res = make([]int,len(nums1))
	 for i:=0;i<len(nums1);i++{
	 	isExist :=false
		for j:=m[nums1[i]];j<len(nums2);j++{
			if nums1[i]<nums2[j] {
				res[i] = nums2[j]
				isExist = true
				break
			}
		}
		 if !isExist {
			 res[i]= -1
		 }
	}
	return res
}
func main() {
	//fmt.Println(nextGreaterElement([]int{4,1,2},[]int{1,3,4,2}))
	fmt.Println(nextGreaterElement([]int{3,1,5,7,9,2,6},[]int{1,2,3,5,6,7,9,11}))
}


<|start_filename|>31next-permutation/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func nextPermutation3(nums []int)  {
	n := len(nums)
	var i = n-1
	for ;i > 0; i-- {
		if nums[i] > nums[i-1] {
			j := i
			for j < n && nums[j] > nums[i-1] {
				j++
			}
			nums[i-1], nums[j-1] = nums[j-1], nums[i-1]
			break
		}
	}
	sort.Ints(nums[i:])
}

func nextPermutation1(nums []int)  {
	i :=len(nums)-2
	len := len(nums)
	for ;i>=0;i--{
		if nums[i]<nums[i+1] {
			break
		}
	}
	if i!=-1 {
		for j :=len-1;j>i;j--{
			if nums[j]>nums[i] {
				tmp :=nums[i]
				nums[i] = nums[j]
				nums[j] = tmp
				break
			}
		}
	}
	for j :=1;j<=(len-i-i)/2;j++{
		tmp :=nums[j+1]
		nums[j+1] = nums[len-j]
		nums[len-j] = tmp
	}

}



func nextPermutation4(nums []int) {
	// 第一步从右往左查找最大的升序队列
	i := len(nums)-2// 倒数第二个数
	for i >= 0&&nums[i]>=nums[i+1] {
		i--
	}

	if i==-1 {
		// 从右往左一直是升序
		sort.Ints(nums)
	}else {
		j := len(nums)-1
		// 从右往左找第一个比i索引值大的数字索引坐标
		for j>=0&&nums[j]<=nums[i]{
			j--
		}

		//交换他两位置
		nums[i], nums[j] = nums[j], nums[i]
		// 重新排序
		sort.Ints(nums[i+1:])

	}
	fmt.Println(nums)
}

func nextPermutation(nums []int){
	// 倒序查找第一个 默认是升序 查找截断升序的索引位置
	index :=-1
	for i:=len(nums)-1;i>0;i--{
		if nums[i-1]<nums[i] {
			index = i-1
			break
		}
	}
	// 从末尾一直是升序
	if index==-1 {
		sort.Ints(nums)
		return
	}
	for i:=len(nums)-1;i>index;i--{
		if nums[i]>nums[index] {
			nums[i],nums[index]=nums[index],nums[i]
			break
		}
	}
	sort.Ints(nums[index+1:])
    fmt.Println(nums)
}


func main() {
	nextPermutation([]int{1,2,3})//1 3 2
	nextPermutation([]int{3,2,1})// 1 2 3
	nextPermutation([]int{1,5,1})// 5 1 1
	nextPermutation([]int{1,1,5})// 1 5 1
	nextPermutation([]int{1,6,9,8,7})// 1 7 6 8 9
}


<|start_filename|>33search-in-rotated-sorted-array/main.go<|end_filename|>
package main

import "fmt"

func search(nums []int, target int) int {
   left :=0
   right :=len(nums)-1
   for left<=right{
   	 mid :=left+(right-left)>>1
   	 if nums[mid]==target{
   	 	return mid
	 }else if nums[left]<=nums[mid]{//中间值递增序列
		 if nums[left]<=target&&target<nums[mid]{
			 right=mid-1
		 }else {
		 	left= mid+1
		 }
	 }else {// 中间值右边递增序列
		 if nums[mid]<target&&target<=nums[right] {
			 left = mid+1
		 }else {
		 	right = mid-1
		 }
	 }
   }
   return -1
}

func main() {
	fmt.Println(search([]int{4,5,6,7,0,1,2},0))
}


<|start_filename|>1013partition-array-into-three-parts-with-equal-sum/main.go<|end_filename|>
package main

import "fmt"

func canThreePartsEqualSum(A []int) bool {
	var sum int
    for _,v:=range A{
		sum +=v
	}
	if sum%3!=0{
		return false
	}
	avg :=sum/3

	currSum :=0
	count :=0
	for _,v:=range A{
		currSum +=v
		if currSum==avg{
			count++
			currSum =0
		}
	}
	if count>=3{
		return true
	}
	return false
}


func main() {
	//fmt.Println(canThreePartsEqualSum([]int{0,2,1,-6,6,-7,9,1,2,0,1}))
	//fmt.Println(canThreePartsEqualSum([]int{0,2,1,-6,6,7,9,-1,2,0,1}))
	//fmt.Println(canThreePartsEqualSum([]int{3,3,6,5,-2,2,5,1,-9,4}))
	fmt.Println(canThreePartsEqualSum([]int{10,-10,10,-10,10,-10,10,-10}))
}


<|start_filename|>347top-k-frequent-elements/main.go<|end_filename|>
package main

import (
	"container/heap"
	"fmt"
)


type Node struct {
	Val int
	Num int
}

type NodeHeap []*Node

func(h NodeHeap)Len()int{
	return len(h)
}
func(h NodeHeap)Swap(i,j int){
	h[i],h[j] = h[j],h[i]
}
func(h NodeHeap)Less(i,j int)bool{
	return h[i].Num<h[j].Num
}
func(h *NodeHeap)Push(x interface{}){
	*h = append(*h,x.(*Node))
}
func (h *NodeHeap)Pop()interface{}  {
	x :=(*h)[len(*h)-1]
	*h =(*h)[:len(*h)-1]
	return x
}

func min(a,b int)int{
	if a<b{
		return a
	}
	return b
}

func topKFrequent(nums []int, k int) []int {
	m :=make(map[int]int)
	for _,v :=range nums{
		m[v]++
	}
	topK :=min(k,len(m))
	size :=0
	h :=&NodeHeap{}
	for k,v :=range m{
		n := &Node{
			Val: k,
			Num: v,
		}
		if size<topK{
			heap.Push(h,n)
			size++
		}else {
			if v >(*h)[0].Num{
				heap.Pop(h)
				heap.Push(h,n)
			}
		}
	}
	var res []int
	for i:=0;i<topK;i++{
		res = append(res,heap.Pop(h).(*Node).Val)
	}
	return res
}
func main() {
	fmt.Println(topKFrequent([]int{1,1,1,2,2,3},2)) //1,2
	fmt.Println(topKFrequent([]int{1},1))
}


<|start_filename|>81search-in-rotated-sorted-array-ii/main.go<|end_filename|>
package main

import "fmt"

func search(nums []int, target int) bool {
	left :=0
	right :=len(nums)-1
	for left<=right{
		mid :=left+(right-left)>>1
		if nums[mid]==target{
			return true
		}else if nums[mid]==nums[left]{
			left++
		} else if nums[left]<nums[mid]{
			if nums[left]<=target&&target<nums[mid]{
				right =mid-1
			}else {
				left = mid+1
			}
		}else {
			if nums[mid]<target&&target<=nums[right]{
				left = mid+1
			}else {
				right = mid-1
			}
		}
	}
	return false
}

func main() {
	fmt.Println(search([]int{2,5,6,0,0,1,2},0))
	fmt.Println(search([]int{2,5,6,0,0,1,2},3))
	fmt.Println(search([]int{3,1},1))
}


<|start_filename|>9palindrome-number/main.go<|end_filename|>
package main

import "fmt"

func main() {
	fmt.Println(isPalindrome(123))
}
// 主要难点在如何反转数字? rev * 10 +tmp % 10
func isPalindrome(x int) bool {
	if x < 0 || (x != 0 && x % 10 == 0) {
		return false
	}
	tmp := x
	rev := 0 // 当前数字的反转数
	for tmp > 0 {
		rev = rev * 10 +tmp % 10 // 反转数公式
		tmp /= 10
	}
	return x==rev
}

<|start_filename|>24fan-zhuan-lian-biao-lcof/main.go<|end_filename|>
package main


type ListNode struct {
	Val int
	Next *ListNode
}


func reverseList(head *ListNode) *ListNode {
     var pre *ListNode
     curr := head
     for curr!= nil{
     	pre,curr,curr.Next = curr,curr.Next,pre
	 }
	 return pre
}
func reverseList1(head *ListNode) *ListNode {
	dummyHead :=&ListNode{
		Val:  -1,
	}
	for head!= nil{
		pre := dummyHead.Next
		next :=head.Next
		dummyHead.Next = head
		head.Next = pre
		head = next
	}
	return dummyHead.Next
}


func main() {
	
}


<|start_filename|>455assign-cookies/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func findContentChildren(g []int, s []int) int {
	if len(g)==0||len(s)==0 {
		return 0
	}
	sort.Ints(g)
	sort.Ints(s)
	var cookie =0
	var child = 0
	for  child<len(g)&&cookie<len(s){
		if g[child]<=s[cookie] {
			child++
		}
		cookie++
	}
	return child
}

func main() {
	g := []int{1,2,3}
	s := []int{1,1}
	fmt.Println(findContentChildren(g,s))
	g1 := []int{1,2}
	s1 := []int{1,2,3}
	fmt.Println(findContentChildren(g1,s1))
	g2 := []int{1,2,3}
	s2 := []int{3}
	fmt.Println(findContentChildren(g2,s2))
}


<|start_filename|>994rotting-oranges/main.go<|end_filename|>
package main

import "fmt"

func bfs(grid[][]int,i,j,depth int){
	grid[i][j]= depth
	if i>0&&(grid[i-1][j]==1||grid[i-1][i]-grid[i][j]>1){
		bfs(grid,i-1,j,depth+1)
	}
	if i<len(grid)-1&&(grid[i+1][j]==1||grid[i+1][j]-grid[i][j]>1){
		bfs(grid,i+1,j,depth+1)
	}
	if j>0&&(grid[i][j-1]==1||grid[i][j-1]-grid[i][j]>1){
		bfs(grid,i,j-1,depth+1)
	}
	if j<len(grid[0])-1&&(grid[i][j+1]==1||grid[i][j+1]-grid[i][j]>1){
		bfs(grid,i,j+1,depth+1)
	}
}

func orangesRotting(grid [][]int) int {
     if len(grid)==0{
     	return -1
	 }
	 for i:=0;i<len(grid);i++{
	 	for j:=0;j<len(grid[i]);j++{
	 		 if grid[i][j]==2{
	 		 	 bfs(grid,i,j,2)
			 }
		}
	 }
	 var max =0
	 for i:=0;i<len(grid);i++{
	 	for j:=0;j<len(grid[i]);j++{
	 		if grid[i][j]==1{
	 			return -1
			}
			if max<grid[i][j]{
				max = grid[i][j]
			}
		}
	 }
	 if max ==0{
	 	return 0
	 }
	 return max-2
}

func main() {
	fmt.Println(orangesRotting([][]int{{2,1,1},{1,1,0},{0,1,1}}))
	fmt.Println(orangesRotting([][]int{{2,1,1},{0,1,1},{1,0,1}}))
	fmt.Println(orangesRotting([][]int{{0,2}}))
}


<|start_filename|>804unique-morse-code-words/main.go<|end_filename|>
package main

import "fmt"

func uniqueMorseRepresentations(words []string) int {
	s :=[26]string {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."}
   m := make(map[string]struct{})
   for _,word :=range words {
   	key :=""
   	for _,v :=range word{
   		key=key+s[v-'a']
	}
	   if _,ok:=m[key];!ok {
		  m[key]= struct{}{}
	   }
   }
	return len(m)
}

func main() {
  fmt.Println(uniqueMorseRepresentations([]string{"gin", "zen", "gig", "msg"}))
}


<|start_filename|>1422maximum-score-after-splitting-a-string/main.go<|end_filename|>
package main

import "fmt"

func maxScore(s string) int {
	var max =0
    for i:=1;i<len(s);i++{
    	z := number(s[:i],'0')
    	o := number(s[i:],'1')
    	if max<z+o{
    		max = z+o
		}
	}
	return max

}

func number(s string,t byte)int{
	var res int
	for i:=range s{
		if s[i] == t{
			res++
		}
	}
	return res
}


func main() {
	fmt.Println(maxScore("011101"))
	fmt.Println(maxScore("00111"))
	fmt.Println(maxScore("1111"))
}


<|start_filename|>700search-in-a-binary-search-tree/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func searchBST1(root *TreeNode, val int) *TreeNode {
	if root==nil{
		return nil
	}
	if root.Val==val{
		return root
	}else if root.Val>val{
	     return searchBST(root.Left,val)
	}else {
		return searchBST(root.Right,val)
	}
}


func searchBST(root *TreeNode, val int) *TreeNode {
	stack := make([]*TreeNode,0)
	if root.Val==val{
		return root
	}else if root.Val>val&&root.Left!=nil{
		stack = append(stack,root.Left)
	}else if root.Val<val&&root.Right!=nil{
		stack = append(stack,root.Right)
	}
	for len(stack)>0{
		node :=stack[len(stack)-1]
		stack = stack[:len(stack)-1]
		if node.Val==val{
			return node
		}else if node.Val>val&&node.Left!=nil{
			stack = append(stack,node.Left)
		}else if node.Val<val&&node.Right!=nil{
			stack = append(stack,node.Right)
		}
	}
	return nil
}



func main() {
	four :=&TreeNode{Val:   4,}
	two :=&TreeNode{Val:   2,}
	seven :=&TreeNode{Val:   7,}
	one :=&TreeNode{Val:   1,}
	three :=&TreeNode{Val:   3,}
	four.Left = two
	four.Right = seven
	two.Left =one
	two.Right = three
	r :=searchBST1(four,7)
	if r!=nil{
		fmt.Println(r.Val)
	}
}


<|start_filename|>67ba-zi-fu-chuan-zhuan-huan-cheng-zheng-shu-lcof/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
	"strings"
)

func strToInt(str string) int {
	str = strings.TrimSpace(str)
	var singn = 1
	var result int
	for i :=range str{
		if i==0&&str[i]=='-' {
			singn = -1
		}else if i==0&&str[i]=='+' {
			singn = 1
		} else if i==0&&(str[i]>'9'||str[i]<'0'){
			return  0
		}else if str[i]<='9'&&str[i]>='0' {
			n := int(str[i]-'0')
			result =result*10+n
		}else {
			break
		}
	}
	 result =result*singn
	if result>math.MaxInt32 {
		return math.MaxInt32
	}
	if result<math.MinInt32 {
		return math.MinInt32
	}
	return result
}
func main() {
	fmt.Println(strToInt("42"))
	fmt.Println(strToInt("   -42"))
	fmt.Println(strToInt("4193 with words"))
	fmt.Println(strToInt("words and 987"))
	fmt.Println(strToInt("-91283472332"))
	fmt.Println(strToInt("3.1415926"))
}


<|start_filename|>121best-time-to-buy-and-sell-stock/main.go<|end_filename|>
package main

import "fmt"

func maxProfit(prices []int) int {
	maxVal := 0
	if len(prices) == 0 {
		return maxVal
	}
	minPrice := prices[0]
	for i := 1; i < len(prices); i++ {
		if minPrice > prices[i] { // 表示当前买入价格最低
			minPrice = prices[i]
		} else { //表示当前价格适合卖出
			v := prices[i] - minPrice
			maxVal = max(maxVal, v)
		}
	}
	return maxVal
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func main() {
	fmt.Println(maxProfit([]int{7, 1, 5, 3, 6, 4}))
	fmt.Println(maxProfit([]int{7, 6, 4, 3, 1}))
	fmt.Println(maxProfit([]int{}))
	fmt.Println(maxProfit([]int{2, 4, 1}))
}


<|start_filename|>5longest-palindromic-substring/main.go<|end_filename|>
package main

import "fmt"


// 动态规划
func longestPalindrome2(s string) string {
	if len(s)<2 {
		return s
	}
	dp :=make([][]bool,len(s))
	for i:=range s{
		dp[i] = make([]bool,len(s))
	}
	result := string(s[0])
	for r:=0;r<len(s);r++{
		for l:=0;l<=r;l++{
			if s[l]==s[r]&&(r-l<=2||dp[l+1][r-1]){
				dp[l][r]= true
				if len(s[l:r+1])>len(result){
					result =s[l:r+1]
				}
			}
		}
	}
	return result
}


// 动态规划
func longestPalindrome(s string) string {
    dp :=make([][]bool,len(s))
    for i:=range s{
    	dp[i] = make([]bool,len(s))
	}
    result := ""
    for i :=len(s)-1;i>=0;i--{
    	for j:=i;j<len(s);j++{
				dp[i][j] = s[i]==s[j]&&(j-i<3||dp[i+1][j-1])
				if dp[i][j]&&j-i+1>len(result) {
						result = s[i:j+1]
				}
		}
	}
	return result
}
func longestPalindrome1(s string) string {
	lenth := len(s)

	if lenth <= 1 {
		return s
	}

	dp := make([][]bool, lenth)

	start := 0
	max := 1

	for r := 0;r<lenth;r++ {
		dp[r] = make([]bool, lenth)
		for l:=0;l<r;l++ {
			dp[l][r]= s[l] == s[r] && (r -l <= 2 || dp[l+1][r-1])
			if dp[l][r] {
				curr := r-l+1
				if curr > max {
					max = curr
					start = l
				}
			}
		}
	}
	return s[start:start+max]
}

func main() {
	fmt.Println(longestPalindrome2("cabbac"))
}


<|start_filename|>287find-the-duplicate-number/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func findDuplicate1(nums []int) int {
      flag :=make([]int,len(nums))
      for i:=0;i<len(nums);i++{
      	 if flag[nums[i]]==1{
           return nums[i]
		 }
		 flag[nums[i]]=1
	  }
	  return -1
}
func findDuplicate2(nums []int) int {
   sort.Ints(nums)
   l :=0
   r :=len(nums)
   for l<r{
   	 mid := l+(r-l)>>1
   	 if nums[mid]>mid{
   	 	l =mid+1
	 }else {
	 	r =mid
	 }
   }
   return l
}
func findDuplicate(nums []int) int {
	sort.Ints(nums)
	for i := 0; i < len(nums); i++ {
		if nums[i] == nums[i+1] {
			return nums[i]
		}
	}
	return 0
}


func main() {
	//1 2,2,3,4
	fmt.Println(findDuplicate([]int{1,3,4,2,2}))
	fmt.Println(findDuplicate([]int{3,1,3,4,2}))
}


<|start_filename|>53maximum-subarray/main.go<|end_filename|>
package main

import "fmt"



func Max(a,b int)int{
	if a>b {
		return a
	}
	return b
}

func maxSubArray(nums []int) int {
    dp := make([]int ,len(nums))
    dp[0]=nums[0]
    res := dp[0]
    for i:=1;i<len(nums);i++{
		dp[i]=Max(nums[i],dp[i-1]+nums[i])
		if res<dp[i] {
			res = dp[i]
		}
	}
	return res
}
//53. 最大子序和 https://leetcode-cn.com/problems/maximum-subarray/
func main() {
	fmt.Println(maxSubArray([]int{-2}))
	fmt.Println(maxSubArray([]int{-2,1}))
	fmt.Println(maxSubArray([]int{-2,1,-3}))
	fmt.Println(maxSubArray([]int{2,1,-3,4,-1,2,1,-5,4}))
}


<|start_filename|>283move-zeroes/main.go<|end_filename|>
package main

import (
	"fmt"
)

func moveZeroes(nums []int)  {
	var l1,l2 []int
      for i:=0;i<len(nums);i++{
		  if nums[i]==0 {
			  l1 = append(l1,nums[i])
		  }else {
			  l2 = append(l2,nums[i])
		  }
	  }
	  l2 = append(l2,l1...)
	  for i:=0;i<len(nums);i++{
	  	nums[i] = l2[i]
	  }
}
func main() {
	 s :=[]int{0,1,0,3,12}
	 moveZeroes(s)
	 fmt.Println(s)
}


<|start_filename|>236lowest-common-ancestor-of-a-binary-tree/main.go<|end_filename|>
package main

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
	  if root==nil||root==p||root==q{
	  	return root
	  }
	  left :=lowestCommonAncestor(root.Left,p,q)
	  right:=lowestCommonAncestor(root.Right,p,q)
	  if left!=nil&&right!=nil{
	  	  return root
	  }else if left!=nil{
	  	return left
	  }
	  return right
}


func main() {
	
}


<|start_filename|>47li-wu-de-zui-da-jie-zhi-lcof/main.go<|end_filename|>
package main

func maxValue(grid [][]int) int {
    row :=len(grid)// 行
    column := len(grid[0])//列
    dp := make([][]int,row)
    for i:=range dp{
    	dp[i] = make([]int,column)
	}
	dp[0][0]= grid[0][0]
	for i :=1;i<row;i++{//第一列行
		dp[i][0]= dp[i-1][0]+grid[i][0]
	}
	for j:=1;j<column;j++{//第一行的列
		dp[0][j] = dp[0][j-1]+grid[0][j]
	}
	for i:=1;i<row;i++{
		for j:=1;j<column;j++{
			dp[i][j]= grid[i][j]+Max(dp[i][j-1],dp[i-1][j])
		}
	}
	return dp[row-1][column-1]
}
func Max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

//面试题47. 礼物的最大价值
func main() {
	
}


<|start_filename|>202happy-number/main.go<|end_filename|>
package main

import "fmt"

func isHappy(n int) bool {
	  n = digitResult(n)
	  m :=make(map[int]struct{},0)
	  for n!=1{
	  	if _,ok:=m[n];!ok{
			m[n]= struct{}{}

			n =digitResult(n)
			fmt.Println(n)
		}else {
			return false
		}
	  }
	  return true
}
func digitResult(n int) int {
	var res int
	for n>0{
		r :=n%10
		res +=r*r
		n =n/10
	}
	return res
}

func main() {
	fmt.Println(isHappy(2))
	fmt.Println(isHappy(19))
}


<|start_filename|>297serialize-and-deserialize-binary-tree/main.go<|end_filename|>
package main

import (
	"fmt"
	"strconv"
	"strings"
)

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}


type Codec struct {

}

func Constructor() Codec {
    return  Codec{}
}

// Serializes a tree to a single string.
func (this *Codec) serialize(root *TreeNode) string {
   queue := make([]*TreeNode,0)
   queue = append(queue,root)
   var res strings.Builder
   res.WriteString("[")
   for len(queue)>0{
   	   l := len(queue)
   	   for i:=0;i<l;i++{
   	   	 if queue[i]!=nil{
			 res.WriteString(fmt.Sprintf("%d,",queue[i].Val))
		 }else {
			 res.WriteString(fmt.Sprintf("null,"))
		 }

	   }
   }
   return res.String()
}

// Deserializes your encoded data to tree.
func (this *Codec) deserialize(data string) *TreeNode {
	data = data[1:len(data)-1]
	strList  := strings.Split(data,",")
	var root *TreeNode
	for i:=0;i<len(strList)/2;i++{
		v,_ :=strconv.Atoi(strList[i])
		node := &TreeNode{Val: v}
		if strList[2*i+1]!="null"{
			left,_ :=strconv.Atoi(strList[2*i+1])
			node.Left = &TreeNode{Val: left}
		}
		if strList[2*i+2]!="null" {
			right,_ :=strconv.Atoi(strList[2*i+2])
			node.Right = &TreeNode{Val: right}
		}
		if i==0{
			root = node
		}
	}
	return root
}


func main() {
	s := "[1,2,3,null,null,4,5]"

	fmt.Println(s)

	var list []*TreeNode
	list = append(list,nil)
	for _,v:=range list{
		if v==nil{
			fmt.Println("xxxx")
		}
	}
}


<|start_filename|>67add-binary/main.go<|end_filename|>
package main

import (
	"fmt"
)

func addBinary(a string, b string) string {
	 i := len(a)-1
	 j := len(b)-1
	 var av ,bv ,carry int
	 var res string
	for i>=0||j>=0{
		if i>=0{
			av =int(a[i]-'0')
			i--
		}else {
			av =0
		}
		if j>=0{
			bv =int(b[j]-'0')
			j--
		}else {
			bv =0
		}
		r := av+bv+carry
		carry =r/2
		r = r%2
		res =fmt.Sprintf("%d%s",r,res)
	}
	if carry==1 {
		res =fmt.Sprintf("%d%s",carry,res)
	}
	return res
}


func main() {
	fmt.Println(addBinary("11","1"))//100
	fmt.Println(addBinary("1010","1011"))//10101
	fmt.Println(addBinary("1111","1111"))//10101
}


<|start_filename|>1403minimum-subsequence-in-non-increasing-order/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func minSubsequence(nums []int) []int {
	sort.Ints(nums)
	sum :=0
	for _,v:=range nums{
		sum+=v
	}
	currSum :=0
	var res []int
	for i:=len(nums)-1;currSum<=sum/2;i--{
		currSum +=nums[i]
		res = append(res,nums[i])
	}
	return res
}


func main() {
	fmt.Println(minSubsequence([]int{4,4,7,6,7}))
}


<|start_filename|>167two-sum-ii-input-array-is-sorted/main.go<|end_filename|>
package main

import "fmt"

func twoSum(numbers []int, target int) []int {
     l :=0
     r :=len(numbers)-1
     for l<=r{
     	res := numbers[l]+numbers[r]
     	if res==target{
     		return []int{l+1,r+1}
		}else if res>target  {
			r--
		}else {
			l++
		}
	 }
	 return nil
}


func main() {
	fmt.Println(twoSum([]int{2, 7, 11, 15},9))
}


<|start_filename|>434number-of-segments-in-a-string/main.go<|end_filename|>
package main

import (
	"fmt"
)

func countSegments(s string) int {
	segmentCount  := 0
	for  i := 0; i < len(s); i++ {
		if ((i == 0 || s[i-1] == ' ') && s[i] != ' ') {
			segmentCount++
		}
	}
	return segmentCount
}

func main() {
	fmt.Println(countSegments("Hello, my name is John"))
	fmt.Println(countSegments("                "))
}


<|start_filename|>944delete-columns-to-make-sorted/main.go<|end_filename|>
package main

import "fmt"

func minDeletionSize(A []string) int {
	var res int
	for i := 0; i < len(A[0]); i++ {
		s := make([]byte, len(A))
		for j := 0; j < len(A); j++ {
			s[j] = A[j][i]//
		}
		// 获取每列字符串
		if !isIncrStr(s) {
			res++
		}
	}
	return res
}

func isIncrStr(s []byte) bool {
	for i := 0; i < len(s)-1; i++ {
		if s[i] > s[i+1] {
			return false
		}
	}
	return true
}


func main() {
	fmt.Println(minDeletionSize([]string{"cba", "daf", "ghi"}))
	//"a", "b"
	fmt.Println(minDeletionSize([]string{"a", "b"}))
	//
	fmt.Println(minDeletionSize([]string{"zyx", "wvu", "tsr"}))

}


<|start_filename|>912sort-an-array/main.go<|end_filename|>
package main

import "sort"

func sortArray(nums []int) []int {
    sort.Ints(nums)
    return nums
}
func main() {
	
}


<|start_filename|>40zui-xiao-de-kge-shu-lcof/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)
//func getLeastNumbers0(arr []int, k int) []int {
//	sort.Sort(Array(arr))
//	return arr[:k]
//}
//
//type Array []int
//
//func (a Array) Len() int           { return len(a) }
//func (a Array) Less(i, j int) bool { return a[i] < a[j] }
//func (a Array) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }


//func getLeastNumbers(arr []int, k int) []int {
//    sort.Ints(arr)
//    return arr[:k]
//}


func getLeastNumbers(arr []int, k int) []int {
	sort.Ints(arr)
	return arr[:k]
}

//func quickSort1(arr []int){
//
//}
func qSort(arr [] int ,left,right int){
	if left<right {
         p := partition(arr,left,right)
		qSort(arr,left,p-1)
		qSort(arr,p+1,right)
	}
}

func partition(arr [] int ,start,end int)int{
	i,j,temp :=start,end,arr[start]
	for i<j{
		// 从左往右找比基准值大的数字
		for arr[j]>temp&&i<j{
			j--
		}
		// 从右往左找比基准值小的数字
		for arr[i]<=temp&&i<j{
			i++
		}
		arr[i],arr[j] = arr[j],arr[i]

	}
	arr[i],arr[start] = arr[start],arr[i]
	return i
}

func swap(arr [] int ,left,right int){
	temp := arr[left]
	arr[left]=arr[right]
	arr[right]= temp
}

func quickSort(arr []int)[]int{
	if len(arr) <2{// 一个元素不需要排序 直接返回
		return arr
	}
	pivot :=arr[0]
	var left []int //小于基准值
	var mid []int // 等于基准值
	var right []int // 大于基准值
	for _,v :=range arr{
		if v<pivot {
			left = append(left,v)
		}else if v>pivot{
			right = append(right,v)
		}else {
			mid = append(mid,v)
		}
	}
	 l :=quickSort(left)
	 l = append(l,mid...)
	 r :=quickSort(right)
	return append(l,r...)
}


func buildHeap(arr []int){
	// 最后一个节点
	last := len(arr)-1
	start :=(last-1)/2
    for start>=0{
       heapfix(arr,start)
       start--
	}
}

func heapfix(arr []int,i int){
	len := len(arr)
	if i>len {
		return
	}
	left :=i<<2+1
	right :=i<<2+2
	max := i
	if left<len&&arr[left]>arr[max]{
		max = left
	}
	if right<len&&arr[right]>arr[max]{
		max = right
	}
	if max!=i {
		arr[max],arr[i] = arr[i],arr[max]
		heapfix(arr,max)
	}
}

func quickSort1(arr []int)[]int{
	if len(arr)<2 {
		return arr
	}
	var min []int
	var max []int
	var mid []int
	m :=arr[0]
	for i:=0;i<len(arr);i++{
		if m<arr[i] {// big data
			max = append(max,arr[i])
		}
		if m>arr[i] { // small
			min = append(min,arr[i])
		}
		if m==arr[i] {// equal
			mid = append(mid,arr[i])
		}
	}
	r := quickSort1(min)
	r = append(r,mid...)
	r =  append(r,quickSort1(max)...)
   return r
}

//面试题40. 最小的k个数 https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/
func main() {
	fmt.Println(getLeastNumbers([]int{3,2,1,4,7,9},2))
	//l :=[]int{10,3,2,1,0,4,7,9}
	//buildHeap(l)
	//fmt.Println(quickSort1(l))
}


<|start_filename|>24swap-nodes-in-pairs/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
	Val int
	Next *ListNode
}


func swapPairs1(head *ListNode) *ListNode {
	dummy := &ListNode{-1, nil}
	dummy.Next = head
	preNode := dummy // 就是前一个节点
	for head != nil && head.Next!= nil{
		first := head
		second := head.Next
		//交换前两个节点

		first.Next = second.Next
		second.Next = first
		preNode.Next =second

		preNode = first //需要把位置移到前一个节点去
		head = first.Next
	}
	return dummy.Next
}

func swapPairs(head *ListNode) *ListNode {
	dummy := &ListNode{}
	dummy.Next = head
	preNode := dummy
	for head!=nil&&head.Next!=nil{
		first := head
		second := head.Next

		// 交换数据
		preNode.Next = second
		first.Next = second.Next
		second.Next = first

		// 移动
		preNode = first
		head = first.Next
	}
	return dummy.Next
}


func swapPairs2(head *ListNode) *ListNode {
	//终止条件：链表只剩一个节点或者没节点了，没得交换了。返回的是已经处理好的链表
	if head ==nil||head.Next==nil {
		return head
	}
	//一共三个节点:head, next, swapPairs(next.next)
	//下面的任务便是交换这3个节点中的前两个节点
	next := head.Next
	head.Next= swapPairs2(next.Next)
	next.Next = head
	//根据第二步：返回给上一级的是当前已经完成交换后，即处理好了的链表部分
	return  next
}


func main() {
	a := &ListNode{Val:  1}
	b := &ListNode{Val:  2}
	c := &ListNode{Val:  3}
	d := &ListNode{Val:  4}
	e := &ListNode{Val:  5}
	f := &ListNode{Val:  6}
	a.Next=b
	b.Next =c
	c.Next = d
	d.Next = e
	e.Next = f
	h := swapPairs2(a)
	for h!=nil{
		fmt.Println(h.Val)
		h=h.Next
	}
}


<|start_filename|>414third-maximum-number/main.go<|end_filename|>
package main

import (
	"container/heap"
	"fmt"

)

type IntList []int

func (l IntList)Len()int{
	return len(l)
}
func(l IntList)Swap(i,j int){
	l[i],l[j] = l[j],l[i]
}

func(l IntList) Less(i, j int) bool{
	return l[i]<l[j]
}
func (l *IntList)Push(x interface{}){
	*l = append(*l,x.(int))
}

func (l *IntList)Pop()interface{}{
	x := (*l)[len(*l)-1]
	*l = (*l)[:len(*l)-1]
	return x
}



func thirdMax(nums []int) int {
	//sort.Ints(nums)
	//list := make(IntList,0)
	m :=make(map[int]struct{})
	//heap.Init(&list)
	var list []int
	for i :=0;i<len(nums);i++{
		if _,ok:=m[nums[i]];!ok{
			list =append(list,nums[i])
			m[nums[i]]= struct {}{}
		}

	}
	heapSort(list)
	if len(list)<3 {
		return list[0]
	}
    return list[2]
}

func heapSort(input []int){
	inputLen := len(input)
	if inputLen == 0 {
		return
	}
	for i:=0; i<inputLen; i++{
		minAjust(input[i:])
	}
}

func minAjust(input []int){
	inputLen := len(input)
	if inputLen <= 1{
		return
	}
	for i:= inputLen/2 -1; i>=0; i--{
		if (2*i+1 <= inputLen-1) && (input[i] <= input[2*i+1]){
			input[i], input[2*i+1] = input[2*i+1], input[i]
		}
		if (2*i+2<= inputLen-1) && (input[i] <= input[2*i+2]){
			input[i], input[2*i+2] = input[2*i+2], input[i]
		}
	}
}

func sift(list []int, left, right int) {
	fIdx := left
	sIdx := fIdx*2 + 1
	// 构造大根堆
	for sIdx <= right {
		//判断左孩子:sIdx 右孩子:sIdx+1
		if sIdx < right && list[sIdx] < list[sIdx+1] {
			sIdx++
		}
		// 最终和大的儿子比较
		if list[fIdx] < list[sIdx] {
			// 交换
			list[fIdx], list[sIdx] = list[sIdx], list[fIdx]
			// 交换后重新检查被修改的子节点为大根堆
			fIdx = sIdx
			sIdx = 2*fIdx + 1
		} else {
			// 已经是大根堆
			break
		}
	}
}

func HeapSort(list []int) {
	length := len(list)
	//建立初始堆
	sift(list, 0, length-1)
	for idx := length / 2; idx >= 0; idx-- {
		// 从后往前调整
		sift(list, idx, length-1)
	}
	// 将大根堆的根节点和堆最后一个元素交换，重新对前面的 length - 1 调整堆
	for idx := length - 1; idx >= 1; idx-- {
		list[0], list[idx] = list[idx], list[0]
		sift(list, 0, idx-1)
	}
	// 结果就是逆序输出大根堆
}

func buidHeap(a []int, n int) {

	//heapify from the last parent node
	for i := n / 2; i >= 1; i-- {
		heapifyUpToDown(a, i, n)
	}

}

//sort by ascend, a index begin from 1, has n elements
func sort(a []int, n int) {
	buidHeap(a, n)

	k := n
	for k >= 1 {
		swap(a, 1, k)
		heapifyUpToDown(a, 1, k-1)
		k--
	}
}

//heapify from up to down , node index = top
func heapifyUpToDown(a []int, top int, count int) {

	for i := top; i <= count/2; {

		maxIndex := i
		if a[i] < a[i*2] {
			maxIndex = i * 2
		}

		if i*2+1 <= count && a[maxIndex] < a[i*2+1] {
			maxIndex = i*2 + 1
		}

		if maxIndex == i {
			break
		}

		swap(a, i, maxIndex)
		i = maxIndex
	}

}

//swap two elements
func swap(a []int, i int, j int) {
	tmp := a[i]
	a[i] = a[j]
	a[j] = tmp
}


func main() {
	//fmt.Println(thirdMax([]int{3,2,1}))//1
	//fmt.Println(thirdMax([]int{1,2}))//2
	//fmt.Println(thirdMax([]int{2,2,3,1}))//1
	//
	//fmt.Println(thirdMax([]int{1,2,2,5,3,5}))//2
	//fmt.Println(thirdMax([]int{1,-2147483648,2}))//-2147483648
	//fmt.Println(thirdMax([]int{1,2,-2147483648}))//-2147483648

	nums1 :=[]int{1,-2147483648,2}
	var list IntList
	for i :=0;i<len(nums1);i++{
		list = append(list,nums1[i])
	}
	heap.Init(&list)
	fmt.Println(list)
	//HeapSort(nums1)
	nums2 :=[]int{1,2,-2147483648}
	//HeapSort(nums2)
	//fmt.Println(nums2)
	var list2 IntList
	for i :=0;i<len(nums2);i++{
		list2 = append(list2,nums2[i])
	}
	heap.Init(&list2)
	fmt.Println(list2)

	//
	//nums3 :=[]int{3,28,1}
	//HeapSort(nums3)
	//fmt.Println(nums3)
}


<|start_filename|>15three-sum/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func threeSum(nums []int) [][]int {
    // 排序
	var res [][]int

	sort.Ints(nums)
	for i:=0;i<len(nums);i+=1{
		// 排序 可以去重 重复值没有必须在执行一遍了，也就做到了去重
		if i>0&&nums[i]==nums[i-1] {
			continue
		}
		l := i+1
		r := len(nums)-1
		for l<r{// 移动左右坐标点
			tmp := nums[i]+nums[l]+nums[r]
			if tmp ==0 {
				res = append(res,[]int{nums[i],nums[l],nums[r]})
				l+=1
				r-=1
				// 数字相同 去重
				for l<r&&nums[l]==nums[l-1] {
					l+=1
				}
				for l<r&&nums[r]==nums[r+1] {
					r-=1
				}
			}else if tmp>0 {// 如果和大说明需要取更小的值
				r-=1
			}else {// 如果和小于0 说明需要取更大的值
				l+=1
			}
		}
	}
	return res
}
func main() {
	fmt.Println(threeSum([]int{-1,0,1,2,-1,-4}))
	fmt.Println(threeSum([]int{0,0,0,0}))
	fmt.Println(threeSum([]int{-2,0,3,-1,4,0,3,4,1,1,1,-3,-5,4,0}))
}


<|start_filename|>43multiply-strings/main.go<|end_filename|>
package main

import (
	"fmt"
)

func multiply(num1 string, num2 string) string {
     var n1 int=0
     for i:=0;i<len(num1);i++{
     	 n :=int (num1[i]-'0')
     	 n1 =n1*10+n
	 }
	 var res string
	for i:=len(num2)-1;i>=0;i--{
		//n := int (num2[i]-'0')

	}
	return fmt.Sprintf("%f",res)
}

func addString(num1 string, num2 string) string {

	n1 :=len(num1)-1
	n2 :=len(num2)-2
	carry :=0
	var res string
	for n1>=0||n2>=0{
		a :=0
		if n1>=0 {
			a = int(num1[n1]-'0')
			n1--
		}
		b :=0
		if n2>=0 {
			b = int(num2[n2]-'0')
			n2--
		}
		r := a+b+carry
		carry =r/10
		r = r%10
		res = fmt.Sprintf("%d%s",r,res)
	}
	if carry==1 {
		res = fmt.Sprintf("%d%s",carry,res)
	}
	return res
}


func main() {
	//fmt.Println(multiply("2","3"))
	//fmt.Println(multiply("123","456"))
	fmt.Println(multiply("498828660196","840477629533"))
}


<|start_filename|>1295find-numbers-with-even-number-of-digits/main.go<|end_filename|>
package main

import "fmt"

func findNumbers(nums []int) int {
	var res int
    for _,v :=range nums{
    	if findDigit(v){
    		res++
		}
	}
	return res
}

func findDigit(num int)bool{
	var n int
	for num!=0{
		num=num/10
		n++
	}
	if n%2==0{
		return true
	}
	return false
}

func main() {
	fmt.Println(findNumbers([]int{12,345,2,6,7896}))
	fmt.Println(findNumbers([]int{555,901,482,1771}))
}


<|start_filename|>54spiral-matrix/main.go<|end_filename|>
package main

import "fmt"

func spiralOrder1(matrix [][]int) []int {
	var res []int
	if len(matrix)==0{
		return res
	}
	row :=len(matrix)
	col := len(matrix[0])
	up :=0//上界
	down :=row-1//下界
	left :=0//左界
	right :=col-1// 右界
	total := row*col//总数
	for {
		// 从左到右
		for i:=left;i<=right;i++{
			res = append(res,matrix[up][i])
		}
		if len(res)==total{
			break
		}
		up++// 上边界+1 向下移动一位
		for i:=up;i<=down;i++{
			res = append(res,matrix[i][right])
		}
		if len(res)==total{
			break
		}
		right--
		for i:=right;i>=left;i--{
			res = append(res,matrix[down][i])
		}
		if len(res)==total{
			break
		}
		down--
		for i:=down;i>=up;i--{
			res = append(res,matrix[i][left])
		}
		if len(res)==total{
			break
		}
		left++
	}
	return res
}


func spiralOrder(matrix [][]int) []int {
	var res []int
	if len(matrix)==0{
		return res
	}
	row :=len(matrix)
	col := len(matrix[0])
    up :=0//上界
    down :=row-1//下界
    left :=0//左界
    right :=col-1// 右界
    for {
    	// 从左到右
    	for i:=left;i<=right;i++{
    		res = append(res,matrix[up][i])
		}
		up++// 上边界+1 向下移动一位
		if up>down{
			break
		}
		// 从上到下
		for i:=up;i<=down;i++{
			res = append(res,matrix[i][right])
		}
		// 右边界移动
		right--
		if right<left{
			break
		}
		//从右到左
		for i:=right;i>=left;i--{
			res = append(res,matrix[down][i])
		}
		//下边界向上移动一位
		down--
		if down<up{
			break
		}
        // 从下到上
		for i:=down;i>=up;i--{
			res = append(res,matrix[i][left])
		}
        // 左边界向右移动一位
		left++
		if left>right{
			break
		}
	}
   return res
}


func main() {
	fmt.Println(spiralOrder1([][]int{
		{1, 2, 3 },
	{ 4, 5, 6 },
	{ 7, 8, 9 },
	}))

	fmt.Println(spiralOrder1([][]int{
		{1, 2, 3, 4},
	{5, 6, 7, 8},
	{9, 10, 11, 12},
	}))
}


<|start_filename|>938range-sum-of-bst/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	     Val int
	     Left *TreeNode
	     Right *TreeNode
 }
 //
func rangeSumBST(root *TreeNode, L int, R int) int {
    stack := make([]*TreeNode,0)
    var res =0
    for root!=nil||len(stack)>0{
    	for root!=nil{
    		stack = append(stack,root)
    		root = root.Left
		}
		node :=stack[len(stack)-1]
		stack =stack[:len(stack)-1]
		 if node.Val>=L&&node.Val<=R{
			res +=node.Val
			if node.Val==R{
				return res
			}
		}
		root = node.Right

	}
	return res
}

func main() {
	ten :=&TreeNode{Val:10}
	five :=&TreeNode{Val:5}
	ten.Left = five
	fifteen :=&TreeNode{Val:15}
	ten.Right = fifteen
	three :=&TreeNode{Val:3}
	five.Left = three
	seven :=&TreeNode{Val:7}
	five.Right = seven
	eighteen :=&TreeNode{Val:18}
	fifteen.Right = eighteen
	fmt.Println(rangeSumBST(ten,7,15))
}


<|start_filename|>6zigzag-conversion/main.go<|end_filename|>
package main

import "fmt"

func convert(s string, numRows int) string {
	if numRows==1||numRows>=len(s) {
		return s
	}
	lookup := map[int][]byte{}
	for i:=0;i<numRows;i++{
		lookup[i]=[]byte{}
	}
	idx :=0
	step :=1
	for i:=0;i<len(s);i++{
		lookup[idx] = append(lookup[idx],s[i])
		if idx==0  {
			step =1
		}else if idx == numRows-1 {
			step =-1
		}
		idx+=step
	}
	var r []byte
	for i:=0;i<numRows;i++{
       r = append(r,lookup[i]...)
	}
   return string(r)
}

func main() {
	fmt.Println(convert("LEETCODEISHIRING",3))
}


<|start_filename|>1385find-the-distance-value-between-two-arrays/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
)

func findTheDistanceValue(arr1 []int, arr2 []int, d int) int {
     var res int
	for i:=0;i<len(arr1);i++{
		isDistanceValue :=true
		for j:=0;j<len(arr2);j++{
			if int(math.Abs(float64(arr1[i]-arr2[j])))<=d {
				isDistanceValue = false
				break
			}
		}
		if isDistanceValue{
			res++
		}
	}
	return res
}


func main() {
	fmt.Println(findTheDistanceValue([]int{4,5,8},[]int{10,9,1,8},2))
	fmt.Println(findTheDistanceValue([]int{1,4,2,3},[]int{-4,-3,6,10,20,30},3))
	fmt.Println(findTheDistanceValue([]int{12,1,100,3},[]int{-5,-2,10,-3,7},6))


}


<|start_filename|>16.11diving-board-lcci/main.go<|end_filename|>
package main

import "fmt"

func divingBoard(shorter int, longer int, k int) []int {
	res := make([]int,0)
	if k==0 {
		return res
	}
	if shorter==longer{
		return append(res,k*shorter)
	}
	for i:=0;i<=k;i++{
		res = append(res,(k-i)*shorter+i*longer)
	}
	return res
}
func main() {
	fmt.Println(divingBoard(1,1,0))//[0]
	fmt.Println(divingBoard(1,1,10000))
	fmt.Println(divingBoard(1,2,3))
}


<|start_filename|>728self-dividing-numbers/main.go<|end_filename|>
package main

import "fmt"

func isDividingNumbers(i int)bool{
	if i<10{
		return true
	}
	v:=i
	for i!=0{
		r :=i%10
		if r==0||v%r!=0{
			return false
		}
		i =i/10
	}
	return true
}
func selfDividingNumbers(left int, right int) []int {
	 var res []int
     for i:=left;i<=right;i++{
		 if isDividingNumbers(i){
		 	res = append(res,i)
		 }
	 }
	 return res
}

func main() {
	fmt.Println(selfDividingNumbers(1,22))
}


<|start_filename|>404sum-of-left-leaves/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	Val int
	Left *TreeNode
	Right *TreeNode
}

func sumOfLeftLeaves(root *TreeNode) int {
     stack := make([]*TreeNode,0)
     var res int
     for root!= nil||len(stack)>0{
     	if root!= nil{
     		stack = append(stack,root)
     		root = root.Left
			if root!=nil&&root.Left==nil&&root.Right==nil{
				res +=root.Val
			}
		}else {
			node :=stack[len(stack)-1]
			stack = stack[:len(stack)-1]
			root = node.Right
		}
	 }
	 return res
}

func main() {
	three :=&TreeNode{Val: 3}
	nine :=&TreeNode{Val: 9}
	three.Left = nine
	twenty :=&TreeNode{Val: 20}
	three.Right = twenty
	fifteen :=&TreeNode{Val: 15}
	twenty.Left = fifteen
	seven :=&TreeNode{Val: 7}
	twenty.Right = seven

    fmt.Println(sumOfLeftLeaves(three))

	t1 :=&TreeNode{Val: 1}
	t2 :=&TreeNode{Val: 2}
	t1.Left = t2
	t3 :=&TreeNode{Val: 3}
	t1.Right = t3
	t4 :=&TreeNode{Val: 4}
	t2.Left = t4
	t5 :=&TreeNode{Val: 5}
	t2.Right = t5

	fmt.Println(sumOfLeftLeaves(t1))



}


<|start_filename|>129sum-root-to-leaf-numbers/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func sumNumbers(root *TreeNode) int {
	var res int
	dfs(root, 0, &res)
	return res
}

func dfs(root *TreeNode, val int, res *int) {
	if root == nil {
		return
	}
	val = val*10 + root.Val
	if root.Left == nil && root.Right == nil {
		*res = *res + val
	}
	dfs(root.Left, val, res)
	dfs(root.Right, val, res)
}

func main() {
	one := &TreeNode{Val: 1}
	two := &TreeNode{Val: 2}
	one.Left = two
	three := &TreeNode{Val: 3}
	one.Right = three
	fmt.Println(sumNumbers(one))

	four := &TreeNode{Val: 4}
	nine := &TreeNode{Val: 9}
	four.Left = nine
	zero := &TreeNode{Val: 0}
	four.Right = zero
	five := &TreeNode{Val: 5}
	nine.Left = five
	one = &TreeNode{Val: 1}
	nine.Right = one
	fmt.Println(sumNumbers(four))
}


<|start_filename|>57he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/main.go<|end_filename|>
package main

import "fmt"

func findContinuousSequence(target int) [][]int {

	left :=1
	right :=2
	var res [][]int
	for left<right{
		sum :=(left+right)*(right-left+1)/2
		if sum==target{
			var list []int
			for i:=left;i<=right;i++{
				list = append(list,i)
			}
			res = append(res,list)
			left++
		}else if sum<target{
			right++
		}else {
			left++
		}
	}
	return res
}

func main() {
	fmt.Println(findContinuousSequence(9))
	fmt.Println(findContinuousSequence(15))
	fmt.Println(findContinuousSequence(1))
}


<|start_filename|>34find-first-and-last-position-of-element-in-sorted-array/main.go<|end_filename|>
package main

import "fmt"

func searchRange(nums []int, target int) []int {
    l :=0
    r :=len(nums)-1
    res :=[]int{-1,-1}
    for l<=r{
    	mid :=l+(r-l)>>1
		if nums[mid]==target{
			res[0]= mid
			res[1] = mid
			i := mid-1
			for i>=0 &&nums[i]==target{
				res[0]=i
				i--
			}
			j := mid+1
			for j<len(nums)&&nums[j]==target  {
				res[1]=j
				j++
			}
			return res
		}else if target>nums[mid] {
			l = mid+1
		}else if target<nums[mid]{
			r = mid-1
		}

    }
    return res
}


func main() {
	fmt.Println(searchRange([]int{5,7,7,8,8,10},8))
}


<|start_filename|>476number-complement/main.go<|end_filename|>
package main

import "fmt"

func findComplement1(num int) int {
	i := 1
	for i <= num {
		i = i<<1
	}
	return i - num - 1
}

func findComplement(num int) int {
	tmp := num
	n :=1
	for tmp>0 {
		n=n<<1
		tmp=tmp>>1
	}
	n=n-1
	return num^n
}

func main() {
	fmt.Println(findComplement(5))
}


<|start_filename|>977squares-of-a-sorted-array/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func sortedSquares1(A []int) []int {
	for i, v := range A {
		A[i] = v * v
	}
	sort.Ints(A)
	return A
}

func sortedSquares(A []int) []int {
	res := make([]int, len(A))
	l := 0
	r := len(A) - 1
	i := len(res) - 1
	var lRes, rRes int
	for l <= r {
		lRes = A[l] * A[l]
		rRes = A[r] * A[r]
		if lRes > rRes {
			res[i] = lRes
			l++
		} else {
			res[i] = rRes
			r--
		}
		i--
	}
	return res
}

func main() {
	//fmt.Println(sortedSquares([]int{-4,-1,0,3,10}))
	fmt.Println(sortedSquares([]int{-7, -3, 2, 3, 11}))
	fmt.Println(sortedSquares([]int{1}))
}


<|start_filename|>200number-of-islands/main.go<|end_filename|>
package main

import "fmt"

func dfs1(grid [][]byte,i,j int){
	if i<0||i>=len(grid) ||j<0||j>=len(grid[i])||grid[i][j]!='1'{
		return
	}
	grid[i][j]= '2'
	dfs(grid,i-1,j)
	dfs(grid,i+1,j)
	dfs(grid,i,j-1)
	dfs(grid,i,j+1)
}

type Point struct {
	i int
	j int
}

func dfs(grid [][]byte,i,j int){
	stack :=make([]*Point,0)
	stack = append(stack,&Point{i: i, j: j})
	for len(stack)>0{
		point := stack[len(stack)-1]
		stack = stack[:len(stack)-1]
		if point.i<0||point.i>=len(grid) ||point.j<0||point.j>=len(grid[point.i])||grid[point.i][point.j]!='1'{
			continue
		}
		grid[point.i][point.j]= '2'
		stack =append(stack,&Point{i: point.i, j: point.j+1})
		stack =append(stack,&Point{i: point.i, j: point.j-1})
		stack =append(stack,&Point{i: point.i-1, j: point.j})
		stack =append(stack,&Point{i: point.i+1, j: point.j})
	}
}



func numIslands(grid [][]byte) int {
	var res int
   for i:=0;i<len(grid);i++{
   	for j:=0;j<len(grid[0]);j++{
   		if grid[i][j]=='0'||grid[i][j]=='2'{
   			continue
		}
		res++
		dfs(grid,i,j)
	}
   }
   return res
}

func main() {
	g :=[][]byte{{'1','1','1','1','0'},
		{'1','1','0','1','0'},
		{'1','1','0','0','0'},
		{'0','0','0','0','0'}}
	fmt.Println(numIslands(g))
}


<|start_filename|>53que-shi-de-shu-zi-lcof/main.go<|end_filename|>
package main

import "fmt"

func missingNumber1(nums []int) int {
    for i:=1;i<len(nums);i++{
        if nums[i]-nums[i-1]!=1{
        	return nums[i-1]+1
		}
	}
	if nums[0]!=0{
		return 0
	}
	return nums[len(nums)-1]+1
}
func missingNumber(nums []int) int {
	left :=0
	right :=len(nums)
	for left<right{
		mid := left+(right-left)>>1
		if nums[mid]==mid{
			left = mid+1
		}else {
			right = mid
		}
	}
	return left
}

func main() {
	fmt.Println(missingNumber([]int{0}))//1
	fmt.Println(missingNumber([]int{1}))//0
	fmt.Println(missingNumber([]int{0,1}))//2
	fmt.Println(missingNumber([]int{0,1,3}))//2
	fmt.Println(missingNumber([]int{0,1,2,3,4,5,6,7,9}))//8
}


<|start_filename|>409longest-palindrome/main.go<|end_filename|>
package main

import "fmt"

func longestPalindrome(s string) int {
     l := len(s)
     m :=make(map[byte]int)
     for i:=0;i<l;i++{
     	 m[s[i]]++
	 }
	 var res int
	 for _,v:=range m{
	 	 if v/2>0{
	 	 	res +=(v/2)*2
		 }
	 }
	 if l%2!=0||(l%2==0&&res!=l){
	 	res=res+1
	 }
	 return res
}

func main() {
	fmt.Println(longestPalindrome("abccccdd"))
	fmt.Println(longestPalindrome("ccc"))
}


<|start_filename|>203remove-linked-list-elements/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

func removeElements(head *ListNode, val int) *ListNode {
	dummyHead := &ListNode{
		Val:  -1,
		Next: head,
	}
	preNode := dummyHead
	for head != nil {
		if head.Val == val {
			next := head.Next
			preNode.Next = next
			head = next
		} else {
			preNode = head
			head = head.Next
		}
	}
	return dummyHead.Next
}

func main() {
	one := &ListNode{Val: 1}
	two := ListNode{Val: 2}
	one.Next = &two
	three := ListNode{Val: 6}
	two.Next = &three
	four := ListNode{Val: 3}
	three.Next = &four
	five := ListNode{Val: 4}
	four.Next = &five
	six := ListNode{Val: 5}
	five.Next = &six
	seven := ListNode{Val: 6}
	six.Next = &seven
	removeElements(one, 6)
	curr := one
	for curr != nil {
		fmt.Println(curr.Val)
		curr = curr.Next
	}
}


<|start_filename|>1351count-negative-numbers-in-a-sorted-matrix/main.go<|end_filename|>
package main

import "fmt"

func countNegatives1(grid [][]int) int {
	 var res int
     for i:=0;i<len(grid);i++{// 行
     	for j:=0;j<len(grid[i]);j++{// 列
     		if grid[i][j]<0{
     			res++
			}
		}
	 }
	 return res
}


func countNegatives(grid [][]int) int {
	var res int
	for i:=0;i<len(grid);i++{
		 c :=len(grid[i])
		 if grid[i][c-1]>=0{
		 	continue
		 }
		if grid[i][0]<0{
			res +=c
			continue
		}
		 r := binarySearch(grid[i])
		 res +=c-r
	}
	return res
}
func binarySearch(g []int) int {
	l:=0
	r :=len(g)-1
	for l<=r{
		mid :=l+(r-l)>>1
		if g[mid]>=0{
			l = mid+1
		}else {
			r =mid-1
		}
	}
	return l
}



func main() {
	grid := [][]int{{4,3,2,-1},{3,2,1,-1},{1,1,-1,-2},{-1,-1,-2,-3}}
	fmt.Println(countNegatives(grid))
	//fmt.Println(binarySearch([]int{4,3,2,-1}))
	//fmt.Println(binarySearch([]int{3,2,1,-1}))
	//fmt.Println(binarySearch([]int{1,1,-1,-2}))
	//fmt.Println(binarySearch([]int{0,1,2,3}))
}


<|start_filename|>58zuo-xuan-zhuan-zi-fu-chuan-lcof/main.go<|end_filename|>
package main

import "fmt"
func reverseLeftWords(s string, n int) string {
 return  s[n:]+s[:n]
}
func main() {
	fmt.Println(reverseLeftWords("abcdefg",2))
	fmt.Println(reverseLeftWords("lrloseumgh",6))
}


<|start_filename|>1374generate-a-string-with-characters-that-have-odd-counts/main.go<|end_filename|>
package main

import (
	"fmt"
	"strings"
)

func generateTheString(n int) string {
	var str strings.Builder
	c :=1
	if n>1&&n%2!=0{
		c=2
	}
	char :=[]byte{'a','b','c'}
	for i:=0;i<c;i++{
		str.WriteByte(char[i])
	}
	for i:=0;i<n-c;i++{
		str.WriteByte(char[c])
	}
	return str.String()
}

func main() {
	fmt.Println(generateTheString(7))
	fmt.Println(generateTheString(4))
	fmt.Println(generateTheString(2))
	fmt.Println(generateTheString(1))
}


<|start_filename|>84largest-rectangle-in-histogram/main.go<|end_filename|>
package main

import "fmt"

func largestRectangleArea(heights []int) int {
	var res int
	for i := 0; i < len(heights); i++ {
		var min = heights[i]
		for j := i; j < len(heights); j++ {
			if min > heights[j] {
				min = heights[j]
			}
			width := j - i + 1
			area := min * width
			if area > res {
				res = area
			}
		}
	}
	return res
}

func main() {
	fmt.Println(largestRectangleArea([]int{2, 1, 5, 6, 2, 3}))
}


<|start_filename|>18shan-chu-lian-biao-de-jie-dian-lcof/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
	     Val int
	     Next *ListNode
}

func deleteNode(head *ListNode, val int) *ListNode {
	dummyHead := &ListNode{
		Next: head,
	}
	preNode := dummyHead
	for head!= nil{
		next := head.Next
		if head.Val==val{
			preNode.Next = next
			break
		}
		preNode = head
		head = next
	}
	return dummyHead.Next
}


func main() {
	four :=&ListNode{Val: 4}
	five :=&ListNode{Val: 5}
	four.Next = five
	one :=&ListNode{Val: 1}
	five.Next = one
	nine :=&ListNode{Val: 9}
	one.Next = nine


	r := deleteNode(four,4)
	for r!=nil{
		fmt.Println(r.Val)
		r = r.Next
	}
}


<|start_filename|>985sum-of-even-numbers-after-queries/main.go<|end_filename|>
package main

import "fmt"

func sumEvenAfterQueries(A []int, queries [][]int) []int {
	m := map[int]int{}
	sum := 0
	for i, v := range A {
		if v%2 == 0 {
			m[i] = v
			sum += v
		}

	}
	var res []int
	for i := 0; i < len(queries); i++ {
		val := queries[i][0]
		idx := queries[i][1]
		preVal := A[idx]
		currVal := A[idx] + val
		A[idx] = currVal
		if preVal%2 != 0 && currVal%2 == 0 {
			sum += currVal
		} else if preVal%2 == 0 && currVal%2 != 0 {
			sum -= preVal
		} else if preVal%2 == 0 && currVal%2 == 0 {
			sum += val
		}
		res = append(res, sum)
	}
	return res
}
func main() {
	fmt.Println(sumEvenAfterQueries([]int{1,2,3,4},[][]int{{1,0},{-3,1},{-4,0},{2,3}}))
}


<|start_filename|>1221split-a-string-in-balanced-strings/main.go<|end_filename|>
package main

import "fmt"

func balancedStringSplit(s string) int {
	if len(s) == 0 {
		return 0
	}
	stack := []byte{s[0]}
	var res int
	for i := 1; i < len(s); i++ {
		if len(stack) > 0 && ((s[i] == 'L' && stack[len(stack)-1] == 'R') || (s[i] == 'R' && stack[len(stack)-1] == 'L')) {
			stack = stack[:len(stack)-1]
			if len(stack) == 0 {
				res++
			}
		} else {
			stack = append(stack, s[i])
		}
	}
	return res
}

func main() {
	fmt.Println(balancedStringSplit("RLRRLLRLRL"))
	fmt.Println(balancedStringSplit("RLLLLRRRLR"))
	fmt.Println(balancedStringSplit("LLLLRRRR"))
	fmt.Println(balancedStringSplit("RLRRRLLRLL"))
}


<|start_filename|>541reverse-string-ii/main.go<|end_filename|>
package main

import "fmt"

func reverseStr(s string, k int) string {
	str :=[]byte(s)
	var i int
	for i<len(str){
		l:=i
		r:=i+k-1
		if r>=len(str){
			r =len(str)-1
		}
		for l<r&&l<len(str){
			str[l],str[r]=str[r],str[l]
			l++
			r--
		}
		i=i+2*k
	}
	return string(str)
}

func main() {
	fmt.Println(reverseStr("abcdefgh",3))
	fmt.Println(reverseStr("a",2))
	s :="hyzqyljrnigxvdtneasepfahmtyhlohwxmkqcdfehybknvdmfrfvtbsovjbdhevlfxpdaovjgunjqlimjkfnqcqnajmebeddqsgl"
	fmt.Println(reverseStr(s,39))
}


<|start_filename|>468validate-ip-address/main.go<|end_filename|>
package main

import (
	"fmt"
	"strconv"
	"strings"
	"unicode"
)

func checkIPv4(IP string)bool{
	str := strings.Split(IP,".")
	if len(str)!=4{
		return false
	}
	for _,s:=range str{
		if len(s)==0||len(s)>1&&s[0]=='0' {
			return false
		}
		if len(s)>0&&!unicode.IsDigit(rune(s[0])){
			return false
		}
		r,err := strconv.Atoi(s)
		if err!=nil {
			return false
		}
		if r <0||r>255{
			return false
		}
	}
	return true
}
func checkIPv6(IP string)bool{
	str := strings.Split(IP,":")
	if len(str)!=8{
		return false
	}
	for _,s :=range str{
		if len(s) <= 0 || len(s) > 4 {
			return false
		}
		for i := 0; i < len(s); i++ {
			if s[i] >= '0' && s[i] <= '9' {
				continue
			}
			if s[i] >= 'A' && s[i] <= 'F' {
				continue
			}
			if s[i] >= 'a' && s[i] <= 'f' {
				continue
			}
			return false
		}
	}
	return true
}

func validIPAddress(IP string) string {
	if checkIPv4(IP) {
		return "IPv4"
	}
	if checkIPv6(IP) {
		return "IPv6"
	}
	return "Neither"
}


func main() {
	//fmt.Println(validIPAddress("172.16.254.1"))
	//fmt.Println(validIPAddress("172.16.254.01"))
	//fmt.Println(validIPAddress("12..33.4"))
	fmt.Println(validIPAddress("192.0.0.1"))
	fmt.Println(validIPAddress("15.16.-0.1"))
	//fmt.Println(validIPAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"))
	//fmt.Println(validIPAddress("2001:0db8:85a3::8A2E:0370:7334"))
}


<|start_filename|>225implement-stack-using-queues/main.go<|end_filename|>
package main

type MyStack struct {
    list []int
}


/** Initialize your data structure here. */
func Constructor() MyStack {
   return MyStack{
   	list: make([]int,0),
   }
}


/** Push element x onto stack. */
func (this *MyStack) Push(x int)  {
      this.list = append(this.list,x)
}


/** Removes the element on top of the stack and returns that element. */
func (this *MyStack) Pop() int {
	x := this.list[len(this.list)-1]
	this.list = this.list[:len(this.list)-1]
	return x
}


/** Get the top element. */
func (this *MyStack) Top() int {
      return this.list[len(this.list)-1]
}


/** Returns whether the stack is empty. */
func (this *MyStack) Empty() bool {
    return len(this.list)==0
}

func main() {
	
}


<|start_filename|>69sqrtx/main.go<|end_filename|>
package main

import "fmt"

func mySqrt(x int) int {
	if x==0 ||x==1{
		return x
	}
	left :=0
	right :=x
	for left<=right{
		mid := (left+right)>>1
		r := mid*mid
		if r==x {
			return mid
		}else if r>x  {
			right =mid-1
		}else {
			left = mid+1
		}
	}
	return left-1
}

func main() {
	fmt.Println(mySqrt(4))
	fmt.Println(mySqrt(6))
	fmt.Println(mySqrt(8))
	fmt.Println(mySqrt(10))
}


<|start_filename|>14longest-common-prefix/main.go<|end_filename|>
package main

import (
	"fmt"
	"strings"
)

func longestCommonPrefix(strs []string) string {
	if len(strs)==0 {
		return ""
	}
	if len(strs)==1 {
		return strs[0]
	}
	preStr :=strs[0]
	for i:=1;i<len(strs);i++{
		for !strings.HasPrefix(strs[i],preStr){
			if len(preStr)==0 {
				return ""
			}
			preStr =preStr[0:len(preStr)-1]
		}
	}
	return preStr
}


func main() {
	fmt.Println(longestCommonPrefix([]string{"flower","flow","flight"}))
	fmt.Println(longestCommonPrefix([]string{"dog","racecar","car"}))
}


<|start_filename|>392is-subsequence/main.go<|end_filename|>
package main

import "fmt"

func isSubsequence(s string, t string) bool {
    s1 :=0
    t1 :=0
    for t1<len(t)&&s1<len(s){
    	if s[s1]==t[t1]{
    		s1++
		}
		t1++
	}
	if s1==len(s){
		return true
	}
	return false
}


func main() {
	fmt.Println(isSubsequence("abc", "ahbgdc"))
	fmt.Println(isSubsequence("axc", "ahbgdc"))
	fmt.Println(isSubsequence("acb", "ahbgdc"))
}


<|start_filename|>9yong-liang-ge-zhan-shi-xian-dui-lie-lcof/main.go<|end_filename|>
package main

type CQueue struct {
   list []int
}


func Constructor() CQueue {
     return CQueue{list:make([]int,0)}
}


func (this *CQueue) AppendTail(value int)  {
      this.list = append(this.list,value)
}


func (this *CQueue) DeleteHead() int {
    if len(this.list)==0{
    	return -1
	}
	v :=this.list[0]
	this.list = this.list[1:]
	return v
}


//面试题09. 用两个栈实现队列
func main() {
	
}


<|start_filename|>783minimum-distance-between-bst-nodes/main.go<|end_filename|>
package main

import (
	"fmt"
	"strconv"
	"strings"
)

type TreeNode struct {
	Val int
	Left *TreeNode
	Right *TreeNode
 }
 // 中序遍历
func minDiffInBST(root *TreeNode) int {
	var res []int
	stack := make([]*TreeNode,0)
	min := 1<<63-1
    for root!=nil||len(stack)>0{
    	for root!=nil{
    		stack = append(stack,root)
    		root =root.Left
		}
		node :=stack[len(stack)-1]
		stack = stack[:len(stack)-1]
		res = append(res,node.Val)
		if len(res)>1 {
			r := node.Val-res[len(res)-2]
			if min>r{
				min = r
			}
		}
		root = node.Right
	}
	if len(res)<2 {
		return res[0]
	}
	return min
}

func BuildBinaryTree(src string)*TreeNode{
	src =src[1:len(src)-1]
	strList := strings.Split(src, ",")
	var currNode *TreeNode
	var root *TreeNode
	var queue []*TreeNode
	for i :=0;i<len(strList);i+=2{
		if i==0 {
			v,_:=strconv.Atoi(strList[i])
			currNode =&TreeNode{Val:v}
			root =currNode
			queue = append(queue,currNode)
		}
		if len(queue)>0 {
			currNode =queue[0]
			queue =queue[1:]
		}else {
			break
		}
		if i+1<len(strList)&&strList[i+1]!="null"{
			v,_:=strconv.Atoi(strList[i+1])
			currNode.Left = &TreeNode{Val:v}
			queue = append(queue,currNode.Left)
		}
		if i+2<len(strList)&&strList[i+2]!="null"{
			v,_:=strconv.Atoi(strList[i+2])
			currNode.Right = &TreeNode{Val:v}
			queue = append(queue,currNode.Right)
		}
	}
	return root
}

func main() {
	root := BuildBinaryTree("[4,2,6,1,3,null,null]")
	fmt.Println(minDiffInBST(root))
	root1 :=BuildBinaryTree("[27,null,34,null,58,50,null,44,null,null,null]")
	fmt.Println(minDiffInBST(root1))
}


<|start_filename|>590n-ary-tree-postorder-traversal/main.go<|end_filename|>
package main

type Node struct {
	Val      int
	Children []*Node
}

func postorder(root *Node) []int {
	if root==nil{
		return nil
	}
	res :=make([]int,0)
	helper(root,&res)
	return res
}

func helper(root *Node,res *[]int){
	if root==nil{
		return
	}
	for _,v:=range root.Children{
		helper(v,res)
	}
	*res = append(*res,root.Val)
}


func postorder1(root *Node) []int {
	if root==nil{
		return nil
	}
	stack := make([]*Node,0)
	stack = append(stack,root)
	res := make([]int,0)
	for len(stack)>0{
		node := stack[len(stack)-1]
		stack = stack[:len(stack)-1]
		res = append([]int{node.Val},res...)
		for i:=0;i<len(node.Children);i++{
			stack = append(stack,node.Children[i])
		}
	}
	return res
}

func main() {
	
}


<|start_filename|>258add-digits/main.go<|end_filename|>
package main

import "fmt"

func addDigits(num int) int {
    for num>=10{
    	res:=0
    	for num>0{
    		res +=num%10
    		num=num/10
		}
		num= res
	}
	return num
}


func main() {
	fmt.Println(addDigits(38))
	fmt.Println(addDigits(59))
	fmt.Println(addDigits(199))
}


<|start_filename|>21merge-two-sorted-lists/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
      Val int
     Next *ListNode
}
//https://leetcode-cn.com/problems/merge-two-sorted-lists/

func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
     dummy := &ListNode{}
     cur := dummy
     for {
		 if l1==nil&&l2==nil {
			 break
		 }
		 if l1==nil {
			 cur.Next = l2
			 break
		 }
		 if l2==nil {
			 cur.Next = l1
			 break
		 }
		 if l1.Val<l2.Val {
			 cur.Next = l1
			 l1 = l1.Next
			 cur = cur.Next
		 }else {
		 	cur.Next = l2
		 	l2 = l2.Next
		 	cur = cur.Next
		 }
	 }
     return dummy.Next
}
func main() {
	a1 := &ListNode{Val:  1}
	b1 := &ListNode{Val:  2}
	c1 := &ListNode{Val:  4}
	a2 := &ListNode{Val:  1}
	b2 := &ListNode{Val:  3}
	c2 := &ListNode{Val:  4}
	a1.Next=b1
	b1.Next = c1
	a2.Next = b2
	b2.Next = c2
	h := mergeTwoLists(a1,a2)
	for h!=nil{
		fmt.Println(h.Val)
		h=h.Next
	}
}


<|start_filename|>561array-partition-i/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func arrayPairSum(nums []int) int {
    sort.Ints(nums)
    var res int
    for i:=0;i<len(nums);i=i+2{
    	 res +=nums[i]
	}
	return res
}


func main() {
	fmt.Println(arrayPairSum([]int{1,4,3,2}))
}


<|start_filename|>64qiu-12n-lcof/main.go<|end_filename|>
package main

import "fmt"

func sumNums(n int) int {
    start :=1
    end :=n
	return ((start+end)*n)>>1
}


//面试题64. 求1+2+…+n
func main() {
	fmt.Println(sumNums(3))
	fmt.Println(sumNums(9))
}


<|start_filename|>1.06compress-string-lcci/main.go<|end_filename|>
package main

import (
	"fmt"
	"strings"
)

func compressString(S string) string {
	if len(S) == 0 {
		return S
	}
	preByte := S[0]
	count := 1
	var res strings.Builder
	for i := 1; i < len(S); i++ {
		if preByte == S[i] {
			count++
		} else {
			res.WriteByte(preByte)
			res.WriteString(fmt.Sprintf("%d", count))
			preByte = S[i]
			count = 1
		}
	}

	res.WriteByte(preByte)
	res.WriteString(fmt.Sprintf("%d", count))
	str := res.String()
	if len(str) >= len(S) {
		return S
	}
	return str
}

// 面试题 1.06 字符串压缩
func main() {
	fmt.Println(compressString("aabcccccaaa"))
	fmt.Println(compressString("abbccd"))
	fmt.Println(compressString("bb"))
}


<|start_filename|>137single-number-ii/main.go<|end_filename|>
package main

func singleNumber(nums []int) int {
	m :=make(map[int]int)
	for i:=0;i<len(nums);i++{
		m[nums[i]]++
	}
	for k,v:=range m{
		if v==1 {
			return k
		}
	}
	return -1
}


func main() {
	
}


<|start_filename|>60permutation-sequence/main.go<|end_filename|>
package main

import (
	"fmt"
	"strconv"
	"strings"
)
var mark []int
func getPermutation(n int, k int) string {
     var nums []string
     for i:=1;i<=n;i++{
     	nums = append(nums,strconv.Itoa(i))
	 }
	 path :=make([]string,0)
	 res :=make([]string,0)
	 mark = make([]int,n)
	 dfs(nums,path,k,&res)
	 //fmt.Println(len(res))
	 return res[k-1]
}

func dfs(nums []string,path []string,length int ,res *[]string){
	if len(path)==len(nums) {
		tem := make([]string,len(nums))
		copy(tem,path)
		s := strings.Join(tem,"")
		*res = append(*res,s)
		return
	}
	if len(*res)==length {
		return
	}
	for i :=0;i<len(nums);i++{
		if mark[i]==1 {
			continue
		}
		mark[i]=1
		path = append(path,nums[i])
		dfs(nums,path,length,res)
		path = path[:len(path)-1]
		mark[i]=0
	}
}

func main() {
	fmt.Println(getPermutation(3,3))
	fmt.Println(getPermutation(4,9))
	fmt.Println(getPermutation(9,13531))
}


<|start_filename|>231power-of-two/main.go<|end_filename|>
package main

import "fmt"

func isPowerOfTwo1(n int) bool {
	if n<=0{
		return false
	}
    for n>1{
    	if n%2!=0{
    		return false
		}
		n=n/2
	}
	return true
}


func isPowerOfTwo(n int) bool {
	return n>0&&n&(n-1)==0
}


func main() {
	fmt.Println(isPowerOfTwo(1))
	fmt.Println(isPowerOfTwo(16))
	fmt.Println(isPowerOfTwo(218))
}


<|start_filename|>10.01sorted-merge-lcci/main.go<|end_filename|>
package main

import "fmt"

func merge(A []int, m int, B []int, n int)  {
       AIndex :=m-1
       BIndex :=n-1
       idx := len(A)-1
       for AIndex>=0&&BIndex>=0{
       	   if A[AIndex]>B[BIndex]{
       	   	  A[idx] = A[AIndex]
       	   	  AIndex--
		   }else if A[AIndex]<=B[BIndex]{
		   	 A[idx] = B[BIndex]
		   	 BIndex--
		   }
		   idx--
	   }
	   for BIndex>=0{
		   A[idx] = B[BIndex]
		   BIndex--
		   idx--
	   }
}

func main() {
	s :=[]int{1,2,3,0,0,0}
	merge(s,3,[]int{2,5,6},3)
	fmt.Println(s)

	s1 :=[]int{0}
	merge(s1,0,[]int{1},1)
	fmt.Println(s1)

	s2 :=[]int{4,5,6,0,0,0}
	merge(s2,3,[]int{1,2,3},3)
	fmt.Println(s2)
}


<|start_filename|>859buddy-strings/main.go<|end_filename|>
package main

import "fmt"

func buddyStrings(A string, B string) bool {
	if len(A)!=len(B) {
		return false
	}

	count := 0
	w := A[0]
	isSameStr := true
	for i :=0;i<len(A)-1;i++{
		if A[i]==B[i] {
			if w !=B[i]&&isSameStr  {
				isSameStr = false
			}
			continue
		}
		if A[i]==B[i+1]&&A[i+1]==B[i] {
			count++
		}
		if count>2 {
			return false
		}
	}
	if isSameStr {
		return true
	}
	return count==1
}

func main() {
	fmt.Println(buddyStrings("ab","ba"))
	fmt.Println(buddyStrings("ab","ab"))
	fmt.Println(buddyStrings("aa","aa"))
	fmt.Println(buddyStrings("aaaaaaabc","aaaaaaacb"))
}


<|start_filename|>257binary-tree-paths/main.go<|end_filename|>
package main

import (
	"fmt"
)

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

type TreeNodeVal struct {
	Node *TreeNode
	Str string
}
func binaryTreePaths1(root *TreeNode) []string {
	if root == nil {
		return nil
	}
	queue := []*TreeNodeVal{&TreeNodeVal{
		Node: root,
		Str: fmt.Sprintf("%d->",root.Val),
	}}
	var res []string
	for len(queue)>0{
		node :=queue[0]
		queue= queue[1:]
		if node.Node.Left!= nil{
			queue = append(queue,&TreeNodeVal{
				Node: node.Node.Left,
				Str: fmt.Sprintf("%s%d->",node.Str,node.Node.Left.Val),
			})
		}
		if node.Node.Right!= nil{
			queue = append(queue,&TreeNodeVal{
				Node: node.Node.Right,
				Str: fmt.Sprintf("%s%d->",node.Str,node.Node.Right.Val),
			})
		}
		if node.Node.Left==nil&&node.Node.Right==nil{
			res = append(res,node.Str[:len(node.Str)-2])
		}
	}
	return res
}

func binaryTreePaths(root *TreeNode) []string {
	if root == nil {
		return nil
	}
	res := make([]string, 0)
	dfs(root, "", &res)
	return res
}

func dfs(root *TreeNode, str string, res *[]string){
	if root == nil{
		return
	}
	str = fmt.Sprintf("%s%d->",str, root.Val)
	if root.Left == nil && root.Right == nil {
		*res = append(*res, str[:len(str)-2])
		return
	}
	dfs(root.Right, str, res)
	dfs(root.Left, str, res)
}

func main() {
	one := &TreeNode{Val: 1}
	two := &TreeNode{Val: 2}
	one.Left = two
	three := &TreeNode{Val: 3}
	one.Right = three
	five := &TreeNode{Val: 5}
	two.Right = five
	fmt.Println(binaryTreePaths1(one))
}


<|start_filename|>599minimum-index-sum-of-two-lists/main.go<|end_filename|>
package main

import "fmt"

func findRestaurant(list1 []string, list2 []string) []string {
      m:= make(map[string]int)
      for i,v:=range list1{
      	 m[v] = i
	  }
	  sum := 1<<63-1
	  var res []string
	  for i,v:=range list2{
		  if j,ok:=m[v];ok {
			  if i+j<sum {
				 sum = i+j
				 res =res[:0]
				 res = append(res,v)
			  }else if i+j==sum{
				  res = append(res,v)
			  }
		  }
	  }
	  return res
}

func main() {
	l1 :=[]string{"Shogun", "Tapioca Express", "Burger King", "KFC"}
	l2 :=[]string{"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"}
	fmt.Println(findRestaurant(l1,l2))
	l11 :=[]string{"Shogun", "Tapioca Express", "Burger King", "KFC"}
	l22 :=[]string{"KFC", "Shogun", "Burger King"}
	fmt.Println(findRestaurant(l11,l22))
}


<|start_filename|>82remove-duplicates-from-sorted-list-ii/main.go<|end_filename|>
package main

type ListNode struct {
	Val int
	Next *ListNode
}

func deleteDuplicates(head *ListNode) *ListNode {
	dummyHead:=&ListNode{
		Val:  -1,
		Next: head,
	}
	pre := dummyHead
	curr := head
	count :=0
	for curr!=nil&&curr.Next!= nil{
		// 有重复一直循环
		for curr.Next!=nil&&curr.Next.Val==curr.Val{
			count ++
			curr = curr.Next
		}
		if count>0 {
			pre.Next= curr.Next
			curr = curr.Next
			count = 0
		}else {
			pre = curr
			curr = curr.Next
		}
	}
	return dummyHead.Next
}

func main() {
	
}


<|start_filename|>20biao-shi-shu-zhi-de-zi-fu-chuan-lcof/main.go<|end_filename|>
package main

import (
	"fmt"
	"unicode"
)

func isNumber(s string) bool {
	point :=0
	for i:=0;i<len(s);i++{
		if (s[i]=='+'||s[i]=='-')&&i!=0{
			return false
		} else if s[i]=='.'&&i==0 {
			return false
		} else if s[i]=='e'&&(i+1>=len(s)||!unicode.IsDigit(rune(s[i+1]))){
			return false
		} else if s[i]=='e'&&(i-1<0||!unicode.IsDigit(rune(s[i-1]))){
			return false
		} else if s[i]=='.'&&point>0{
			return false
		}else if s[i]=='.'&&point==0{
			point++
		} else if !unicode.IsDigit(rune(s[i]))&&!(
			s[i]=='e'||s[i]=='+'||s[i]=='-'){
			return false
		}
	}
	return true
}

func main() {
	fmt.Println(isNumber("."))
	fmt.Println(isNumber("e9"))
	//fmt.Println(isNumber("0123"))
	//fmt.Println(isNumber("3.1415"))
	//fmt.Println(isNumber("+100"))
	//fmt.Println(isNumber("12e"))
	//fmt.Println(isNumber("1a3.14"))
	//fmt.Println(isNumber("1.2.3"))
	//fmt.Println(isNumber("+-5"))
	//fmt.Println(isNumber("-1E-16"))
	//fmt.Println(isNumber("12e+5.4"))
}


<|start_filename|>28dui-cheng-de-er-cha-shu-lcof/main.go<|end_filename|>
package main
 type TreeNode struct {
	     Val int
	     Left *TreeNode
	     Right *TreeNode
}
func isSymmetric(root *TreeNode) bool {
	if root==nil {
		return true
	}
	return helper(root.Left,root.Right)
}

func helper(a ,b *TreeNode)bool{
	if a==nil&&b==nil {
		return true
	}
	if a==nil||b==nil {
		return false
	}

	if a.Val!=b.Val {
		return false
	}
	return helper(a.Left,b.Right)&&helper(a.Right,b.Left)
}

func main() {
	
}


<|start_filename|>32.03cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof/main.go<|end_filename|>
package main

import (
	"fmt"
	"strconv"
	"strings"
)

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func levelOrder(root *TreeNode) [][]int {
	var res [][]int
	if root == nil {
		return res
	}
	queue := []*TreeNode{root}
	var isReverse bool
	for len(queue) > 0 {
		size := len(queue)
		list := make([]int, size)
		for i := 0; i < size; i++ {
			node := queue[i]
			if !isReverse {
				list[i] = node.Val
			} else {
				list[size-i-1] = node.Val
			}
			if node.Left != nil {
				queue = append(queue, node.Left)
			}
			if node.Right != nil {
				queue = append(queue, node.Right)
			}
		}
		queue = queue[size:]
		isReverse = !isReverse
		res = append(res, list)
	}
	return res
}

func BuildBinaryTree(src string) *TreeNode {
	src = src[1 : len(src)-1]
	strList := strings.Split(src, ",")
	var currNode *TreeNode
	var root *TreeNode
	var queue []*TreeNode
	for i := 0; i < len(strList); i += 2 {
		if i == 0 {
			v, _ := strconv.Atoi(strList[i])
			currNode = &TreeNode{Val: v}
			root = currNode
			queue = append(queue, currNode)
		}
		if len(queue) > 0 {
			currNode = queue[0]
			queue = queue[1:]
		} else {
			break
		}
		if i+1 < len(strList) && strList[i+1] != "null" {
			v, _ := strconv.Atoi(strList[i+1])
			currNode.Left = &TreeNode{Val: v}
			queue = append(queue, currNode.Left)
		}
		if i+2 < len(strList) && strList[i+2] != "null" {
			v, _ := strconv.Atoi(strList[i+2])
			currNode.Right = &TreeNode{Val: v}
			queue = append(queue, currNode.Right)
		}
	}
	return root
}
func main() {
	root := BuildBinaryTree("[3,9,20,null,null,15,7]")
	fmt.Println(levelOrder(root))
}


<|start_filename|>4er-wei-shu-zu-zhong-de-cha-zhao-lcof/main.go<|end_filename|>
package main

import "fmt"

func findNumberIn2DArray(matrix [][]int, target int) bool {
	for i := 0; i < len(matrix); i++ {
		 l :=len(matrix[i])
		if l>0 &&matrix[i][0] <= target && target <= matrix[i][l-1] {
			ok := BinarySearch(matrix[i], target)
			if ok {
				return true
			}
		}
	}

	return false
}

func BinarySearch(a []int, target int) bool {
	l := 0
	r := len(a) - 1
	for l <= r {
		mid := l + (r-l)>>1
		if a[mid] == target {
			return true
		} else if a[mid] > target {
			r = mid - 1
		} else {
			l = mid + 1
		}
	}
	return false
}

//面试题04. 二维数组中的查找
func main() {
	fmt.Println(findNumberIn2DArray(
		[][]int{{1, 4, 7, 11, 15},
			{2, 5, 8, 12, 19},
			{3, 6, 9, 16, 22},
			{10, 13, 14, 17, 24},
			{18, 21, 23, 26, 30}}, 20))
	fmt.Println(findNumberIn2DArray(
		[][]int{{}}, 1))
}


<|start_filename|>560subarray-sum-equals-k/main.go<|end_filename|>
package main

import "fmt"

func subarraySum1(nums []int, k int) int {
	var count =0
	for i :=0;i<len(nums);i++{
		sum :=0
		for j:=i;j<len(nums);j++{
			sum +=nums[j]
			if sum==k{
				count++
			}
		}
	}
	return count
}

func subarraySum(nums []int, k int) int {
	m := map[int]int{0:1}
	var sum int
	var count int
	for i :=0;i<len(nums);i++{
		sum +=nums[i]
		if v,ok:=m[sum-k];ok{
			count+=v
		}
		m[sum]++
	}
	return count
}



func main() {
	fmt.Println(subarraySum([]int{1,1,1},2))
	fmt.Println(subarraySum([]int{3,4,7,2,-3,1,4,2},7))
}


<|start_filename|>709to-lower-case/main.go<|end_filename|>
package main

import (
	"fmt"
)

func toLowerCase(str string) string {
	b := []byte(str)
	for i:=0;i<len(b);i++{
		if str[i]>='A'&&str[i]<='Z'{
			b[i]=str[i]+32
		}
	}
	return string(b)
}

func main() {
	fmt.Println(toLowerCase("Hello"))
}


<|start_filename|>199binary-tree-right-side-view/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func rightSideView(root *TreeNode) []int {
	if root == nil {
		return nil
	}
	queue := make([]*TreeNode, 0)
	queue = append(queue, root)
	var res []int
	for len(queue) > 0 {
		l := len(queue)
		node := queue[l-1]
		res = append(res, node.Val)
		for i := range queue {
			currNode := queue[i]
			if currNode.Left != nil {
				queue = append(queue, currNode.Left)
			}
			if currNode.Right != nil {
				queue = append(queue, currNode.Right)

			}
		}
		queue = queue[l:]
	}
	return res
}

func main() {
	one := &TreeNode{Val: 1}
	two := &TreeNode{Val: 2}
	one.Left = two
	three := &TreeNode{Val: 3}
	one.Right = three
	//five :=&TreeNode{Val: 5}
	//two.Right = five
	four := &TreeNode{Val: 4}
	two.Left = four
	fmt.Println(rightSideView(one))

	//fmt.Println(rightSideView(one))
}


<|start_filename|>108convert-sorted-array-to-binary-search-tree/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	     Val int
	     Left *TreeNode
	     Right *TreeNode
 }


func sortedArrayToBST(nums []int) *TreeNode {
     if len(nums)==0{
     	return  nil
	 }
	 mid := len(nums)/2
	 root := &TreeNode{Val:nums[mid]}
	 root.Left = sortedArrayToBST(nums[:mid])
	 root.Right = sortedArrayToBST(nums[mid+1:])
	 return root
}

func printTreeNode(h *TreeNode){
	if h==nil{
		return
	}
	printTreeNode(h.Left)
	fmt.Println(h.Val)
	printTreeNode(h.Right)
}


func main() {
	r := sortedArrayToBST([]int{-10,-3,0,5,9})
	printTreeNode(r)
}


<|start_filename|>63gu-piao-de-zui-da-li-run-lcof/main.go<|end_filename|>
package main

import "fmt"

func Max(a,b int)int{
	if a>b{
		return a
	}
	return b
}
func maxProfit(prices []int) int {
    //dp:= make([][]int,len(prices))
    //for i:=range prices{
    //	dp[i] = make([]int,len(prices))
	//}
	max :=0
	for i:=0;i<len(prices);i++{
		for j:=i+1;j<len(prices);j++{
			if prices[j]>prices[i]  {
				max = Max(max,prices[j]-prices[i])
			}
		}
	}
   return max
}

func main() {
	fmt.Println(maxProfit([]int{7,1,5,3,6,4}))
	fmt.Println(maxProfit([]int{7,6,4,3,1}))
}


<|start_filename|>485max-consecutive-ones/main.go<|end_filename|>
package main

import (
	"math"
	"fmt"
)

func findMaxConsecutiveOnes1(nums []int) int {
	start :=0
	end :=0
	max :=0
	for end<len(nums){
		for start<len(nums)&&nums[start]==0{
			start++
		}
		end =start
		for end<len(nums)&&nums[end]==1{
			end++
		}
		max =int(math.Max(float64(max),float64(end-start)))
		start = end
	}
	return max
}


func max(a,b int)int{
	if a>b{
		return a
	}
	return b
}

func findMaxConsecutiveOnes2(nums []int) int {
	currCount :=0
	maxVal :=0
	for i:=0;i<len(nums);i++{
		if nums[i]==1{
			currCount ++
		}else {
			maxVal = max(maxVal,currCount)
			currCount =0
		}
	}
	if currCount!=0{// 如果最后几位都是1
		maxVal = max(maxVal,currCount)
	}
	return maxVal
}

func findMaxConsecutiveOnes(nums []int) int {
	left :=0
	right :=0
	maxVal :=0
	for right<len(nums){
		if nums[right]==0{
			maxVal = max(maxVal,right-left)
			left = right+1
		}
		right++
	}
	if left<right{
		maxVal = max(maxVal,right-left)
	}

	return maxVal
}

func main() {
	fmt.Println(findMaxConsecutiveOnes1([]int{1}))
	fmt.Println(findMaxConsecutiveOnes([]int{0}))
	fmt.Println(findMaxConsecutiveOnes1([]int{0,0,1}))
	fmt.Println(findMaxConsecutiveOnes([]int{1,1,0,1,1,1}))
}


<|start_filename|>22lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

func getKthFromEnd1(head *ListNode, k int) *ListNode {
	var res []*ListNode
	for head != nil {
		res = append(res, head)
		head = head.Next
	}
	l := len(res)
	if l >= k {
		return res[l-k]
	}
	return nil
}

func getKthFromEnd(head *ListNode, k int) *ListNode {
	var fast = head
	for k > 0 && fast != nil {
		fast = fast.Next
		k--
	}
	var slow = head
	for fast != nil {
		fast = fast.Next
		slow = slow.Next
	}
	return slow
}

func main() {
	one := &ListNode{Val: 1}
	two := &ListNode{Val: 2}
	one.Next = two
	three := &ListNode{Val: 3}
	two.Next = three
	four := &ListNode{Val: 4}
	three.Next = four
	five := &ListNode{Val: 5}
	four.Next = five
	r := getKthFromEnd(one, 2)
	for r != nil {
		fmt.Println(r.Val)
		r = r.Next
	}
}


<|start_filename|>665non-decreasing-array/main.go<|end_filename|>
package main

import "fmt"

func checkPossibility(nums []int) bool {
	count := 0
	for i:=0;i<len(nums)-1;i++{
		if nums[i]<nums[i+1] {
			count++
		}
		if count>1 {
			return false
		}
	}
	return  count==1
}

func main() {
	fmt.Println(checkPossibility([]int{4,2,3}))
	fmt.Println(checkPossibility([]int{4,2,1}))
	fmt.Println(checkPossibility([]int{-1,4,2,3}))
}


<|start_filename|>66gou-jian-cheng-ji-shu-zu-lcof/main.go<|end_filename|>
package main

import "fmt"

func constructArr1(a []int) []int {
	var res []int
    for i :=0;i<len(a);i++{
    	v :=1
    	for j:=0;j<len(a);j++{
    		if i==j{
    			continue
			}
			v*=a[j]
		}
		res = append(res,v)
	}
	return res
}
func constructArr2(a []int) []int {
	if len(a)<=1{
		return a
	}
	size := len(a)
	leftRes :=make([]int,size)
	leftRes[0]= 1
	for i :=1;i<size;i++{
		leftRes[i] = leftRes[i-1]*a[i-1]
	}
	rightRes :=make([]int,size)
	rightRes[size-1] =1
	for i :=size-2;i>=0;i--{
		rightRes[i] = rightRes[i+1]*a[i+1]
	}
	res := make([]int,size)
	for i := 0; i < size; i++{
		res[i] = leftRes[i] * rightRes[i]
	}
	return res
}
func constructArr(a []int) []int {
	if len(a)<=1{
		return a
	}
	size := len(a)
	res :=make([]int,size)
	mutVal :=1
	for i :=0;i<size;i++{
		res[i] = mutVal // 先乘左边的数(不包括自己)
		mutVal *=a[i]
	}
	mutVal =1
	for i :=size-1;i>=0;i--{
		res[i] *= mutVal //再乘右边的数(不包括自己)
		mutVal *=a[i]
	}
	return res
}

func main() {
	fmt.Println(constructArr([]int{1,2,3,4,5}))
	fmt.Println(constructArr([]int{1,2,0,4,5}))
}


<|start_filename|>1323maximum-69-number/main.go<|end_filename|>
package main

import "fmt"

func maximum69Number(num int) int {
	var nums []int
	for num != 0 {
		nums = append(nums, num%10)
		num = num / 10
	}
	var isReplace bool
	var res int
	for i := len(nums)-1; i >=0; i-- {
		if !isReplace && nums[i] == 6 {
			nums[i] = 9
			isReplace = true
		}
		res = res*10 + nums[i]
	}
	return res
}

func main() {
	fmt.Println(maximum69Number(669))//969
	fmt.Println(maximum69Number(9669))//9969
	fmt.Println(maximum69Number(9996))//9999
	fmt.Println(maximum69Number(9999))//9999
}


<|start_filename|>6cong-wei-dao-tou-da-yin-lian-biao-lcof/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

func reversePrint(head *ListNode) []int {
    stack :=make([]*ListNode,0)
    for head!=nil{
    	stack = append(stack,head)
    	head =head.Next
	}
	var res []int
	for len(stack)>0{
		node :=stack[len(stack)-1]
		res = append(res,node.Val)
		stack = stack[:len(stack)-1]
	}
	return res
}

func main() {
	one :=&ListNode{Val:1}
	three :=&ListNode{Val:3}
	one.Next = three
	two :=&ListNode{Val:2}
	three.Next=two
	fmt.Println(reversePrint(one))
}


<|start_filename|>32.02cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func levelOrder(root *TreeNode) [][]int {
	if root == nil {
		return nil
	}
	nodes := make([]*TreeNode, 0)
	nodes = append(nodes, root)
	var res [][]int
	for len(nodes) > 0 {
		var nodeList []*TreeNode
		var values []int
		for i := 0; i < len(nodes); i++ {
			node := nodes[i]
			if node.Left != nil {
				nodeList = append(nodeList, node.Left)
			}
			if node.Right != nil {
				nodeList = append(nodeList, node.Right)
			}
			values = append(values, node.Val)
		}
		if len(values) > 0 {
			res = append(res, values, )
		}
		nodes = append(nodes[len(nodes):], nodeList...)
	}
	return res
}
func main() {
	three := &TreeNode{Val: 3}
	nine := &TreeNode{Val: 9}
	three.Left = nine
	twenty := &TreeNode{Val: 20}
	three.Right = twenty
	fifteen := &TreeNode{Val: 15}
	twenty.Left = fifteen
	severn := &TreeNode{Val: 7}
	twenty.Right = severn
	fmt.Println(levelOrder(three))
}


<|start_filename|>8string-to-integer-atoi/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
	"strings"
)

func myAtoi(str string) int {
	//去掉收尾空格
	str = strings.TrimSpace(str)
	result := 0
	sign := 1
	for i, v := range str {
		if v >= '0' && v <= '9' {
			result = result*10 + int(v-'0')
		} else if v == '-' && i==0 {
			sign = -1
		} else if v == '+' && i==0 {
			sign = 1

		} else {
			break
		}
       // 数值最大检测
		if result > math.MaxInt32 {
			if sign == -1 {
				return math.MinInt32
			}
			return math.MaxInt32
		}
	}
	return sign * result
}

func main() {
	fmt.Println(myAtoi("42"))
	fmt.Println(myAtoi(   " -42"))
	fmt.Println(myAtoi("4193 with words"))//4193
	fmt.Println(myAtoi("words and 987"))//0
}


<|start_filename|>344reverse-string/main.go<|end_filename|>
package main

import "fmt"

func reverseString(s []byte)  {
    l :=0
    r :=len(s)-1
    for l<r{
    	s[l],s[r] = s[r],s[l]
    	l++
    	r--
	}
}
func main() {
	s := []byte{'h','e','l','l','o'}
	fmt.Println(string(s))
	reverseString(s)
	fmt.Println(string(s))
}


<|start_filename|>7reverse-integer/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
)

func reverse1(x int) int {
   var stack []int
   for x!=0{
   	   r :=x%10
   	   stack = append(stack,r)
   	   x =x/10
   }
   var r int
   index :=0
   for len(stack)>0{
   	   top :=stack[len(stack)-1]
   	   stack = stack[:len(stack)-1]
	   r = int(math.Pow10(index))*top+r
   	   index++
   }
   // 边界
	if !( -(1<<31) <= r && r <= (1<<31)-1) {
		return 0
	}
   return r
}

func reverse(x int) int {

	var y =0
	for x!=0{
		y =y*10+x%10
		if !( -(1<<31) <= y && y <= (1<<31)-1) {
			return 0
		}
		x =x/10
	}
	return y
}
func main() {
	fmt.Println(reverse(-1234))
	fmt.Println(reverse(120))
	fmt.Println(reverse(1534236469))
}


<|start_filename|>1313decompress-run-length-encoded-list/main.go<|end_filename|>
package main

import "fmt"

func decompressRLElist(nums []int) []int {
	 var res []int
     for i:=0;i<len(nums);i=i+2{
     	 for j:=0;j<nums[i];j++{
     	 	res = append(res,nums[i+1])
		 }
	 }
	 return res
}


func main() {
	fmt.Println(decompressRLElist([]int{1,2,3,4}))
}


<|start_filename|>150evaluate-reverse-polish-notation/main.go<|end_filename|>
package main

import (
	"fmt"
	"strconv"
)

func evalRPN(tokens []string) int {
    stack :=make([]int,0)
    for i:=0;i<len(tokens);i++{
		if tokens[i]=="+" {
			f :=stack[len(stack)-1]
			s:= stack[len(stack)-2]
			stack =stack[:len(stack)-2]
			stack = append(stack,f+s)
		}else if tokens[i]=="-" {
			f :=stack[len(stack)-1]
			s:= stack[len(stack)-2]
			stack =stack[:len(stack)-2]
			stack = append(stack,s-f)
		}else if tokens[i]=="*" {
			f :=stack[len(stack)-1]
			s:= stack[len(stack)-2]
			stack =stack[:len(stack)-2]
			stack = append(stack,s*f)
		}else if tokens[i]=="/" {
			f :=stack[len(stack)-1]
			s:= stack[len(stack)-2]
			stack =stack[:len(stack)-2]
			stack = append(stack,s/f)
		}else {
		    s,_:= strconv.Atoi(tokens[i])
			stack = append(stack,s)
		}
	}
    return stack[0]
}


func main() {
	fmt.Println(evalRPN([]string{"2", "1", "+", "3", "*"}))
	fmt.Println(evalRPN([]string{"4", "13", "5", "/", "+"}))
	fmt.Println(evalRPN([]string{"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"}))
}


<|start_filename|>5ti-huan-kong-ge-lcof/main.go<|end_filename|>
package main

import (
	"fmt"
	"strings"
)

func replaceSpace(s string) string {
	var res strings.Builder
    for i:=range s{
    	if s[i]==' '{
			res.WriteString("%20")
		}else {
			res.WriteByte(s[i])
		}
	}
	return res.String()
}


func main() {
	fmt.Println(replaceSpace("We are happy."))
}


<|start_filename|>0utils/tree/sortlist.go<|end_filename|>
package tree


func buildTree(nums []int, l int, r int) *TreeNode {
	if l > r {
		return nil
	}
	mid := (l + r) >> 1
	node := &TreeNode{Val:nums[mid]}
	node.Left = buildTree(nums, l, mid-1)
	node.Right = buildTree(nums, mid+1, r)
	return node
}

func sortedArrayToBST(nums []int) *TreeNode {
	return buildTree(nums, 0, len(nums)-1)
}

<|start_filename|>905sort-array-by-parity/main.go<|end_filename|>
package main

import "fmt"

func sortArrayByParity(A []int) []int {
    l:=0
    r :=len(A)-1
    for l<r{
    	if A[l]%2!=0&&A[r]%2==0{
    		A[l],A[r] = A[r],A[l]
			l++
			r--
		}else if A[l]%2==0{
			l++
		}else {
			r--
		}
	}
	return A
}
func main() {
	fmt.Println(sortArrayByParity([]int{3,1,2,4}))
	fmt.Println(sortArrayByParity([]int{2,3,1,4,5}))
}


<|start_filename|>169majority-element/main.go<|end_filename|>
package main

import "fmt"

func majorityElement(nums []int) int {
    m := make(map[int]int)
    max :=0
    num :=nums[0]
    for _,v:=range nums{
    	m[v]++
		if max<m[v] {
			max = m[v]
			num= v
		}
	}
	fmt.Println(m)
	return num
}

func main() {
	//fmt.Println(majorityElement([]int{3,2,3}))
	//fmt.Println(majorityElement([]int{2,2,1,1,1,2,2}))
	fmt.Println(majorityElement([]int{3,3,4}))
}


<|start_filename|>66plus-one/main.go<|end_filename|>
package main

import "fmt"

func plusOne(digits []int) []int {
   for i := len(digits)-1;i>=0;i--{
	   digits[i] = (digits[i]+1)%10
	   if digits[i]!=0{// 没有产生进位 直接返回就行
	   	return digits
	   }
   }
   return append([]int{1},digits...)
}


func main() {
	fmt.Println(plusOne([]int{1,2,3}))
	fmt.Println(plusOne([]int{9,9,9,9}))
}


<|start_filename|>94binary-tree-inorder-traversal/main.go<|end_filename|>
package main

// 中序遍历
type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}
// 左中右
func inorderTraversal(root *TreeNode) []int {
	var l []int
	//如果是空直接返回
	if root == nil {
		return nil
	}
	// 创建栈 递归从根源就是栈结构
	stack := make([]*TreeNode, 0)
	node := root
	// 只要当前节点不为空，或者栈不为空，就一直循环查找左节点
	for node != nil || len(stack) > 0 {
		//查找左节点
		if node != nil {
			// 将左节点放入栈中
			stack = append(stack, node)
			node = node.Left
		} else {
			// 获取站顶节点
			curr := stack[len(stack)-1]
			// 出栈操作
			stack = stack[:len(stack)-1]
			l = append(l, curr.Val)
			// 遍历右节点
			node = curr.Right
		}
	}
	return l
}





func main() {
	a := &TreeNode{
		Val: 1,
	}
	b := &TreeNode{
		Val: 2,
	}
	a.Left = b
	c := &TreeNode{
		Val: 3,
	}
	a.Right = c
	d := &TreeNode{
		Val: 4,
	}
	b.Left = d
	e := &TreeNode{
		Val: 5,
	}
	b.Right = e
	f := &TreeNode{
		Val: 6,
	}
	c.Left = f
	// 4,2,5,1,6,3
	inorderTraversal(a)
}


<|start_filename|>145binary-tree-postorder-traversal/main.go<|end_filename|>
package main

import "fmt"

// 后续遍历 https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func postorderTraversal(root *TreeNode) []int {
	var list []int
	if root == nil {
		return list
	}
	stack1 := make([]*TreeNode,0)
	stack2 := make([]*TreeNode,0)
	stack1 = append(stack1,root)
	for len(stack1)>0{
		curr := stack1[len(stack1)-1]
		fmt.Println(curr.Val)
		//list = append(list,curr.Val)
		stack1 = stack1[:len(stack1)-1]
		stack2 = append(stack2,curr)
		if curr.Left!= nil{
			stack1 = append(stack1,curr.Left)
		}
		if curr.Right!= nil{
			stack1 = append(stack1,curr.Right)
		}
	}
	for len(stack2)>0 {
		curr := stack2[len(stack2)-1]
		stack2 = stack2[:len(stack2)-1]
		list = append(list,curr.Val)
	}
	return list
}
func postorderTraversal1(root *TreeNode) []int {
	if root==nil{
		return nil
	}
	postorderTraversal1(root.Left)
	postorderTraversal1(root.Right)
	fmt.Println(root.Val)
	return nil
}

func postorderTraversal2(root *TreeNode) []int {
	var result []int
	if root==nil {
		return result
	}
	stack :=make([]*TreeNode,0)
	curr := root
	var lastNodeVistited *TreeNode //保存上一个遍历过的节点TreeNode  = null; //保存上一个遍历过的节点
	for curr!= nil||len(stack)>0{
		// 把左孩子押入栈 ，到叶子节点为止
		if curr != nil {
			stack = append(stack,curr)
			curr = curr.Left
		}else {
			top := stack[len(stack)-1]
			////如果栈顶节点没有右孩子，或者已经遍历过了它的右孩子，就遍历该节点
			if top.Right==nil||lastNodeVistited == top.Right {
				result = append(result,top.Val)
				lastNodeVistited = top
				stack =stack[:len(stack)-1]
			}else {
				curr = top.Right////否则，准备将栈顶节点的右孩子入栈
			}
		}
	}
	return result
}

func main() {
	a := &TreeNode{
		Val: 1,
	}
	b := &TreeNode{
		Val: 2,
	}
	a.Left = b
	c := &TreeNode{
		Val: 3,
	}
	a.Right = c
	d := &TreeNode{
		Val: 4,
	}
	b.Left = d
	e := &TreeNode{
		Val: 5,
	}
	b.Right = e
	f := &TreeNode{
		Val: 6,
	}
	c.Left = f
	// 4 5 2 6 3 1
	fmt.Println(postorderTraversal2(a))
}


<|start_filename|>98validate-binary-search-tree/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
)

type TreeNode struct {
	    Val int
	    Left *TreeNode
	    Right *TreeNode
 }

func isValidBST1(root *TreeNode) bool {
	if root==nil{
		return true
	}
	stack := make([]*TreeNode,0)
	preVal :=math.MinInt64
	for root!=nil||len(stack)>0{
		for root!=nil{
			stack = append(stack,root)
			root =root.Left
		}
		node := stack[len(stack)-1]
		stack =stack[:len(stack)-1]
		if node.Val<=preVal {
			return false
		}
		preVal = node.Val
		root =node.Right
	}
	return true
}

func isValidBST(root *TreeNode) bool {
	if root==nil{
		return true
	}
	return helper(root,math.MinInt64,math.MaxInt64)
}

func helper(root *TreeNode,left,right int)bool{
	if root==nil{
		return true
	}
	if root.Val<=left||root.Val>=right{
		return false
	}
	return helper(root.Left,left,root.Val)&&helper(root.Right,root.Val,right)
}



func main() {
	t :=&TreeNode{Val: 1}
	one :=&TreeNode{Val: 1}
	//three :=&TreeNode{Val: 3}
	t.Left = one
	//t.Right = three
	fmt.Println(isValidBST(t))
}


<|start_filename|>3.04implement-queue-using-stacks-lcci/main.go<|end_filename|>
package main
type MyQueue struct {
     list []int
}


/** Initialize your data structure here. */
func Constructor() MyQueue {
   return MyQueue{list:make([]int,0)}
}


/** Push element x to the back of queue. */
func (this *MyQueue) Push(x int)  {
    this.list = append(this.list,x)
}


/** Removes the element from in front of queue and returns that element. */
func (this *MyQueue) Pop() int {
	v :=this.list[0]
	this.list = this.list[1:]
	return v
}


/** Get the front element. */
func (this *MyQueue) Peek() int {
    return this.list[0]
}


/** Returns whether the queue is empty. */
func (this *MyQueue) Empty() bool {
    return len(this.list)==0
}
func main() {
	
}


<|start_filename|>1122relative-sort-array/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func relativeSortArray(arr1 []int, arr2 []int) []int {
    m :=make(map[int][]int)
    for _,v :=range  arr2{
    	m[v]= make([]int,0)
	}
	var other []int
	for _,v:=range arr1{
		if l,ok:=m[v];ok{
			l = append(l,v)
			m[v]= l
		}else {
			other = append(other,v)
		}
	}
	sort.Ints(other)
	var res []int
	for _,v :=range  arr2{
		res = append(res,m[v]...)
	}
	res = append(res,other...)
	return res
}

func main() {
	fmt.Println(relativeSortArray([]int{2,3,1,3,2,4,6,7,9,2,19},
	[]int{2,1,4,3,9,6}))
}


<|start_filename|>179largest-number/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
	"strconv"
	"strings"
)
type StrList []string
func(s StrList)Len()int {
	return len(s)
}
func (s StrList)Less(i,j int)bool{
	return  s[i]+s[j]>s[j]+s[i]
}
func(s StrList)Swap(i,j int){
	s[i],s[j] = s[j],s[i]
}

func largestNumber(nums []int) string {
	strs :=make(StrList,len(nums))
	for i,v:=range nums{
       strs[i] = strconv.Itoa(v)
	}
	sort.Slice(strs, func(i ,j int)bool {
		return strs[i]+strs[j]>strs[j]+strs[i]
	})
	s := strings.Join(strs,"")
	if s[0]=='0' {
		return "0"
	}
	return s
}
func main() {
	fmt.Println(largestNumber([]int{3,30,34,5,9}))
	fmt.Println(largestNumber([]int{0,0}))
}


<|start_filename|>5.07exchange-lcci/main.go<|end_filename|>
package main

import (
	"fmt"
)

func exchangeBits1(num int) int {
	//0xaaaaaaaa 10101010101010101010101010101010 (偶数位为1，奇数位为0）
	 even := (num & 0xaaaaaaaa) >> 1 // 获取当前数字的偶数位，然后向右移动一位
	 //0x55555555  1010101010101010101010101010101 (偶数位为0，奇数位为1）
	 odd := (num & 0x55555555) << 1 // 获取当前位数的奇数 然后向左移动一位
	return even + odd
}


func exchangeBits(num int) int {
	//0xaaaaaaaa 10101010101010101010101010101010 (偶数位为1，奇数位为0）
	even := (num & 0b10101010_10101010_10101010_10101010) >> 1 // 获取当前数字的偶数位，然后向右移动一位
	//0x55555555  1010101010101010101010101010101 (偶数位为0，奇数位为1）
	odd := (num & 0b01010101_01010101_01010101_01010101) << 1 // 获取当前位数的奇数 然后向左移动一位
	return even | odd
}


func main() {
	fmt.Println(exchangeBits(1))
	fmt.Println(exchangeBits(2))
	fmt.Println(exchangeBits(3))
	fmt.Println(exchangeBits(4))
}


<|start_filename|>112path-sum/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}
//递归解决思路
func hasPathSum(root *TreeNode, sum int) bool {
	if root == nil{
		return false
	}
	// 当前是叶子节点 判断当前是否是等于目标值
	if root.Left==nil&&root.Right==nil{
		return root.Val==sum
	}
	return hasPathSum(root.Left,sum-root.Val)||hasPathSum(root.Right,sum-root.Val)
}


func hasPathSum1(root *TreeNode, sum int) bool {
	if root == nil{
		return false
	}
	stack := make([]*TreeNode,0)
	vals := make([]int,0)
	stack = append(stack,root)
	vals = append(vals,sum)
	for len(stack)>0{
		curr :=stack[len(stack)-1]
		stack = stack[:len(stack)-1]//出栈
		val :=vals[len(vals)-1]
		vals = vals[:len(vals)-1] // 出栈
		if curr.Left==nil&&curr.Right==nil&&val ==curr.Val {// 如果当前节点已经为叶子节点并且节点上的数字等于当前和s,则返回true
			return true
		}
		if curr.Left!=nil {
			stack = append(stack,curr.Left)
			vals = append(vals,val-curr.Val)
		}
		if curr.Right!=nil {
			stack = append(stack,curr.Right)
			vals = append(vals,val-curr.Val)
		}
	}
	return false
}

//https://leetcode-cn.com/problems/path-sum/
func main() {
	a := &TreeNode{
		Val: 5,
	}
	b := &TreeNode{
		Val: 4,
	}
	a.Left = b
	c := &TreeNode{
		Val: 8,
	}
	a.Right = c
	d := &TreeNode{
		Val: 11,
	}
	b.Left = d
	e := &TreeNode{
		Val: 7,
	}
	d.Left = e
	f := &TreeNode{
		Val: 2,
	}
	d.Right = f
	// 4,2,5,1,6,3
	fmt.Println(hasPathSum1(a,21))
	fmt.Println(hasPathSum1(a,22))
}


<|start_filename|>26remove-duplicates-from-sorted-array/main.go<|end_filename|>
package main

import "fmt"

func removeDuplicates2(nums []int) int {
	 m := make(map[int]struct{})
	 index :=0
     for _,v:=range nums{
     	if _,ok:=m[v];!ok{
     		m[v] = struct{}{}
			nums[index]=v
     		index++
		 }
	 }
	 nums = nums[:index]
	 return len(m)
}
//快慢指针
func removeDuplicates1(nums []int) int {
	 slow := 0//慢指针
	 //快指针变 块指针便利数组
	for quick :=1; quick<len(nums) ;quick++  {
		if nums[slow]!=nums[quick] {
			slow++// 只有当前值和快指针不相同
			nums[slow]=nums[quick]
		}
	}
	return slow +1
}
func removeDuplicates(nums []int) int {
	for i:=len(nums)-1;i>0;i--{
		if nums[i]==nums[i-1]{
			nums = append(nums[:i],nums[i+1:]...)
		}
	}

	return len(nums)
}

func main() {
	l := []int{1,1,2}
	r := removeDuplicates(l)
	for i:=0;i<r;i++{
		fmt.Println(l[i])
	}
}


<|start_filename|>52liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/main.go<|end_filename|>
package main

import "unsafe"

type ListNode struct {
	Val  int
	Next *ListNode
}

func getIntersectionNode(headA, headB *ListNode) *ListNode {

	  m := make(map[uintptr]struct{})
     for headA!=nil{
     	 m[uintptr(unsafe.Pointer(headA))]= struct{}{}
     	 headA = headA.Next
	 }
	 for headB!=nil{
	 	 if _,ok:=m[uintptr(unsafe.Pointer(headB))];ok{
	 	 	return headB
		 }
		 headB = headB.Next
	 }
	 return nil
}


func main() {
	
}


<|start_filename|>876middle-of-the-linked-list/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
	     Val int
	     Next *ListNode
	 }

func middleNode1(head *ListNode) *ListNode {
	var res []*ListNode
    for head!=nil{
    	res =append(res,head)
    	head = head.Next
	}
	return res[len(res)/2]
}

func middleNode(head *ListNode) *ListNode {
	slow :=head
	fast :=head
	for fast.Next!=nil{
		slow = slow.Next
		fast = fast.Next.Next
	}
	return slow
}


func main() {
	one :=&ListNode{Val:  1}
	two :=&ListNode{Val:  2}
	one.Next=two
	three :=&ListNode{Val:  3}
	two.Next=three
	four :=&ListNode{Val:  4}
	three.Next=four
	five :=&ListNode{Val:  5}
	four.Next=five
	//six :=&ListNode{Val:  6}
	//five.Next=six
    fmt.Println(middleNode(one).Val)
}


<|start_filename|>922sort-array-by-parity-ii/main.go<|end_filename|>
package main

import "fmt"

func sortArrayByParityII(A []int) []int {
	   j :=1
	  for i:=0;i<len(A);i=i+2{
		  if (A[i]&1)!=0 {// 偶数位查找不是偶数的
			  for A[j]%2!=0{// 奇数查找不是奇数的
				  j=j+2
			  }
			  A[i],A[j]=A[j],A[i]
		  }
	  }
	  return A
}


func main() {
	fmt.Println(sortArrayByParityII([]int{2,3,1,1,4,0,0,4,3,3}))
	fmt.Println(sortArrayByParityII([]int{4,2,5,7}))
}


<|start_filename|>57he-wei-sde-liang-ge-shu-zi-lcof/main.go<|end_filename|>
package main

import "fmt"

func twoSum(nums []int, target int) []int {
	m := make(map[int]struct{})
    for _,v :=range nums{
    	if _,ok:=m[target-v];ok{
    		return []int{target-v,v}
		}else {
			m[v]= struct{}{}
		}
	}
	return nil
}

func twoSum1(nums []int, target int) []int {
	left :=0
	right :=len(nums)-1
	for left<right{
		 sum := nums[left]+nums[right]
		if sum==target{
			return []int{nums[left],nums[right]}
		}else if sum>target{
			right--
		}else {
			left++
		}
	}
	return nil
}



func main() {
	fmt.Println(twoSum([]int{2,7,11,15},9))
	fmt.Println(twoSum([]int{16,16,18,24,30,32},48))
}


<|start_filename|>1380lucky-numbers-in-a-matrix/main.go<|end_filename|>
package main

import "fmt"

func luckyNumbers (matrix [][]int) []int {
	//// 为了减少重复计算 k 是列坐标 v 行坐标 表示当前列最大值的行坐标
    m :=make(map[int]int)
	for i:=0;i<len(matrix);i++{
		var min =matrix[i][0]
		var c = 0
		// 计算每行最小的列坐标点
		for j:=1;j<len(matrix[i]);j++{
            if min>matrix[i][j]{
            	min = matrix[i][j]
            	c = j
			}
		}
		//如果已经计算过当前列， 看看是否相等
		if v,ok:=m[c];ok{//
			if v==i{
				 return []int{matrix[i][c]}
			}else {
				continue
			}
		}
		//  已经知道当前行最小值的列坐标，计算当前列最大值行坐标信息
		var max =matrix[0][c]
		var r = 0
		//求列最大的行坐标值
		for k:=1;k<len(matrix);k++{
			if max<matrix[k][c]{
				max = matrix[k][c]
				r = k
			}
		}
		if i==r{// 判断是否相等
			return []int{matrix[i][c]}
		}else {
			m[c]=r //记录当前列最大的行坐标
		}
	}
	return nil
}

func main() {
	fmt.Println(luckyNumbers([][]int{{3,7,8},{9,11,13},{15,16,17}}))
	fmt.Println(luckyNumbers([][]int{{1,10,4,2},{9,3,8,7},{15,16,17,12}}))
	fmt.Println(luckyNumbers([][]int{{7,8},{1,2}}))

}


<|start_filename|>39combination-sum/main.go<|end_filename|>
package main

import "fmt"

func combinationSum(candidates []int, target int) [][]int {
  res := make([][]int,0)
  path := make([]int,0)
  dfs(candidates,target,path,&res,0)
  return res
}

func dfs(candidates []int, target int,path[]int,res*[][]int,idx int){
	if target==0 {
		*res=append(*res,append([]int{},path...))
	}
	for i:=idx;i<len(candidates);i++{
		if target-candidates[i]>=0 {
			path = append(path,candidates[i])
			dfs(candidates,target-candidates[i],path,res,i)
			path = path[:len(path)-1]
		}
	}
}

func main() {
	fmt.Println(combinationSum([]int{2,3,6,7},7))
}


<|start_filename|>114flatten-binary-tree-to-linked-list/main.go<|end_filename|>
package main

type TreeNode struct {
	v int
	left *TreeNode
	right *TreeNode
}

func flatten(root *TreeNode)  {
	if root== nil{
		return
	}
	stack :=[]*TreeNode{root}
	var preNode *TreeNode
	for len(stack)>0{
		node := stack[len(stack)-1]
		stack = stack[:len(stack)-1]
		preNode.left = nil
		preNode.right = node
		if node.right !=nil{
			stack = append(stack,node.right)
		}
		if node.left !=nil{
			stack = append(stack,node.left)
		}
		preNode = preNode.right
	}
}

func main() {
	one :=&TreeNode{v:1}
	two :=&TreeNode{v:2}
	one.left = two
	three :=&TreeNode{v:3}
	two.left = three
	four :=&TreeNode{v:4}
	two.right = four
	five :=&TreeNode{v:5}
	one.right = five
	six :=&TreeNode{v:6}
	five.right= six

	flatten(one)


}


<|start_filename|>680valid-palindrome-ii/main.go<|end_filename|>
package main

import "fmt"

func validPalindrome(s string) bool {
	left :=0
	right :=len(s)-1
	for left<right{
		if s[left]!=s[right]{
			return isPalindrome(s[left:right])||isPalindrome(s[left+1:right+1])
		}
		left++
		right--
	}
	return true
}

func isPalindrome(s string)bool{
	left :=0
	right :=len(s)-1
	for left<right{
		if s[left]!=s[right]{
			return false
		}
		left++
		right--
	}
	return true
}

func palindromeHelp(s string,isDelete bool)bool{
	left :=0
	right :=len(s)-1
	for left<right{
		if s[left]!=s[right]{
			if isDelete{
				return palindromeHelp(s[left:right],false)||palindromeHelp(s[left+1:right+1],false)
			}else {
				return false
			}
		}
		left++
		right--
	}
	return true
}

func main() {
	fmt.Println(validPalindrome("tebbem"))//f
	fmt.Println(validPalindrome("aba")) //t
	fmt.Println(validPalindrome("abca")) //t
	fmt.Println(validPalindrome("abc")) //f
	fmt.Println(validPalindrome("cdbeeeabddddbaeedebdc"))//t
	fmt.Println(validPalindrome("aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuculmgmqfvnbgtapekouga"))//t
}


<|start_filename|>107binary-tree-level-order-traversal-ii/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func levelOrderBottom(root *TreeNode) [][]int {
	var res [][]int
    if root==nil{
    	return res
	}
	queue := []*TreeNode{root}
	for len(queue)>0{
		l :=len(queue)
		list := make([]int,0)
		for i:=0;i<l;i++{
			node :=queue[i]
			list = append(list,node.Val)
			if node.Left!=nil{
				queue = append(queue,node.Left)
			}
			if node.Right!=nil{
				queue = append(queue,node.Right)
			}
		}
		res = append([][]int{list},res...)
		queue =queue[l:]
	}
	return res
}


func main() {
	three :=&TreeNode{Val: 3}
	nine :=&TreeNode{Val: 9}
	twenty :=&TreeNode{Val: 20}
	three.Left = nine
	three.Right = twenty
	fifteen :=&TreeNode{Val: 15}
	seven :=&TreeNode{Val: 7}
	twenty.Left = fifteen
	twenty.Right = seven
	fmt.Println(levelOrderBottom(three))
}


<|start_filename|>55er-cha-shu-de-shen-du-lcof/main.go<|end_filename|>
package main

type TreeNode struct {
	     Val int
	    Left *TreeNode
	     Right *TreeNode
}

func Max(a,b int)int  {
	if a>b{
		return a
	}
	return b
}

func maxDepth(root *TreeNode) int {
	if root==nil{
		return 0
	}
	lHeight := maxDepth(root.Left)
	rHeight := maxDepth(root.Right)
	return Max(lHeight,rHeight)+1
}
func maxDepth1(root *TreeNode) int {
	if root==nil{
		return 0
	}
	ques := make([]*TreeNode,0)
	ques = append(ques,root)
	height :=0
	for len(ques)!=0{
		l :=len(ques)
		for i:=0;i<l;i++{
			node :=ques[i]
			if node.Left!=nil {
				ques = append(ques,node.Left)
			}
			if node.Right!=nil {
				ques = append(ques,node.Right)
			}
		}
		ques =ques[l:]
		height++
	}
	return height
}

func main() {
	
}


<|start_filename|>62yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof/main.go<|end_filename|>
package main

import (
	"container/list"
	"fmt"
)


func lastRemaining(n int, m int) int {
	l:= list.New()
	for i:=0;i<n;i++{
		l.PushBack(i)
	}
	i :=0
	for l.Len()>1{
		for i=0;i<m;i++{
			e :=l.Front()
			if i !=m-1 {
				l.MoveToBack(e)
			}else {
				l.Remove(e)
			}
		}
	}
	v := l.Front().Value.(int)
	return v
}
func main() {
	fmt.Println(lastRemaining(5,3))
	fmt.Println(lastRemaining(10,17))
	fmt.Println(lastRemaining(70866,116922))
}


<|start_filename|>100same-tree/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	     Val int
	     Left *TreeNode
	     Right *TreeNode
 }
func isSameTree(p *TreeNode, q *TreeNode) bool {
     if p==nil&&q==nil{
     	return  true
	 }
	 if p!=nil&&q==nil||p==nil&&q!=nil||p.Val!=q.Val{
	 	return false
	 }
	 return isSameTree(p.Left,q.Left)&&isSameTree(p.Right,q.Right)
}


func isSameTree1(p *TreeNode, q *TreeNode) bool {
	if p==nil&&q==nil{
		return  true
	}
	if p!=nil&&q==nil||p==nil&&q!=nil||p.Val!=q.Val{
		return false
	}
	queue :=make([]*TreeNode,0)
	queue = append(queue,p)
	queue = append(queue,q)
	for len(queue)>0{
		pNode :=queue[0]
		qNode :=queue[1]

		if pNode.Val!=qNode.Val{
			return false
		}
		if pNode.Left!=nil&&qNode.Left!=nil {
			queue = append(queue,pNode.Left)
			queue = append(queue,qNode.Left)
		} else if pNode.Left==nil&&qNode.Left!=nil {
			return false
		}else if pNode.Left!=nil&&qNode.Left==nil {
			return false
		}
		if pNode.Right!=nil&&qNode.Right!=nil {
			queue = append(queue,pNode.Right)
			queue = append(queue,qNode.Right)
		}else if pNode.Right==nil&&qNode.Right!=nil {
			return false
		}else if pNode.Right!=nil&&qNode.Right==nil {
			return false
		}
		queue =queue[2:]
	}
	return true
}



func main() {
	t11 := &TreeNode{Val: 1}
	t12 := &TreeNode{Val: 2}
	t11.Left = t12
	t13 := &TreeNode{Val: 1}
	t11.Right = t13

	t21 := &TreeNode{Val: 1}
	t22 := &TreeNode{Val: 1}
	t21.Left = t22
	t23 := &TreeNode{Val: 2}
	t21.Right = t23


	fmt.Println(isSameTree1(t11,t21))
}


<|start_filename|>1431kids-with-the-greatest-number-of-candies/main.go<|end_filename|>
package main

import "fmt"

func kidsWithCandies(candies []int, extraCandies int) []bool {
	max := candies[0]
	for i := 1; i < len(candies); i++ {
		if max < candies[i] {
			max = candies[i]
		}
	}
	res := make([]bool, len(candies))
	for i := 0; i < len(candies); i++ {
		// 当前拥有的糖果数加上额外的糖果数大于等于最大值就可以满足
		if candies[i]+extraCandies >= max {
			res[i] = true
		}
	}
	return res
}

func main() {
	fmt.Println(kidsWithCandies([]int{2,3,5,1,3},3))
    fmt.Println(kidsWithCandies([]int{4,2,1,1,},1))
}


<|start_filename|>1137n-th-tribonacci-number/main.go<|end_filename|>
package main

import "fmt"

func tribonacci(n int) int {
	if n<2 {
		return n
	}
    dp:=make([]int,n+1)
    dp[0]=0
    dp[1]=1
    dp[2]=1
    for i:=3;i<=n;i++{
    	dp[i] = dp[i-3]+dp[i-2]+dp[i-1]
	}
	return dp[n]
}

func main() {
	fmt.Println(tribonacci(3))
	fmt.Println(tribonacci(4))
	fmt.Println(tribonacci(5))
	fmt.Println(tribonacci(25))
}


<|start_filename|>13roman-to-integer/main.go<|end_filename|>
package main

import "fmt"

func romanToInt(s string) int {
   m :=map[string]int{"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
   temp :=0
   var result int
   for i:=len(s)-1;i>=0;i--{
   	   num := m[string(s[i])]
	   //'IV' 4
	   if num<temp&&(num==1||num==10||num==100) {
		   result -=num
	   }else {
	   	   result +=num
	   }
	   temp = num
   }
   return result
}

func main() {
	fmt.Println(romanToInt("IV"))
	fmt.Println(romanToInt("MCMXCIV"))
}


<|start_filename|>19remove-nth-node-from-end-of-list/main.go<|end_filename|>
package main

import "fmt"
type ListNode struct {
     Val int
     Next *ListNode
 }

func removeNthFromEnd(head *ListNode, n int) *ListNode {
      //dump node
      d := &ListNode{Next:head}
      len :=0
      first := head
      for first!=nil{
      	first = first.Next
      	len++
	  }
      len =len-n// revert index
      first = d
      for len >0{
      	len--
      	first = first.Next
	  }
      first.Next = first.Next.Next
      return d.Next
}

func removeNthFromEnd1(head *ListNode, n int) *ListNode {
	//dump node
	dummyNode := &ListNode{}// 创建哨兵节点
	dummyNode.Next = head // 将哨兵节点指向头结点
	curr := dummyNode // 从哨兵节点开始存储 可以保证n=len(list)正数第一个节点
	var list []*ListNode // 遍历数组存储
	for curr!=nil{
		list = append(list,curr)
		curr = curr.Next
	}
	pre := list[len(list)-n-1]
	curr = list[len(list)-n]
	pre.Next = curr.Next // 去掉当前节点
	return dummyNode.Next
}

func removeNthFromEnd2(head *ListNode, n int) *ListNode {
	//dump node
	dummyNode := &ListNode{}// 创建哨兵节点
	dummyNode.Next = head // 将哨兵节点指向头结点
	var fast = head
	for i:=0;i<n;i++{
		fast =fast.Next
	}
	var slow = head
	var pro = dummyNode
	for fast!=nil{
		pro = slow
		slow= slow.Next
		fast = fast.Next
	}
	pro.Next = slow.Next
	return dummyNode.Next
}





func main() {
	head := &ListNode{Val:1}
	head.Next = &ListNode{Val:2}
	node := head.Next
	node.Next = &ListNode{Val:3}
	node = node.Next
	node.Next = &ListNode{Val:4}
	node = node.Next
	node.Next = &ListNode{Val:5}
	d :=removeNthFromEnd2(head,2)
	for  d!=nil {
		fmt.Println(d.Val)
		d = d.Next
	}
}


<|start_filename|>300longest-increasing-subsequence/main.go<|end_filename|>
package main

import (
	"fmt"
)

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func lengthOfLIS(nums []int) int {
     dp :=make([]int,len(nums))//定义规则 dp[i] 到i表达当前最大的升序
     maxLen := 0
     for i:=0;i<len(nums);i++{
     	 dp[i] =1
     	 for j:=0;j<i;j++{
			 if nums[j]<nums[i] { // 遍历i的子串 如果存在升序
				 dp[i] = max(dp[j]+1,dp[i])
			 }
		 }
		 maxLen = max(maxLen,dp[i])
	 }
	 return maxLen
}







//300. 最长上升子序列 https://leetcode-cn.com/problems/longest-increasing-subsequence/
func main() {
	fmt.Println(lengthOfLIS([]int{10, 9, 2, 5, 3, 7, 101, 18}))
}


<|start_filename|>415add-strings/main.go<|end_filename|>
package main

import "fmt"

func addStrings(num1 string, num2 string) string {
	l1 := len(num1)-1
	l2 := len(num2)-1
	carry :=0
	str :=""
	for l1>=0||l2>=0{
		n1 :=0
		if l1>=0{
			n1 = int(num1[l1]-'0')
			l1--
		}
		n2 :=0
		if l2>=0{
			n2 = int(num2[l2]-'0')
			l2--
		}
		fmt.Println(n2,n1)
		r := n1+n2+carry
		carry= r/10
        str =fmt.Sprintf("%d%s",r%10,str)
	}
	if carry>0{
		str =fmt.Sprintf("1%s",str)
	}
	return str
}

func main() {
	fmt.Println(addStrings("900","211"))
}


<|start_filename|>235lowest-common-ancestor-of-a-binary-search-tree/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	     Val int
	     Left *TreeNode
	     Right *TreeNode
}

func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
	for root!= nil{
		if root.Val>p.Val&&root.Val>q.Val {
			root = root.Left
		} else if root.Val<p.Val&&root.Val<q.Val {
			root = root.Right
		}else {
			return root
		}
	}
  return nil
}
func lowestCommonAncestor1(root, p, q *TreeNode) *TreeNode {
	if root.Val > p.Val && root.Val > q.Val {
		root = lowestCommonAncestor1(root.Left, p, q)
	}
	if root.Val < p.Val && root.Val < q.Val {
		root = lowestCommonAncestor1(root.Right, p, q)
	}
	return root
}

func main() {
	six := &TreeNode{
		Val: 6,
	}
	two := &TreeNode{
		Val: 2,
	}
	six.Left = two
	eight := &TreeNode{
		Val: 8,
	}
	six.Right = eight
	zero := &TreeNode{
		Val: 0,
	}
	two.Left = zero
	four := &TreeNode{
		Val: 4,
	}
	two.Right = four
	three := &TreeNode{
		Val: 3,
	}
	four.Left = three
	five := &TreeNode{
		Val: 5,
	}
	four.Right = five

	seven := &TreeNode{
		Val: 7,
	}
	eight.Left = seven
	nine := &TreeNode{
		Val: 9,
	}
	eight.Right = nine
	fmt.Println(lowestCommonAncestor1(six,two,eight).Val)
	fmt.Println(lowestCommonAncestor1(six,two,four).Val)
}


<|start_filename|>16.07maximum-lcci/main.go<|end_filename|>
package main


func maximum(a int, b int) int {
	 k := ((a - b) >> 63) & 1
	 return b * k + a * (k ^ 1)
}
func main() {
	
}


<|start_filename|>1281subtract-the-product-and-sum-of-digits-of-an-integer/main.go<|end_filename|>
package main

import "fmt"

func subtractProductAndSum(n int) int {
     var sum int =0
     var res int =1
     for n!=0{
     	r := n%10
     	n =n/10
     	sum +=r
     	res *=r
	 }
     return res-sum
}
func main() {
	fmt.Println(subtractProductAndSum(234))
	fmt.Println(subtractProductAndSum(4421))
}


<|start_filename|>39shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func majorityElement1(nums []int) int {
     m :=make(map[int]int)
     t :=len(nums)/2
     for _,v:=range nums{
     	if m[v]>=t{
     		return v
		}
		m[v]++
	 }
	 return -1
}

func majorityElement(nums []int) int {
	sort.Ints(nums)
	return nums[len(nums)/2]
}


func main() {
	fmt.Println(majorityElement([]int{1, 2, 3, 2, 2, 2, 5, 4, 2}))
}


<|start_filename|>221maximal-square/main.go<|end_filename|>
package main

import "fmt"

func maximalSquare(matrix [][]byte) int {
	maxSide :=0
	if len(matrix)==0 ||len(matrix[0])==0{
		return maxSide
	}
	for i:=0;i<len(matrix);i++{
		for j:=0;j<len(matrix[i]);j++{
			if matrix[i][j]=='1'{
				maxSide = max(maxSide,1)
				//以i点为左上角 做大的边长
				currMaxSide :=min(len(matrix)-i,len(matrix[i])-j)
				for k:=1;k<currMaxSide;k++{
					flag :=true
					// 以i点为左上角 向右下方的斜角位置点
					if matrix[i+k][j+k]=='0'{
						break
					}
					for  m := 0; m < k; m++ {
						if matrix[i + k][j + m] == '0' || matrix[i + m][j + k] == '0' {
							flag = false
							break
						}
					}
					if flag{
						maxSide = max(maxSide,k+1)
					}else {
						break
					}
				}
			}
		}
	}
	return maxSide*maxSide
}

func max(a,b int)int{
	if a>b{
		return a
	}
	return b
}

func min(a,b int)int{
	if a<b{
		return a
	}
	return b
}
func main() {
	//g :=[][]byte{
	//	{'1','0','1','0','0'},
	//	{'1','0','1','1','1'},
	//	{'1','1','1','1','1'},
	//	{'1','0','0','1','0'},
	//}
	//fmt.Println(maximalSquare(g))
	//g1 :=[][]byte{
	//	{'1','0','1','1','1'},
	//	{'0','1','0','1','0'},
	//	{'1','1','0','1','1'},
	//	{'1','1','0','1','1'},
	//	{'0','1','1','1','1'},
	//}
	//fmt.Println(maximalSquare(g1))


	g2 :=[][]byte{
		{'1','0','1','1','0','1'},
		{'1','1','1','1','1','1'},
		{'0','1','1','0','1','1'},
		{'1','1','1','0','1','0'},
		{'0','1','1','1','1','1'},
		{'1','1','0','1','1','1'},
	}
	fmt.Println(maximalSquare(g2))
}


<|start_filename|>56merge-intervals/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func merge(intervals [][]int) [][]int {
	if len(intervals)==0 {
		return nil
	}
	sort.Slice(intervals, func(i, j int) bool {
		 return intervals[i][0]<intervals[j][0]
	})
	queue :=[][]int{intervals[0]}
	for i:=1;i<len(intervals);i++{
		top :=queue[len(queue)-1]
		curr :=intervals[i]
		if curr[0]>top[1] {//当前值首地址大于栈顶的尾部元素
			queue = append(queue,curr)
			continue
		}
		//{a,b}{c,d} b>c
		if curr[1]>top[1] {
			top[1]=curr[1]// 合并区间
		}
	}
	return queue
}

func subarraySum(nums []int, k int) int {
	var res,sum int
	m := map[int]int{0:1}
	for i:=0;i<len(nums);i++{
		sum += nums[i]
		if m[sum-k]>0{
			res += m[sum-k]
		}
		m[sum]++
	}
	return res
}
func main() {

	fmt.Println(merge([][]int{{1,3},{2,4},{8,10},{15,18}}))
	fmt.Println(merge([][]int{{1,4},{4,5}}))
}


<|start_filename|>349intersection-of-two-arrays/main.go<|end_filename|>
package main

import "fmt"

func intersection(nums1 []int, nums2 []int) []int {
      m :=make(map[int]struct{})
      for i:=range nums1{
      	if _,ok:=m[nums1[i]];!ok{
			m[nums1[i]]= struct{}{}
		}
	  }
	  var res []int
	  hasMap :=make(map[int]struct{})
	  for _,v:=range nums2{
		  _,ok:=m[v]
		 _,hasOK:= hasMap[v]
	  	 if ok&&!hasOK{
	  	 	res =append(res,v)
	  	 	hasMap[v]= struct{}{}
		 }
	  }
	  return res
}

func main() {
	fmt.Println(intersection([]int{1,2,2,1},[]int{2,2}))
	fmt.Println(intersection([]int{4,9,5},[]int{9,4,9,8,4}))
}


<|start_filename|>139word-break/main.go<|end_filename|>
package main

import "strings"

func wordBreak(s string, wordDict []string) bool {
    for _,v:=range wordDict{
    	if strings.Contains(s,v){
    		return true
		}
	}
	return false
}

func main() {
	
}


<|start_filename|>965univalued-binary-tree/main.go<|end_filename|>
package main

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func isUnivalTree(root *TreeNode) bool {
	if root == nil {
		return true
	}
	stack := []*TreeNode{root}
	var v = root.Val
	for len(stack) > 0 {
		size := len(stack)
		for i := 0; i < size; i++ {
			node := stack[i]
			if node.Val != v {
				return false
			}
			if node.Left != nil {
				stack = append(stack, node.Left)
			}
			if node.Right != nil {
				stack = append(stack, node.Right)
			}
		}
		stack = stack[size:]
	}
	return true
}

func isUnivalTree1(root *TreeNode) bool {
	if root == nil {
		return true
	}
	if root.Left != nil && root.Val != root.Left.Val {
		return false
	}
	if root.Right != nil && root.Val != root.Right.Val {
		return false
	}
	return isUnivalTree(root.Left) && isUnivalTree(root.Right)
}




func main() {

}


<|start_filename|>128longest-consecutive-sequence/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func longestConsecutive(nums []int) int {
	if len(nums)==0{
		return 0
	}
    sort.Ints(nums)
    count :=1
    max := count
    for i:=1;i<len(nums);i++{
    	if nums[i]==nums[i-1]{
			continue
		}
    	if nums[i-1]+1==nums[i]{
    		count++
		}else {
			if max<count{
				max = count
			}
			count=1
		}
	}
	if max<count{
		max = count
	}
	return max
}
func main() {
	fmt.Println(longestConsecutive([]int{100, 4, 200, 1, 3, 2}))
	fmt.Println(longestConsecutive([]int{0,-1}))
	fmt.Println(longestConsecutive([]int{1,2,0,1}))
}


<|start_filename|>10qing-wa-tiao-tai-jie-wen-ti-lcof/main.go<|end_filename|>
package main

import "fmt"

func numWays(n int) int {
	if n<=1{
		return 1
	}
   dp := make([]int ,n+1)
   dp[1]=1
   dp[2]=2
   for i:=3;i<=n;i++{
   	  dp[i]= (dp[i-1]+dp[i-2])%1000000007
	}
	return dp[n]
}

func main() {
	fmt.Println(numWays(0))
	fmt.Println(numWays(2))
	fmt.Println(numWays(7))
	fmt.Println(numWays(92))
}


<|start_filename|>1309decrypt-string-from-alphabet-to-integer-mapping/main.go<|end_filename|>
package main

import (
	"fmt"
	"strconv"
)

func freqAlphabets(s string) string {
	 i:=len(s)-1
	 var res =""
     for i>=0{
     	if s[i]=='#'{
     		b:=s[i-2:i]
     		v,_:= strconv.Atoi(b)
     		res= fmt.Sprintf("%s%s",string('a'+v-1),res)
     		i=i-3
		}else {
			v,_:= strconv.Atoi(string(s[i]))
			res= fmt.Sprintf("%s%s",string('a'+v-1),res)
			i--
		}
	 }
	 return res
}

func main() {
	fmt.Println(freqAlphabets("10#11#12"))
	fmt.Println(freqAlphabets("1326#"))

	fmt.Println(freqAlphabets("25#"))
}


<|start_filename|>16three-sum-closest/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
	"sort"
)


func threeSumClosest(nums []int, target int) int {
	res :=nums[0]+nums[1]+nums[2]
	sort.Ints(nums)
     for i:=0;i<len(nums)-2;i++{
     	l :=i+1
     	r:=len(nums)-1
     	for l<r{
     		tmp:=nums[i]+nums[l]+nums[r]
			if tmp>target {
				r--
			}else if tmp<target {
				l++
			}else {
				return target
			}
			if int(math.Abs(float64(tmp-target)))<int(math.Abs(float64(res-target))) {
				res = tmp
			}
		}
	 }
	 return res
}
func main() {
	fmt.Println(threeSumClosest([]int{-1,2,1,-4},1))
	fmt.Println(threeSumClosest([]int{0,0,0},1))
}


<|start_filename|>58length-of-last-word/main.go<|end_filename|>
package main

import (
	"fmt"
	"strings"
)

func lengthOfLastWord(s string) int {
     s =strings.TrimSpace(s)
	i:=len(s)-1
    for ;i>=0;i--{
		if string(s[i]) == " "{
			break
		}
	}
	return len(s)-i-1
}

func main() {
	fmt.Println(lengthOfLastWord("a "))
	fmt.Println(lengthOfLastWord("Hello World"))
}


<|start_filename|>154find-minimum-in-rotated-sorted-array-ii/main.go<|end_filename|>
package main

import "math"

func findMin(nums []int) int {
     left :=0
     right :=len(nums)-1
     for left<right{
     	mid :=left+(right-left)>>1
     	if nums[mid]>nums[right]{
     		left = mid+1
		}else {
			right= right-1
		}
	 }
	 return nums[left]
}

func main() {
	math.Pow()
}


<|start_filename|>35search-insert-position/main.go<|end_filename|>
package main

import "fmt"

func searchInsert(nums []int, target int) int {
	left:=0
	right:= len(nums)-1
	for left<=right{
		mid := (left+(right-right)) >>1
		if nums[mid]==target {
			return mid
		} else if nums[mid] <target{
			left=mid+1
		}else {
			right= mid-1
		}
	}
	return left
}

func main() {
	fmt.Println(searchInsert([]int{1,3,5,6},5))
	fmt.Println(searchInsert([]int{1,3,5,6},7))
	fmt.Println(searchInsert([]int{1},1))
}


<|start_filename|>438find-all-anagrams-in-a-string/main.go<|end_filename|>
package main

import (
	"fmt"
)


func isCorrect(s string,m map[byte]int)bool{
	mm := make(map[byte]int)
	for i:=range s{
		if _,ok:=m[s[i]];!ok {
           return false
		}else {
			 v :=mm[s[i]]
			 mm[s[i]] = v+1
		}
	}
	if len(mm) !=len(m){
		return false
	}
	for k,v :=range m{
		if mm[k]!=v {
			return false
		}
	}
	return true
}

func findAnagrams(s string, p string) []int {
	var result []int
	var left, right, match int
	tt := []byte(p)
	needs := make(map[byte]int, len(p))
	windows := make(map[byte]int, len(p))
	for _, v := range tt {
		needs[v]++
	}
	for right <= len(s)-1 {
		// 是子串的内容
		if _, ok := needs[s[right]]; ok {
			// 窗口中字符  对应 数加一
			windows[s[right]]++
			// 匹配了某个字符
			if windows[s[right]] == needs[s[right]] {
				match++
			}
		}

		// 全部匹配
		for match == len(needs) {
			// 把长度为 子串的结果 加进去
			if len(p) == right-left+1 {
				result = append(result, left)
			}

			// left边界是子串中的字符，则要计算
			if _, ok := needs[s[left]]; ok {
				// 窗口中字符  对应 数减一
				windows[s[left]]--
				if windows[s[left]] < needs[s[left]] {
					match--
				}
			}
			left++
		}
		right++
	}
	return result
}

func main() {

	fmt.Println(findAnagrams("cbaebabacd","abc"))
	fmt.Println(findAnagrams("abab","ab"))
}


<|start_filename|>557reverse-words-in-a-string-iii/main.go<|end_filename|>
package main

import (
	"fmt"
	"strings"
)

func reverseWords1(s string) string {
    l := strings.Split(s," ")
    var str strings.Builder
    for i,v:=range l{
		str.Write(revString(v))
    	if i!=len(l)-1{
			str.WriteString(" ")
		}
	}
	return str.String()
}

func revString(s string)[]byte{
	 l:=0
	 r :=len(s)-1
	 b :=[]byte(s)
	 for l<r{
	 	b[l],b[r] =b[r],b[l]
	 	l++
	 	r--
	 }
	 return b
}
func reverseWords(s string) string {
	b :=[]byte(s)
	l :=0
	for i,v :=range s{
		if v==' '||i==len(s)-1{
			r :=i-1
			if i==len(s)-1{
				r =i
			}
			for l<r{
				b[l],b[r] =b[r],b[l]
				l++
				r--
			}
			l=i+1
		}
	}
	return string(b)
}

func main() {
	                            //s'teL ekat edoCteeL tsetnoc
	fmt.Println(reverseWords("Let's take LeetCode contest"))
}


<|start_filename|>543diameter-of-binary-tree/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	Val int
	Left *TreeNode
	Right *TreeNode
}
var res *int // leetcode bug 全局变量最好是指针
func diameterOfBinaryTree(root *TreeNode) int {
	t :=1
	res =&t
	if root==nil {
		return 0
	}
	dfs(root)
	return *res
}

func max(a,b int)int{
	if a>b {
		return a
	}
	return b
}

func dfs(root *TreeNode)int{
	if root==nil {
		return 0
	}
	l := dfs(root.Left)
	r := dfs(root.Right)
	*res = max(*res,l+r)
	return max(l,r)+1
}


func main() {
	a := &TreeNode{
		Val: 1,
	}
	b := &TreeNode{
		Val: 2,
	}
	a.Left = b
	c := &TreeNode{
		Val: 3,
	}
	a.Right = c
	d := &TreeNode{
		Val: 4,
	}
	b.Left = d
	e := &TreeNode{
		Val: 5,
	}
	b.Right = e
	fmt.Println(diameterOfBinaryTree(a))

	g := &TreeNode{
		Val: 1,
	}
	fmt.Println(diameterOfBinaryTree(g))
}


<|start_filename|>42trapping-rain-water/main.go<|end_filename|>
package main

import (
	"fmt"
)

func max(a,b int)int{
	if a>b{
		return a
	}
	return b
}
func min(a,b int)int{
	if a>b{
		return b
	}
	return a
}
func trap(height []int)int{
	l :=len(height)
	res :=0
	// 起始位置是第一个，而不是0
	// 结束位置是倒数第二个，而不是倒数第一个
	for i:=1;i<l-1;i++{
		leftMax :=0
		for k:=0;k<=i;k++{
			leftMax =max(height[k],leftMax)
		}
		rightMax :=0
		for j:=i;j<l;j++{
			rightMax =max(height[j],rightMax)
		}
		minVal := min(leftMax,rightMax)
        water := minVal-height[i]
        res =res+water
	}
	return res
}





func trap1(height []int) int {
	if len(height)==0{
		return 0
	}
	left :=0
	right :=len(height)-1
	leftMax :=0
	rightMax :=0
	res :=0
	for left< right{
		if height[left]<=height[right]{
			if height[left]>=leftMax{
				leftMax = height[left]
			}else {
				res +=leftMax-height[left]
			}
			left++
		}else {
			if height[right]>=rightMax{
				rightMax = height[right]
			}else {
				res +=rightMax-height[right]
			}
			right--
		}
	}
	return res
}

func trap2(height []int)int{
	l :=len(height)
	res :=0
	// 起始位置是第一个，而不是0
	// 结束位置是倒数第二个，而不是倒数第一个
	for i:=1;i<l-1;i++{
		leftMax :=height[i]
		for k:=i-1;k>=0;k--{
			if leftMax<height[k]{
				leftMax = height[k]
			}
		}
		if leftMax<=height[i]{
			continue
		}
		rightMax :=height[i]
		for j:=i+1;j<l;j++{
			if rightMax<height[j]{
				rightMax = height[j]
			}
		}
		if rightMax<=height[i]{
			continue
		}
		minVal := min(leftMax,rightMax)
		water := minVal-height[i]
		res =res+water
	}
	return res
}

// 动态规划
func trap3(height []int)int{
	if len(height)==0{
		return 0
	}
	size :=len(height)
	res :=0
	leftDP :=make([]int,size)
	rightDP :=make([]int,size)
	leftDP[0]= height[0]
	rightDP[size-1] = height[size-1]

	for i:=1;i<size;i++{
		leftDP[i] = max(leftDP[i-1],height[i])
	}
	for i:=size-2;i>=0;i--{
		rightDP[i] = max(rightDP[i+1],height[i])
	}
	for i:=1;i<size-1;i++{
		minHeight := min(leftDP[i],rightDP[i])
		waterVal :=minHeight-height[i]
		res +=waterVal
	}
	return res
}

func main() {
	fmt.Println(trap3([]int{0,1,0,2,1,0,1,3,2,1,2,1}))
}


<|start_filename|>589n-ary-tree-preorder-traversal/main.go<|end_filename|>
package main

type Node struct {
	Val      int
	Children []*Node
}


func preorder(root *Node) []int {
    if root==nil{
    	return nil
	}
	res :=make([]int,0)
	helper(root,&res)
	return res
}

func helper(root *Node,res *[]int){
	if root==nil{
		return
	}
	*res = append(*res,root.Val)
	for i:=0;i<len(root.Children);i++{
		helper(root.Children[i],res)
	}
}

func preorder1(root *Node) []int {
	if root==nil{
		return nil
	}
	stack := make([]*Node,0)
	stack = append(stack,root)
	res :=make([]int,0)
	for len(stack)>0{
		node := stack[len(stack)-1]
		stack = stack[:len(stack)-1]
		res = append(res,node.Val)
		for i:=len(node.Children)-1;i>=0;i--{
			stack = append(stack,node.Children[i])
		}
	}
	return res
}



func main() {
	
}


<|start_filename|>1304find-n-unique-integers-sum-up-to-zero/main.go<|end_filename|>
package main

import "fmt"

func sumZero(n int) []int {
	if n==1{
		return []int{0}
	}
	var sum =n
	res :=[]int{n}
    for i:=n-2;i>0;i--{
   	    res=append(res,i)
   	    sum +=i
    }
	res=append(res,0-sum)
	return res
}

func main() {
	fmt.Println(sumZero(5))
	fmt.Println(sumZero(3))
	fmt.Println(sumZero(2))
	fmt.Println(sumZero(1))
}


<|start_filename|>152maximum-product-subarray/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
	"sort"
)

func maxProduct(nums []int) int {
	max := math.MinInt64
	for i := 0; i < len(nums); i++ {
		var res = 1
		for j := i; j < len(nums); j++ {
			res = res * nums[j]
			if max < res {
				max = res
			}
		}
	}
	return max
}

func maxProduct2(nums []int) int {
	maxDP := make([]int, len(nums))
	minDP := make([]int, len(nums))
	maxDP[0] = nums[0]
	minDP[0] = nums[0]
	max := nums[0]
	for i := 1; i < len(nums); i++ {
		maxDP[i], minDP[i] = numCompare(minDP[i-1]*nums[i], maxDP[i-1]*nums[i], nums[i])
		if max < maxDP[i] {
			max = maxDP[i]
		}
	}
	return max
}

//三个数字排列 获取最大值和最小值
func numCompare(a, b, c int) (max, min int) {
	s := []int{a, b, c,}
	sort.Ints(s)
	return s[2], s[0]
}

func main() {
	fmt.Println(maxProduct2([]int{-2, 0, -1}))
	fmt.Println(maxProduct2([]int{2, 3, -2, 4}))
	fmt.Println(maxProduct2([]int{-2, 3, -4}))
}


<|start_filename|>989add-to-array-form-of-integer/main.go<|end_filename|>
package main

import "fmt"

func addToArrayForm(A []int, K int) []int {
	var B []int
	for K>0{
		B = append([]int{K%10},B...)
		K=K/10
	}
	a1 :=len(A)-1
	b1 :=len(B)-1
	carry :=0
	var res []int
	for a1>=0||b1>=0{
		n1 :=0
		if a1>=0{
			n1=A[a1]
			a1--
		}
		n2 :=0
		if b1>=0{
			n2=B[b1]
			b1--
		}
		r := n1+n2+carry
		carry=r/10
		res = append([]int{r%10},res...)
	}
	if carry>0 {
		res = append([]int{carry},res...)
	}
	return res
}

func main() {
	fmt.Println(addToArrayForm([]int{1,2,0,0},34))
	fmt.Println(addToArrayForm([]int{2,7,4},181))
}


<|start_filename|>394decode-string/main.go<|end_filename|>
package main

import (
	"fmt"
	"strconv"
)

func decodeString1(s string) string {
	stack := make([]byte, 0)
	for i := 0; i < len(s); i++ {
		if s[i] != ']' {
			stack = append(stack, s[i])
		} else {
			var index int
			for j := len(stack) - 1; j >= 0; j-- {
				if stack[j] == '[' {
					index = j
					break
				}
			}
			list := make([]byte, len(stack)-index+1)
			copy(list, stack[index-1:])
			var countStr = ""
			for index = index - 1; index >= 0; index-- {
				if stack[index] >= '0' && stack[index] <= '9' {
					countStr = fmt.Sprintf("%d%s", stack[index]-'0', countStr)
				} else {
					break
				}
			}
			count, _ := strconv.Atoi(countStr)
			if index < 0 {
				stack = stack[:0]
			} else {
				stack = stack[:index+1]
			}
			list = list[2:]
			for n := 0; n < count; n++ {
				for m := 0; m < len(list); m++ {
					stack = append(stack, list[m])
				}

			}
		}
	}
	return string(stack)
}

func decodeString(s string) string {
	alphaStack := make([]byte, 0)
	countStack := make([]int,0)
	alphaIndex := make([]int,0)
	var count int
	for i:=0 ;i < len(s);i++{
		if s[i] >='0'&&s[i]<='9' {
			count =count*10 +int(s[i]-'0')
		}else  if s[i]=='['{ // [ 后面肯定是字母
			// 可以把当前的数字放入次数数组
			countStack = append(countStack,count)
			count =0

			alphaIndex = append(alphaIndex,len(alphaStack))
		}else if s[i] ==']'{ // 对当前字母段出站
			// 获取当前字母段的次数 然后次数出栈
			c :=countStack[len(countStack)-1]
			countStack = countStack[:len(countStack)-1]

			// 获取当前字母段开始的索引 然后索引出栈
			index :=alphaIndex[len(alphaIndex)-1]
			alphaIndex = alphaIndex[:len(alphaIndex)-1]

			//获取当前的字母段
			str := string(alphaStack[index:])
			// 字母数组出站
			alphaStack = alphaStack[:index]

			for j:=0;j<c;j++{
				alphaStack = append(alphaStack,[]byte(str)...)
			}
		}else {
			alphaStack = append(alphaStack,s[i])
		}
	}
	return string(alphaStack)
}



func main() {
	fmt.Println(decodeString("3[a]2[bc]"))
	fmt.Println(decodeString("3[a2[c]]"))
	fmt.Println(decodeString("2[abc]3[cd]ef"))
	fmt.Println(decodeString("100[leetcode]"))
	fmt.Println(decodeString("3[a]2[b4[F]c]"))
}


<|start_filename|>151reverse-words-in-a-string/main.go<|end_filename|>
package main

import (
	"fmt"
	"strings"
)

func reverseWords(s string) string {
     list :=  strings.Split(s ," ")
     var res []string
     for i:=len(list)-1;i>=0;i--{
     	if len(list[i])>0{
     		res = append(res,list[i])
		}
	 }
	 s =strings.Join(res," ")
	 return s
}

func main() {
	fmt.Println(reverseWords("the sky is blue"))
	fmt.Println(reverseWords("  hello world!  "))
}


<|start_filename|>17add-without-plus-lcci/main.go<|end_filename|>
package main

import "fmt"

func add(a int, b int) int {
	for b != 0 {
		sum := a ^ b
		carry := (a & b) << 1
		a = sum
		b = carry
	}
	return a
}

func main() {
	fmt.Println(add(1,1))
	fmt.Println(add(2,3))
}


<|start_filename|>88merge-sorted-array/main.go<|end_filename|>
package main

import "fmt"

func merge1(nums1 []int, m int, nums2 []int, n int)  {
     n1:= m-1
     n2 :=n-1
     l := m+n-1
     for n1>=0&&n2>=0{
     	if nums1[n1]<=nums2[n2]{
     		nums1[l]= nums2[n2]
     		n2--
		}else {
			nums1[l]= nums1[n1]
			n1--
		}
		l--
	 }
	 for n2>=0&&l>=0{
		 nums1[l]= nums2[n2]
		 n2--
		 l--
	 }
}


func merge(nums1 []int, m int, nums2 []int, n int)  {
	n1:= m-1
	n2 :=n-1
	l := m+n-1
	for n2>=0{
		if n1>=0&&nums1[n1]>nums2[n2]{
			nums1[l]= nums1[n1]
			n1--
		}else {
			nums1[l]= nums2[n2]
			n2--
		}
		l--
	}
}

func main() {
	num1 := []int{1,2,3,0,0,0}
	merge(num1,3,[]int{2,5,6},3)
	fmt.Println(num1)
	num2 := []int{0}
	merge(num2,0,[]int{1},1)
	fmt.Println(num2)

	num3 := []int{4,5,6,0,0,0}
	merge(num3,3,[]int{1,2,3},3)
	fmt.Println(num3)
}


<|start_filename|>328odd-even-linked-list/main.go<|end_filename|>
package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

func oddEvenList(head *ListNode) *ListNode {
	var oddDummy = &ListNode{}
	var evenDummy = &ListNode{}
	var i = 1
	var preOdd = oddDummy
	var preEven = evenDummy
	for head != nil {
		next := head.Next
		if i%2 != 0 {
			preOdd.Next = head
			preOdd = head
		} else {
			preEven.Next = head
			preEven = head
		}
		i++
		head = next
	}
	preEven.Next = nil
	preOdd.Next = evenDummy.Next
	return oddDummy.Next
}

func main() {
	one := &ListNode{Val: 1}
	two := &ListNode{Val: 2}
	one.Next = two
	three := &ListNode{Val: 3}
	two.Next = three
	four := &ListNode{Val: 4}
	three.Next = four
	five := &ListNode{Val: 5}
	four.Next = five
	//for one!=nil{
	//	fmt.Println(one.Val)
	//	one = one.Next
	//}
	h := oddEvenList(one)
	for h != nil {
		fmt.Println(h.Val)
		h = h.Next
	}
}


<|start_filename|>49chou-shu-lcof/main.go<|end_filename|>
package main

import "fmt"

func nthUglyNumber(n int) int {
	count := 0
	number :=1
	uglyNumber :=1
    for count<n{
    	if isUglyNum(number){
    		count++
			uglyNumber = number
		}
		number++
	}
	return uglyNumber
}

func isUglyNum(n int)bool{
	for n%2==0{
		n=n/2
	}
	for n%3==0{
		n=n/3
	}
	for n%5==0{
		n=n/5
	}
	return n<=1
}

//面试题49. 丑数
func main() {
	fmt.Println(nthUglyNumber(10))
	fmt.Println(nthUglyNumber(11))
}


<|start_filename|>1two-sum/main.go<|end_filename|>
package main

import (
	"fmt"
)
//https://leetcode-cn.com/problems/two-sum/
func main() {
	n :=[]int{2,7,11,15}
	fmt.Println( twoSum(n,9))
}

func twoSum(nums []int, target int) []int {
	m := make (map[int]int)// key 当前已经被遍历的数据值 value 是当前值的位置
	for i,v :=range nums{
		k := target-v // 通过相减获取我们要查找的另外一个值
		if l,ok:=m[k];ok {// 判断是不是在当前的hash 表中
			return []int{l,i}
		}else {
			m[v]= i // 不在就把当前值作为key 索引位置作为value
		}
	}
	return nil
}


<|start_filename|>11container-with-most-water/main.go<|end_filename|>
package main

import "fmt"

func max(a,b int)int{
	if a>b{
		return a
	}
	return b
}

func maxArea(height []int) int {
     left :=0
     right := len(height)-1
     area :=0
     h :=0
     for left<right{
     	 w :=right-left
		 if height[left]<=height[right] {
			 h = height[left]
			 left++
		 }else {
			 h = height[right]
			 right--
		 }
		 area = max(area,h*w)
	 }
     return area
}



func main() {
	fmt.Println(maxArea([]int{1,8,6,2,5,4,8,3,7}))
}


<|start_filename|>70climbing-stairs/main.go<|end_filename|>
package main

import "fmt"

func climbStairs(n int) int {
	if n==1 {
		return 1
	}
	dp :=make([]int,n+1)
	dp[1]=1
	dp[2]=2
	for i:=3;i<=n;i++{
		dp[i]=dp[i-1]+dp[i-2]
	}
	return dp[n]
}


//70. 爬楼梯 https://leetcode-cn.com/problems/climbing-stairs/
func main() {
	fmt.Println(climbStairs(4))
	fmt.Println(climbStairs(5))
	fmt.Println(climbStairs(6))
	fmt.Println(climbStairs(7))
}


<|start_filename|>974subarray-sums-divisible-by-k/main.go<|end_filename|>
package main

import "fmt"

func subarraysDivByK1(A []int, K int) int {
	var res int
	for i := 0; i < len(A); i++ {
		var sum =0
		for j := i ; j <len(A); j++ {
			sum +=A[j]
			if sum%K == 0 {
				res++
			}
		}
	}
	return res
}
func subarraysDivByK(A []int, K int) int {
	record := map[int]int{0: 1} // 如果A中有元素能整除K， 那么相关元素单独能构成符合题意的子数组，需要统计
	prefixSum, result := 0, 0
	for i := 0; i < len(A); i++ {
		prefixSum += A[i]
		mod := (prefixSum % K+K)%K
		//if mod < 0 { // prefixSum可能是负数，导致mod为负数， 取余的结果mod为负，跟取余结果为mod+K是等价的
		//	mod += K
		//}
		fmt.Println(mod)
		result += record[mod]
		record[mod]++
	}
	return result
}


func main() {
	subarraysDivByK([]int{4, 5, 0, -2, -3, 1}, 5)
	fmt.Println()
}


<|start_filename|>322coin-change/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func coinChange(coins []int, amount int) int {
    sort.Ints(coins)
    var res = 0
    var list []int
	for i:=len(coins)-1;i>=0;i--{
		for amount>=coins[i]{
			amount-=coins[i]
			list = append(list,coins[i])
			res++
		}
		if amount==0{
			break
		}
	}
	fmt.Println(list)
	if amount>0{
		fmt.Println(amount)
		return -1
	}
	return res
}

func main() {
	//fmt.Println(coinChange([]int{1,2,5},11))
	//fmt.Println(coinChange([]int{2},3))
	fmt.Println(coinChange([]int{186,419,83,408},6249))
}


<|start_filename|>1252cells-with-odd-values-in-a-matrix/main.go<|end_filename|>
package main

import "fmt"

func oddCells(n int, m int, indices [][]int) int {
      grip :=make([][]int,n)
      for i:=0;i<n;i++{
      	grip[i] = make([]int,m)
	  }
	  for i:=0;i<len(indices);i++{
	  	  r := indices[i][0]
	  	  for j:=0;j<len(grip[r]);j++{
	  	  	  grip[r][j]++
		  }
		  c := indices[i][1]
		  for j:=0;j<len(grip);j++{
			  grip[j][c]++
		  }
	  }
	  var res int
	  for i:=0;i<n;i++{
	  	for j:=0;j<m;j++{
	  		if grip[i][j]%2!=0{
	  			res++
			}
		}
	  }
	  return res
}

func main() {
	fmt.Println(oddCells(2,3,[][]int{{0,1},{1,1}}))
	fmt.Println(oddCells(2,2,[][]int{{1,1},{0,0}}))
}


<|start_filename|>17letter-combinations-of-a-phone-number/main.go<|end_filename|>
package main

import (
	"fmt"
)

func letterCombinations1(digits string) []string {
	if digits == "" || len(digits) == 0 {
		return nil
	}
	nums :=[]string{"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}
	list :=[]string{""}// 默认为空字符
	for i:=0;i<len(digits);i++{
		s :=nums[digits[i]-'0']
		size :=len(list)// 原先的数组长度记录 涉及到底下的入队和出队
		for j:=0;j<size;j++{
			first := list[0]
			list = list[1:]
			for k:=0;k<len(s);k++{
				list = append(list,first+string(s[k]))
			}
		}
	}
	return list
}
func letterCombinations(digits string) []string {
	if digits == "" || len(digits) == 0 {
		return nil
	}
	nums :=[]string{"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}
	var res []string
	res = append(res,"")
	for i :=range digits{
		 s := nums[digits[i]-'0']
		 var temp []string
		 for i:=0;i<len(s);i++{
		 	for j:=0;j<len(res);j++{
		 		temp = append(temp,res[j]+string(s[i]))

			}
		 }
		 res = temp
	}
	return res
}


func main() {
	fmt.Println(letterCombinations("234"))
}


<|start_filename|>1299replace-elements-with-greatest-element-on-right-side/main.go<|end_filename|>
package main

import "fmt"

func max(a,b int)int{
	if a>b{
		return a
	}
	return b
}

func replaceElements(arr []int) []int {
	if len(arr)==0{
		return arr
	}
     dp:=make([]int,len(arr)+1)
     dp[len(dp)-1]=-1
     for i:=len(arr)-1;i>=0;i--{
     	  dp[i]=max(dp[i+1],arr[i])
	 }
	 return dp[1:]
}


func main() {
	fmt.Println(replaceElements([]int{400}))
	fmt.Println(replaceElements([]int{17,18,5,4,6,1}))
}


<|start_filename|>1365how-many-numbers-are-smaller-than-the-current-number/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func smallerNumbersThanCurrent1(nums []int) []int {
	var res []int

	for i:=0;i<len(nums);i++{
		var list []int
		list =append(list,nums[:i]...)
		list =append(list,nums[i+1:]...)
		fmt.Println(list)
		r := findSmallerNumbers(list,nums[i])
		res = append(res,r)
	}
	return res
}


func smallerNumbersThanCurrent(nums []int) []int {
	copyNum :=make([]int,len(nums))
	copy(copyNum,nums)
	sort.Ints(copyNum)
	m := make(map[int]int)
	for i,v :=range copyNum{
		if _,ok :=m[v];!ok{
			m[v]=i
		}
	}
	for i,v :=range nums{
		copyNum[i] = m[v]
	}
	return copyNum
}

func findSmallerNumbers(nums []int,t int)int{
	var res int
	for i:=0;i<len(nums);i++{
		if nums[i]<t{
			res++
		}
	}
	return res
}

func main() {
	fmt.Println(smallerNumbersThanCurrent([]int{8,1,2,2,3}))
}

<|start_filename|>30substring-with-concatenation-of-all-words/main.go<|end_filename|>
package main

import "fmt"

func findSubstring(s string, words []string) []int {
	if len(s)==0{
		return []int{}
	}
	allMap :=make(map[string]int)
	for _,v:=range words{
		allMap[v]++
	}
	wordNum :=len(words)
	wordLen :=len(words[0])
	var res []int
	fmt.Println(len(s)-wordLen*wordNum)
	for i:=0;i<=len(s)-wordLen*wordNum;i++{
		hasMap :=make(map[string]int)
		num :=0
		for num<wordNum{
			word :=s[i+num*wordLen:i+(num+1)*wordLen]
			if v,ok:=allMap[word];ok{
				hasMap[word]++
				if hasMap[word]>v{
					break
				}
			}else {
				break
			}
			num++
		}
		if num==wordNum{
			res = append(res,i)
		}
	}
	return res
}
func findSubstring1(s string, words []string) []int{
	if len(s)==0||len(words)==0 {
		return nil
	}
	allMap :=make(map[string]int)
	for _,v:=range words{
		allMap[v]++
	}
	wordsLen :=len(words)*len(words[0])
	var res []int
	for i:=0;i<=len(s)-wordsLen;i++{
		str :=s[i:i+wordsLen]
		hasMap :=make(map[string]int)
		star:=0
		for ;star<=len(str)-len(words[0]);{
			ss :=str[star:star+len(words[0])]
			if allMap[ss]>0 {
				hasMap[ss]++
				if hasMap[ss]> allMap[ss]{
					break
				}
			}else {
				break
			}
			star = star+len(words[0])
		}
		if star==len(str) {
			res = append(res,i)
		}

	}
	return res
}

func findSubstring2(s string, words []string) []int{
	if len(s)==0||len(words)==0 {
		return nil
	}
	allMap :=make(map[string]int)
	for _,v:=range words{
		allMap[v]++
	}
	wordsLen :=len(words)*len(words[0])
	var res []int
	for i:=0;i<=len(s)-wordsLen;i++{
		str :=s[i:i+wordsLen]
		hasMap :=make(map[string]int)
		start:=0
		for ;start<len(words);start++{
			ss :=str[len(words[0])*start:len(words[0])*(start+1)]
			//fmt.Println(ss)
			if allMap[ss]>0 {
				hasMap[ss]++
				if hasMap[ss]> allMap[ss]{
					break
				}
			}else {
				break
			}
		}
		if start==len(words) {
			res = append(res,i)
		}

	}
	return res
}

func main() {
	fmt.Println(findSubstring2("barfoothefoobarman",[]string{"bar","foo"}))
}


<|start_filename|>1002find-common-characters/main.go<|end_filename|>
package main

import "fmt"

func commonChars(A []string) []string {
     m :=make(map[byte]int,0)
     for idx,v:=range A{
     	if idx==0{
			for i:=range v{
				m[v[i]]++
			}
		}else {
			n :=make(map[byte]int,0)
			for i:=range v{
				n[v[i]]++
			}
			for k,v:=range m{
				 if r,ok:=n[k];ok{
					m[k]=min(v,r)
				}else {
					delete(m,k)
				 }
			}
		}
	 }
	 var res []string
	 for k,v:=range m{
	 	for i:=0;i<v;i++{
	 		res =append(res,string(k))
		}
	 }
	 return res
}

func min(a,b int)int{
	if a<b{
		return a
	}
	return b
}

func main() {
	fmt.Println(commonChars([]string{"bella","label","roller"}))
	fmt.Println(commonChars([]string{"cool","lock","cook"}))
}


<|start_filename|>1207unique-number-of-occurrences/main.go<|end_filename|>
package main

import "fmt"

func uniqueOccurrences(arr []int) bool {
    m :=map[int]int{}
    for _,v :=range arr{
    	m[v]++
	}
    f := map[int]bool{}
    for _,v :=range m{
    	if f[v]{
    		return false
		}else {
			f[v] = true
		}
	}
    return true
}


func main() {
	fmt.Println(uniqueOccurrences([]int{1,2,2,1,1,3}))
	fmt.Println(uniqueOccurrences([]int{1,2}))
	fmt.Println(uniqueOccurrences([]int{-3,0,1,-3,1,1,1,-3,10,0}))
}


<|start_filename|>29shun-shi-zhen-da-yin-ju-zhen-lcof/main.go<|end_filename|>
package main

import "fmt"
func spiralOrder(matrix [][]int) []int {
	if len(matrix) == 0 {
		return nil
	}
    left :=0
    right :=len(matrix[0])-1
    top :=0
    bottom :=len(matrix)-1
    grid := make([]int,(right+1)*(bottom+1))
    index :=0
    for left<=right&&top<=bottom{
    	// 上层 边界判断
    	for i:=left;i<=right&&top<=bottom;i++{
    		grid[index] = matrix[top][i]
    		index++
		}
		//fmt.Println("top","=",grid)
		top++
		// 右侧
		for i:=top;i<=bottom&&left<=right;i++{
			grid[index]= matrix[i][right]
			index++
		}
		//fmt.Println("right","=",grid)
		right--

		//下
		for i:=right;i>=left&&top<=bottom;i--{
			grid[index]= matrix[bottom][i]
			index++
		}
		//fmt.Println("bottom","=",grid)
		bottom--
		//左
		for i:=bottom;i>=top&&left<=right;i--{
			grid[index]= matrix[i][left]
			index++
		}
		//fmt.Println("left","=",grid)
		left++
		//fmt.Println(grid)
	}
	return grid
}


func main() {
	matrix :=[][]int{{1,2,3,4},{5,6,7,8},{9,10,11,12}}
	fmt.Println("1","=",spiralOrder(matrix))

}


<|start_filename|>771jewels-and-stones/main.go<|end_filename|>
package main

import "fmt"

func numJewelsInStones(J string, S string) int {
	 m :=make(map[byte]struct{})
    for i :=range J{
         m[J[i]]= struct{}{}
	}
	var res int
	for i :=range S{
		if  _,ok:=m[S[i]];ok{
			res++
		}
	}
	return res
}


func main() {
	fmt.Println(numJewelsInStones("aA","aAAbbbb"))
	fmt.Println(numJewelsInStones("z","ZZ"))
}


<|start_filename|>645set-mismatch/main.go<|end_filename|>
package main

import "fmt"

func findErrorNums(nums []int) []int {
   m :=make(map[int]int)
   var n,j int
   for i,v:=range nums{
	   if k,ok:=m[v];ok {
		   n = v
		   j = nums[i]+(i-k)
	   }else {
	   	m[v]=i
	   }
   }
   return []int{n,j}
}

func main() {
	fmt.Println(findErrorNums([]int{1,2,2,4}))
}


<|start_filename|>784letter-case-permutation/main.go<|end_filename|>
package main

import (
	"fmt"
	"strings"
	"unicode"
)

func main() {
	fmt.Println(letterCasePermutation("a1b2"))
}
func letterCasePermutation(S string) []string {
	res :=make([]string,0)
	dfs1([]byte(S),0,&res)
	return res
}

func dfs(char []byte,index int,res *[]string){
	if index==len(char) {
		*res = append(*res,string(char))
		return
	}
	dfs(char,index+1,res)
	if unicode.IsLetter(rune(char[index])) {
		if unicode.IsLower(rune(char[index])) {
			char[index] = strings.ToUpper(string(char[index]))[0]
		}else {
			char[index] = strings.ToLower(string(char[index]))[0]
		}
		dfs(char,index+1,res)
	}
}

func dfs1(char []byte,index int,res *[]string){
	if index==len(char) {
		*res = append(*res,string(char))
		return
	}
	dfs(char,index+1,res)
	if unicode.IsLetter(rune(char[index])) {
		char[index] = char[index]^1<<5
		dfs(char,index+1,res)
	}
}

<|start_filename|>217contains-duplicate/main.go<|end_filename|>
package main

import "fmt"

func containsDuplicate(nums []int) bool {
	m := make(map[int]struct{},0)
	for _,v:=range nums{
		if _,ok:=m[v];ok{
			return true
		}
		m[v]= struct{}{}
	}
	return false
}

func main() {
	fmt.Println(containsDuplicate([]int{1,2,3,1}))
	fmt.Println(containsDuplicate([]int{1,2,3,4}))
}


<|start_filename|>1290convert-binary-number-in-a-linked-list-to-integer/main.go<|end_filename|>
package main

import (
	"fmt"
	"math"
)

type ListNode struct {
	Val int
	Next *ListNode
}

func getDecimalValue(head *ListNode) int {
    var list []int
	for head!=nil{
		list = append(list,head.Val)
		head = head.Next
	}
	var res int
	l := len(list)
	for i:=0;i<l;i++{
		if list[i]==1{
			r := int(math.Pow(2,float64(l-i-1)))
			res +=r
		}
	}
	return res
}

func main() {
	head :=&ListNode{Val:1}
	one :=&ListNode{Val:0}
	head.Next= one
	two :=&ListNode{Val:1}
	one.Next = two

	fmt.Println(getDecimalValue(head))
}


<|start_filename|>18four-sum/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
)

func fourSum(nums []int, target int) [][]int {
     var res [][]int
     sort.Ints(nums)
     for i:=0;i<len(nums)-3;i++{
		 if i>0&&nums[i]==nums[i-1] {
			continue
		 }
     	for j:=i+1;j<len(nums)-2;j++{
			if j>i+1&&nums[j]==nums[j-1] {
				continue
			}
     		l :=j+1
     		r :=len(nums)-1
			for l<r {

				temp :=nums[i]+nums[j]+nums[l]+nums[r]
				if temp<target {
					l++
				}else if temp>target {
					r--
				}else {
					res = append(res,[]int{nums[i],nums[j],nums[l],nums[r]})
					l++
					r--
					for l>j+1&&nums[l]==nums[l-1] {
						l++
					}
					for l<len(nums)-2&&nums[r]==nums[r+1]{
						r--
					}
				}

			}
		}
	 }
	 return res
}


func main() {
	fmt.Println(fourSum([]int{1, 0, -1, 0, -2, 2},0))
	fmt.Println(fourSum([]int{-3,-2,-1,0,0,1,2,3},0))
	fmt.Println(fourSum([]int{-1,0,-5,-2,-2,-4,0,1,-2},-9))

}


<|start_filename|>237delete-node-in-a-linked-list/main.go<|end_filename|>
package main

import "fmt"




func deleteNodeByHead(head,node *ListNode)*ListNode {
	dummyHead :=&ListNode{
		Val:  -1,
		Next: head,
	}
	var pre = dummyHead
	for head!=nil{
		if head.Val==node.Val {
			next := node.Next
			pre.Next = next
			head = next
		}else {
			pre = head
			head = head.Next
		}
	}
	return dummyHead.Next
}


type ListNode struct {
	Val int
	Next *ListNode
}
// 无法获取前置节点 只能把下一个节点干掉 ，然后把一个节点的值复制给当前节点
func deleteNode(node *ListNode) {
	next := node.Next// 当前的下一个节点
	node.Val = next.Val // 替换值
	node.Next = next.Next //让当前节点指向的是下一个节点的下一个
}


func main() {
	head :=&ListNode{Val:4}
	n1 :=&ListNode{Val:5}
	head.Next = n1
	n2 :=&ListNode{Val:1}
	n1.Next = n2
	n2.Next =&ListNode{Val:9}
	deleteNode(n1)// 去掉5个节点
	curr :=head
	for curr != nil{
		fmt.Println(curr.Val)
		curr = curr.Next
	}
}


<|start_filename|>45ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/main.go<|end_filename|>
package main

import (
	"fmt"
	"sort"
	"strings"
)

func minNumber(nums []int) string {
    sort.Slice(nums, func(i, j int) bool {
		return compareNumber(nums[i],nums[j])
	})
    var res strings.Builder
    for i:=0;i<len(nums);i++{
    	res.WriteString(fmt.Sprintf("%d",nums[i]))
	}
	return res.String()
}

func compareNumber(a,b int)bool{
	str1 := fmt.Sprintf("%d%d",a,b)
	str2 := fmt.Sprintf("%d%d",b,a)
	if str1<str2{
		return true
	}
	return false
}




func main() {
	fmt.Println(minNumber([]int{8,2}))
	fmt.Println(minNumber([]int{22,21}))
	fmt.Println(minNumber([]int{2,21}))//212
	fmt.Println(minNumber([]int{3,30,34}))
	fmt.Println(minNumber([]int{3,30,34,5,9}))
	//1399439856075703697382478249389609
	//1399439856075703697382478249389609
	fmt.Println(minNumber([]int{8247,824}))
    fmt.Println(minNumber([]int{824,938,1399,5607,6973,5703,9609,4398,8247}))
}


<|start_filename|>189rotate-array/main.go<|end_filename|>
package main

import "fmt"

func rotate(nums []int, k int)  {
     tmp :=make([]int,len(nums))
     copy(tmp,nums)
     for i,v:=range tmp{
     	 index :=(i+k)%len(tmp)
     	 nums[index]= v
	 }
}


func main() {
	s :=[]int{1,2,3,4,5,6,7}
	rotate(s,3)
	fmt.Println(s)
	s1 :=[]int{-1,-100,3,99}
	rotate(s1,2)
	fmt.Println(s1)
}


<|start_filename|>21diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/main.go<|end_filename|>
package main

import "fmt"

func exchange(nums []int) []int {
     l:=0
     r :=len(nums)-1
     for l<r{
		if nums[l]%2==0&&nums[r]%2!=0{
			nums[l],nums[r]=nums[r],nums[l]
		}
		 for l<len(nums)&&nums[l]%2!=0{
			 l++
		 }
		 for r>=0&&nums[r]%2==0{
			 r--
		 }

	 }
	 return nums
}

func main() {
	fmt.Println(exchange([]int{1,3,5}))
	fmt.Println(exchange([]int{1,2,3,4}))
	fmt.Println(exchange([]int{1,2,3,4,5,6,7}))
}


<|start_filename|>102binary-tree-level-order-traversal/main.go<|end_filename|>
package main

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func levelOrder(root *TreeNode) [][]int {
	var res [][]int
	if root == nil {
		return res
	}
	queue := make([]*TreeNode, 0)
	queue = append(queue, root)
	for len(queue) > 0 {
		l := len(queue)
		ans := make([]int, 0)
		for i := 0; i < len(queue); i++ {
			node := queue[i]
			ans = append(ans, node.Val)
			if node.Left != nil {
				queue = append(queue, node.Left)
			}
			if node.Right != nil {
				queue = append(queue, node.Right)
			}
		}
		res = append(res, ans)
		queue = queue[l:]
	}
	return res
}
func main() {

}


<|start_filename|>617merge-two-binary-trees/main.go<|end_filename|>
package main

import "fmt"

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func mergeTrees1(t1 *TreeNode, t2 *TreeNode) *TreeNode {
	if t1 == nil {
		return t2
	}
	if t2 == nil {
		return t1
	}
	t1.Val = t1.Val + t2.Val
	t1.Left = mergeTrees1(t1.Left, t2.Left)
	t1.Right = mergeTrees1(t1.Right, t2.Right)
	return t1
}

func mergeTrees(t1 *TreeNode, t2 *TreeNode) *TreeNode {
	if t1 == nil {
		return t2
	}
	if t2 == nil {
		return t1
	}
	queue := []*TreeNode{t1, t2}
	for len(queue) > 0 {
		n1 := queue[0]
		n2 := queue[1]
		n1.Val = n1.Val + n2.Val
		if n1.Left != nil && n2.Left != nil {
			queue = append(queue, n1.Left, n2.Left)
		} else if n1.Left == nil && n2.Left != nil {
			n1.Left = n2.Left
		}
		if n1.Right != nil && n2.Right != nil {
			queue = append(queue, n1.Right, n2.Right)
		} else if n1.Right == nil && n2.Right != nil {
			n1.Right = n2.Right
		}
		queue = queue[2:]
	}
	return t1
}

func main() {

	t1 := &TreeNode{Val: 1}
	t11 := &TreeNode{Val: 3}
	t1.Left = t11
	t12 := &TreeNode{Val: 2}
	t1.Right = t12
	t13 := &TreeNode{Val: 5}
	t11.Left = t13

	t2 := &TreeNode{Val: 2}
	t21 := &TreeNode{Val: 1}
	t2.Left = t21
	t22 := &TreeNode{Val: 3}
	t2.Right = t22
	t23 := &TreeNode{Val: 4}
	t21.Right = t23
	t24 := &TreeNode{Val: 7}
	t22.Right = t24

	printNode(mergeTrees(t1, t2)) //3 4 5 4 5 7
	fmt.Println()

	t3 := &TreeNode{Val: 1}
	t31 := &TreeNode{Val: 2}
	t3.Left = t31
	t32 := &TreeNode{Val: 3}
	t31.Left = t32

	t4 := &TreeNode{Val: 1}
	t41 := &TreeNode{Val: 2}
	t4.Right = t41
	t42 := &TreeNode{Val: 3}
	t41.Right = t42
	printNode(mergeTrees(t3, t4)) //2 2 3 2 3
}

func printNode(t1 *TreeNode) {
	if t1 == nil {
		return
	}
	fmt.Println(t1.Val)
	printNode(t1.Left)
	printNode(t1.Right)
}
