1. Description
Determine whether an integer is a palindrome. Do this without extra space.
2. Runtime Distribution
3. Submission Details
4. Example
input -1 return false
input 12344321 return true
5. Code
Java
package Digital;
public class LeetCode0009 {
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
int count = 1, num = x;
while (x > 9) {
count *= 10;
x /= 10;
}
while (count > 1) {
int high = num / count;
int low = num % 10;
if (high != low) {
return false;
}
num %= count;
num /= 10;
count /= 100;
}
return true;
}
public static void main(String[] args) {
LeetCode0009 leetcode = new LeetCode0009();
System.out.println(leetcode.isPalindrome(120000121));
}
}
C
#include<stdio.h>
#include <stdbool.h>
bool isPalindrome(int x) {
if (x < 0)
{
return false;
}
int mDigits = 1;
for (int num = x; (num /= 10) > 0; mDigits *= 10);
for (; mDigits != 0 && x / mDigits == x % 10; x %= mDigits, mDigits /= 100, x /= 10);
return x == 0;
}
int main() {
int x = -1;
printf("%d\n", isPalindrome(x));
system("pause");
return 0;
}
Comments | NOTHING