π₯ Palindrome Numberπ₯
Example 1:
Input: x = 121;
Output: true;
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.
Example 4:
Input: x = -101;
Output: false;
μ κ·Ό μμ
- μ
λ ₯λ°μ μ«μλ₯Ό λ¬Έμμ΄λ‘ λ³κ²½
- λ¬Έμμ΄μ λ°°μ΄λ‘ λ³ν
- λ°°μ΄μ reverse νκΈ°
- λ°°μ΄ μμ νλμ λ¬Έμμ΄λ‘ ν©μΉκΈ°
- λ¬Έμμ΄μ μ«μλ‘ λ³ν
- μλ³Έκ³Ό κ±°κΎΈλ‘ λ€μ§μ μ«μκ° κ°μΌλ©΄ true, μλλ©΄ false 리ν΄νκΈ°.
μ½λ μμ±
let isPalindrome = function(x) {
let reversedX = parseInt(
x
.toString()
.split("")
.reverse()
.join("")
);
return x === reversedX ? true : false;
};
WILT : What I Learned Today π€
- μκ³ λ¦¬μ¦ λΏλ§ μλλΌ κ°μΈ νλ‘μ νΈμμλ μ
λ ₯λ°μ λ°μ΄ν°μ νμ
μ 체ν¬νμ§ μμμ μ½λ μ€λ₯κ° λ°μνλ κ²½νμ νλ€. μ§κΈ μ
λ ₯λ°μ λ°μ΄ν°μ νμ
μ΄ λ¬΄μμΈμ§, λ΄κ° λ°κΎΈκ³ μ νλ λ°μ΄ν°μ νμ
μ 무μμ΄κ³ , μ΄λ€ λ©μλλ₯Ό μ¬μ©νλ©΄ λλμ§μ λν΄ κ³ λ―Όνλ μκ°μ μΆ©λΆν κ°μ ΈμΌκ² λ€.
- μ’ λ λμ μ μΈ μλ ¨μ μν΄μ μκ³ λ¦¬μ¦ λμ΄λλ‘€ μμ΄μ νμ΄λ³΄λ €κ³ νλ€. νμμΌμλ λμ΄λ
medium
μ λμ ν μμ μ΄λ€.