[leet code - easy] Palindrome Number ๋ฌธ์ ํ์ด
2023. 8. 22. 10:35ใFrontEnd/leet code
Description
Given an integer x, return true if x is a palindrome, and false otherwise.
๋ฆฌ๋ฒ์ค ๋ ๊ฒ x๋ ๊ฐ์ผ๋ฉด true ๋ผ๋ ๊ฒ ๊ฐ์
์ ์ถ๋ ฅ ์์
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.
Example 3:
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
๋ฌธ์ ํ์ด
var isPalindrome = function(x) {
const reversed = `${x}`.split('').reverse().join('');
return `${x}` === reversed;
};
x๊ฐ์ ๋ฌธ์์ด ์๋ฅด๊ธฐ(SPLIT)์ผ๋ก ์๋ฅด๊ณ ๋ ํ ๋ฆฌ๋ฒ์ค๋ฅผ ์์ผ์ฃผ๊ณ ๋ค์ ํฉ์น ๊ฐ์ reversed์ ๋ด์์ฃผ๊ณ x === reversed ํด์คฌ๋๋ฐ ์ด๋ ๊ฒ ๊ฐ๋จํ๊ฒ ํด๋ ๋๋....?
๊ฒฐ๊ณผ
๊ฒฐ๊ณผ๋ ์ ๋์ด!
'FrontEnd > leet code' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[leet code - easy] Valid Parentheses ๋ฌธ์ ํ์ด (0) | 2023.08.23 |
---|---|
[leet code - easy] two sum ๋ฌธ์ ํ์ด (0) | 2023.08.21 |