-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDigitPalindrome.go
69 lines (51 loc) · 1.24 KB
/
DigitPalindrome.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
// check if an input integer's decimal form is a palindrome
package main
import (
"fmt"
"math"
)
func main() {
tests := []int{456, 454, 498894, 2, 0, 1, 7, 11, 121, 333, 2147447412, -1, 12, 100, 2147483647}
for _, test := range tests {
fmt.Println(test, isPal(test))
fmt.Println(test, palindrome(test))
}
}
func palindrome(x int) bool {
if x <= 0 {
return x == 0
}
numDigits := int(math.Floor(math.Log10(float64(x)))) + 1
msdMask := int(math.Pow10(numDigits - 1))
for i := 0; i < (numDigits / 2); i++ {
if x/msdMask != x%10 {
return false
}
x %= msdMask // remove most significant digit of x
x /= 10 // remove least significant digit of x
msdMask /= 100
}
return true
}
func isPal(x int) bool {
x_digits := []int{}
var rem int
if x < 0 {
return false
}
for x > 0 {
rem = x % 10
x = x / 10
x_digits = append(x_digits, rem)
}
startIndex := 0
endIndex := len(x_digits) - 1
for startIndex <= endIndex {
if x_digits[startIndex] != x_digits[endIndex] {
return false
}
startIndex++
endIndex--
}
return true
}