반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- JUnit
- java 11
- el1008e
- OpenFeign
- springbatch error
- xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools)
- easy
- querydsl no sources given
- java 버전 변경
- No tests found for given includes
- java version
- property or field 'jobparameters' cannot be found on object of type
- aws
- Java 1.8
- log error
- springboot
- java 1.8 11
- parse
- maybe not public or not valid?
- AWS CLI
- error
- mac os git error
- java
- java 여러개 버전
- Medium
- 스프링부트테스트
- yum install java
- no sources given
- springboottest
- LeetCode
Archives
- Today
- Total
쩨이엠 개발 블로그
[ Leetcode ] 9. Palindrome Number - Java 본문
728x90
반응형
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 palindrome while 123 is not.
Example 1:
Input: x = 121 Output: true
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.
Example 3:
Input: x = 10 Output: false
Explanation: Reads 01 from right to left.
Therefore it is not a palindrome.
Example 4:
Input: x = -101 Output: false
Constraints:
- -231 <= x <= 231 - 1
Solution
class Solution {
public boolean isPalindrome(int x) {
if(x < 0){
return false;
}
int reverse=0;
int temp=x;
while(temp!=0){
reverse = reverse*10 + temp%10;
temp /= 10;
}
return reverse == x;
}
}
풀다보니 int값을 줬을 때 String으로 캐스팅해서 풀면 속도가 엄청 줄어든다 거의 5배정도 차이가 나서
숫자로 풀어보기로 했다
1. 0보다 작은 경우는 -가 붙으므로 무조건 false
2. 숫자를 reverse한다. 나머지를 구하고 다음턴부터 10씩 곱해준다
3. 거꾸로 한 숫자와 같은지 확인한다
728x90
반응형
'개발 > Programming' 카테고리의 다른 글
[ Leetcode ] 11. Container With Most Water - Java (0) | 2021.03.12 |
---|---|
[ Leetcode ] 8. String to Integer (atoi) - Java (0) | 2021.03.09 |
[ Leetcode ] 6. ZigZag Conversion - Java (0) | 2021.02.25 |
[ Leetcode ] 1669. Merge In Between Linked Lists - Java (0) | 2021.02.13 |
[ Leetcode ] 148. Sort List - Java (0) | 2021.02.10 |
Comments