Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7b369a9

Browse files
fea: 2 / 이진 변환 반복하기 / 새로운 문제
1 parent c7bed2b commit 7b369a9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//더 좋은 풀이가 존재할 수 있습니다.
3+
//정답 1 - codeisneverodd
4+
function solution(s) {
5+
const removeZero = s => {
6+
const removed = s
7+
.split('')
8+
.filter(n => n !== '0')
9+
.join('');
10+
return { removed, count: s.length - removed.length };
11+
};
12+
13+
const convertToBinary = (s, turnCount, removedCount) => {
14+
if (s === '1') return [turnCount, removedCount];
15+
const { removed, count } = removeZero(s);
16+
return convertToBinary(removed.length.toString(2), turnCount + 1, removedCount + count);
17+
};
18+
19+
return convertToBinary(s, 0, 0);
20+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /