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 |
Tags
- 스프링 클라우드
- GIT
- map()
- IntelliJ
- 도커
- map
- 프로그래머스
- date
- 자바스크립트
- spring boot
- gitlab
- spring security
- JS
- OAuth
- leetcode
- SpringBoot
- Java
- Spring Cloud
- STS
- 스프링
- 스프링부트
- docker
- Spring
- 코딩테스트
- 자바
- JavaScript
- 비동기
- 유레카
- EUREKA
- jQuery
Archives
- Today
- Total
RATSENO
[Leetcode]Palindrome Number 본문
class Solution {
public boolean isPalindrome(int x) {
// Special cases:
// As discussed above, when x < 0, x is not a palindrome.
// Also if the last digit of the number is 0, in order to be a palindrome,
// the first digit of the number also needs to be 0.
// Only 0 satisfy this property.
if(x < 0 || (x % 10 == 0 && x != 0)) {
return false;
}
int revertedNumber = 0;
while(x > revertedNumber) {
revertedNumber = revertedNumber * 10 + x % 10;
x /= 10;
}
// When the length is an odd number, we can get rid of the middle digit by revertedNumber/10
// For example when the input is 12321, at the end of the while loop we get x = 12, revertedNumber = 123,
// since the middle digit doesn't matter in palidrome(it will always equal to itself), we can simply get rid of it.
return x == revertedNumber || x == revertedNumber/10;
}
}'DEV > 코딩테스트 문제풀기' 카테고리의 다른 글
| [Leetcode]Longest Common Prefix (0) | 2020.03.16 |
|---|---|
| [Leetcode]Roman to Integer (0) | 2020.03.16 |
| [JAVA]프로그래머스(level1) - 정수 제곱근 판별 (0) | 2020.01.06 |
| [JAVA]프로그래머스(level1) - 정수 내림차순으로 배치하기 (0) | 2020.01.06 |
| [JAVA]프로그래머스(level1) - 자연수 뒤집어 배열로 만들기 (0) | 2020.01.06 |
Comments