-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_add_two_numbers.go
158 lines (128 loc) · 3.42 KB
/
2_add_two_numbers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package medium
import (
"math/big"
)
/*
https://leetcode.com/problems/add-two-numbers/
Constraints:
The number of nodes in each linked list is in the range [1, 100].
0 <= Node.val <= 9
It is guaranteed that the list represents a number that does not have leading zeros.
*/
/**
* Definition for singly-linked list.
*/
type ListNode struct {
Val int
Next *ListNode
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
return addTwoNumbersVar2(l1, l2)
}
/*
Runtime: 9 ms, faster than 74.67% of Go online submissions for Add Two Numbers.
Memory Usage: 6.7 MB, less than 5.00% of Go online submissions for Add Two Numbers.
*/
func addTwoNumbersVar2(l1 *ListNode, l2 *ListNode) *ListNode {
l1Slice := addTwoNumbersNodesToSlice(l1)
l2Slice := addTwoNumbersNodesToSlice(l2)
value1 := sliceToIntAddTwoNumbersReversed(l1Slice)
value2 := sliceToIntAddTwoNumbersReversed(l2Slice)
sum := value1.Add(value1, value2) // sum := value1 + value2
result := splitToDigitsAddTwoNumbers(sum)
var l3 = &ListNode{}
var answer *ListNode = nil
for _, item := range result {
l3.Next = &ListNode{item, nil}
if answer == nil {
answer = l3.Next
}
l3 = l3.Next
}
if answer == nil {
return &ListNode{}
}
return answer
}
/*
Runtime: 13 ms, faster than 50.78% of Go online submissions for Add Two Numbers.
Memory Usage: 6.5 MB, less than 5.00% of Go online submissions for Add Two Numbers.
*/
func addTwoNumbersVar1(l1 *ListNode, l2 *ListNode) *ListNode {
l1Slice := addTwoNumbersNodesToSlice(l1)
reverseIntAddTwoNumbers(l1Slice)
l2Slice := addTwoNumbersNodesToSlice(l2)
reverseIntAddTwoNumbers(l2Slice)
value1 := sliceToIntAddTwoNumbers(l1Slice)
value2 := sliceToIntAddTwoNumbers(l2Slice)
sum := value1.Add(value1, value2) // sum := value1 + value2
result := splitToDigitsAddTwoNumbers(sum)
var l3 = &ListNode{}
var answer *ListNode = nil
for _, item := range result {
l3.Next = &ListNode{item, nil}
if answer == nil {
answer = l3.Next
}
l3 = l3.Next
}
if answer == nil {
return &ListNode{}
}
return answer
}
func addTwoNumbersNodesToSlice(l *ListNode) []int {
nodes := make([]int, 0)
head := l
for head != nil {
nodes = append(nodes, head.Val)
head = head.Next
}
return nodes
}
func sliceToIntAddTwoNumbersReversed(s []int) *big.Int {
res := big.NewInt(0)
op := big.NewInt(1)
big10 := big.NewInt(10)
for i := 0; i < len(s); i++ {
//res += int64(s[i]) * op
si := big.NewInt(int64(s[i])) // int64(s[i])
siMulOp := si.Mul(si, op) //int64(s[i]) * op
res.Add(res, siMulOp) //res += int64(s[i]) * op
// op *= 10
op.Mul(op, big10)
}
return res
}
func reverseIntAddTwoNumbers(s []int) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
func splitToDigitsAddTwoNumbers(n *big.Int) []int {
var ret []int
big10 := big.NewInt(10)
big0 := big.NewInt(0)
for n.Cmp(big0) != 0 {
n10 := big.NewInt(0).Mod(n, big10) //n%10
ret = append(ret /*n%10*/, int(n10.Int64()))
n.Div(n, big10) //n /= 10
}
//we need a reversed result
//so a slice is already nice
//reverseIntAddTwoNumbers(ret)
return ret
}
func sliceToIntAddTwoNumbers(s []int) *big.Int {
res := big.NewInt(0)
op := big.NewInt(1)
for i := len(s) - 1; i >= 0; i-- {
//res += int64(s[i]) * op
si := big.NewInt(int64(s[i])) // int64(s[i])
siMulOp := si.Mul(si, op) //int64(s[i]) * op
res.Add(res, siMulOp) //res += int64(s[i]) * op
// op *= 10
op.Mul(op, big.NewInt(10))
}
return res
}