File tree Expand file tree Collapse file tree 2 files changed +38
-1
lines changed
Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change 11package leetcodeEasyLevel ;
22
3+ //How to solve :
4+
35public 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}
Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments