문제
https://leetcode.com/problems/palindrome-number/description/
Palindrome : 거꾸로 읽어도 제대로 읽는 것과 같은 문장이나 낱말, 숫자, 문자열 등
앞뒤 똑같으면 true, 다른게 있으면 false 반환
1.
function isPalindrome(x: number): boolean {
const string = x.toString().split('');
let left = 0;
let right = string.length - 1;
while(left < right) {
if (string[left] !== string[right]) {
return false
}
left++;
right--;
}
return true
};
주어진 number 를 string[] 으로 만들어 앞뒤로 하나씩 비교하며 뱉어냈다.
2. 다른 사람 풀이
function isPalindrome(x: number): boolean {
if (x < 0 || (x % 10 === 0 && x !== 0)) { // 음수이거나 10의 배수인 경우 false
return false;
}
let reverse: number = 0;
let temp: number = x;
while (temp !== 0) {
reverse = (reverse * 10) + (temp % 10);
temp = Math.floor(temp / 10);
}
return reverse === x;
};
이게 어떤 원리로 되는 건지??
다시 공부하기
'코딩테스트' 카테고리의 다른 글
[LeetCode/TS] 20. Valid Parentheses (0) | 2024.12.30 |
---|---|
[LeetCode/TS] 13. Roman to Integer (0) | 2024.12.27 |
[Leetcode/TS] 2. Add Tow Number (0) | 2024.12.18 |
[Leetcode/TS] 1. Two Sum (1) | 2024.12.17 |
프로그래머스 | 레벨0 | Kotlin (0) | 2024.02.02 |