Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/0086.Partition-List/ListNode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Solution

type ListNode struct {
Val int
Next *ListNode
}

func MakeListNode(nodes []int) *ListNode {
if len(nodes) == 0 {
return &ListNode{}
}
list := &ListNode{}
head := list
list.Val = nodes[0]
for i := 1; i < len(nodes); i++ {
list.Next = &ListNode{}
list = list.Next
list.Val = nodes[i]
}
return head
}
19 changes: 17 additions & 2 deletions src/0086.Partition-List/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(head *ListNode, x int) *ListNode {
if head == nil || head.Next == nil {
return head
}
before, after := &ListNode{}, &ListNode{}
bh, ah := before, after
for ; head != nil; head = head.Next {
if head.Val < x {
before.Next = head
before = before.Next
} else {
after.Next = head
after = after.Next
}
}
after.Next, before.Next = nil, ah.Next
return bh.Next
}
17 changes: 9 additions & 8 deletions src/0086.Partition-List/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
head *ListNode
x int
expect *ListNode
}{
{"TestCacse 1", true, true},
{"TestCacse 1", true, true},
{"TestCacse 1", false, false},
{"TestCase 1", MakeListNode([]int{1, 4, 3, 2, 5, 2}), 3, MakeListNode([]int{1, 2, 2, 4, 3, 5})},
{"TestCase 2", MakeListNode([]int{}), 0, MakeListNode([]int{})},
{"TestCase 3", MakeListNode([]int{1, 4, 3, 2, 5, 2}), 0, MakeListNode([]int{1, 4, 3, 2, 5, 2})},
}

// 开始测试
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ret := Solution(c.inputs)
ret := Solution(c.head, c.x)
if !reflect.DeepEqual(ret, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, ret, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, ret, c.head, c.x)
}
})
}
Expand Down
21 changes: 21 additions & 0 deletions src/0328.Odd-Even-Linked-List/ListNode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Solution

type ListNode struct {
Val int
Next *ListNode
}

func MakeListNode(nodes []int) *ListNode {
if len(nodes) == 0 {
return &ListNode{}
}
list := &ListNode{}
head := list
list.Val = nodes[0]
for i := 1; i < len(nodes); i++ {
list.Next = &ListNode{}
list = list.Next
list.Val = nodes[i]
}
return head
}
16 changes: 14 additions & 2 deletions src/0328.Odd-Even-Linked-List/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(head *ListNode) *ListNode {
if head == nil {
return nil
}
odd, even := head, head.Next
eHead := even
for even != nil && even.Next != nil {
odd.Next = even.Next
odd = odd.Next
even.Next = odd.Next
even = even.Next
}
odd.Next = eHead
return head
}
10 changes: 5 additions & 5 deletions src/0328.Odd-Even-Linked-List/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs *ListNode
expect *ListNode
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase", MakeListNode([]int{1, 2, 3, 4, 5}), MakeListNode([]int{1, 3, 5, 2, 4})},
{"TestCase", MakeListNode([]int{2, 1, 3, 5, 6, 4, 7}), MakeListNode([]int{2, 3, 6, 7, 1, 5, 4})},
{"TestCase", MakeListNode([]int{}), MakeListNode([]int{})},
}

// 开始测试
Expand Down
35 changes: 35 additions & 0 deletions src/0430.Flatten-a-Multilevel-Doubly-Linked-List/Node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package Solution

type Node struct {
Val int
Next *Node
Prev *Node
Child *Node
}

func MakeNode(nodes []int) *Node {
if nodes == nil || len(nodes) < 1 {
return &Node{}
}
node := &Node{}
head := node
node.Val = nodes[0]
for i := 1; i < len(nodes); i++ {
node.Next = &Node{Val: nodes[i], Prev: node}
node = node.Next
}
return head
}

func Check(headA, headB *Node) bool {
if headA == nil && headB == nil {
return true
}
for curA, curB := headA, headB; curA != nil && curB != nil; curA, curB = curA.Next, curB.Next {
if curA.Val != curB.Val {
return false
}
//if curA.Child != nil || curB.Child != nil { return false }
}
return true
}
23 changes: 21 additions & 2 deletions src/0430.Flatten-a-Multilevel-Doubly-Linked-List/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(root *Node) *Node {
curr := root
for curr != nil {
if curr.Child == nil {
curr = curr.Next
} else {
next := curr.Next
node := Solution(curr.Child)
curr.Child = nil
curr.Next, node.Prev = node, curr
for curr.Next != nil {
curr = curr.Next
}
if next == nil {
break
}
curr.Next, next.Prev = next, curr
curr = curr.Next
}
}
return root
}
58 changes: 36 additions & 22 deletions src/0430.Flatten-a-Multilevel-Doubly-Linked-List/Solution_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
package Solution

import (
"reflect"
"strconv"
"testing"
)

func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
}
})
}
t.Run("TestCase", func(t *testing.T) {
main := MakeNode([]int{1, 2, 3, 4, 5, 6})
child1 := MakeNode([]int{7, 8, 9, 10})
child2 := MakeNode([]int{11, 12})
child1.Next.Child = child2
curr := main
for i := 0; i < 2; i++ {
curr = curr.Next
}
curr.Child = child1
got := Solution(main)
expect := MakeNode([]int{1, 2, 3, 7, 8, 11, 12, 9, 10, 4, 5, 6})
if !Check(got, expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
expect, got, main)
}
})
t.Run("TestCase", func(t *testing.T) {
main := MakeNode([]int{1, 2})
main.Child = &Node{Val: 3}
got := Solution(main)
expect := MakeNode([]int{1, 3, 2})
if !Check(got, expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
expect, got, main)
}
})
t.Run("TestCase", func(t *testing.T) {
main := MakeNode([]int{})
got := Solution(main)
expect := MakeNode([]int{})
if !Check(got, expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
expect, got, main)
}
})
}

// 压力测试
Expand Down
21 changes: 21 additions & 0 deletions src/0445.Add-Two-Numbers-II/ListNode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Solution

type ListNode struct {
Val int
Next *ListNode
}

func MakeListNode(nodes []int) *ListNode {
if len(nodes) == 0 {
return &ListNode{}
}
list := &ListNode{}
head := list
list.Val = nodes[0]
for i := 1; i < len(nodes); i++ {
list.Next = &ListNode{}
list = list.Next
list.Val = nodes[i]
}
return head
}
52 changes: 50 additions & 2 deletions src/0445.Add-Two-Numbers-II/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
package Solution

func Solution(x bool) bool {
return x
func reverse(arr []int) []int {
for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
arr[i], arr[j] = arr[j], arr[i]
}
return arr
}

func Solution(l1 *ListNode, l2 *ListNode) *ListNode {
var a1 []int
for l1 != nil {
a1 = append(a1, l1.Val)
l1 = l1.Next
}
var a2 []int
for l2 != nil {
a2 = append(a2, l2.Val)
l2 = l2.Next
}
a1 = reverse(a1)
a2 = reverse(a2)
n1, n2 := len(a1), len(a2)
n, carry := 0, 0
var ans []int
if n1 > n2 {
n = n1
} else {
n = n2
}
for i := 0; i < n; i++ {
t := carry
if i < n1 {
t = t + a1[i]
}
if i < n2 {
t = t + a2[i]
}
ans = append(ans, t%10)
carry = t / 10
}
if carry > 0 {
ans = append(ans, carry)
}
ans = reverse(ans)
head := &ListNode{Val: ans[0]}
curr := head
for i := 1; i < len(ans); i++ {
curr.Next = &ListNode{Val: ans[i]}
curr = curr.Next
}
return head
}
20 changes: 11 additions & 9 deletions src/0445.Add-Two-Numbers-II/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
input1 *ListNode
input2 *ListNode
expect *ListNode
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase", MakeListNode([]int{7, 2, 4, 3}), MakeListNode([]int{5, 6, 4}), MakeListNode([]int{7, 8, 0, 7})},
{"TestCase", MakeListNode([]int{}), MakeListNode([]int{1, 2, 3}), MakeListNode([]int{1, 2, 3})},
{"TestCase", MakeListNode([]int{0, 0, 8}), MakeListNode([]int{9, 9, 2}), MakeListNode([]int{1, 0, 0, 0})},
{"TestCase", MakeListNode([]int{}), MakeListNode([]int{}), MakeListNode([]int{})},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
t.Run(c.name+" "+strconv.Itoa(i+1), func(t *testing.T) {
got := Solution(c.input1, c.input2)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.input1, c.input2)
}
})
}
Expand Down
11 changes: 9 additions & 2 deletions src/0485.Max-Consecutive-Ones/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int) int {
max, c := 0, 0
for i := 0; i < len(nums); i++ {
c = nums[i]*c + nums[i]
if c > max {
max = c
}
}
return max
}
Loading