-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path7.整数反转.go
188 lines (175 loc) · 3.3 KB
/
7.整数反转.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* @lc app=leetcode.cn id=7 lang=golang
*
* [7] 整数反转
*
* https://leetcode-cn.com/problems/reverse-integer/description/
*
* algorithms
* Easy (32.38%)
* Likes: 1034
* Dislikes: 0
* Total Accepted: 124.6K
* Total Submissions: 384.9K
* Testcase Example: '123'
*
* 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
*
* 示例 1:
*
* 输入: 123
* 输出: 321
*
*
* 示例 2:
*
* 输入: -123
* 输出: -321
*
*
* 示例 3:
*
* 输入: 120
* 输出: 21
*
*
* 注意:
*
* 假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−2^31, 2^31 − 1]。请根据这个假设,如果反转后整数溢出那么就返回
* 0。
*
*/
func reverse(x int) int {
if x == 0 {
return 0
} else if x > 0 {
return reversePositive(x)
} else {
return reverseNegative(x)
}
}
func reversePositive(x int) int {
digits := process(digitsOfPositiveInt(x))
maxDigits := digitsReverse(digitsOfPositiveInt32())
if len(digits) > len(maxDigits) {
return 0
} else if len(maxDigits) == len(digits) {
result := 0
isBigger := false
for idx := 0; idx < len(maxDigits); idx++ {
d1 := maxDigits[idx]
d2 := digits[idx]
if d2 > d1 {
if !isBigger {
return 0
} else {
result = result*10 + d2
}
} else {
if d2 < d1 {
isBigger = true
}
result = result*10 + d2
}
}
return result
} else {
result := 0
for _, digit := range digits {
result = result*10 + digit
}
return result
}
}
func reverseNegative(x int) int {
digits := process(digitsOfNegativeInt(x))
minDigits := digitsReverse(digitsOfNegativeInt32())
if len(digits) > len(minDigits) {
return 0
} else if len(digits) == len(minDigits) {
result := 0
isBigger := false
for idx := 0; idx < len(minDigits); idx++ {
d1 := minDigits[idx]
d2 := digits[idx]
if d2 > d1 {
if !isBigger {
return 0
} else {
result = result*10 + d2
}
} else {
if d2 < d1 {
isBigger = true
}
result = result*10 + d2
}
}
return -1 * result
} else {
result := 0
for _, digit := range digits {
result = result*10 + digit
}
return -1 * result
}
}
func process(data []int) []int {
idx := 0
for _, v := range data {
if v != 0 {
break
} else {
idx++
}
}
return data[idx:]
}
func digitsOfPositiveInt(x int) []int {
result := make([]int, 0)
for x > 0 {
result = append(result, int(x%10))
x = x / 10
}
return result
}
func digitsOfNegativeInt(x int) []int {
result := make([]int, 0)
for x < 0 {
digit := x % 10
result = append(result, -1*digit)
x = x / 10
}
return result
}
func digitsReverse(data []int) []int {
left, right := 0, len(data)-1
for left < right {
data[left], data[right] = data[right], data[left]
left++
right--
}
return data
}
func digitsOfPositiveInt32() []int {
max, _ := maxMinInt32()
result := make([]int, 0)
for max > 0 {
result = append(result, int(max%10))
max = max / 10
}
return result
}
func digitsOfNegativeInt32() []int {
_, min := maxMinInt32()
result := make([]int, 0)
for min < 0 {
result = append(result, int((min%10)*-1))
min = min / 10
}
return result
}
func maxMinInt32() (int32, int32) {
const MaxUint32 = ^uint32(0)
return int32(MaxUint32 >> 1), -int32(MaxUint32>>1) - 1
}