-
Notifications
You must be signed in to change notification settings - Fork 21
/
PalindromeNumber.swift
42 lines (36 loc) 路 1.19 KB
/
PalindromeNumber.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
//
// PalindromeNumber.swift
// LeetCode
//
// Created by 榛庤档澶儙 on 25/11/2017.
// Copyright 漏 2017 lizhaotailang. 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.
//
// Accepted. See [PalindromeNumberTests](./LeetCode/PalindromeNumberTests.swift) for the test cases.
//
import Foundation
class PalindromeNumber {
func isPalindrome(_ x: Int) -> Bool {
var num = x
if [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].contains(num) {
return true
}
if (num % 10) == 0 {
return false
}
var y = 0
while num > y {
y = y * 10 + num % 10
num /= 10
}
return (num == y) || (num == y / 10)
}
}