Skip to content

Commit 863d92b

Browse files
committed
issue #11 191-Done
1 parent 7c00eae commit 863d92b

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/leetcodeEasyLevel/_0190ReverseBits.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package leetcodeEasyLevel;
22

3+
//How to solve :
4+
35
public class _0190ReverseBits {
46
// you need treat n as an unsigned value
5-
public int reverseBits(int n) {
7+
public static int reverseBits(int n) {
68
int ret=0;
79
// because there are 32 bits in total
810
for (int i = 0; i <32;i++){
11+
//비트 이동연산자 <<n: n만큼 왼쪽으로 이동
912
ret = ret<<1;
1013
// If the bit is 1 we OR it with 1, ie add 1
1114
if((n & 1) > 0){
@@ -15,4 +18,9 @@ public int reverseBits(int n) {
1518
}
1619
return ret;
1720
}
21+
22+
public static void main(String[] args) {
23+
String n = "00000010100101000001111010011100";
24+
//System.out.println(reverseBits(Integer.parseUnsignedInt(n)));
25+
}
1826
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package leetcodeEasyLevel;
2+
3+
public class _0191Numberof1Bits {
4+
5+
public static int hammingWeight(int n) {
6+
//sol1 Runtime 0 ms Memory 36 MB
7+
// >>>n : unsigned 우측 쉬프트 연산자는 오른편으로 n칸씩이동하며 왼편에 있는 부호와 무관하게 0으로 채운다.
8+
int cnt = 0;
9+
while(n != 0){
10+
if((n&1)==1){
11+
cnt ++;
12+
}
13+
n = n>>>1;
14+
}
15+
16+
return cnt;
17+
18+
//sol2 Runtime 0 ms Memory 36 MB
19+
/*
20+
return Integer.bitCount(n);
21+
*/
22+
}
23+
24+
public static void main(String[] args) {
25+
int n = 011101110;
26+
System.out.println(hammingWeight(n));
27+
}
28+
}
29+

0 commit comments

Comments
 (0)