Skip to content

Commit 3d00023

Browse files
author
Charlene Jiang
committed
Add solution for palindrome number
1 parent 3d52897 commit 3d00023

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Math/PalindromeNumber.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/palindrome-number/
3+
* Primary idea: Negative numbers are not palindromes.
4+
*
5+
* Time Complexity: O(1), Space Complexity: O(1)
6+
*/
7+
class Solution {
8+
func isPalindrome(x: Int) -> Bool {
9+
if x < 0 {
10+
return false
11+
}
12+
13+
var chars: [Character] = [Character](String(x).characters)
14+
var b = 0
15+
var e = chars.count - 1
16+
17+
while b < e {
18+
if chars[b] != chars[e] {
19+
return false
20+
}
21+
22+
b += 1
23+
e -= 1
24+
}
25+
26+
return true
27+
}
28+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@
618618
| | 12 | [Integer to Roman](https://oj.leetcode.com/problems/integer-to-roman/) | &hearts; &hearts; |
619619
| | 11 | [Container With Most Water](https://oj.leetcode.com/problems/container-with-most-water/) | &hearts; &hearts; |
620620
| | 10 | [Regular Expression Matching](https://oj.leetcode.com/problems/regular-expression-matching/) | &hearts; &hearts; &hearts; |
621-
| | 9 | [Palindrome Number](https://oj.leetcode.com/problems/palindrome-number/) | &hearts; |
621+
| [Swift](./Math/PalindromeNumber.swift) | 9 | [Palindrome Number](https://oj.leetcode.com/problems/palindrome-number/) | &hearts; |
622622
| [Swift](./Math/Atoi.swift) | 8 | [String to Integer (atoi)](https://oj.leetcode.com/problems/string-to-integer-atoi/) | &hearts; |
623623
| [Swift](./Math/ReverseInteger.swift) | 7 | [Reverse Integer](https://oj.leetcode.com/problems/reverse-integer/) | &hearts; |
624624
| [Swift](./String/ZigZagConversion.swift) | 6 | [ZigZag Conversion](https://oj.leetcode.com/problems/zigzag-conversion/) | &hearts; |

0 commit comments

Comments
 (0)