mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
更改 problem 0001
This commit is contained in:
@ -1,43 +0,0 @@
|
|||||||
package leetcode
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Definition for singly-linked list.
|
|
||||||
* type ListNode struct {
|
|
||||||
* Val int
|
|
||||||
* Next *ListNode
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
|
|
||||||
if l1 == nil || l2 == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
head := &ListNode{Val: 0, Next: nil}
|
|
||||||
current := head
|
|
||||||
carry := 0
|
|
||||||
for l1 != nil || l2 != nil {
|
|
||||||
var x, y int
|
|
||||||
if l1 == nil {
|
|
||||||
x = 0
|
|
||||||
} else {
|
|
||||||
x = l1.Val
|
|
||||||
}
|
|
||||||
if l2 == nil {
|
|
||||||
y = 0
|
|
||||||
} else {
|
|
||||||
y = l2.Val
|
|
||||||
}
|
|
||||||
current.Next = &ListNode{Val: (x + y + carry) % 10, Next: nil}
|
|
||||||
current = current.Next
|
|
||||||
carry = (x + y + carry) / 10
|
|
||||||
if l1 != nil {
|
|
||||||
l1 = l1.Next
|
|
||||||
}
|
|
||||||
if l2 != nil {
|
|
||||||
l2 = l2.Next
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if carry > 0 {
|
|
||||||
current.Next = &ListNode{Val: carry % 10, Next: nil}
|
|
||||||
}
|
|
||||||
return head.Next
|
|
||||||
}
|
|
@ -1,79 +0,0 @@
|
|||||||
package leetcode
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
type question2 struct {
|
|
||||||
para2
|
|
||||||
ans2
|
|
||||||
}
|
|
||||||
|
|
||||||
// para 是参数
|
|
||||||
// one 代表第一个参数
|
|
||||||
type para2 struct {
|
|
||||||
one []int
|
|
||||||
another []int
|
|
||||||
}
|
|
||||||
|
|
||||||
// ans 是答案
|
|
||||||
// one 代表第一个答案
|
|
||||||
type ans2 struct {
|
|
||||||
one []int
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_Problem2(t *testing.T) {
|
|
||||||
|
|
||||||
qs := []question2{
|
|
||||||
|
|
||||||
question2{
|
|
||||||
para2{[]int{}, []int{}},
|
|
||||||
ans2{[]int{}},
|
|
||||||
},
|
|
||||||
|
|
||||||
question2{
|
|
||||||
para2{[]int{1}, []int{1}},
|
|
||||||
ans2{[]int{2}},
|
|
||||||
},
|
|
||||||
|
|
||||||
question2{
|
|
||||||
para2{[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}},
|
|
||||||
ans2{[]int{2, 4, 6, 8}},
|
|
||||||
},
|
|
||||||
|
|
||||||
question2{
|
|
||||||
para2{[]int{1, 2, 3, 4, 5}, []int{1, 2, 3, 4, 5}},
|
|
||||||
ans2{[]int{2, 4, 6, 8, 0, 1}},
|
|
||||||
},
|
|
||||||
|
|
||||||
question2{
|
|
||||||
para2{[]int{1}, []int{9, 9, 9, 9, 9}},
|
|
||||||
ans2{[]int{0, 0, 0, 0, 0, 1}},
|
|
||||||
},
|
|
||||||
|
|
||||||
question2{
|
|
||||||
para2{[]int{9, 9, 9, 9, 9}, []int{1}},
|
|
||||||
ans2{[]int{0, 0, 0, 0, 0, 1}},
|
|
||||||
},
|
|
||||||
|
|
||||||
question2{
|
|
||||||
para2{[]int{2, 4, 3}, []int{5, 6, 4}},
|
|
||||||
ans2{[]int{7, 0, 8}},
|
|
||||||
},
|
|
||||||
|
|
||||||
question2{
|
|
||||||
para2{[]int{1, 8, 3}, []int{7, 1}},
|
|
||||||
ans2{[]int{8, 9, 3}},
|
|
||||||
},
|
|
||||||
// 如需多个测试,可以复制上方元素。
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("------------------------Leetcode Problem 2------------------------\n")
|
|
||||||
|
|
||||||
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("\n\n\n")
|
|
||||||
}
|
|
13
Algorithms/0001. Two Sum/1. Two Sum.go
Normal file
13
Algorithms/0001. Two Sum/1. Two Sum.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
func twoSum(nums []int, target int) []int {
|
||||||
|
m := make(map[int]int)
|
||||||
|
for i := 0; i < len(nums); i++ {
|
||||||
|
another := target - nums[i]
|
||||||
|
if _, ok := m[another]; ok {
|
||||||
|
return []int{m[another], i}
|
||||||
|
}
|
||||||
|
m[nums[i]] = i
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
34
Algorithms/0001. Two Sum/1. Two Sum_test.go
Normal file
34
Algorithms/0001. Two Sum/1. Two Sum_test.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTwoSum(t *testing.T) {
|
||||||
|
tests := [][]int{
|
||||||
|
[]int{3, 2, 4},
|
||||||
|
[]int{3, 2, 4},
|
||||||
|
[]int{0, 8, 7, 3, 3, 4, 2},
|
||||||
|
[]int{0, 1},
|
||||||
|
}
|
||||||
|
targets := []int{
|
||||||
|
6,
|
||||||
|
5,
|
||||||
|
11,
|
||||||
|
1,
|
||||||
|
}
|
||||||
|
results := [][]int{
|
||||||
|
[]int{1, 2},
|
||||||
|
[]int{0, 1},
|
||||||
|
[]int{1, 3},
|
||||||
|
[]int{0, 1},
|
||||||
|
}
|
||||||
|
fmt.Printf("------------------------Leetcode Problem 1------------------------\n")
|
||||||
|
for i := 0; i < len(targets); i++ {
|
||||||
|
fmt.Printf("nums = %v target = %v result = %v\n", tests[i], targets[i], twoSum(tests[i], targets[i]))
|
||||||
|
if ret := twoSum(tests[i], targets[i]); ret[0] != results[i][0] && ret[1] != results[i][1] {
|
||||||
|
t.Fatalf("case %d fails: %v\n", i, ret)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user