코딩테스트/프로그래머스
[프로그래머스] 이진 변환 반복하기 - 자바스크립트
핸디(Handy)
2021. 12. 6. 22:38
[ 문제 설명 ]
코딩테스트 연습 - 이진 변환 반복하기
programmers.co.kr
[ 아이디어 ]
1레벨인데 오랜만에 풀겸 그리고 프로그래머스의 불편함으로 잠깐 글을 적는다.
아이디어는 아래의 로직을 반복하면 된다.
[ 코드 ]
function solution(s) {
let zeroCount = 0;
let transformCount = 0;
while (s > 1) {
let temp = s.length;
s = s.replace(/0/g, "");
zeroCount += temp - s.length;
s = s.length.toString(2);
transformCount++;
}
return [transformCount, zeroCount];
}
원래라면 replaceAll를 써서 할수가 있다. 굳이 정규식을 통해 replace를 안써도 되는데 프로그래머스에 이를 지원안해서 살짝 당황했다.