Last active
August 23, 2021 05:55
-
-
Save emayom/f767f847322f12d707422c090345c3b3 to your computer and use it in GitHub Desktop.
[프로그래머스] 다음 큰 숫자
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//solution 01 | |
function solution(n) { | |
function cnt(number) { | |
return number.toString(2) | |
.match(/1/g) | |
.length; | |
} | |
const n_cnt = cnt(n); | |
let start = n; | |
do { | |
start++; | |
if(cnt(start) == n_cnt){ | |
return start; | |
} | |
} while(true) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//solution 02 | |
function solution(n) { | |
function cnt(number) { | |
return number.toString(2) | |
.match(/1/g) | |
.length; | |
} | |
const n_cnt = cnt(n); | |
let start = n+1; | |
while(cnt(start) != n_cnt) { | |
start++; | |
} | |
return start; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//solution 03 | |
function solution(n) { | |
function cnt(number) { | |
let temp = []; | |
do { | |
temp.push(number%2); | |
number = Math.floor(number/2); | |
} while(number > 0) | |
return temp.join('').match(/1/g).length; | |
} | |
const n_cnt = cnt(n); | |
let start = n; | |
do { | |
start++; | |
if(cnt(start) == n_cnt){ | |
return start; | |
} | |
} while(true) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//solution 04 | |
function solution(n) { | |
function cnt(number) { | |
return number.toString(2) | |
.split('') | |
.filter((el) => el == 1) | |
.length; | |
} | |
const n_cnt = cnt(n); | |
let start = n + 1; | |
for(start; ; start++){ | |
if(cnt(start) == n_cnt){ | |
return start; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment