-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
64 lines (58 loc) · 1.98 KB
/
Contents.swift
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
//: # Palindrome Number
//:
//: **Question:** Determine whether an integer is a palindrome. Do this without extra space.
//:
//: **Clarification:**
//: * Q: Does negative integer such as –1 qualify as a palindrome?
//: * A: For the purpose of discussion here, we define negative integers as non palindrome.
//:
//: **Solution:** One approach is to first reverse the number. If the number is the same as its reversed, then it must be a palindrome. You could reverse a number by doing the following:
import Foundation
func reverse(_ i: Int) -> Int {
var x = i
var rtn = 0
while x != 0 {
rtn = rtn * 10 + x % 10
x /= 10
}
return rtn
}
func isPalindrome(_ i: Int) -> Bool {
return i < 0 ? false : i - reverse(i) == 0
}
isPalindrome(123)
isPalindrome(12121)
isPalindrome(1)
isPalindrome(0)
isPalindrome(-123)
isPalindrome(-12121)
//: This seemed to work too, but did you consider the possibility that the reversed number might overflow?
//:
//: We could construct a better and more generic solution. One pointer is that, we must start comparing the digits somewhere. And you know there could only be two ways, either expand from the middle or compare from the two ends.
//:
//: It turns out that comparing from the two ends is easier. First, compare the first and last digit. If they are not the same, it must not be a palindrome. If they are the same, chop off one digit from both ends and continue until you have no digits left, which you conclude that it must be a palindrome.
func isPalindrome2(_ i: Int) -> Bool {
var x = i
if x < 0 {
return false
} else {
var div = 1
while x / div >= 10 {
div *= 10
}
while x != 0 {
let left = x / div
let right = x % 10
if left != right {
return false
}
x = x % div / 10
div /= 100
}
return true
}
}
isPalindrome2(123)
isPalindrome2(12121)
isPalindrome(1)
isPalindrome(0)