[백준] 1259 팰린드롬수 - Node.js

송철진·2023년 5월 13일
0

백준-Node.js

목록 보기
64/69

문제

https://www.acmicpc.net/problem/1259

solution

const fs = require('fs')
const input = fs.readFileSync('/dev/stdin').toString().trim()

const solution = (input) => {
  const arr = input.split('\n')
  arr.splice(-1)
  let result = []
  for(let i=0; i<arr.length; i++){
      const v = arr[i]
      for(let j=0; j<v.length/2; j++){
          if(v[j] !== v[v.length-1-j]){
              result.push('no')
              break;
          }else if(j>=v.length/2-1){
              result.push('yes')
          }   
      }
  }
  return result.join('\n')
}
console.log(solution(input))

순회 횟수를 줄이기 위해 for문의 범위를 주어진 '숫자'의 길이값의 절반까지만 순회하도록 한다.
break에 걸리면 'no'를, break에 걸리지 않고 모두 순회했을 때 'yes'를 빈배열 result에 push한다.

j>=v.length/2-1에서 =을 빼먹지 않도록 주의할 것!

profile
검색하고 기록하며 학습하는 백엔드 개발자

0개의 댓글