File tree 6 files changed +278
-0
lines changed 6 files changed +278
-0
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ LeetCode Solution in Java
5
5
## Topics
6
6
7
7
- [ Math] ( #math )
8
+ - [ Bit] ( #math )
8
9
- [ Array/String] ( #stringarray )
9
10
- [ Two Pointers] ( #two-pointers )
10
11
- [ Matrix] ( #matrix )
@@ -34,6 +35,16 @@ LeetCode Solution in Java
34
35
| 989| [ Add to Array-Form of Integer] ( https://leetcode.com/problems/add-to-array-form-of-integer/ ) | [ Java] ( math/add-to-array-form-of-integer/README.md ) | Easy|
35
36
| 1232| [ Check If It Is a Straight Line] ( https://leetcode.com/problems/check-if-it-is-a-straight-line/ ) | [ Java] ( math/check-if-it-is-a-straight-line/README.md ) | Easy|
36
37
38
+ ## Bit
39
+
40
+ | # | Title | Solution | Difficulty |
41
+ | :-:| -| -| -|
42
+ | 191| [ Number of 1 Bits] ( https://leetcode.com/problems/number-of-1-bits/ ) | [ Java] ( bit/number-of-1-bits/README.md ) | Easy|
43
+ | 231| [ Power of Two] ( https://leetcode.com/problems/power-of-two/ ) | [ Java] ( bit/power-of-two/README.md ) | Easy|
44
+ | 371| [ Sum of Two Integers] ( https://leetcode.com/problems/sum-of-two-integers/ ) | [ Java] ( bit/sum-of-two-integers/README.md ) | Medium|
45
+ | 461| [ Hamming Distance] ( https://leetcode.com/problems/hamming-distance/ ) | [ Java] ( bit/hamming-distance/README.md ) | Easy|
46
+ | 477| [ Total Hamming Distance] ( https://leetcode.com/problems/total-hamming-distance/ ) | [ Java] ( bit/total-hamming-distance/README.md ) | Medium|
47
+
37
48
## Array/String
38
49
39
50
| # | Title | Solution | Difficulty |
Original file line number Diff line number Diff line change
1
+ # Hamming Distance
2
+
3
+ ## Solution 1
4
+
5
+ ``` java
6
+ /**
7
+ * Question : 461. Hamming Distance
8
+ * Complexity : Time: O(1) ; Space: O(1)
9
+ * Topics : Bit
10
+ */
11
+ class Solution {
12
+ public int hammingDistance (int x , int y ) {
13
+ int res = 0 ;
14
+ while (x != 0 || y != 0 ) {
15
+ if ((x & 1 ) != (y & 1 )) {
16
+ res++ ;
17
+ }
18
+ x >>> = 1 ;
19
+ y >>> = 1 ;
20
+ }
21
+ return res;
22
+ }
23
+ }
24
+ ```
25
+
26
+ ## Solution 2
27
+
28
+ ``` java
29
+ /**
30
+ * Question : 461. Hamming Distance
31
+ * Complexity : Time: O(1) ; Space: O(1)
32
+ * Topics : Bit
33
+ */
34
+ class Solution {
35
+ public int hammingDistance (int x , int y ) {
36
+ int res = 0 ;
37
+ int diff = x ^ y;
38
+
39
+ while (diff != 0 ) {
40
+ res++ ;
41
+ diff = diff & (diff - 1 );
42
+ }
43
+
44
+ return res;
45
+ }
46
+ }
47
+ ```
Original file line number Diff line number Diff line change
1
+ # Number of 1 Bits
2
+
3
+ # Solution 1
4
+
5
+ ``` java
6
+ /**
7
+ * Question : 191. Number of 1 Bits
8
+ * Complexity : Time: O(1) ; Space: O(1)
9
+ * Topics : Bit
10
+ */
11
+ public class Solution {
12
+ public int hammingWeight (int n ) {
13
+ int count = 0 ;
14
+ for (int i = 0 ; i < 32 ; i++ ) {
15
+ count += n & 1 ;
16
+ n >>> = 1 ;
17
+ }
18
+ return count;
19
+ }
20
+ }
21
+ ```
22
+
23
+ # Solution 2
24
+
25
+ ``` java
26
+ /**
27
+ * Question : 191. Number of 1 Bits
28
+ * Complexity : Time: O(1) ; Space: O(1)
29
+ * Topics : Bit
30
+ */
31
+ public class Solution {
32
+ public int hammingWeight (int n ) {
33
+ int count = 0 ;
34
+ for (int i = 0 ; i < 32 ; i++ ) {
35
+ if ((n & (1 << i)) != 0 ) {
36
+ count++ ;
37
+ }
38
+ }
39
+ return count;
40
+ }
41
+ }
42
+ ```
43
+
44
+ # Solution 3
45
+
46
+ ``` java
47
+ /**
48
+ * Question : 191. Number of 1 Bits
49
+ * Complexity : Time: O(1) ; Space: O(1)
50
+ * Topics : Bit
51
+ */
52
+ public class Solution {
53
+ public int hammingWeight (int n ) {
54
+ int count = 0 ;
55
+ while (n != 0 ) {
56
+ count++ ;
57
+ n = n & (n - 1 );
58
+ }
59
+ return count;
60
+ }
61
+ }
62
+ ```
Original file line number Diff line number Diff line change
1
+ # Power of Two
2
+
3
+ ## Solution 1
4
+
5
+ ``` java
6
+ /**
7
+ * Question : 231. Power of Two
8
+ * Complexity : Time: O(log(n)) ; Space: O(1)
9
+ * Topics : Bit
10
+ */
11
+ class Solution {
12
+ public boolean isPowerOfTwo (int n ) {
13
+ if (n == 0 ) {
14
+ return false ;
15
+ }
16
+ if (n == 1 ) {
17
+ return true ;
18
+ }
19
+ if (n % 2 != 0 ) {
20
+ return false ;
21
+ }
22
+ return isPowerOfTwo(n / 2 );
23
+ }
24
+ }
25
+ ```
26
+
27
+ ## Solution 2
28
+
29
+ ``` java
30
+ /**
31
+ * Question : Power of Two
32
+ * Complexity : Time: O(1) ; Space: O(1)
33
+ * Topics : Bit
34
+ */
35
+ class Solution {
36
+ public boolean isPowerOfTwo (int n ) {
37
+ if (n < 0 ) {
38
+ return false ;
39
+ }
40
+
41
+ int count = 0 ;
42
+
43
+ while (n != 0 ) {
44
+ count++ ;
45
+ n = n & (n - 1 );
46
+ }
47
+
48
+ return count == 1 ;
49
+ }
50
+ }
51
+ ```
Original file line number Diff line number Diff line change
1
+ # Sum of Two Integers
2
+
3
+ ## Solution 1
4
+
5
+ ``` java
6
+ /**
7
+ * Question : 371. Sum of Two Integers
8
+ * Complexity : Time: O(1) ; Space: O(1)
9
+ * Topics : Bit
10
+ */
11
+ class Solution {
12
+ public int getSum (int a , int b ) {
13
+ while (b != 0 ) {
14
+ int carry = (a & b) << 1 ;
15
+ a = a ^ b;
16
+ b = carry;
17
+ }
18
+
19
+ // Round1:
20
+ // a: 0111
21
+ // b: 0110
22
+ // (a & b) << 1 = 0110 << 1 = 1100 (carry part)
23
+ // (a ^ b) = 0001 (add without carry)
24
+ // Round2:
25
+ // a: 0001
26
+ // b: 1100
27
+ // (a & b) << 1 = 0000 << 1 = 0000 (carry part)
28
+ // (a ^ b) = 1101 (add without carry)
29
+
30
+ return a;
31
+ }
32
+ }
33
+ ```
Original file line number Diff line number Diff line change
1
+ # Total Hamming Distance
2
+
3
+ ## Solution 1
4
+
5
+ Time Limit Exceeded
6
+
7
+ ``` java
8
+ /**
9
+ * Question : 477. Total Hamming Distance
10
+ * Complexity : Time: O(n^2) ; Space: O(1)
11
+ * Topics : Bit
12
+ */
13
+ class Solution {
14
+ public int totalHammingDistance (int [] nums ) {
15
+ int total = 0 ;
16
+
17
+ for (int i = 0 ; i < nums. length; i++ ) {
18
+ for (int j = i + 1 ; j < nums. length; j++ ) {
19
+ total += hammingDistance(nums[i], nums[j]);
20
+ }
21
+ }
22
+
23
+ return total;
24
+ }
25
+
26
+ private int hammingDistance (int a , int b ) {
27
+ int diff = a ^ b;
28
+
29
+ int res = 0 ;
30
+ while (diff != 0 ) {
31
+ res++ ;
32
+ diff = diff & (diff - 1 );
33
+ }
34
+
35
+ return res;
36
+ }
37
+ }
38
+ ```
39
+
40
+ ## Solution 2
41
+
42
+ ``` java
43
+ /**
44
+ * Question : 477. Total Hamming Distance
45
+ * Complexity : Time: O(n) ; Space: O(1)
46
+ * Topics : Bit
47
+ */
48
+ class Solution {
49
+ public int totalHammingDistance (int [] nums ) {
50
+ int n = nums. length;
51
+
52
+ // count stores the number of 1 in each bit.
53
+ int [] count = new int [32 ];
54
+
55
+ for (int i = 0 ; i < n; i++ ) {
56
+ int num = nums[i];
57
+ int j = 0 ;
58
+ while (num > 0 ) {
59
+ count[j] += num & 1 ;
60
+ num >>> = 1 ;
61
+ j++ ;
62
+ }
63
+ }
64
+
65
+ int res = 0 ;
66
+ for (int i = 0 ; i < count. length; i++ ) {
67
+ // (number of 1) * (number of 0) in this bit.
68
+ res += count[i] * (n - count[i]);
69
+ }
70
+
71
+ return res;
72
+ }
73
+ }
74
+ ```
You can’t perform that action at this time.
0 commit comments