Determine whether an integer is a palindrome. Do this without extra space.
Solution: O(n)
public class Solution {
public boolean isPalindrome(int x) {
int reverse = doRevere(x);
return reverse == x;
}
public int doRevere(int x){
int ret = 0;
while(x>0){
ret *=10;
ret += (x%10);
x/=10;
}
return ret;
}
}
Key Concept: We could consider if x is negative
沒有留言:
張貼留言