Skip to content

Commit 1cf4c9e

Browse files
author
Botao Xiao
committed
[Function add]
1. Add leetcode solutions.
1 parent 90a99bf commit 1cf4c9e

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@
335335

336336
[187. Repeated DNA Sequences](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/187.%20Repeated%20DNA%20Sequences.md)
337337

338+
[189. Rotate Array](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/189.%20Rotate%20Array.md)
339+
340+
[190. Reverse Bits](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/190.%20Reverse%20Bits.md)
341+
342+
[191. Number of 1 Bits](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/191.%20Number%20of%201%20Bits.md)
343+
338344
## Algorithm(4th_Edition)
339345
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
340346
All java realization codes are placed in different packages.

leetcode/189. Rotate Array.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,28 @@ class Solution {
6767
}
6868
}
6969
}
70-
```
70+
```
71+
72+
### 二刷
73+
1. 先计算余数。
74+
2. 保存从尾向前余数个个数。
75+
3. 将剩余的数向后以后。
76+
4. 最后将保存的数填充到数组的最开头。
77+
78+
```Java
79+
class Solution {
80+
public void rotate(int[] nums, int k) {
81+
int len = nums.length;
82+
int t = k % len;
83+
int[] arr = new int[t];
84+
for(int i = 0; i < t; i++){
85+
arr[i] = nums[len - t + i];
86+
}
87+
for(int i = len - t - 1; i >= 0; i--){
88+
nums[i + t] = nums[i];
89+
}
90+
for(int i = 0; i < t; i++)
91+
nums[i] = arr[i];
92+
}
93+
}
94+
```

leetcode/190. Reverse Bits.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,21 @@ public class Solution {
3737
return result;
3838
}
3939
}
40-
```
40+
```
41+
42+
### 二刷
43+
1. 唯一要注意的就是右移补零是>>>
44+
```Java
45+
public class Solution {
46+
// you need treat n as an unsigned value
47+
public int reverseBits(int n) {
48+
int result = 0;
49+
int ref = 1 << 31;
50+
for(int i = 0; i < 16; i++){
51+
result += ((n & (1 << i)) << (31 - i * 2));
52+
result += ((n & (ref >>> i)) >>> (31 - i * 2));
53+
}
54+
return result;
55+
}
56+
}
57+
```

leetcode/191. Number of 1 Bits.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,20 @@ public class Solution {
3232
return count;
3333
}
3434
}
35-
```
35+
```
36+
37+
### 二刷
38+
```Java
39+
public class Solution {
40+
// you need to treat n as an unsigned value
41+
public int hammingWeight(int n) {
42+
int count = 0;
43+
int cur = 1;
44+
for(int i = 0; i < 32; i++){
45+
if((n & (1 << i)) != 0)
46+
count ++;
47+
}
48+
return count;
49+
}
50+
}
51+
```

0 commit comments

Comments
 (0)