class Solution {
public int solution(int n) {
String ex1 = Integer.toBinaryString(n);
int c = count(ex1);
while (true) {
String ex2 = Integer.toBinaryString(n+1);
if (c == count(ex2))
return n+1;
n++;
}
}
public int count(String num) {
int count =0;
for (int i =0;i<num.length();i++) {
if (num.charAt(i)=='1')
count++;
}
return count;
}
}
이진 변환을 위해 Integer.toBinaryString(n)을 이용하였다.
이후 charAt()를 통해 넘겨진 String의 단어 하나하나를 비교 하였다.
while문의 조건값도 제대로 설정안해주었고 비교하는 것도 뭔가 더 간소화 할 수 있을 것 같다. 개선의 여지가 많은 코드이다.
출처: 프로그래머스 코딩테스트 연습 https://school.programmers.co.kr/learn/courses/30/lessons/12911