change path

This commit is contained in:
YDZ
2020-08-06 22:51:50 +08:00
parent ef3da5d363
commit dfde398a50
253 changed files with 3103 additions and 460 deletions

View File

@ -7,7 +7,6 @@ go:
branches: branches:
only: only:
- master - master
- stable
script: script:
- go get -t -v ./... - go get -t -v ./...

17
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,17 @@
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": []
}
]
}

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {
@ -7,6 +14,7 @@ package leetcode
* Next *ListNode * Next *ListNode
* } * }
*/ */
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
if l1 == nil || l2 == nil { if l1 == nil || l2 == nil {
return nil return nil

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question2 struct { type question2 struct {
@ -73,7 +75,7 @@ func Test_Problem2(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans2, q.para2 _, p := q.ans2, q.para2
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(addTwoNumbers(S2l(p.one), S2l(p.another)))) fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(addTwoNumbers(structures.Ints2List(p.one), structures.Ints2List(p.another))))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -20,3 +20,10 @@ func lengthOfLongestSubstring(s string) int {
} }
return result return result
} }
func max(a int, b int) int {
if a > b {
return a
}
return b
}

View File

@ -41,3 +41,17 @@ func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
} }
return float64(midLeft+midRight) / 2 return float64(midLeft+midRight) / 2
} }
func max(a int, b int) int {
if a > b {
return a
}
return b
}
func min(a int, b int) int {
if a > b {
return b
}
return a
}

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question19 struct { type question19 struct {
@ -50,23 +52,13 @@ func Test_Problem19(t *testing.T) {
para19{[]int{1, 2, 3, 4, 5}, 5}, para19{[]int{1, 2, 3, 4, 5}, 5},
ans19{[]int{2, 3, 4, 5}}, ans19{[]int{2, 3, 4, 5}},
}, },
question19{
para19{[]int{1, 2, 3, 4, 5}, 0},
ans19{[]int{1, 2, 3, 4, 5}},
},
question19{
para19{[]int{1, 2, 3, 4, 5}, 10},
ans19{[]int{1, 2, 3, 4, 5}},
},
} }
fmt.Printf("------------------------Leetcode Problem 19------------------------\n") fmt.Printf("------------------------Leetcode Problem 19------------------------\n")
for _, q := range qs { for _, q := range qs {
_, p := q.ans19, q.para19 _, p := q.ans19, q.para19
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(removeNthFromEnd(S2l(p.one), p.n))) fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(removeNthFromEnd(structures.Ints2List(p.one), p.n)))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question21 struct { type question21 struct {
@ -73,7 +75,7 @@ func Test_Problem21(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans21, q.para21 _, p := q.ans21, q.para21
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(mergeTwoLists(S2l(p.one), S2l(p.another)))) fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(mergeTwoLists(structures.Ints2List(p.one), structures.Ints2List(p.another))))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question23 struct { type question23 struct {
@ -94,9 +96,9 @@ func Test_Problem23(t *testing.T) {
for _, q := range qs { for _, q := range qs {
var ls []*ListNode var ls []*ListNode
for _, qq := range q.para23.one { for _, qq := range q.para23.one {
ls = append(ls, S2l(qq)) ls = append(ls, structures.Ints2List(qq))
} }
fmt.Printf("【input】:%v 【output】:%v\n", q.para23.one, L2s(mergeKLists(ls))) fmt.Printf("【input】:%v 【output】:%v\n", q.para23.one, structures.List2Ints(mergeKLists(ls)))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question24 struct { type question24 struct {
@ -53,39 +55,7 @@ func Test_Problem24(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans24, q.para24 _, p := q.ans24, q.para24
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(swapPairs(S2l(p.one)))) fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(swapPairs(structures.Ints2List(p.one))))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }
// convert *ListNode to []int
func L2s(head *ListNode) []int {
res := []int{}
for head != nil {
res = append(res, head.Val)
head = head.Next
}
return res
}
// convert []int to *ListNode
func S2l(nums []int) *ListNode {
if len(nums) == 0 {
return nil
}
res := &ListNode{
Val: nums[0],
}
temp := res
for i := 1; i < len(nums); i++ {
temp.Next = &ListNode{
Val: nums[i],
}
temp = temp.Next
}
return res
}

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question25 struct { type question25 struct {
@ -48,7 +50,7 @@ func Test_Problem25(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans25, q.para25 _, p := q.ans25, q.para25
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(reverseKGroup(S2l(p.one), p.two))) fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(reverseKGroup(structures.Ints2List(p.one), p.two)))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -91,3 +91,10 @@ func divide1(divided int, divisor int) int {
} }
return sign * result return sign * result
} }
func abs(a int) int {
if a > 0 {
return a
}
return -a
}

View File

@ -39,3 +39,10 @@ func maxSubArray1(nums []int) int {
} }
return maxSum return maxSum
} }
func max(a int, b int) int {
if a > b {
return a
}
return b
}

View File

@ -81,10 +81,10 @@ func spiralOrder2(matrix [][]int) []int {
} }
// top、left、right、bottom 分别是剩余区域的上、左、右、下的下标 // top、left、right、bottom 分别是剩余区域的上、左、右、下的下标
top, left, bottom, right := 0, 0, m-1, n-1 top, left, bottom, right := 0, 0, m-1, n-1
count, sum := 0, m*n count, sum := 0, m*n
res := []int{} res := []int{}
// 外层循环每次遍历一圈 // 外层循环每次遍历一圈
for count < sum { for count < sum {
i, j := top, left i, j := top, left
@ -93,19 +93,19 @@ func spiralOrder2(matrix [][]int) []int {
count++ count++
j++ j++
} }
i, j = top + 1, right i, j = top+1, right
for i <= bottom && count < sum { for i <= bottom && count < sum {
res = append(res, matrix[i][j]) res = append(res, matrix[i][j])
count++ count++
i++ i++
} }
i, j = bottom, right - 1 i, j = bottom, right-1
for j >= left && count < sum { for j >= left && count < sum {
res = append(res, matrix[i][j]) res = append(res, matrix[i][j])
count++ count++
j-- j--
} }
i, j = bottom - 1, left i, j = bottom-1, left
for i > top && count < sum { for i > top && count < sum {
res = append(res, matrix[i][j]) res = append(res, matrix[i][j])
count++ count++

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// Interval define
type Interval = structures.Interval
/** /**
* Definition for an interval. * Definition for an interval.
* type Interval struct { * type Interval struct {
@ -8,12 +15,6 @@ package leetcode
* } * }
*/ */
// Interval define
type Interval struct {
Start int
End int
}
func merge56(intervals []Interval) []Interval { func merge56(intervals []Interval) []Interval {
if len(intervals) == 0 { if len(intervals) == 0 {
return intervals return intervals

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// Interval define
type Interval = structures.Interval
/** /**
* Definition for an interval. * Definition for an interval.
* type Interval struct { * type Interval struct {
@ -32,3 +39,17 @@ func insert(intervals []Interval, newInterval Interval) []Interval {
} }
return res return res
} }
func max(a int, b int) int {
if a > b {
return a
}
return b
}
func min(a int, b int) int {
if a > b {
return b
}
return a
}

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question61 struct { type question61 struct {
@ -62,7 +64,7 @@ func Test_Problem61(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans61, q.para61 _, p := q.ans61, q.para61
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(rotateRight(S2l(p.one), p.k))) fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(rotateRight(structures.Ints2List(p.one), p.k)))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -55,3 +55,10 @@ func minPathSum1(grid [][]int) int {
} }
return dp[m-1][n-1] return dp[m-1][n-1]
} }
func min(a int, b int) int {
if a > b {
return b
}
return a
}

View File

@ -51,9 +51,9 @@ func subsets2(nums []int) [][]int {
sum := 1 << uint(len(nums)) sum := 1 << uint(len(nums))
for i := 0; i < sum; i++ { for i := 0; i < sum; i++ {
stack := []int{} stack := []int{}
tmp := i // i 从 000...000 到 111...111 tmp := i // i 从 000...000 到 111...111
for j := len(nums) - 1; j >= 0; j-- { // 遍历 i 的每一位 for j := len(nums) - 1; j >= 0; j-- { // 遍历 i 的每一位
if tmp & 1 == 1 { if tmp&1 == 1 {
stack = append([]int{nums[j]}, stack...) stack = append([]int{nums[j]}, stack...)
} }
tmp >>= 1 tmp >>= 1

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {
@ -66,3 +73,21 @@ func deleteDuplicates2(head *ListNode) *ListNode {
head.Next = deleteDuplicates(head.Next) head.Next = deleteDuplicates(head.Next)
return head return head
} }
func deleteDuplicates(head *ListNode) *ListNode {
cur := head
if head == nil {
return nil
}
if head.Next == nil {
return head
}
for cur.Next != nil {
if cur.Next.Val == cur.Val {
cur.Next = cur.Next.Next
} else {
cur = cur.Next
}
}
return head
}

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question82 struct { type question82 struct {
@ -76,7 +78,7 @@ func Test_Problem82(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans82, q.para82 _, p := q.ans82, q.para82
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(deleteDuplicates1(S2l(p.one)))) fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(deleteDuplicates1(structures.Ints2List(p.one))))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question83 struct { type question83 struct {
@ -46,7 +48,7 @@ func Test_Problem83(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans83, q.para83 _, p := q.ans83, q.para83
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(deleteDuplicates(S2l(p.one)))) fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(deleteDuplicates(structures.Ints2List(p.one))))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -30,3 +30,10 @@ func largestRectangleArea(heights []int) int {
} }
return maxArea return maxArea
} }
func max(a int, b int) int {
if a > b {
return a
}
return b
}

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question86 struct { type question86 struct {
@ -67,7 +69,7 @@ func Test_Problem86(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans86, q.para86 _, p := q.ans86, q.para86
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(partition(S2l(p.one), p.x))) fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(partition(structures.Ints2List(p.one), p.x)))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question92 struct { type question92 struct {
@ -62,7 +64,7 @@ func Test_Problem92(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans92, q.para92 _, p := q.ans92, q.para92
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(reverseBetween(S2l(p.one), p.m, p.n))) fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(reverseBetween(structures.Ints2List(p.one), p.m, p.n)))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -8,6 +15,7 @@ package leetcode
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func inorderTraversal(root *TreeNode) []int { func inorderTraversal(root *TreeNode) []int {
var result []int var result []int
inorder(root, &result) inorder(root, &result)

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question94 struct { type question94 struct {
@ -37,7 +39,7 @@ func Test_Problem94(t *testing.T) {
}, },
question94{ question94{
para94{[]int{1, NULL, 2, 3}}, para94{[]int{1, structures.NULL, 2, 3}},
ans94{[]int{1, 2, 3}}, ans94{[]int{1, 2, 3}},
}, },
} }
@ -47,7 +49,7 @@ func Test_Problem94(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans94, q.para94 _, p := q.ans94, q.para94
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
root := Ints2TreeNode(p.one) root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", inorderTraversal(root)) fmt.Printf("【output】:%v \n", inorderTraversal(root))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -8,6 +15,7 @@ package leetcode
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func generateTrees(n int) []*TreeNode { func generateTrees(n int) []*TreeNode {
if n == 0 { if n == 0 {
return []*TreeNode{} return []*TreeNode{}

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question95 struct { type question95 struct {
@ -49,7 +51,7 @@ func Test_Problem95(t *testing.T) {
fmt.Printf("【input】:%v \n", p) fmt.Printf("【input】:%v \n", p)
trees := generateTrees(p.one) trees := generateTrees(p.one)
for _, t := range trees { for _, t := range trees {
fmt.Printf("【output】:%v\n", Tree2Preorder(t)) fmt.Printf("【output】:%v\n", structures.Tree2Preorder(t))
} }
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -2,6 +2,13 @@ package leetcode
import "math" import "math"
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question98 struct { type question98 struct {
@ -27,7 +29,7 @@ func Test_Problem98(t *testing.T) {
qs := []question98{ qs := []question98{
question98{ question98{
para98{[]int{10, 5, 15, NULL, NULL, 6, 20}}, para98{[]int{10, 5, 15, structures.NULL, structures.NULL, 6, 20}},
ans98{false}, ans98{false},
}, },
@ -42,7 +44,7 @@ func Test_Problem98(t *testing.T) {
}, },
question98{ question98{
para98{[]int{5, 1, 4, NULL, NULL, 3, 6}}, para98{[]int{5, 1, 4, structures.NULL, structures.NULL, 3, 6}},
ans98{false}, ans98{false},
}, },
} }
@ -52,7 +54,7 @@ func Test_Problem98(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans98, q.para98 _, p := q.ans98, q.para98
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
rootOne := Ints2TreeNode(p.one) rootOne := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", isValidBST(rootOne)) fmt.Printf("【output】:%v \n", isValidBST(rootOne))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -8,6 +15,7 @@ package leetcode
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func recoverTree(root *TreeNode) { func recoverTree(root *TreeNode) {
var prev, target1, target2 *TreeNode var prev, target1, target2 *TreeNode
_, target1, target2 = inOrderTraverse(root, prev, target1, target2) _, target1, target2 = inOrderTraverse(root, prev, target1, target2)

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question99 struct { type question99 struct {
@ -27,13 +29,13 @@ func Test_Problem99(t *testing.T) {
qs := []question99{ qs := []question99{
question99{ question99{
para99{[]int{1, 3, NULL, NULL, 2}}, para99{[]int{1, 3, structures.NULL, structures.NULL, 2}},
ans99{[]int{3, 1, NULL, NULL, 2}}, ans99{[]int{3, 1, structures.NULL, structures.NULL, 2}},
}, },
question99{ question99{
para99{[]int{3, 1, 4, NULL, NULL, 2}}, para99{[]int{3, 1, 4, structures.NULL, structures.NULL, 2}},
ans99{[]int{2, 1, 4, NULL, NULL, 3}}, ans99{[]int{2, 1, 4, structures.NULL, structures.NULL, 3}},
}, },
} }
@ -42,7 +44,7 @@ func Test_Problem99(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans99, q.para99 _, p := q.ans99, q.para99
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
rootOne := Ints2TreeNode(p.one) rootOne := structures.Ints2TreeNode(p.one)
recoverTree(rootOne) recoverTree(rootOne)
fmt.Printf("【output】:%v \n", p) fmt.Printf("【output】:%v \n", p)
} }

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -8,6 +15,7 @@ package leetcode
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func isSameTree(p *TreeNode, q *TreeNode) bool { func isSameTree(p *TreeNode, q *TreeNode) bool {
if p == nil && q == nil { if p == nil && q == nil {
return true return true

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question100 struct { type question100 struct {
@ -48,7 +50,7 @@ func Test_Problem100(t *testing.T) {
}, },
question100{ question100{
para100{[]int{1, 2}, []int{1, NULL, 2}}, para100{[]int{1, 2}, []int{1, structures.NULL, 2}},
ans100{false}, ans100{false},
}, },
@ -63,8 +65,8 @@ func Test_Problem100(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans100, q.para100 _, p := q.ans100, q.para100
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
rootOne := Ints2TreeNode(p.one) rootOne := structures.Ints2TreeNode(p.one)
rootTwo := Ints2TreeNode(p.two) rootTwo := structures.Ints2TreeNode(p.two)
fmt.Printf("【output】:%v \n", isSameTree(rootOne, rootTwo)) fmt.Printf("【output】:%v \n", isSameTree(rootOne, rootTwo))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -8,9 +15,33 @@ package leetcode
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func isSymmetric(root *TreeNode) bool { func isSymmetric(root *TreeNode) bool {
if root == nil { if root == nil {
return true return true
} }
return isSameTree(invertTree(root.Left), root.Right) return isSameTree(invertTree(root.Left), root.Right)
} }
func isSameTree(p *TreeNode, q *TreeNode) bool {
if p == nil && q == nil {
return true
} else if p != nil && q != nil {
if p.Val != q.Val {
return false
}
return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
} else {
return false
}
}
func invertTree(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
invertTree(root.Left)
invertTree(root.Right)
root.Left, root.Right = root.Right, root.Left
return root
}

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question101 struct { type question101 struct {
@ -27,12 +29,12 @@ func Test_Problem101(t *testing.T) {
qs := []question101{ qs := []question101{
question101{ question101{
para101{[]int{3, 4, 4, 5, NULL, NULL, 5, 6, NULL, NULL, 6}}, para101{[]int{3, 4, 4, 5, structures.NULL, structures.NULL, 5, 6, structures.NULL, structures.NULL, 6}},
ans101{true}, ans101{true},
}, },
question101{ question101{
para101{[]int{1, 2, 2, NULL, 3, 3}}, para101{[]int{1, 2, 2, structures.NULL, 3, 3}},
ans101{true}, ans101{true},
}, },
@ -57,7 +59,7 @@ func Test_Problem101(t *testing.T) {
}, },
question101{ question101{
para101{[]int{1, 2, 2, NULL, 3, NULL, 3}}, para101{[]int{1, 2, 2, structures.NULL, 3, structures.NULL, 3}},
ans101{false}, ans101{false},
}, },
} }
@ -67,7 +69,7 @@ func Test_Problem101(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans101, q.para101 _, p := q.ans101, q.para101
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
rootOne := Ints2TreeNode(p.one) rootOne := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", isSymmetric(rootOne)) fmt.Printf("【output】:%v \n", isSymmetric(rootOne))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question102 struct { type question102 struct {
@ -37,7 +39,7 @@ func Test_Problem102(t *testing.T) {
}, },
question102{ question102{
para102{[]int{3, 9, 20, NULL, NULL, 15, 7}}, para102{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
ans102{[][]int{[]int{3}, []int{9, 20}, []int{15, 7}}}, ans102{[][]int{[]int{3}, []int{9, 20}, []int{15, 7}}},
}, },
} }
@ -47,7 +49,7 @@ func Test_Problem102(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans102, q.para102 _, p := q.ans102, q.para102
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
root := Ints2TreeNode(p.one) root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", levelOrder(root)) fmt.Printf("【output】:%v \n", levelOrder(root))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -8,6 +15,7 @@ package leetcode
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func zigzagLevelOrder(root *TreeNode) [][]int { func zigzagLevelOrder(root *TreeNode) [][]int {
if root == nil { if root == nil {
return [][]int{} return [][]int{}

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question103 struct { type question103 struct {
@ -37,12 +39,12 @@ func Test_Problem103(t *testing.T) {
}, },
question103{ question103{
para103{[]int{3, 9, 20, NULL, NULL, 15, 7}}, para103{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
ans103{[][]int{[]int{3}, []int{9, 20}, []int{15, 7}}}, ans103{[][]int{[]int{3}, []int{9, 20}, []int{15, 7}}},
}, },
question103{ question103{
para103{[]int{1, 2, 3, 4, NULL, NULL, 5}}, para103{[]int{1, 2, 3, 4, structures.NULL, structures.NULL, 5}},
ans103{[][]int{[]int{1}, []int{3, 2}, []int{4, 5}}}, ans103{[][]int{[]int{1}, []int{3, 2}, []int{4, 5}}},
}, },
} }
@ -52,7 +54,7 @@ func Test_Problem103(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans103, q.para103 _, p := q.ans103, q.para103
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
root := Ints2TreeNode(p.one) root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", zigzagLevelOrder(root)) fmt.Printf("【output】:%v \n", zigzagLevelOrder(root))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -8,9 +15,17 @@ package leetcode
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func maxDepth(root *TreeNode) int { func maxDepth(root *TreeNode) int {
if root == nil { if root == nil {
return 0 return 0
} }
return max(maxDepth(root.Left), maxDepth(root.Right)) + 1 return max(maxDepth(root.Left), maxDepth(root.Right)) + 1
} }
func max(a int, b int) int {
if a > b {
return a
}
return b
}

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question104 struct { type question104 struct {
@ -32,7 +34,7 @@ func Test_Problem104(t *testing.T) {
}, },
question104{ question104{
para104{[]int{3, 9, 20, NULL, NULL, 15, 7}}, para104{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
ans104{3}, ans104{3},
}, },
} }
@ -42,7 +44,7 @@ func Test_Problem104(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans104, q.para104 _, p := q.ans104, q.para104
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
root := Ints2TreeNode(p.one) root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", maxDepth(root)) fmt.Printf("【output】:%v \n", maxDepth(root))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -8,6 +15,7 @@ package leetcode
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func buildTree(preorder []int, inorder []int) *TreeNode { func buildTree(preorder []int, inorder []int) *TreeNode {
inPos := make(map[int]int) inPos := make(map[int]int)
for i := 0; i < len(inorder); i++ { for i := 0; i < len(inorder); i++ {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question105 struct { type question105 struct {
@ -29,7 +31,7 @@ func Test_Problem105(t *testing.T) {
question105{ question105{
para105{[]int{3, 9, 20, 15, 7}, []int{9, 3, 15, 20, 7}}, para105{[]int{3, 9, 20, 15, 7}, []int{9, 3, 15, 20, 7}},
ans105{[]int{3, 9, 20, NULL, NULL, 15, 7}}, ans105{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
}, },
} }
@ -38,7 +40,7 @@ func Test_Problem105(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans105, q.para105 _, p := q.ans105, q.para105
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
fmt.Printf("【output】:%v \n", Tree2ints(buildTree(p.preorder, p.inorder))) fmt.Printf("【output】:%v \n", structures.Tree2ints(buildTree(p.preorder, p.inorder)))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -8,7 +15,8 @@ package leetcode
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func buildTree106(inorder []int, postorder []int) *TreeNode {
func buildTree(inorder []int, postorder []int) *TreeNode {
inPos := make(map[int]int) inPos := make(map[int]int)
for i := 0; i < len(inorder); i++ { for i := 0; i < len(inorder); i++ {
inPos[inorder[i]] = i inPos[inorder[i]] = i

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question106 struct { type question106 struct {
@ -29,7 +31,7 @@ func Test_Problem106(t *testing.T) {
question106{ question106{
para106{[]int{9, 3, 15, 20, 7}, []int{9, 15, 7, 20, 3}}, para106{[]int{9, 3, 15, 20, 7}, []int{9, 15, 7, 20, 3}},
ans106{[]int{3, 9, 20, NULL, NULL, 15, 7}}, ans106{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
}, },
} }
@ -38,7 +40,7 @@ func Test_Problem106(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans106, q.para106 _, p := q.ans106, q.para106
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
fmt.Printf("【output】:%v \n", Tree2ints(buildTree106(p.inorder, p.postorder))) fmt.Printf("【output】:%v \n", structures.Tree2ints(buildTree(p.inorder, p.postorder)))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -8,6 +15,7 @@ package leetcode
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func levelOrderBottom(root *TreeNode) [][]int { func levelOrderBottom(root *TreeNode) [][]int {
tmp := levelOrder(root) tmp := levelOrder(root)
res := [][]int{} res := [][]int{}
@ -16,3 +24,35 @@ func levelOrderBottom(root *TreeNode) [][]int {
} }
return res return res
} }
func levelOrder(root *TreeNode) [][]int {
if root == nil {
return [][]int{}
}
queue := []*TreeNode{}
queue = append(queue, root)
curNum, nextLevelNum, res, tmp := 1, 0, [][]int{}, []int{}
for len(queue) != 0 {
if curNum > 0 {
node := queue[0]
if node.Left != nil {
queue = append(queue, node.Left)
nextLevelNum++
}
if node.Right != nil {
queue = append(queue, node.Right)
nextLevelNum++
}
curNum--
tmp = append(tmp, node.Val)
queue = queue[1:]
}
if curNum == 0 {
res = append(res, tmp)
curNum = nextLevelNum
nextLevelNum = 0
tmp = []int{}
}
}
return res
}

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question107 struct { type question107 struct {
@ -37,7 +39,7 @@ func Test_Problem107(t *testing.T) {
}, },
question107{ question107{
para107{[]int{3, 9, 20, NULL, NULL, 15, 7}}, para107{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
ans107{[][]int{[]int{15, 7}, []int{9, 20}, []int{3}}}, ans107{[][]int{[]int{15, 7}, []int{9, 20}, []int{3}}},
}, },
} }
@ -47,7 +49,7 @@ func Test_Problem107(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans107, q.para107 _, p := q.ans107, q.para107
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
root := Ints2TreeNode(p.one) root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", levelOrderBottom(root)) fmt.Printf("【output】:%v \n", levelOrderBottom(root))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -8,6 +15,7 @@ package leetcode
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func sortedArrayToBST(nums []int) *TreeNode { func sortedArrayToBST(nums []int) *TreeNode {
if len(nums) == 0 { if len(nums) == 0 {
return nil return nil

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question108 struct { type question108 struct {
@ -28,7 +30,7 @@ func Test_Problem108(t *testing.T) {
question108{ question108{
para108{[]int{-10, -3, 0, 5, 9}}, para108{[]int{-10, -3, 0, 5, 9}},
ans108{[]int{0, -3, 9, -10, NULL, 5}}, ans108{[]int{0, -3, 9, -10, structures.NULL, 5}},
}, },
} }
@ -37,7 +39,7 @@ func Test_Problem108(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans108, q.para108 _, p := q.ans108, q.para108
arr := []int{} arr := []int{}
T2s(sortedArrayToBST(p.one), &arr) structures.T2s(sortedArrayToBST(p.one), &arr)
fmt.Printf("【input】:%v 【output】:%v\n", p, arr) fmt.Printf("【input】:%v 【output】:%v\n", p, arr)
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {
@ -7,6 +14,10 @@ package leetcode
* Next *ListNode * Next *ListNode
* } * }
*/ */
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -16,13 +27,6 @@ package leetcode
* } * }
*/ */
// TreeNode define
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func sortedListToBST(head *ListNode) *TreeNode { func sortedListToBST(head *ListNode) *TreeNode {
if head == nil { if head == nil {
return nil return nil

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question109 struct { type question109 struct {
@ -28,7 +30,7 @@ func Test_Problem109(t *testing.T) {
question109{ question109{
para109{[]int{-10, -3, 0, 5, 9}}, para109{[]int{-10, -3, 0, 5, 9}},
ans109{[]int{0, -10, 5, NULL, -3, NULL, 9}}, ans109{[]int{0, -10, 5, structures.NULL, -3, structures.NULL, 9}},
}, },
question109{ question109{
@ -38,7 +40,7 @@ func Test_Problem109(t *testing.T) {
question109{ question109{
para109{[]int{1, 2}}, para109{[]int{1, 2}},
ans109{[]int{2, 1}}, ans109{[]int{1, 2}},
}, },
question109{ question109{
@ -52,7 +54,7 @@ func Test_Problem109(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans109, q.para109 _, p := q.ans109, q.para109
arr := []int{} arr := []int{}
T2s(sortedListToBST(S2l(p.one)), &arr) structures.T2s(sortedListToBST(structures.Ints2List(p.one)), &arr)
fmt.Printf("【input】:%v 【output】:%v\n", p, arr) fmt.Printf("【input】:%v 【output】:%v\n", p, arr)
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -8,6 +15,7 @@ package leetcode
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func isBalanced(root *TreeNode) bool { func isBalanced(root *TreeNode) bool {
if root == nil { if root == nil {
return true return true
@ -23,3 +31,17 @@ func depth(root *TreeNode) int {
} }
return max(depth(root.Left), depth(root.Right)) + 1 return max(depth(root.Left), depth(root.Right)) + 1
} }
func abs(a int) int {
if a > 0 {
return a
}
return -a
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question110 struct { type question110 struct {
@ -27,12 +29,12 @@ func Test_Problem110(t *testing.T) {
qs := []question110{ qs := []question110{
question110{ question110{
para110{[]int{3, 4, 4, 5, NULL, NULL, 5, 6, NULL, NULL, 6}}, para110{[]int{3, 4, 4, 5, structures.NULL, structures.NULL, 5, 6, structures.NULL, structures.NULL, 6}},
ans110{false}, ans110{false},
}, },
question110{ question110{
para110{[]int{1, 2, 2, NULL, 3, 3}}, para110{[]int{1, 2, 2, structures.NULL, 3, 3}},
ans110{true}, ans110{true},
}, },
@ -57,7 +59,7 @@ func Test_Problem110(t *testing.T) {
}, },
question110{ question110{
para110{[]int{1, 2, 2, NULL, 3, NULL, 3}}, para110{[]int{1, 2, 2, structures.NULL, 3, structures.NULL, 3}},
ans110{true}, ans110{true},
}, },
} }
@ -67,7 +69,7 @@ func Test_Problem110(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans110, q.para110 _, p := q.ans110, q.para110
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
rootOne := Ints2TreeNode(p.one) rootOne := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", isBalanced(rootOne)) fmt.Printf("【output】:%v \n", isBalanced(rootOne))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -8,6 +15,7 @@ package leetcode
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func minDepth(root *TreeNode) int { func minDepth(root *TreeNode) int {
if root == nil { if root == nil {
return 0 return 0
@ -20,3 +28,10 @@ func minDepth(root *TreeNode) int {
} }
return min(minDepth(root.Left), minDepth(root.Right)) + 1 return min(minDepth(root.Left), minDepth(root.Right)) + 1
} }
func min(a int, b int) int {
if a > b {
return b
}
return a
}

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question111 struct { type question111 struct {
@ -37,7 +39,7 @@ func Test_Problem111(t *testing.T) {
}, },
question111{ question111{
para111{[]int{3, 9, 20, NULL, NULL, 15, 7}}, para111{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
ans111{2}, ans111{2},
}, },
@ -52,7 +54,7 @@ func Test_Problem111(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans111, q.para111 _, p := q.ans111, q.para111
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
root := Ints2TreeNode(p.one) root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", minDepth(root)) fmt.Printf("【output】:%v \n", minDepth(root))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -8,6 +15,7 @@ package leetcode
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func hasPathSum(root *TreeNode, sum int) bool { func hasPathSum(root *TreeNode, sum int) bool {
if root == nil { if root == nil {
return false return false

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question112 struct { type question112 struct {
@ -33,7 +35,7 @@ func Test_Problem112(t *testing.T) {
}, },
question112{ question112{
para112{[]int{5, 4, 8, 11, NULL, 13, 4, 7, 2, NULL, NULL, NULL, 1}, 22}, para112{[]int{5, 4, 8, 11, structures.NULL, 13, 4, 7, 2, structures.NULL, structures.NULL, structures.NULL, 1}, 22},
ans112{true}, ans112{true},
}, },
} }
@ -43,7 +45,7 @@ func Test_Problem112(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans112, q.para112 _, p := q.ans112, q.para112
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
root := Ints2TreeNode(p.one) root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", hasPathSum(root, p.sum)) fmt.Printf("【output】:%v \n", hasPathSum(root, p.sum))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question113 struct { type question113 struct {
@ -37,7 +39,7 @@ func Test_Problem113(t *testing.T) {
}, },
question113{ question113{
para113{[]int{5, 4, 8, 11, NULL, 13, 4, 7, 2, NULL, NULL, 5, 1}, 22}, para113{[]int{5, 4, 8, 11, structures.NULL, 13, 4, 7, 2, structures.NULL, structures.NULL, 5, 1}, 22},
ans113{[][]int{[]int{5, 4, 11, 2}, []int{5, 8, 4, 5}}}, ans113{[][]int{[]int{5, 4, 11, 2}, []int{5, 8, 4, 5}}},
}, },
} }
@ -47,7 +49,7 @@ func Test_Problem113(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans113, q.para113 _, p := q.ans113, q.para113
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
root := Ints2TreeNode(p.one) root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", pathSum(root, p.sum)) fmt.Printf("【output】:%v \n", pathSum(root, p.sum))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -22,6 +29,14 @@ func flatten(root *TreeNode) {
return return
} }
func preorder(root *TreeNode, output *[]int) {
if root != nil {
*output = append(*output, root.Val)
preorder(root.Left, output)
preorder(root.Right, output)
}
}
// 解法二 递归 // 解法二 递归
func flatten1(root *TreeNode) { func flatten1(root *TreeNode) {
if root == nil || (root.Left == nil && root.Right == nil) { if root == nil || (root.Left == nil && root.Right == nil) {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question114 struct { type question114 struct {
@ -37,9 +39,9 @@ func Test_Problem114(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans114, q.para114 _, p := q.ans114, q.para114
fmt.Printf("【input】:%v \n", p) fmt.Printf("【input】:%v \n", p)
rootOne := Ints2TreeNode(p.one) rootOne := structures.Ints2TreeNode(p.one)
flatten(rootOne) flatten(rootOne)
fmt.Printf("【output】:%v \n", Tree2Preorder(rootOne)) fmt.Printf("【output】:%v \n", structures.Tree2Preorder(rootOne))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -17,6 +17,13 @@ func minimumTotal(triangle [][]int) int {
return triangle[0][0] return triangle[0][0]
} }
func min(a int, b int) int {
if a > b {
return b
}
return a
}
// 解法二 正常 DP空间复杂度 O(n) // 解法二 正常 DP空间复杂度 O(n)
func minimumTotal1(triangle [][]int) int { func minimumTotal1(triangle [][]int) int {
if len(triangle) == 0 { if len(triangle) == 0 {

View File

@ -40,3 +40,10 @@ func maxProfit1(prices []int) int {
} }
return res return res
} }
func max(a int, b int) int {
if a > b {
return a
}
return b
}

View File

@ -2,6 +2,13 @@ package leetcode
import "math" import "math"
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -10,6 +17,7 @@ import "math"
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func maxPathSum(root *TreeNode) int { func maxPathSum(root *TreeNode) int {
if root == nil { if root == nil {
return 0 return 0
@ -30,3 +38,10 @@ func getPathSum(root *TreeNode, maxSum *int) int {
*maxSum = max(*maxSum, max(currMax, left+right+root.Val)) *maxSum = max(*maxSum, max(currMax, left+right+root.Val))
return currMax return currMax
} }
func max(a int, b int) int {
if a > b {
return a
}
return b
}

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question124 struct { type question124 struct {
@ -42,7 +44,7 @@ func Test_Problem124(t *testing.T) {
}, },
question124{ question124{
para124{[]int{-10, 9, 20, NULL, NULL, 15, 7}}, para124{[]int{-10, 9, 20, structures.NULL, structures.NULL, 15, 7}},
ans124{42}, ans124{42},
}, },
} }
@ -52,7 +54,7 @@ func Test_Problem124(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans124, q.para124 _, p := q.ans124, q.para124
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
root := Ints2TreeNode(p.one) root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", maxPathSum(root)) fmt.Printf("【output】:%v \n", maxPathSum(root))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,12 +1,12 @@
package leetcode package leetcode
import ( import (
"strings" "strings"
) )
func isPalindrome(s string) bool { func isPalindrome(s string) bool {
s = strings.ToLower(s) s = strings.ToLower(s)
i, j := 0, len(s)-1 i, j := 0, len(s)-1
for i < j { for i < j {

View File

@ -36,6 +36,13 @@ func longestConsecutive(nums []int) int {
return res return res
} }
func max(a int, b int) int {
if a > b {
return a
}
return b
}
// 解法二 并查集 // 解法二 并查集
func longestConsecutive1(nums []int) int { func longestConsecutive1(nums []int) int {
if len(nums) == 0 { if len(nums) == 0 {

View File

@ -4,6 +4,13 @@ import (
"strconv" "strconv"
) )
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -12,6 +19,7 @@ import (
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func sumNumbers(root *TreeNode) int { func sumNumbers(root *TreeNode) int {
res, nums := 0, binaryTreeNums(root) res, nums := 0, binaryTreeNums(root)
for _, n := range nums { for _, n := range nums {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question129 struct { type question129 struct {
@ -47,7 +49,7 @@ func Test_Problem129(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans129, q.para129 _, p := q.ans129, q.para129
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
root := Ints2TreeNode(p.one) root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", sumNumbers(root)) fmt.Printf("【output】:%v \n", sumNumbers(root))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -4,6 +4,13 @@ import (
"github.com/halfrost/LeetCode-Go/template" "github.com/halfrost/LeetCode-Go/template"
) )
var dir = [][]int{
[]int{-1, 0},
[]int{0, 1},
[]int{1, 0},
[]int{0, -1},
}
// 解法一 并查集 // 解法一 并查集
func solve(board [][]byte) { func solve(board [][]byte) {
if len(board) == 0 { if len(board) == 0 {

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* struct ListNode { * struct ListNode {

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question143 struct { type question143 struct {
@ -50,7 +52,7 @@ func Test_Problem143(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans143, q.para143 _, p := q.ans143, q.para143
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(reorderList(S2l(p.one)))) fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(reorderList(structures.Ints2List(p.one))))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question144 struct { type question144 struct {
@ -37,7 +39,7 @@ func Test_Problem144(t *testing.T) {
}, },
question144{ question144{
para144{[]int{1, NULL, 2, 3}}, para144{[]int{1, structures.NULL, 2, 3}},
ans144{[]int{1, 2, 3}}, ans144{[]int{1, 2, 3}},
}, },
} }
@ -47,167 +49,8 @@ func Test_Problem144(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans144, q.para144 _, p := q.ans144, q.para144
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
root := Ints2TreeNode(p.one) root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", preorderTraversal(root)) fmt.Printf("【output】:%v \n", preorderTraversal(root))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }
var NULL = -1 << 63
// Ints2TreeNode 利用 []int 生成 *TreeNode
func Ints2TreeNode(ints []int) *TreeNode {
n := len(ints)
if n == 0 {
return nil
}
root := &TreeNode{
Val: ints[0],
}
queue := make([]*TreeNode, 1, n*2)
queue[0] = root
i := 1
for i < n {
node := queue[0]
queue = queue[1:]
if i < n && ints[i] != NULL {
node.Left = &TreeNode{Val: ints[i]}
queue = append(queue, node.Left)
}
i++
if i < n && ints[i] != NULL {
node.Right = &TreeNode{Val: ints[i]}
queue = append(queue, node.Right)
}
i++
}
return root
}
func indexOf(val int, nums []int) int {
for i, v := range nums {
if v == val {
return i
}
}
msg := fmt.Sprintf("%d 不存在于 %v 中", val, nums)
panic(msg)
}
// PreIn2Tree 把 preorder 和 inorder 切片转换成 二叉树
func PreIn2Tree(pre, in []int) *TreeNode {
if len(pre) != len(in) {
panic("preIn2Tree 中两个切片的长度不相等")
}
if len(in) == 0 {
return nil
}
res := &TreeNode{
Val: pre[0],
}
if len(in) == 1 {
return res
}
idx := indexOf(res.Val, in)
res.Left = PreIn2Tree(pre[1:idx+1], in[:idx])
res.Right = PreIn2Tree(pre[idx+1:], in[idx+1:])
return res
}
// InPost2Tree 把 inorder 和 postorder 切片转换成 二叉树
func InPost2Tree(in, post []int) *TreeNode {
if len(post) != len(in) {
panic("inPost2Tree 中两个切片的长度不相等")
}
if len(in) == 0 {
return nil
}
res := &TreeNode{
Val: post[len(post)-1],
}
if len(in) == 1 {
return res
}
idx := indexOf(res.Val, in)
res.Left = InPost2Tree(in[:idx], post[:idx])
res.Right = InPost2Tree(in[idx+1:], post[idx:len(post)-1])
return res
}
// Tree2Preorder 把 二叉树 转换成 preorder 的切片
func Tree2Preorder(root *TreeNode) []int {
if root == nil {
return nil
}
if root.Left == nil && root.Right == nil {
return []int{root.Val}
}
res := []int{root.Val}
res = append(res, Tree2Preorder(root.Left)...)
res = append(res, Tree2Preorder(root.Right)...)
return res
}
// Tree2Inorder 把 二叉树转换成 inorder 的切片
func Tree2Inorder(root *TreeNode) []int {
if root == nil {
return nil
}
if root.Left == nil && root.Right == nil {
return []int{root.Val}
}
res := Tree2Inorder(root.Left)
res = append(res, root.Val)
res = append(res, Tree2Inorder(root.Right)...)
return res
}
// Tree2Postorder 把 二叉树 转换成 postorder 的切片
func Tree2Postorder(root *TreeNode) []int {
if root == nil {
return nil
}
if root.Left == nil && root.Right == nil {
return []int{root.Val}
}
res := Tree2Postorder(root.Left)
res = append(res, Tree2Postorder(root.Right)...)
res = append(res, root.Val)
return res
}
// Equal return ture if tn == a
func (tn *TreeNode) Equal(a *TreeNode) bool {
if tn == nil && a == nil {
return true
}
if tn == nil || a == nil || tn.Val != a.Val {
return false
}
return tn.Left.Equal(a.Left) && tn.Right.Equal(a.Right)
}
// Tree2ints 把 *TreeNode 按照行还原成 []int
func Tree2ints(tn *TreeNode) []int {
res := make([]int, 0, 1024)
queue := []*TreeNode{tn}
for len(queue) > 0 {
size := len(queue)
for i := 0; i < size; i++ {
nd := queue[i]
if nd == nil {
res = append(res, NULL)
} else {
res = append(res, nd.Val)
queue = append(queue, nd.Left, nd.Right)
}
}
queue = queue[size:]
}
i := len(res)
for i > 0 && res[i-1] == NULL {
i--
}
return res[:i]
}

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -8,6 +15,7 @@ package leetcode
* Right *TreeNode * Right *TreeNode
* } * }
*/ */
func postorderTraversal(root *TreeNode) []int { func postorderTraversal(root *TreeNode) []int {
var result []int var result []int
postorder(root, &result) postorder(root, &result)

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question145 struct { type question145 struct {
@ -37,7 +39,7 @@ func Test_Problem145(t *testing.T) {
}, },
question145{ question145{
para145{[]int{1, NULL, 2, 3}}, para145{[]int{1, structures.NULL, 2, 3}},
ans145{[]int{1, 2, 3}}, ans145{[]int{1, 2, 3}},
}, },
} }
@ -47,7 +49,7 @@ func Test_Problem145(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans145, q.para145 _, p := q.ans145, q.para145
fmt.Printf("【input】:%v ", p) fmt.Printf("【input】:%v ", p)
root := Ints2TreeNode(p.one) root := structures.Ints2TreeNode(p.one)
fmt.Printf("【output】:%v \n", postorderTraversal(root)) fmt.Printf("【output】:%v \n", postorderTraversal(root))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question147 struct { type question147 struct {
@ -50,7 +52,7 @@ func Test_Problem147(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans147, q.para147 _, p := q.ans147, q.para147
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(insertionSortList(S2l(p.one)))) fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(insertionSortList(structures.Ints2List(p.one))))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -1,5 +1,12 @@
package leetcode package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {
@ -55,3 +62,18 @@ func mergeTwoLists148(l1 *ListNode, l2 *ListNode) *ListNode {
l2.Next = mergeTwoLists(l1, l2.Next) l2.Next = mergeTwoLists(l1, l2.Next)
return l2 return l2
} }
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
if l1 == nil {
return l2
}
if l2 == nil {
return l1
}
if l1.Val < l2.Val {
l1.Next = mergeTwoLists(l1.Next, l2)
return l1
}
l2.Next = mergeTwoLists(l1, l2.Next)
return l2
}

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question148 struct { type question148 struct {
@ -50,7 +52,7 @@ func Test_Problem148(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans148, q.para148 _, p := q.ans148, q.para148
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(sortList(S2l(p.one)))) fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(sortList(structures.Ints2List(p.one))))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -12,3 +12,17 @@ func maxProduct(nums []int) int {
} }
return res return res
} }
func max(a int, b int) int {
if a > b {
return a
}
return b
}
func min(a int, b int) int {
if a > b {
return b
}
return a
}

View File

@ -2,6 +2,13 @@ package leetcode
import "fmt" import "fmt"
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/** /**
* Definition for singly-linked list. * Definition for singly-linked list.
* type ListNode struct { * type ListNode struct {

View File

@ -3,6 +3,8 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
type question160 struct { type question160 struct {
@ -62,7 +64,7 @@ func Test_Problem160(t *testing.T) {
for _, q := range qs { for _, q := range qs {
_, p := q.ans160, q.para160 _, p := q.ans160, q.para160
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(getIntersectionNode(S2l(p.one), S2l(p.another)))) fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(getIntersectionNode(structures.Ints2List(p.one), structures.Ints2List(p.another))))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")
} }

View File

@ -85,3 +85,10 @@ func maximumGap1(nums []int) int {
return maxValue return maxValue
} }
func max(a int, b int) int {
if a > b {
return a
}
return b
}

View File

@ -2,6 +2,13 @@ package leetcode
import "container/heap" import "container/heap"
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
* type TreeNode struct { * type TreeNode struct {
@ -28,6 +35,14 @@ func Constructor173(root *TreeNode) BSTIterator {
return bs return bs
} }
func postorder(root *TreeNode, output *[]int) {
if root != nil {
postorder(root.Left, output)
postorder(root.Right, output)
*output = append(*output, root.Val)
}
}
/** @return the next smallest number */ /** @return the next smallest number */
func (this *BSTIterator) Next() int { func (this *BSTIterator) Next() int {
this.count-- this.count--

View File

@ -3,11 +3,13 @@ package leetcode
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/halfrost/LeetCode-Go/structures"
) )
func Test_Problem173(t *testing.T) { func Test_Problem173(t *testing.T) {
root := Ints2TreeNode([]int{9, 7, 15, 3, NULL, NULL, 20}) root := structures.Ints2TreeNode([]int{9, 7, 15, 3, structures.NULL, structures.NULL, 20})
obj := Constructor173(root) obj := Constructor173(root)
fmt.Printf("obj = %v\n", obj) fmt.Printf("obj = %v\n", obj)
param1 := obj.Next() param1 := obj.Next()

View File

@ -27,6 +27,20 @@ func calculateMinimumHP(dungeon [][]int) int {
return dp[0][0] return dp[0][0]
} }
func max(a int, b int) int {
if a > b {
return a
}
return b
}
func min(a int, b int) int {
if a > b {
return b
}
return a
}
// 解法二 二分搜索 // 解法二 二分搜索
func calculateMinimumHP1(dungeon [][]int) int { func calculateMinimumHP1(dungeon [][]int) int {
low, high := 1, math.MaxInt64 low, high := 1, math.MaxInt64

Some files were not shown because too many files have changed in this diff Show More