Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- 코드스테이츠
- Native select
- 배열
- 정규표현식
- 코딩테스트
- next/Image
- input class
- 코드스테이츠 메인프로젝트
- 백준
- MUI
- react js
- CSS
- 이벤트 루프
- primitive type
- Node js
- 알고리즘
- 자료구조
- nodejs
- 프론트엔드
- 페이지네이션
- 백준 nodeJS
- 버블정렬
- OSI 7계층
- 자바스크립트
- JavaScript Deep Dive
- 유데미
- kakao map api
- javascript
- 재귀함수
- sort
Archives
- Today
- Total
신입 개발자에서 시니어 개발자가 되기까지
[백준 node js] 25501 재귀함수 Palindrome 본문
문제
- 팰린드롬인지 확인하고, 재귀함수가 호출된 횟수 출력.
- 팰린드롬이란? 앞에서 읽었을 때와 뒤에서 읽었을 때가 같은 문자열
코드
- index를 조작하는 방법
const strings = require("fs")
.readFileSync("/dev/stdin")
.toString()
.trim()
.split("\n");
let result = "";
function recursion(string, startIdx, endIdx, count = 0) {
count++;
if (string[startIdx] !== string[endIdx]) {
return `0 ${count}`;
} else if (startIdx >= endIdx) {
return `1 ${count}`;
}
return recursion(string, startIdx + 1, endIdx - 1, count);
}
function isPalindrome(string) {
return `${recursion(string, 0, string.length - 1)}\n`;
}
function resultRecur(string) {
result += isPalindrome(string);
}
for (let i = 1; i < strings.length; i++) {
resultRecur(strings[i]);
}
console.log(result);
count에 기본값을 0을 주고, 호출될 때마다 +1이 되도록하고 마지막에 return count.
- slice로 string자체를 줄이는 법
function recursion(string) {
console.log(string);
if (string.length <= 1) return true;
if (string[0] !== string[string.length - 1]) return false;
// const newString = string.substring(1);
// return isPalindrome(newString.slice(0, newString.length - 1));
return recursion(string.slice(1, -1));
}
const test = isPalindrome("amanaplanacanalpanama");
console.log(test);
'javascript > 알고리즘' 카테고리의 다른 글
[백준 node JS] 25305 커트라인 (0) | 2022.10.06 |
---|---|
[백준] 2750번 오름차순 정렬(node.js) (0) | 2022.10.05 |
기본수학2 9020 - 골드바흐의 추측 (0) | 2022.08.22 |
백준 javascript 재귀함수 10872 - 팩토리얼 (0) | 2022.08.22 |
javascript 백준 재귀함수 10870 - 피보나치 (0) | 2022.08.22 |