Notice
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- Programmers
- 알pdf #파일탐색기미리보기안될때
- 웹개발종합반
- 사전준비
- TS
- 챗GPT
- Expo
- 멍친구
- 프론트엔드
- 달리기반
- 7기
- Ai
- 필수강의
- TDD
- 항해99
- 코린이
- rn
- 맥린이
- 팀워크최고
- 프로그래머스
- ReactNative
- NotionAI
- 실전프로젝트
- 스파르타코딩클럽
- REACT
- 알고리즘기초주차
- typeScript
- D반8조
- 리액트
- ChatGPT
Archives
- Today
- Total
FrontEnd :-)
[LeetCode-JS] Palindrome Number 본문
🙋 문제: Palindrome Number (대칭수)
Given an integer x, return true if x is a palindrome*, and false otherwise.
*palindrome: An integer is a palindrome when it reads the same forward and backward. For example, 121 is a palindrome while 123 is not.
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.
🅰️ 풀이:
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
const arr = x.toString().split("");
for(let i = 0; i < arr.length; i++){
if(arr[i] !== arr[arr.length -(i+1)]){
return false;
}
}
return true;
};
✅ 다른 사람 풀이:
var s = '' + x; //숫자 => 문자열
var l = 0;
var r = s.length - 1;
while (l < r) {
if (s[l] !== s[r]) return false;
l++;
r--;
}
return true;
>> while 루프는 이상하게 잘 안 써진다.
x = "" + x
return x === x.split("").reverse().join("");
>> reverse, join 방법도 생각하긴 했었다.
'JavaScript > Algorithm' 카테고리의 다른 글
[LeetCode-JS] Merge Two Sorted Lists (0) | 2022.12.15 |
---|---|
[LeetCode-JS] Valid Parentheses (0) | 2022.12.10 |
[LeetCode-JS] Longest Common Prefix (0) | 2022.12.09 |
[LeetCode-JS] Roman to Integer (0) | 2022.12.08 |
[LeetCode-JS] Two Sum (0) | 2022.12.05 |
Comments