parent
7e163186ad
commit
45b4b2e629
@ -0,0 +1,18 @@
|
||||
package palindromenumber
|
||||
|
||||
import "strconv"
|
||||
|
||||
func isPalindrome(x int) bool {
|
||||
str := strconv.Itoa(x)
|
||||
for i, digit := range str {
|
||||
if byte(digit) != str[len(str)-i-1] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// public for test
|
||||
func IsPalindrome(x int) bool {
|
||||
return isPalindrome(x)
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package palindromenumber_test
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"git.jxs.me/leetgo/palindromenumber"
|
||||
)
|
||||
|
||||
func TestCases(t *testing.T) {
|
||||
cases := map[int]bool{
|
||||
0: true,
|
||||
10: false,
|
||||
11: true,
|
||||
100: false,
|
||||
101: true,
|
||||
-121: false,
|
||||
}
|
||||
|
||||
for input, expected := range cases {
|
||||
t.Run(strconv.Itoa(input), func(t *testing.T) {
|
||||
result := palindromenumber.IsPalindrome(input)
|
||||
|
||||
if expected != result {
|
||||
t.Errorf("%v != %v", expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
Given an integer x, return true if x is palindrome integer.
|
||||
|
||||
An integer is a palindrome when it reads the same backward as forward.
|
||||
|
||||
For example, 121 is a palindrome while 123 is not.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: x = 121
|
||||
Output: true
|
||||
Explanation: 121 reads as 121 from left to right and from right to left.
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: x = -121
|
||||
Output: false
|
||||
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
|
||||
|
||||
Example 3:
|
||||
|
||||
Input: x = 10
|
||||
Output: false
|
||||
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
-231 <= x <= 231 - 1
|
||||
|
||||
|
||||
https://leetcode.com/problems/palindrome-number/
|
Loading…
Reference in new issue