diff --git a/palindromenumber/palindromenumber.go b/palindromenumber/palindromenumber.go new file mode 100644 index 0000000..dc7d183 --- /dev/null +++ b/palindromenumber/palindromenumber.go @@ -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) +} diff --git a/palindromenumber/palindromenumber_test.go b/palindromenumber/palindromenumber_test.go new file mode 100644 index 0000000..a93a805 --- /dev/null +++ b/palindromenumber/palindromenumber_test.go @@ -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) + } + }) + } + +} diff --git a/palindromenumber/readme.md b/palindromenumber/readme.md new file mode 100644 index 0000000..64f4f8d --- /dev/null +++ b/palindromenumber/readme.md @@ -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/