반응형
📖문제. 숫자가 입력되면 해당 숫자가 팰린드롬이면 true, 아니면 false 를 출력하세요.
Given an integer x, return true if x is palindrome integer.
An integer is a palindrome when it reads the same backward as forward.
- For example, 121 is a palindrome while 123 is not.
Example 1:
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
Example 2:
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
class Solution {
public boolean isPalindrome(int x) {
int num = x;
int palindrome = 0;
while(num > 0){
palindrome=palindrome*10+num%10;
num/=10;
}
if(x == palindrome) return true;
return false;
}
}
300x250
'Algorithm > LeetCode' 카테고리의 다른 글
[LeetCode] 371. Sum of Two Integers (두 숫자의 합) (1) | 2022.05.24 |
---|---|
[LeetCode] 35. Search Insert Position (0) | 2022.04.20 |
[LeetCode] 3. Longest Substring Without Repeating Characters (0) | 2022.04.18 |
[LeetCode] 58. Length of Last Word (0) | 2022.04.17 |
[LeetCode] 릿코드 시작하기, Github에 자동 커밋(LeetHub) 방법 (0) | 2022.04.16 |