문제
Given an integer x, return true if x is a palindrome, and false otherwise.
https://leetcode.com/problems/palindrome-number/
예시
코드
function isPalindrome(x: number): boolean {
return x.toString().split('').reverse().join('') === x.toString() ? true : false;
};
거꾸로 읽어도 제대로 읽는 것과 같은 문장이나 낱말, 숫자, 문자열을 팰린드롬이라고 한다. 이 문제는 숫자가 주어지고, 해당 숫자가 팰린드롬 수인지 판단하면 된다.
따라서, x를 뒤집은 것과 x가 같은지 판단하였다. x를 먼저 문자열로 바꾼 뒤, `split()`과 `reverse()`를 이용해 거꾸로 뒤집힌 배열로 만들어주고, `join()`으로 다시 문자열로 합쳐서 기존 x와 같은지 비교하였다.
728x90
'Algorithm > leetcode' 카테고리의 다른 글
[leetcode][TS] 20. Valid Parentheses (0) | 2024.08.23 |
---|---|
[leetcode][TS] 13. Roman to Integer (0) | 2024.08.19 |
[leetcode][TS] 1. Two Sum (0) | 2024.08.14 |
[leetcode][JS] 2721. Execute Asynchronous Functions in Parallel (0) | 2024.08.07 |
[leetcode][JS] 2624. Snail Traversal (0) | 2024.08.01 |