Skip to content

Commit f623034

Browse files
authored
Merge pull request doocs#269 from J-Cod3r/master
feat:add insert into bits,convert integer,exchange java solution
2 parents 9effb55 + c4b58b7 commit f623034

File tree

9 files changed

+60
-6
lines changed

9 files changed

+60
-6
lines changed

lcci/05.01.Insert Into Bits/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@
3434
<!-- 这里可写当前语言的特殊实现逻辑 -->
3535

3636
```java
37-
37+
class Solution {
38+
public int insertBits(int N, int M, int i, int j) {
39+
for (int k = i; k <= j; k++) {
40+
N &= ~(1 << k);
41+
}
42+
return N ^ (M << i);
43+
}
44+
}
3845
```
3946

4047
### ...

lcci/05.01.Insert Into Bits/README_EN.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@
3131
### Java
3232

3333
```java
34-
34+
class Solution {
35+
public int insertBits(int N, int M, int i, int j) {
36+
for (int k = i; k <= j; k++) {
37+
N &= ~(1 << k);
38+
}
39+
return N ^ (M << i);
40+
}
41+
}
3542
```
3643

3744
### ...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public int insertBits(int N, int M, int i, int j) {
3+
for (int k = i; k <= j; k++) {
4+
N &= ~(1 << k);
5+
}
6+
return N ^ (M << i);
7+
}
8+
}

lcci/05.06.Convert Integer/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@
4040
<!-- 这里可写当前语言的特殊实现逻辑 -->
4141

4242
```java
43-
43+
class Solution {
44+
public int convertInteger(int A, int B) {
45+
return Integer.bitCount(A ^ B);
46+
}
47+
}
4448
```
4549

4650
### ...

lcci/05.06.Convert Integer/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@
5656
### Java
5757

5858
```java
59-
59+
class Solution {
60+
public int convertInteger(int A, int B) {
61+
return Integer.bitCount(A ^ B);
62+
}
63+
}
6064
```
6165

6266
### ...
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public int convertInteger(int A, int B) {
3+
return Integer.bitCount(A ^ B);
4+
}
5+
}

lcci/05.07.Exchange/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@
4040
<!-- 这里可写当前语言的特殊实现逻辑 -->
4141

4242
```java
43-
43+
class Solution {
44+
public int exchangeBits(int num) {
45+
int t1 = num >> 1;
46+
int t2 = num << 1;
47+
return t1 & 0x55555555 | t2 & 0xaaaaaaaa;
48+
}
49+
}
4450
```
4551

4652
### ...

lcci/05.07.Exchange/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@
5757
### Java
5858

5959
```java
60-
60+
class Solution {
61+
public int exchangeBits(int num) {
62+
int t1 = num >> 1;
63+
int t2 = num << 1;
64+
return t1 & 0x55555555 | t2 & 0xaaaaaaaa;
65+
}
66+
}
6167
```
6268

6369
### ...

lcci/05.07.Exchange/Solution.java

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public int exchangeBits(int num) {
3+
int t1 = num >> 1;
4+
int t2 = num << 1;
5+
return t1 & 0x55555555 | t2 & 0xaaaaaaaa;
6+
}
7+
}

0 commit comments

Comments
 (0)