-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path9.回文数.go
64 lines (63 loc) · 1.16 KB
/
9.回文数.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
/*
* @lc app=leetcode.cn id=9 lang=golang
*
* [9] 回文数
*
* https://leetcode-cn.com/problems/palindrome-number/description/
*
* algorithms
* Easy (55.97%)
* Likes: 593
* Dislikes: 0
* Total Accepted: 108.4K
* Total Submissions: 193.6K
* Testcase Example: '121'
*
* 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
*
* 示例 1:
*
* 输入: 121
* 输出: true
*
*
* 示例 2:
*
* 输入: -121
* 输出: false
* 解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
*
*
* 示例 3:
*
* 输入: 10
* 输出: false
* 解释: 从右向左读, 为 01 。因此它不是一个回文数。
*
*
* 进阶:
*
* 你能不将整数转为字符串来解决这个问题吗?
*
*/
func isPalindrome(x int) bool {
if x < 0 {
return false
} else {
backup := x
result := 0
maxInt := maxInt()
for backup > 0 {
digit := backup % 10
result = result*10 + digit
backup = backup / 10
if result > maxInt {
return false
}
}
return result == x
}
}
func maxInt() int {
return int(^uint(0) >> 1)
}