mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 08:02:30 +08:00
change path
This commit is contained in:
@ -7,7 +7,6 @@ go:
|
||||
branches:
|
||||
only:
|
||||
- master
|
||||
- stable
|
||||
|
||||
script:
|
||||
- go get -t -v ./...
|
||||
|
17
.vscode/launch.json
vendored
Normal file
17
.vscode/launch.json
vendored
Normal 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": []
|
||||
}
|
||||
]
|
||||
}
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
@ -7,6 +14,7 @@ package leetcode
|
||||
* Next *ListNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
|
||||
if l1 == nil || l2 == nil {
|
||||
return nil
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question2 struct {
|
||||
@ -73,7 +75,7 @@ func Test_Problem2(t *testing.T) {
|
||||
|
||||
for _, q := range qs {
|
||||
_, 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")
|
||||
}
|
||||
|
@ -20,3 +20,10 @@ func lengthOfLongestSubstring(s string) int {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func max(a int, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
@ -41,3 +41,17 @@ func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question19 struct {
|
||||
@ -50,23 +52,13 @@ func Test_Problem19(t *testing.T) {
|
||||
para19{[]int{1, 2, 3, 4, 5}, 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")
|
||||
|
||||
for _, q := range qs {
|
||||
_, 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")
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question21 struct {
|
||||
@ -73,7 +75,7 @@ func Test_Problem21(t *testing.T) {
|
||||
|
||||
for _, q := range qs {
|
||||
_, 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")
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question23 struct {
|
||||
@ -94,9 +96,9 @@ func Test_Problem23(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
var ls []*ListNode
|
||||
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")
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question24 struct {
|
||||
@ -53,39 +55,7 @@ func Test_Problem24(t *testing.T) {
|
||||
|
||||
for _, q := range qs {
|
||||
_, 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")
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question25 struct {
|
||||
@ -48,7 +50,7 @@ func Test_Problem25(t *testing.T) {
|
||||
|
||||
for _, q := range qs {
|
||||
_, 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")
|
||||
}
|
||||
|
@ -91,3 +91,10 @@ func divide1(divided int, divisor int) int {
|
||||
}
|
||||
return sign * result
|
||||
}
|
||||
|
||||
func abs(a int) int {
|
||||
if a > 0 {
|
||||
return a
|
||||
}
|
||||
return -a
|
||||
}
|
||||
|
@ -39,3 +39,10 @@ func maxSubArray1(nums []int) int {
|
||||
}
|
||||
return maxSum
|
||||
}
|
||||
|
||||
func max(a int, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// Interval define
|
||||
type Interval = structures.Interval
|
||||
|
||||
/**
|
||||
* Definition for an interval.
|
||||
* type Interval struct {
|
||||
@ -8,12 +15,6 @@ package leetcode
|
||||
* }
|
||||
*/
|
||||
|
||||
// Interval define
|
||||
type Interval struct {
|
||||
Start int
|
||||
End int
|
||||
}
|
||||
|
||||
func merge56(intervals []Interval) []Interval {
|
||||
if len(intervals) == 0 {
|
||||
return intervals
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// Interval define
|
||||
type Interval = structures.Interval
|
||||
|
||||
/**
|
||||
* Definition for an interval.
|
||||
* type Interval struct {
|
||||
@ -32,3 +39,17 @@ func insert(intervals []Interval, newInterval Interval) []Interval {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question61 struct {
|
||||
@ -62,7 +64,7 @@ func Test_Problem61(t *testing.T) {
|
||||
|
||||
for _, q := range qs {
|
||||
_, 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")
|
||||
}
|
||||
|
@ -55,3 +55,10 @@ func minPathSum1(grid [][]int) int {
|
||||
}
|
||||
return dp[m-1][n-1]
|
||||
}
|
||||
|
||||
func min(a int, b int) int {
|
||||
if a > b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
@ -66,3 +73,21 @@ func deleteDuplicates2(head *ListNode) *ListNode {
|
||||
head.Next = deleteDuplicates(head.Next)
|
||||
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
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question82 struct {
|
||||
@ -76,7 +78,7 @@ func Test_Problem82(t *testing.T) {
|
||||
|
||||
for _, q := range qs {
|
||||
_, 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")
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question83 struct {
|
||||
@ -46,7 +48,7 @@ func Test_Problem83(t *testing.T) {
|
||||
|
||||
for _, q := range qs {
|
||||
_, 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")
|
||||
}
|
||||
|
@ -30,3 +30,10 @@ func largestRectangleArea(heights []int) int {
|
||||
}
|
||||
return maxArea
|
||||
}
|
||||
|
||||
func max(a int, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question86 struct {
|
||||
@ -67,7 +69,7 @@ func Test_Problem86(t *testing.T) {
|
||||
|
||||
for _, q := range qs {
|
||||
_, 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")
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question92 struct {
|
||||
@ -62,7 +64,7 @@ func Test_Problem92(t *testing.T) {
|
||||
|
||||
for _, q := range qs {
|
||||
_, 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")
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,6 +15,7 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func inorderTraversal(root *TreeNode) []int {
|
||||
var result []int
|
||||
inorder(root, &result)
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question94 struct {
|
||||
@ -37,7 +39,7 @@ func Test_Problem94(t *testing.T) {
|
||||
},
|
||||
|
||||
question94{
|
||||
para94{[]int{1, NULL, 2, 3}},
|
||||
para94{[]int{1, structures.NULL, 2, 3}},
|
||||
ans94{[]int{1, 2, 3}},
|
||||
},
|
||||
}
|
||||
@ -47,7 +49,7 @@ func Test_Problem94(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans94, q.para94
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := Ints2TreeNode(p.one)
|
||||
root := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", inorderTraversal(root))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,6 +15,7 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func generateTrees(n int) []*TreeNode {
|
||||
if n == 0 {
|
||||
return []*TreeNode{}
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question95 struct {
|
||||
@ -49,7 +51,7 @@ func Test_Problem95(t *testing.T) {
|
||||
fmt.Printf("【input】:%v \n", p)
|
||||
trees := generateTrees(p.one)
|
||||
for _, t := range trees {
|
||||
fmt.Printf("【output】:%v\n", Tree2Preorder(t))
|
||||
fmt.Printf("【output】:%v\n", structures.Tree2Preorder(t))
|
||||
}
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
@ -2,6 +2,13 @@ package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question98 struct {
|
||||
@ -27,7 +29,7 @@ func Test_Problem98(t *testing.T) {
|
||||
qs := []question98{
|
||||
|
||||
question98{
|
||||
para98{[]int{10, 5, 15, NULL, NULL, 6, 20}},
|
||||
para98{[]int{10, 5, 15, structures.NULL, structures.NULL, 6, 20}},
|
||||
ans98{false},
|
||||
},
|
||||
|
||||
@ -42,7 +44,7 @@ func Test_Problem98(t *testing.T) {
|
||||
},
|
||||
|
||||
question98{
|
||||
para98{[]int{5, 1, 4, NULL, NULL, 3, 6}},
|
||||
para98{[]int{5, 1, 4, structures.NULL, structures.NULL, 3, 6}},
|
||||
ans98{false},
|
||||
},
|
||||
}
|
||||
@ -52,7 +54,7 @@ func Test_Problem98(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans98, q.para98
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
rootOne := Ints2TreeNode(p.one)
|
||||
rootOne := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", isValidBST(rootOne))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,6 +15,7 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func recoverTree(root *TreeNode) {
|
||||
var prev, target1, target2 *TreeNode
|
||||
_, target1, target2 = inOrderTraverse(root, prev, target1, target2)
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question99 struct {
|
||||
@ -27,13 +29,13 @@ func Test_Problem99(t *testing.T) {
|
||||
qs := []question99{
|
||||
|
||||
question99{
|
||||
para99{[]int{1, 3, NULL, NULL, 2}},
|
||||
ans99{[]int{3, 1, NULL, NULL, 2}},
|
||||
para99{[]int{1, 3, structures.NULL, structures.NULL, 2}},
|
||||
ans99{[]int{3, 1, structures.NULL, structures.NULL, 2}},
|
||||
},
|
||||
|
||||
question99{
|
||||
para99{[]int{3, 1, 4, NULL, NULL, 2}},
|
||||
ans99{[]int{2, 1, 4, NULL, NULL, 3}},
|
||||
para99{[]int{3, 1, 4, structures.NULL, structures.NULL, 2}},
|
||||
ans99{[]int{2, 1, 4, structures.NULL, structures.NULL, 3}},
|
||||
},
|
||||
}
|
||||
|
||||
@ -42,7 +44,7 @@ func Test_Problem99(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans99, q.para99
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
rootOne := Ints2TreeNode(p.one)
|
||||
rootOne := structures.Ints2TreeNode(p.one)
|
||||
recoverTree(rootOne)
|
||||
fmt.Printf("【output】:%v \n", p)
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,6 +15,7 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func isSameTree(p *TreeNode, q *TreeNode) bool {
|
||||
if p == nil && q == nil {
|
||||
return true
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question100 struct {
|
||||
@ -48,7 +50,7 @@ func Test_Problem100(t *testing.T) {
|
||||
},
|
||||
|
||||
question100{
|
||||
para100{[]int{1, 2}, []int{1, NULL, 2}},
|
||||
para100{[]int{1, 2}, []int{1, structures.NULL, 2}},
|
||||
ans100{false},
|
||||
},
|
||||
|
||||
@ -63,8 +65,8 @@ func Test_Problem100(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans100, q.para100
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
rootOne := Ints2TreeNode(p.one)
|
||||
rootTwo := Ints2TreeNode(p.two)
|
||||
rootOne := structures.Ints2TreeNode(p.one)
|
||||
rootTwo := structures.Ints2TreeNode(p.two)
|
||||
fmt.Printf("【output】:%v \n", isSameTree(rootOne, rootTwo))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,9 +15,33 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func isSymmetric(root *TreeNode) bool {
|
||||
if root == nil {
|
||||
return true
|
||||
}
|
||||
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
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question101 struct {
|
||||
@ -27,12 +29,12 @@ func Test_Problem101(t *testing.T) {
|
||||
qs := []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},
|
||||
},
|
||||
|
||||
question101{
|
||||
para101{[]int{1, 2, 2, NULL, 3, 3}},
|
||||
para101{[]int{1, 2, 2, structures.NULL, 3, 3}},
|
||||
ans101{true},
|
||||
},
|
||||
|
||||
@ -57,7 +59,7 @@ func Test_Problem101(t *testing.T) {
|
||||
},
|
||||
|
||||
question101{
|
||||
para101{[]int{1, 2, 2, NULL, 3, NULL, 3}},
|
||||
para101{[]int{1, 2, 2, structures.NULL, 3, structures.NULL, 3}},
|
||||
ans101{false},
|
||||
},
|
||||
}
|
||||
@ -67,7 +69,7 @@ func Test_Problem101(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans101, q.para101
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
rootOne := Ints2TreeNode(p.one)
|
||||
rootOne := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", isSymmetric(rootOne))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question102 struct {
|
||||
@ -37,7 +39,7 @@ func Test_Problem102(t *testing.T) {
|
||||
},
|
||||
|
||||
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}}},
|
||||
},
|
||||
}
|
||||
@ -47,7 +49,7 @@ func Test_Problem102(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans102, q.para102
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := Ints2TreeNode(p.one)
|
||||
root := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", levelOrder(root))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,6 +15,7 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func zigzagLevelOrder(root *TreeNode) [][]int {
|
||||
if root == nil {
|
||||
return [][]int{}
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question103 struct {
|
||||
@ -37,12 +39,12 @@ func Test_Problem103(t *testing.T) {
|
||||
},
|
||||
|
||||
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}}},
|
||||
},
|
||||
|
||||
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}}},
|
||||
},
|
||||
}
|
||||
@ -52,7 +54,7 @@ func Test_Problem103(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans103, q.para103
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := Ints2TreeNode(p.one)
|
||||
root := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", zigzagLevelOrder(root))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,9 +15,17 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func maxDepth(root *TreeNode) int {
|
||||
if root == nil {
|
||||
return 0
|
||||
}
|
||||
return max(maxDepth(root.Left), maxDepth(root.Right)) + 1
|
||||
}
|
||||
|
||||
func max(a int, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question104 struct {
|
||||
@ -32,7 +34,7 @@ func Test_Problem104(t *testing.T) {
|
||||
},
|
||||
|
||||
question104{
|
||||
para104{[]int{3, 9, 20, NULL, NULL, 15, 7}},
|
||||
para104{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
|
||||
ans104{3},
|
||||
},
|
||||
}
|
||||
@ -42,7 +44,7 @@ func Test_Problem104(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans104, q.para104
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := Ints2TreeNode(p.one)
|
||||
root := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", maxDepth(root))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,6 +15,7 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func buildTree(preorder []int, inorder []int) *TreeNode {
|
||||
inPos := make(map[int]int)
|
||||
for i := 0; i < len(inorder); i++ {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question105 struct {
|
||||
@ -29,7 +31,7 @@ func Test_Problem105(t *testing.T) {
|
||||
|
||||
question105{
|
||||
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 {
|
||||
_, p := q.ans105, q.para105
|
||||
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")
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,7 +15,8 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
func buildTree106(inorder []int, postorder []int) *TreeNode {
|
||||
|
||||
func buildTree(inorder []int, postorder []int) *TreeNode {
|
||||
inPos := make(map[int]int)
|
||||
for i := 0; i < len(inorder); i++ {
|
||||
inPos[inorder[i]] = i
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question106 struct {
|
||||
@ -29,7 +31,7 @@ func Test_Problem106(t *testing.T) {
|
||||
|
||||
question106{
|
||||
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 {
|
||||
_, p := q.ans106, q.para106
|
||||
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")
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,6 +15,7 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func levelOrderBottom(root *TreeNode) [][]int {
|
||||
tmp := levelOrder(root)
|
||||
res := [][]int{}
|
||||
@ -16,3 +24,35 @@ func levelOrderBottom(root *TreeNode) [][]int {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question107 struct {
|
||||
@ -37,7 +39,7 @@ func Test_Problem107(t *testing.T) {
|
||||
},
|
||||
|
||||
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}}},
|
||||
},
|
||||
}
|
||||
@ -47,7 +49,7 @@ func Test_Problem107(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans107, q.para107
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := Ints2TreeNode(p.one)
|
||||
root := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", levelOrderBottom(root))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,6 +15,7 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func sortedArrayToBST(nums []int) *TreeNode {
|
||||
if len(nums) == 0 {
|
||||
return nil
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question108 struct {
|
||||
@ -28,7 +30,7 @@ func Test_Problem108(t *testing.T) {
|
||||
|
||||
question108{
|
||||
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 {
|
||||
_, p := q.ans108, q.para108
|
||||
arr := []int{}
|
||||
T2s(sortedArrayToBST(p.one), &arr)
|
||||
structures.T2s(sortedArrayToBST(p.one), &arr)
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, arr)
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
@ -7,6 +14,10 @@ package leetcode
|
||||
* Next *ListNode
|
||||
* }
|
||||
*/
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* 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 {
|
||||
if head == nil {
|
||||
return nil
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question109 struct {
|
||||
@ -28,7 +30,7 @@ func Test_Problem109(t *testing.T) {
|
||||
|
||||
question109{
|
||||
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{
|
||||
@ -38,7 +40,7 @@ func Test_Problem109(t *testing.T) {
|
||||
|
||||
question109{
|
||||
para109{[]int{1, 2}},
|
||||
ans109{[]int{2, 1}},
|
||||
ans109{[]int{1, 2}},
|
||||
},
|
||||
|
||||
question109{
|
||||
@ -52,7 +54,7 @@ func Test_Problem109(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans109, q.para109
|
||||
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("\n\n\n")
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,6 +15,7 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func isBalanced(root *TreeNode) bool {
|
||||
if root == nil {
|
||||
return true
|
||||
@ -23,3 +31,17 @@ func depth(root *TreeNode) int {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question110 struct {
|
||||
@ -27,12 +29,12 @@ func Test_Problem110(t *testing.T) {
|
||||
qs := []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},
|
||||
},
|
||||
|
||||
question110{
|
||||
para110{[]int{1, 2, 2, NULL, 3, 3}},
|
||||
para110{[]int{1, 2, 2, structures.NULL, 3, 3}},
|
||||
ans110{true},
|
||||
},
|
||||
|
||||
@ -57,7 +59,7 @@ func Test_Problem110(t *testing.T) {
|
||||
},
|
||||
|
||||
question110{
|
||||
para110{[]int{1, 2, 2, NULL, 3, NULL, 3}},
|
||||
para110{[]int{1, 2, 2, structures.NULL, 3, structures.NULL, 3}},
|
||||
ans110{true},
|
||||
},
|
||||
}
|
||||
@ -67,7 +69,7 @@ func Test_Problem110(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans110, q.para110
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
rootOne := Ints2TreeNode(p.one)
|
||||
rootOne := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", isBalanced(rootOne))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,6 +15,7 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func minDepth(root *TreeNode) int {
|
||||
if root == nil {
|
||||
return 0
|
||||
@ -20,3 +28,10 @@ func minDepth(root *TreeNode) int {
|
||||
}
|
||||
return min(minDepth(root.Left), minDepth(root.Right)) + 1
|
||||
}
|
||||
|
||||
func min(a int, b int) int {
|
||||
if a > b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question111 struct {
|
||||
@ -37,7 +39,7 @@ func Test_Problem111(t *testing.T) {
|
||||
},
|
||||
|
||||
question111{
|
||||
para111{[]int{3, 9, 20, NULL, NULL, 15, 7}},
|
||||
para111{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
|
||||
ans111{2},
|
||||
},
|
||||
|
||||
@ -52,7 +54,7 @@ func Test_Problem111(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans111, q.para111
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := Ints2TreeNode(p.one)
|
||||
root := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", minDepth(root))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,6 +15,7 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func hasPathSum(root *TreeNode, sum int) bool {
|
||||
if root == nil {
|
||||
return false
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question112 struct {
|
||||
@ -33,7 +35,7 @@ func Test_Problem112(t *testing.T) {
|
||||
},
|
||||
|
||||
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},
|
||||
},
|
||||
}
|
||||
@ -43,7 +45,7 @@ func Test_Problem112(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans112, q.para112
|
||||
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("\n\n\n")
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question113 struct {
|
||||
@ -37,7 +39,7 @@ func Test_Problem113(t *testing.T) {
|
||||
},
|
||||
|
||||
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}}},
|
||||
},
|
||||
}
|
||||
@ -47,7 +49,7 @@ func Test_Problem113(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans113, q.para113
|
||||
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("\n\n\n")
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -22,6 +29,14 @@ func flatten(root *TreeNode) {
|
||||
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) {
|
||||
if root == nil || (root.Left == nil && root.Right == nil) {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question114 struct {
|
||||
@ -37,9 +39,9 @@ func Test_Problem114(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans114, q.para114
|
||||
fmt.Printf("【input】:%v \n", p)
|
||||
rootOne := Ints2TreeNode(p.one)
|
||||
rootOne := structures.Ints2TreeNode(p.one)
|
||||
flatten(rootOne)
|
||||
fmt.Printf("【output】:%v \n", Tree2Preorder(rootOne))
|
||||
fmt.Printf("【output】:%v \n", structures.Tree2Preorder(rootOne))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
|
@ -17,6 +17,13 @@ func minimumTotal(triangle [][]int) int {
|
||||
return triangle[0][0]
|
||||
}
|
||||
|
||||
func min(a int, b int) int {
|
||||
if a > b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// 解法二 正常 DP,空间复杂度 O(n)
|
||||
func minimumTotal1(triangle [][]int) int {
|
||||
if len(triangle) == 0 {
|
||||
|
@ -40,3 +40,10 @@ func maxProfit1(prices []int) int {
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func max(a int, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
@ -2,6 +2,13 @@ package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -10,6 +17,7 @@ import "math"
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func maxPathSum(root *TreeNode) int {
|
||||
if root == nil {
|
||||
return 0
|
||||
@ -30,3 +38,10 @@ func getPathSum(root *TreeNode, maxSum *int) int {
|
||||
*maxSum = max(*maxSum, max(currMax, left+right+root.Val))
|
||||
return currMax
|
||||
}
|
||||
|
||||
func max(a int, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question124 struct {
|
||||
@ -42,7 +44,7 @@ func Test_Problem124(t *testing.T) {
|
||||
},
|
||||
|
||||
question124{
|
||||
para124{[]int{-10, 9, 20, NULL, NULL, 15, 7}},
|
||||
para124{[]int{-10, 9, 20, structures.NULL, structures.NULL, 15, 7}},
|
||||
ans124{42},
|
||||
},
|
||||
}
|
||||
@ -52,7 +54,7 @@ func Test_Problem124(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans124, q.para124
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := Ints2TreeNode(p.one)
|
||||
root := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", maxPathSum(root))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
@ -36,6 +36,13 @@ func longestConsecutive(nums []int) int {
|
||||
return res
|
||||
}
|
||||
|
||||
func max(a int, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// 解法二 并查集
|
||||
func longestConsecutive1(nums []int) int {
|
||||
if len(nums) == 0 {
|
||||
|
@ -4,6 +4,13 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -12,6 +19,7 @@ import (
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func sumNumbers(root *TreeNode) int {
|
||||
res, nums := 0, binaryTreeNums(root)
|
||||
for _, n := range nums {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question129 struct {
|
||||
@ -47,7 +49,7 @@ func Test_Problem129(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans129, q.para129
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := Ints2TreeNode(p.one)
|
||||
root := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", sumNumbers(root))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
@ -4,6 +4,13 @@ import (
|
||||
"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) {
|
||||
if len(board) == 0 {
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* struct ListNode {
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question143 struct {
|
||||
@ -50,7 +52,7 @@ func Test_Problem143(t *testing.T) {
|
||||
|
||||
for _, q := range qs {
|
||||
_, 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")
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question144 struct {
|
||||
@ -37,7 +39,7 @@ func Test_Problem144(t *testing.T) {
|
||||
},
|
||||
|
||||
question144{
|
||||
para144{[]int{1, NULL, 2, 3}},
|
||||
para144{[]int{1, structures.NULL, 2, 3}},
|
||||
ans144{[]int{1, 2, 3}},
|
||||
},
|
||||
}
|
||||
@ -47,167 +49,8 @@ func Test_Problem144(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans144, q.para144
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := Ints2TreeNode(p.one)
|
||||
root := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", preorderTraversal(root))
|
||||
}
|
||||
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]
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,6 +15,7 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func postorderTraversal(root *TreeNode) []int {
|
||||
var result []int
|
||||
postorder(root, &result)
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question145 struct {
|
||||
@ -37,7 +39,7 @@ func Test_Problem145(t *testing.T) {
|
||||
},
|
||||
|
||||
question145{
|
||||
para145{[]int{1, NULL, 2, 3}},
|
||||
para145{[]int{1, structures.NULL, 2, 3}},
|
||||
ans145{[]int{1, 2, 3}},
|
||||
},
|
||||
}
|
||||
@ -47,7 +49,7 @@ func Test_Problem145(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans145, q.para145
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := Ints2TreeNode(p.one)
|
||||
root := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", postorderTraversal(root))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question147 struct {
|
||||
@ -50,7 +52,7 @@ func Test_Problem147(t *testing.T) {
|
||||
|
||||
for _, q := range qs {
|
||||
_, 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")
|
||||
}
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
@ -55,3 +62,18 @@ func mergeTwoLists148(l1 *ListNode, l2 *ListNode) *ListNode {
|
||||
l2.Next = mergeTwoLists(l1, l2.Next)
|
||||
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
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question148 struct {
|
||||
@ -50,7 +52,7 @@ func Test_Problem148(t *testing.T) {
|
||||
|
||||
for _, q := range qs {
|
||||
_, 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")
|
||||
}
|
||||
|
@ -12,3 +12,17 @@ func maxProduct(nums []int) int {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
@ -2,6 +2,13 @@ package leetcode
|
||||
|
||||
import "fmt"
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
|
@ -3,6 +3,8 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question160 struct {
|
||||
@ -62,7 +64,7 @@ func Test_Problem160(t *testing.T) {
|
||||
|
||||
for _, q := range qs {
|
||||
_, 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")
|
||||
}
|
||||
|
@ -85,3 +85,10 @@ func maximumGap1(nums []int) int {
|
||||
|
||||
return maxValue
|
||||
}
|
||||
|
||||
func max(a int, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
@ -2,6 +2,13 @@ package leetcode
|
||||
|
||||
import "container/heap"
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -28,6 +35,14 @@ func Constructor173(root *TreeNode) BSTIterator {
|
||||
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 */
|
||||
func (this *BSTIterator) Next() int {
|
||||
this.count--
|
||||
|
@ -3,11 +3,13 @@ package leetcode
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
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)
|
||||
fmt.Printf("obj = %v\n", obj)
|
||||
param1 := obj.Next()
|
||||
|
@ -27,6 +27,20 @@ func calculateMinimumHP(dungeon [][]int) int {
|
||||
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 {
|
||||
low, high := 1, math.MaxInt64
|
||||
|
@ -18,6 +18,13 @@ func rob198(nums []int) int {
|
||||
return dp[n-1]
|
||||
}
|
||||
|
||||
func max(a int, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// 解法二 DP 优化辅助空间,把迭代的值保存在 2 个变量中
|
||||
func rob198_1(nums []int) int {
|
||||
n := len(nums)
|
||||
|
@ -1,5 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
@ -8,6 +15,7 @@ package leetcode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func rightSideView(root *TreeNode) []int {
|
||||
if root == nil {
|
||||
return []int{}
|
||||
|
@ -37,12 +37,12 @@ func Test_Problem199(t *testing.T) {
|
||||
},
|
||||
|
||||
question199{
|
||||
para199{[]int{3, 9, 20, NULL, NULL, 15, 7}},
|
||||
para199{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
|
||||
ans199{[]int{3, 20, 7}},
|
||||
},
|
||||
|
||||
question199{
|
||||
para199{[]int{1, 2, 3, 4, NULL, NULL, 5}},
|
||||
para199{[]int{1, 2, 3, 4, structures.NULL, structures.NULL, 5}},
|
||||
ans199{[]int{1, 3, 5}},
|
||||
},
|
||||
}
|
||||
@ -52,7 +52,7 @@ func Test_Problem199(t *testing.T) {
|
||||
for _, q := range qs {
|
||||
_, p := q.ans199, q.para199
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := Ints2TreeNode(p.one)
|
||||
root := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", rightSideView(root))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user