-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path9.go
More file actions
52 lines (45 loc) · 926 Bytes
/
Copy path9.go
File metadata and controls
52 lines (45 loc) · 926 Bytes
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
/*
* @Time : 2019/5/10 16:07
* @Author : cancan
* @File : 9.go
* @Function : 回文数
*/
/*
* Question:
* 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
*
* Example 1:
* 输入: 121
* 输出: true
*
* Example 2:
* 输入: -121
* 输出: false
* 解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
*
* Example 3:
* 输入: 10
* 输出: false
* 解释: 从右向左读, 为 01 。因此它不是一个回文数。
*
* Follow up:
* 你能不将整数转为字符串来解决这个问题吗?
*/
package QuestionBank
import "strconv"
func isPalindrome9(x int) bool {
if x < 0 {
return false
}
s := strconv.Itoa(x)
end := len(s) - 1
start := 0
for start < end {
if s[start] != s[end] {
return false
}
start++
end--
}
return true
}