Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3192b25

Browse files
add some tests, go fmt, reorganize code
1 parent 85bb5a3 commit 3192b25

File tree

120 files changed

+406
-138
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+406
-138
lines changed

‎leetcode/accounts_merge/accounts_merge_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func Test_accountsMerge(t *testing.T) {
1616
},
1717
{
1818
[][]string{{"Gabe", "Gabe0@m.co", "Gabe3@m.co", "Gabe1@m.co"}, {"Kevin", "Kevin3@m.co", "Kevin5@m.co", "Kevin0@m.co"}, {"Ethan", "Ethan5@m.co", "Ethan4@m.co", "Ethan0@m.co"}, {"Hanzo", "Hanzo3@m.co", "Hanzo1@m.co", "Hanzo0@m.co"}, {"Fern", "Fern5@m.co", "Fern1@m.co", "Fern0@m.co"}},
19-
[][]string{{"Ethan", "Ethan0@m.co", "Ethan4@m.co", "Ethan5@m.co"}, {"Gabe", "Gabe0@m.co", "Gabe1@m.co", "Gabe3@m.co"}, {"Hanzo", "Hanzo0@m.co", "Hanzo1@m.co", "Hanzo3@m.co"}, {"Kevin", "Kevin0@m.co", "Kevin3@m.co", "Kevin5@m.co"}, {"Fern", "Fern0@m.co", "Fern1@m.co", "Fern5@m.co"}},
19+
[][]string{{"Gabe", "Gabe0@m.co", "Gabe1@m.co", "Gabe3@m.co"}, {"Kevin", "Kevin0@m.co", "Kevin3@m.co", "Kevin5@m.co"}, {"Ethan", "Ethan0@m.co", "Ethan4@m.co", "Ethan5@m.co"}, {"Hanzo", "Hanzo0@m.co", "Hanzo1@m.co", "Hanzo3@m.co"}, {"Fern", "Fern0@m.co", "Fern1@m.co", "Fern5@m.co"}},
2020
},
2121
}
2222

‎leetcode/add_two_numbers.go renamed to ‎leetcode/add_two_numbers/add_two_numbers.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://leetcode.com/problems/add-two-numbers/
2-
package main
2+
package add_two_numbers
33

44
/**
55
* Definition for singly-linked list.
@@ -8,6 +8,12 @@ package main
88
* Next *ListNode
99
* }
1010
*/
11+
12+
type ListNode struct {
13+
Val int
14+
Next *ListNode
15+
}
16+
1117
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
1218
cur1 := l1
1319
cur2 := l2
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package add_two_numbers
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_addTwoNumbers(t *testing.T) {
9+
cases := []struct {
10+
l1Param []int
11+
l2Param []int
12+
exp []int
13+
}{
14+
{
15+
l1Param: []int{2, 4, 3},
16+
l2Param: []int{5, 6, 4},
17+
exp: []int{7, 0, 8},
18+
},
19+
{
20+
l1Param: []int{0},
21+
l2Param: []int{0},
22+
exp: []int{0},
23+
},
24+
{
25+
l1Param: []int{9, 9, 9, 9, 9, 9, 9},
26+
l2Param: []int{9, 9, 9, 9},
27+
exp: []int{8, 9, 9, 9, 0, 0, 0, 1},
28+
},
29+
}
30+
31+
for idx, tc := range cases {
32+
l1 := arrayToList(tc.l1Param)
33+
l2 := arrayToList(tc.l2Param)
34+
res := addTwoNumbers(l1, l2)
35+
resArr := listToArray(res)
36+
37+
if reflect.DeepEqual(resArr, tc.exp) == false {
38+
t.Errorf("case %d failed: expecting %v, got %v", idx, tc.exp, resArr)
39+
}
40+
}
41+
}
42+
43+
func arrayToList(arr []int) *ListNode {
44+
var root *ListNode
45+
curr := root
46+
for _, el := range arr {
47+
node := &ListNode{
48+
Val: el,
49+
Next: nil,
50+
}
51+
if curr == nil {
52+
root = node
53+
curr = node
54+
} else {
55+
curr.Next = node
56+
curr = curr.Next
57+
}
58+
}
59+
return root
60+
}
61+
62+
func listToArray(root *ListNode) []int {
63+
res := make([]int, 0, 4)
64+
for curr := root; curr != nil; curr = curr.Next {
65+
res = append(res, curr.Val)
66+
}
67+
return res
68+
}

‎leetcode/add_two_numbers_ii.go renamed to ‎leetcode/add_two_numbers_ii/add_two_numbers_ii.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://leetcode.com/problems/add-two-numbers-ii/
2-
package main
2+
package add_two_numbers_ii
33

44
/**
55
* Definition for singly-linked list.
@@ -8,6 +8,12 @@ package main
88
* Next *ListNode
99
* }
1010
*/
11+
12+
type ListNode struct {
13+
Val int
14+
Next *ListNode
15+
}
16+
1117
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
1218
ll1 := LLen(l1)
1319
ll2 := LLen(l2)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package add_two_numbers_ii
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_addTwoNumbers(t *testing.T) {
9+
cases := []struct {
10+
l1Param []int
11+
l2Param []int
12+
exp []int
13+
}{
14+
{
15+
l1Param: []int{7, 2, 4, 3},
16+
l2Param: []int{5, 6, 4},
17+
exp: []int{7, 8, 0, 7},
18+
},
19+
{
20+
l1Param: []int{2, 4, 3},
21+
l2Param: []int{5, 6, 4},
22+
exp: []int{8, 0, 7},
23+
},
24+
{
25+
l1Param: []int{0},
26+
l2Param: []int{0},
27+
exp: []int{0},
28+
},
29+
}
30+
31+
for idx, tc := range cases {
32+
l1 := arrayToList(tc.l1Param)
33+
l2 := arrayToList(tc.l2Param)
34+
res := addTwoNumbers(l1, l2)
35+
resArr := listToArray(res)
36+
37+
if reflect.DeepEqual(resArr, tc.exp) == false {
38+
t.Errorf("case %d failed: expecting %v, got %v", idx, tc.exp, resArr)
39+
}
40+
}
41+
}
42+
43+
func arrayToList(arr []int) *ListNode {
44+
var root *ListNode
45+
curr := root
46+
for _, el := range arr {
47+
node := &ListNode{
48+
Val: el,
49+
Next: nil,
50+
}
51+
if curr == nil {
52+
root = node
53+
curr = node
54+
} else {
55+
curr.Next = node
56+
curr = curr.Next
57+
}
58+
}
59+
return root
60+
}
61+
62+
func listToArray(root *ListNode) []int {
63+
res := make([]int, 0, 4)
64+
for curr := root; curr != nil; curr = curr.Next {
65+
res = append(res, curr.Val)
66+
}
67+
return res
68+
}

‎leetcode/basic_calculator.go renamed to ‎leetcode/basic_calculator/basic_calculator.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
// https://leetcode.com/problems/basic-calculator/
2-
package main
3-
4-
import (
5-
"fmt"
6-
)
2+
package basic_calculator
73

84
func calculate(s string) int {
95
s = reverse(s)
@@ -55,7 +51,6 @@ func calc(st string) int {
5551
}
5652
}
5753

58-
//s.Print()
5954
ans := s.Pop()
6055
for s.Size() > 0 {
6156
op := s.Pop()
@@ -111,15 +106,3 @@ func (s *Stack) Pop() int {
111106
func (s *Stack) Size() int {
112107
return len(s.data)
113108
}
114-
115-
func (s *Stack) Print() {
116-
for i := len(s.data) - 1; i >= 0; i-- {
117-
v := s.data[i]
118-
st := string(v)
119-
if !isOp(byte(v)) {
120-
st = string(v + '0')
121-
}
122-
fmt.Printf("%v", st)
123-
}
124-
fmt.Println()
125-
}

‎leetcode/best_time_to_buy_and_sell_stock.go renamed to ‎leetcode/best_time_to_buy_and_sell_stock/best_time_to_buy_and_sell_stock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock
2-
package main
2+
package best_time_to_buy_and_sell_stock
33

44
func maxProfit(prices []int) int {
55
minPrice := 1<<31 - 1

‎leetcode/binary_search_tree_iterator.go renamed to ‎leetcode/binary_search_tree_iterator/binary_search_tree_iterator.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://leetcode.com/problems/binary-search-tree-iterator/
2-
package main
2+
package binary_search_tree_iterator
33

44
/**
55
* Definition for a binary tree node.
@@ -9,6 +9,13 @@ package main
99
* Right *TreeNode
1010
* }
1111
*/
12+
13+
type TreeNode struct {
14+
Val int
15+
Left *TreeNode
16+
Right *TreeNode
17+
}
18+
1219
type BSTIterator struct {
1320
s *Stack
1421
}

‎leetcode/binary_tree_maximum_path_sum.go renamed to ‎leetcode/binary_tree_maximum_path_sum/binary_tree_maximum_path_sum.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// https://leetcode.com/problems/binary-tree-maximum-path-sum/
2-
package main
2+
package binary_tree_maximum_path_sum
33

44
/**
55
* Definition for a binary tree node.
@@ -9,6 +9,13 @@ package main
99
* Right *TreeNode
1010
* }
1111
*/
12+
13+
type TreeNode struct {
14+
Val int
15+
Left *TreeNode
16+
Right *TreeNode
17+
}
18+
1219
func maxPathSum(root *TreeNode) int {
1320
_, maxSum := maxSumForNode(root)
1421
return maxSum

‎leetcode/binary_tree_paths.go renamed to ‎leetcode/binary_tree_paths/binary_tree_paths.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// https://leetcode.com/problems/binary-tree-paths/
2-
package main
2+
package binary_tree_paths
3+
4+
import "fmt"
35

46
/**
57
* Definition for a binary tree node.
@@ -9,6 +11,13 @@ package main
911
* Right *TreeNode
1012
* }
1113
*/
14+
15+
type TreeNode struct {
16+
Val int
17+
Left *TreeNode
18+
Right *TreeNode
19+
}
20+
1221
func binaryTreePaths(root *TreeNode) []string {
1322
paths := make([]string, 0)
1423
paths = getPaths(root, "", paths)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /