-
Notifications
You must be signed in to change notification settings - Fork 41
/
9.swift
58 lines (42 loc) · 1.5 KB
/
9.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
//
// PalindromeNumber.swift
// PalindromeNumber
//
// Created by Lex Tang on 5/4/15.
// Copyright (c) 2015 Lex Tang. All rights reserved.
//
/*
Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
@see also: https://leetcode.com/discuss/12693/neat-ac-java-code-o-n-time-complexity
*/
import Foundation
import XCTest
func isPalindrome(_ x: Int) -> Bool {
var parlinedromeX = 0
var tmpX = x
while tmpX > 0 {
if parlinedromeX >= Int.max / 10 {
return false
}
parlinedromeX = parlinedromeX * 10 + tmpX % 10
tmpX /= 10
}
return parlinedromeX == x
}
class PalindromeNumberTest: XCTestCase {
func testPalindromeNumber() {
XCTAssertFalse(isPalindrome(-121), "")
XCTAssertTrue(isPalindrome(123321), "")
XCTAssertTrue(isPalindrome(1234567890987654321), "")
XCTAssertFalse(isPalindrome(9223372036854775807), "")
XCTAssertTrue(isPalindrome(0), "")
XCTAssertFalse(isPalindrome(23), "")
XCTAssertFalse(isPalindrome(Int.max), "")
XCTAssertFalse(isPalindrome(Int.min), "")
}
}