https://leetcode.com/problems/palindrome-number/
문자열로 푸는 방식
class Solution {
public boolean isPalindrome(int x) {
String str = String.valueOf(x);
for(int i=0; i<str.length()/2; i++){
if(str.charAt(i) != str.charAt(str.length()-i-1)) return false;
}
return true;
}
}
솔루션
class Solution {
public boolean isPalindrome(int x) {
if(x < 0) return false;
int y = x;
int res = 0;
while(y != 0){
res = res * 10 + y % 10;
y /= 10;
}
return x == res;
}
}
'Leetcode 100문제 도전' 카테고리의 다른 글
[Leetcode 6/100] Same Tree - Easy (0) | 2020.08.25 |
---|---|
[Leetcode 5/100] Roman to Integer - Easy (1) | 2020.06.29 |
[Leetcode 3/100] Reverse Integer - Easy (1) | 2020.06.17 |
[Leetcode 2/100] Add Two Numbers - Medium (0) | 2020.06.16 |
[Leetcode 1/100] Two Sum - Easy (0) | 2020.06.14 |