Skip to content

Commit e04c374

Browse files
authored
feat: add solutions to lc problems: No.190~195 (doocs#2633)
1 parent e6be3b5 commit e04c374

File tree

16 files changed

+205
-83
lines changed

16 files changed

+205
-83
lines changed

solution/0100-0199/0190.Reverse Bits/README.md

+32-28
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,36 @@
4949

5050
## 解法
5151

52-
### 方法一
52+
### 方法一:位运算
53+
54+
我们可以从低位到高位,逐个取出 `n` 的每一位,然后将其放到 `ans` 的对应位置上。
55+
56+
比如,对于第 $i$ 位,我们可以通过 `(n & 1) << (31 - i)` 来取出 `n` 的第 $i$ 位,并将其放到 `ans` 的第 $31 - i$ 位上,然后将 `n` 右移一位。
57+
58+
时间复杂度 $(\log n)$,空间复杂度 $O(1)$。
5359

5460
<!-- tabs:start -->
5561

5662
```python
5763
class Solution:
5864
def reverseBits(self, n: int) -> int:
59-
res = 0
65+
ans = 0
6066
for i in range(32):
61-
res |= (n & 1) << (31 - i)
67+
ans |= (n & 1) << (31 - i)
6268
n >>= 1
63-
return res
69+
return ans
6470
```
6571

6672
```java
6773
public class Solution {
6874
// you need treat n as an unsigned value
6975
public int reverseBits(int n) {
70-
int res = 0;
76+
int ans = 0;
7177
for (int i = 0; i < 32 && n != 0; ++i) {
72-
res |= ((n & 1) << (31 - i));
78+
ans |= (n & 1) << (31 - i);
7379
n >>>= 1;
7480
}
75-
return res;
81+
return ans;
7682
}
7783
}
7884
```
@@ -81,36 +87,35 @@ public class Solution {
8187
class Solution {
8288
public:
8389
uint32_t reverseBits(uint32_t n) {
84-
uint32_t res = 0;
85-
for (int i = 0; i < 32; ++i) {
86-
res |= ((n & 1) << (31 - i));
90+
uint32_t ans = 0;
91+
for (int i = 0; i < 32 && n; ++i) {
92+
ans |= (n & 1) << (31 - i);
8793
n >>= 1;
8894
}
89-
return res;
95+
return ans;
9096
}
9197
};
9298
```
9399
94100
```go
95-
func reverseBits(num uint32) uint32 {
96-
var ans uint32 = 0
101+
func reverseBits(n uint32) (ans uint32) {
97102
for i := 0; i < 32; i++ {
98-
ans |= (num & 1) << (31 - i)
99-
num >>= 1
103+
ans |= (n & 1) << (31 - i)
104+
n >>= 1
100105
}
101-
return ans
106+
return
102107
}
103108
```
104109

105110
```rust
106111
impl Solution {
107-
pub fn reverse_bits(mut x: u32) -> u32 {
108-
let mut res = 0;
109-
for _ in 0..32 {
110-
res = (res << 1) | (x & 1);
111-
x >>= 1;
112+
pub fn reverse_bits(mut n: u32) -> u32 {
113+
let mut ans = 0;
114+
for i in 0..32 {
115+
ans |= (n & 1) << (31 - i);
116+
n >>= 1;
112117
}
113-
res
118+
ans
114119
}
115120
}
116121
```
@@ -121,13 +126,12 @@ impl Solution {
121126
* @return {number} - a positive integer
122127
*/
123128
var reverseBits = function (n) {
124-
let res = 0;
125-
for (let i = 0; i < 32 && n > 0; ++i) {
126-
res |= (n & 1) << (31 - i);
127-
n >>>= 1;
129+
let ans = 0;
130+
for (let i = 0; i < 32 && n; ++i) {
131+
ans |= (n & 1) << (31 - i);
132+
n >>= 1;
128133
}
129-
// 无符号右移
130-
return res >>> 0;
134+
return ans >>> 0;
131135
};
132136
```
133137

solution/0100-0199/0190.Reverse Bits/README_EN.md

+32-28
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,36 @@
4444

4545
## Solutions
4646

47-
### Solution 1
47+
### Solution 1: Bit Manipulation
48+
49+
We can extract each bit of `n` from the least significant bit to the most significant bit, and then place it in the corresponding position of `ans`.
50+
51+
For example, for the $i$-th bit, we can use `(n & 1) << (31 - i)` to extract the $i$-th bit of `n` and place it on the $31 - i$-th bit of `ans`, then right shift `n` by one bit.
52+
53+
The time complexity is $O(\log n)$, and the space complexity is $O(1)$.
4854

4955
<!-- tabs:start -->
5056

5157
```python
5258
class Solution:
5359
def reverseBits(self, n: int) -> int:
54-
res = 0
60+
ans = 0
5561
for i in range(32):
56-
res |= (n & 1) << (31 - i)
62+
ans |= (n & 1) << (31 - i)
5763
n >>= 1
58-
return res
64+
return ans
5965
```
6066

6167
```java
6268
public class Solution {
6369
// you need treat n as an unsigned value
6470
public int reverseBits(int n) {
65-
int res = 0;
71+
int ans = 0;
6672
for (int i = 0; i < 32 && n != 0; ++i) {
67-
res |= ((n & 1) << (31 - i));
73+
ans |= (n & 1) << (31 - i);
6874
n >>>= 1;
6975
}
70-
return res;
76+
return ans;
7177
}
7278
}
7379
```
@@ -76,36 +82,35 @@ public class Solution {
7682
class Solution {
7783
public:
7884
uint32_t reverseBits(uint32_t n) {
79-
uint32_t res = 0;
80-
for (int i = 0; i < 32; ++i) {
81-
res |= ((n & 1) << (31 - i));
85+
uint32_t ans = 0;
86+
for (int i = 0; i < 32 && n; ++i) {
87+
ans |= (n & 1) << (31 - i);
8288
n >>= 1;
8389
}
84-
return res;
90+
return ans;
8591
}
8692
};
8793
```
8894
8995
```go
90-
func reverseBits(num uint32) uint32 {
91-
var ans uint32 = 0
96+
func reverseBits(n uint32) (ans uint32) {
9297
for i := 0; i < 32; i++ {
93-
ans |= (num & 1) << (31 - i)
94-
num >>= 1
98+
ans |= (n & 1) << (31 - i)
99+
n >>= 1
95100
}
96-
return ans
101+
return
97102
}
98103
```
99104

100105
```rust
101106
impl Solution {
102-
pub fn reverse_bits(mut x: u32) -> u32 {
103-
let mut res = 0;
104-
for _ in 0..32 {
105-
res = (res << 1) | (x & 1);
106-
x >>= 1;
107+
pub fn reverse_bits(mut n: u32) -> u32 {
108+
let mut ans = 0;
109+
for i in 0..32 {
110+
ans |= (n & 1) << (31 - i);
111+
n >>= 1;
107112
}
108-
res
113+
ans
109114
}
110115
}
111116
```
@@ -116,13 +121,12 @@ impl Solution {
116121
* @return {number} - a positive integer
117122
*/
118123
var reverseBits = function (n) {
119-
let res = 0;
120-
for (let i = 0; i < 32 && n > 0; ++i) {
121-
res |= (n & 1) << (31 - i);
122-
n >>>= 1;
124+
let ans = 0;
125+
for (let i = 0; i < 32 && n; ++i) {
126+
ans |= (n & 1) << (31 - i);
127+
n >>= 1;
123128
}
124-
// 无符号右移
125-
return res >>> 0;
129+
return ans >>> 0;
126130
};
127131
```
128132

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public:
33
uint32_t reverseBits(uint32_t n) {
4-
uint32_t res = 0;
5-
for (int i = 0; i < 32; ++i) {
6-
res |= ((n & 1) << (31 - i));
4+
uint32_t ans = 0;
5+
for (int i = 0; i < 32 && n; ++i) {
6+
ans |= (n & 1) << (31 - i);
77
n >>= 1;
88
}
9-
return res;
9+
return ans;
1010
}
1111
};
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
func reverseBits(num uint32) uint32 {
2-
var ans uint32 = 0
1+
func reverseBits(n uint32) (ans uint32) {
32
for i := 0; i < 32; i++ {
4-
ans |= (num & 1) << (31 - i)
5-
num >>= 1
3+
ans |= (n & 1) << (31 - i)
4+
n >>= 1
65
}
7-
return ans
6+
return
87
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
public class Solution {
22
// you need treat n as an unsigned value
33
public int reverseBits(int n) {
4-
int res = 0;
4+
int ans = 0;
55
for (int i = 0; i < 32 && n != 0; ++i) {
6-
res |= ((n & 1) << (31 - i));
6+
ans |= (n & 1) << (31 - i);
77
n >>>= 1;
88
}
9-
return res;
9+
return ans;
1010
}
1111
}

solution/0100-0199/0190.Reverse Bits/Solution.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
* @return {number} - a positive integer
44
*/
55
var reverseBits = function (n) {
6-
let res = 0;
7-
for (let i = 0; i < 32 && n > 0; ++i) {
8-
res |= (n & 1) << (31 - i);
9-
n >>>= 1;
6+
let ans = 0;
7+
for (let i = 0; i < 32 && n; ++i) {
8+
ans |= (n & 1) << (31 - i);
9+
n >>= 1;
1010
}
11-
// 无符号右移
12-
return res >>> 0;
11+
return ans >>> 0;
1312
};
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def reverseBits(self, n: int) -> int:
3-
res = 0
3+
ans = 0
44
for i in range(32):
5-
res |= (n & 1) << (31 - i)
5+
ans |= (n & 1) << (31 - i)
66
n >>= 1
7-
return res
7+
return ans
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
impl Solution {
2-
pub fn reverse_bits(mut x: u32) -> u32 {
3-
let mut res = 0;
4-
for _ in 0..32 {
5-
res = (res << 1) | (x & 1);
6-
x >>= 1;
2+
pub fn reverse_bits(mut n: u32) -> u32 {
3+
let mut ans = 0;
4+
for i in 0..32 {
5+
ans |= (n & 1) << (31 - i);
6+
n >>= 1;
77
}
8-
res
8+
ans
99
}
1010
}

solution/0100-0199/0192.Word Frequency/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,15 @@ day 1
4545

4646
## 解法
4747

48+
### 方法一:awk
49+
50+
<!-- tabs:start -->
51+
52+
```bash
53+
# Read from the file words.txt and output the word frequency list to stdout.
54+
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}'
55+
```
56+
57+
<!-- tabs:end -->
58+
4859
<!-- end -->

solution/0100-0199/0192.Word Frequency/README_EN.md

+11
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,15 @@ day 1
4343

4444
## Solutions
4545

46+
### Solution 1: awk
47+
48+
<!-- tabs:start -->
49+
50+
```bash
51+
# Read from the file words.txt and output the word frequency list to stdout.
52+
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}'
53+
```
54+
55+
<!-- tabs:end -->
56+
4657
<!-- end -->

solution/0100-0199/0193.Valid Phone Numbers/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,15 @@
3535

3636
## 解法
3737

38+
### 方法一:awk
39+
40+
<!-- tabs:start -->
41+
42+
```bash
43+
# Read from the file file.txt and output all valid phone numbers to stdout.
44+
awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt
45+
```
46+
47+
<!-- tabs:end -->
48+
3849
<!-- end -->

solution/0100-0199/0193.Valid Phone Numbers/README_EN.md

+11
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,15 @@
3131

3232
## Solutions
3333

34+
### Solution 1: awk
35+
36+
<!-- tabs:start -->
37+
38+
```bash
39+
# Read from the file file.txt and output all valid phone numbers to stdout.
40+
awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt
41+
```
42+
43+
<!-- tabs:end -->
44+
3445
<!-- end -->

0 commit comments

Comments
 (0)