[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