LeetCode/Algorithms
9. Palindrome Number
챠오위
2021. 10. 12. 23:47
i) x<0일 경우, output: false
ii) x=0일 경우, output: true
iii) x>0일 경우, output: true OR false
iii)에서 x의 값을 1의 자리부터 순차적으로 추출한 다음
해당 값에 10을 곱하여 10의 자리로 올림
이 과정을 반복
class Solution {
public boolean isPalindrome(int x) {
if( x<0 ){
return false;
} else {
long ans=0, temp=x;
while( temp>=1 ){
ans = ans*10 + temp%10;
temp /= 10;
}
if( x==ans ){
return true;
}
return false;
}
}
}
해당 코딩의 효율은 좋지 않다.
리팩터링의 시간을 가져야 할 듯...