본문 바로가기
LeetCode/코딩 테스트 스터디 1주차

[LeetCode] 9. Palindrome-Number 풀이

by inwoo1324 2022. 7. 10.

0. 문제

https://leetcode.com/problems/palindrome-number/

 

Palindrome Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

정수가 주어지면 회문일 경우 true  회문이 아닐 경우 false를 반환한다.

#1
Input: x = 121
Output: true
#2
Input: x = -121
Output: false
#3
Input: x = 10
Output: false

 

1.언어

자바스크립트(JavaScript)

 

 

 

2. 문제 풀이

isPalindrome = function(x) {
  return  String(x).split('').reverse().join('') == x
};
Runtime: 193 ms, faster than 86.66% of JavaScript online submissions for Palindrome Number.
Memory Usage: 51.3 MB, less than 42.65% of JavaScript online submissions for Palindrome

입력값 x(121)를 문자열('121')  ->  배열(['1','2','1'])  ->  배열 뒤집기(['1','2','1']) -> 배열 합쳐서 문자열로 변환( '121') -> 비교 값 반환

한번에 통과!