💻 문제 출처 : 프로그래머스_점프와 순간 이동
public class Solution {
public int solution(int n) {
int answer = 0;
while(n != 0) {
if(n % 2 == 0) {
n /= 2;
} else {
n--;
answer++;
}
}
return answer;
}
}
public class Solution {
public int solution(int n) {
int answer = 0;
for(char c : Integer.toBinaryString(n).toCharArray()) {
if (c == '1') answer++;
}
return answer;
}
}
public class Solution {
public int solution(int n) {
int sub = 1;
int ans = 0;
while(n != 0){
if(n % 2 == 1){
n -= sub;
ans += 1;
}
n /= 2;
}
return ans;
}
}