public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
String number = Integer.toBinaryString(n);
int answer = 0;
for(int i=0;i<number.length();i++){
if(number.charAt(i)=='1')
answer++;
}
return answer;
}
}
'문제풀이' 카테고리의 다른 글
Leetcode Happy Number JAVA (0) | 2021.01.04 |
---|---|
Leetcode Best Time to Buy and Sell Stock JAVA (0) | 2021.01.04 |
Leetcode Intersection of Two Arrays II JAVA (0) | 2020.12.28 |
LeetCode Missing Number JAVA (0) | 2020.12.26 |
Leetcode First Unique Character in a String JAVA (0) | 2020.12.25 |