LeetCode 9. Palindrome Number
class Solution {
public boolean isPalindrome(int x) {
String str = String.valueOf(x);
int n = s.length();
for (int i=0; i< n/2; i++) {
if (str .charAt(i) != str .charAt(n - i - 1))
return false;
}
return true;
}
}
Time Complexity: O(n)
Space Complexity: O(n)
class Solution {
public boolean isPalindrome(int x) {
if(x < 0 || (x != 0 && x % 10 == 0)) return false;
int reversed = 0;
while(x > reversed) {
reversed = reversed * 10 + x % 10;
x = x / 10;
}
return (x == reversed) || (x == reversed / 10);
}
}
Time Complexity: O(n)
Space Complexity: O(1)
Comments
Post a Comment