leetcode.com/problems/palindrome-number/
Palindrome Number - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
1. Problem
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Follow up: Could you solve it without converting the integer to a string?
2.Code
public class PalindromeNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean result = isPalindrome(121);
}
public static boolean isPalindrome(int x) {
String beforeString = Integer.toString(x);
StringBuffer afterString = new StringBuffer();
for(int i = beforeString.length()-1; i >= 0 ; i--) {
afterString.append(beforeString.charAt(i));
}
if(beforeString.equals(afterString.toString())) {
return true;
} else {
return false;
}
}
}
3. Report
affterString를 String Class에서 StringBuffer Class로 수정하여 실행하니 실행 속도가 3배 빨랐다.
'algorithm' 카테고리의 다른 글
[LeetCode] 28. Implement strStr() (0) | 2021.01.17 |
---|---|
[LeetCode] 58. Length Of LastWord (0) | 2021.01.14 |
[LeetCode] 7. ReverseInteger (0) | 2021.01.07 |
[LeetCode] 2. Add Two Numbers (0) | 2020.12.26 |
[LeetCode] 1. TwoSum (0) | 2020.12.26 |