public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
String s = Integer.toString(n);
int ham = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != '0') {
ham++;
}
}
return ham;
}
}
난독이 찐으로 온 좌,,
믿기지 않는 난이도의 문제로 오예하는데,,
짧은 꿈이라도 행복했읍니다....
너무 달콤한 꿈은 꿈이 아닌걸로^^
public class Solution {
public int hammingWeight(int n) {
int ham = 0;
while (n != 0) {
if (n % 2 != 0) {
ham++;
}
n = n / 2;
}
return ham;
}
}
unsigned value로 생각 안해서 실패
ㅠㅠㅠㅠㅠ 이거 생각보다 빡쎄서 슬프네요
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
String s = Integer.toBinaryString(n);
int ham = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != '0') {
ham++;
}
}
return ham;
}
}
Integer.toBinaryString 으로 해결.
구글 검색 아시죠?^^
Runtime: 2 ms, faster than 6.13% of Java online submissions for Number of 1 Bits.
Memory Usage: 36.2 MB, less than 19.77% of Java online submissions for Number of 1 Bits.
but~ 믿기지 않는 런타임..