👽
frontend-survival
  • Dev Note
    • 🐣1주차
      • 개발환경
      • TypeScript
      • React
      • Testing Library
      • Parcel & ESlint
    • 🐥2주차
      • JSX
      • Virtual DOM
    • 🐥3주차
      • React Component
      • React State
    • 🐥4주차
      • Express
      • fetch API & CORS
      • React Hooks
      • useRef & custom hook
      • usehooks-ts
    • 🐥5주차
      • TDD
      • React Testing Library
      • MSW
    • 🐥6주차
      • external store
      • tsyringe
    • 🐥7주차
      • routing
      • routes
      • router
      • navigation
    • 🐥8주차
      • design system
      • style basics
      • styled components
    • 🐥javascript
      • promise
    • 🐥practice
      • 240102
      • 240103
      • 240423
      • 240424
      • 240426
      • 240430
      • 240501
      • 240506
      • 240508
      • 240512
      • 240514
      • 240515
      • 240714
      • 240716
      • 240717
      • 240808
      • 240813
      • 240816
      • 240819
      • 240820
      • 240826
      • 240904
      • 240906
      • 240909
      • 241021
    • 🐥react
      • state
      • global state
      • Context
      • module state
    • 🐥Next.js
      • app router
    • 🐥algorithm
      • reverse string
      • palindrome
      • reverse int
      • max char
      • chunk
      • anagrams
      • capitalize
      • printing step
      • pyramids
      • vowels
      • matrix spiral
      • queue from stack
    • 🐥css
      • mobile inner height
    • 🐥Clean Architecture
      • object oriented programming
Powered by GitBook
On this page
  1. Dev Note
  2. 🐥practice

240904

문제 : 다음 큰 숫자 난이도 : Lv.2

풀이

  function solution(n) {
      // 주어진 수 n의 2진수에서 1의 개수 구하기
    const countOnes = (n) => n.toString(2).split('1').length - 1;
    
    const nOnes = countOnes(n); // n의 1의 개수
    let nextNum = n + 1; // n보다 큰 수를 찾기 위해 n+1부터 시작
    
    // 1의 개수가 동일한 수를 찾을 때까지 반복
    while (countOnes(nextNum) !== nOnes) {
        nextNum++; // 다음 숫자로 증가
    }
    
    return nextNum; // 조건을 만족하는 가장 작은 숫자 반환
}
Previous240826Next240906

Last updated 9 months ago