programmers_이진 변환 반복하기_java
2023. 4. 1. 17:16ㆍAlgorithm/Programmers
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/70129
class Solution {
public int[] solution(String s) {
int[] answer = new int[2];
while(!s.equals("1")){
int[] res = removeZero(s);
s = toBinary(res[1]);
answer[0]++;
answer[1]+=res[0];
}
return answer;
}
// 0의 개수 count
public int[] removeZero(String s){
int sLength = s.length();
int cnt = 0;
for(int i = 0; i<sLength; i++){
if(s.charAt(i) == '0')
cnt++;
}
int[] result = {cnt, sLength - cnt};
return result;
}
// 2진수로 변환
public String toBinary(int n){
String s = "";
while(n >= 1){
int remain = n%2;
n = n/2;
s = remain + "" + s;
}
return s;
}
}
'Algorithm > Programmers' 카테고리의 다른 글
programmers_구명보트_java (0) | 2023.04.03 |
---|---|
programmers_이진 변환 반복하기_java (0) | 2023.04.01 |
programmers_디펜스 게임_java (0) | 2023.04.01 |
programmers_무인도 여행_java (0) | 2023.03.30 |
programmers_미로 탈출_java (0) | 2023.03.28 |