-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindrome_number_test.go
97 lines (80 loc) · 1.96 KB
/
palindrome_number_test.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Problem:
- Determine whether an integer is a palindrome.
Assumption:
- Do this without extra space.
- Define negative integers as non-palindrome.
Example:
- Input: 101
Output: true
- Input: 106
Output: false
Approach:
- Use two-pointer approach where one starts at the first digit and one starts
at the last digit, have them walk toward the middle and compare them at each
step.
Solution:
- Calculate the division factor for the number to divide by to get its first digit.
- While the number is not equal to 0:
- Get the first digit by diving the number by the division factor above.
- Get the last digit by applying modulo the number by 10.
- Return false immediately if they are equal.
- Otherwise, chop of both digit by applying modulo the number by the division factor
divide the result by 10.
- Update the division factor by dividing it by 100 since we already chopped of the first
and last digits.
Cost:
- O(n) time, O(1) space.
*/
package leetcode
import (
"testing"
"github.com/pcaruana/algo/common"
)
func TestIsIntPalindrome(t *testing.T) {
tests := []struct {
in int
expected bool
}{
{-1, false},
{0, true},
{1, true},
{10, false},
{11, true},
{101, true},
{111, true},
{110, false},
{-101, false},
}
for _, tt := range tests {
result := isIntPalindrome(tt.in)
common.Equal(t, tt.expected, result)
}
}
func isIntPalindrome(x int) bool {
// return false immediately if the input is negative.
if x < 0 {
return false
}
// calculate the division factor for the number to divide by to get its
// first digit.
div := 1
for x/div >= 10 {
div *= 10
}
for x != 0 {
l := x / div
r := x % 10
// return false immediately if two corresponding digits are the not
// equal.
if l != r {
return false
}
// chop off the first and last digits.
x = (x % div) / 10
// update the division factor by dividing it by 100 since we already
// chopped of the first and last digits.
div /= 100
}
return true
}