Skip to content

feat: add solutions to lc problems: No.190~195 #2633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 32 additions & 28 deletions solution/0100-0199/0190.Reverse Bits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,36 @@

## 解法

### 方法一
### 方法一:位运算

我们可以从低位到高位,逐个取出 `n` 的每一位,然后将其放到 `ans` 的对应位置上。

比如,对于第 $i$ 位,我们可以通过 `(n & 1) << (31 - i)` 来取出 `n` 的第 $i$ 位,并将其放到 `ans` 的第 $31 - i$ 位上,然后将 `n` 右移一位。

时间复杂度 $(\log n)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

```python
class Solution:
def reverseBits(self, n: int) -> int:
res = 0
ans = 0
for i in range(32):
res |= (n & 1) << (31 - i)
ans |= (n & 1) << (31 - i)
n >>= 1
return res
return ans
```

```java
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int res = 0;
int ans = 0;
for (int i = 0; i < 32 && n != 0; ++i) {
res |= ((n & 1) << (31 - i));
ans |= (n & 1) << (31 - i);
n >>>= 1;
}
return res;
return ans;
}
}
```
Expand All @@ -81,36 +87,35 @@ public class Solution {
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t res = 0;
for (int i = 0; i < 32; ++i) {
res |= ((n & 1) << (31 - i));
uint32_t ans = 0;
for (int i = 0; i < 32 && n; ++i) {
ans |= (n & 1) << (31 - i);
n >>= 1;
}
return res;
return ans;
}
};
```

```go
func reverseBits(num uint32) uint32 {
var ans uint32 = 0
func reverseBits(n uint32) (ans uint32) {
for i := 0; i < 32; i++ {
ans |= (num & 1) << (31 - i)
num >>= 1
ans |= (n & 1) << (31 - i)
n >>= 1
}
return ans
return
}
```

```rust
impl Solution {
pub fn reverse_bits(mut x: u32) -> u32 {
let mut res = 0;
for _ in 0..32 {
res = (res << 1) | (x & 1);
x >>= 1;
pub fn reverse_bits(mut n: u32) -> u32 {
let mut ans = 0;
for i in 0..32 {
ans |= (n & 1) << (31 - i);
n >>= 1;
}
res
ans
}
}
```
Expand All @@ -121,13 +126,12 @@ impl Solution {
* @return {number} - a positive integer
*/
var reverseBits = function (n) {
let res = 0;
for (let i = 0; i < 32 && n > 0; ++i) {
res |= (n & 1) << (31 - i);
n >>>= 1;
let ans = 0;
for (let i = 0; i < 32 && n; ++i) {
ans |= (n & 1) << (31 - i);
n >>= 1;
}
// 无符号右移
return res >>> 0;
return ans >>> 0;
};
```

Expand Down
60 changes: 32 additions & 28 deletions solution/0100-0199/0190.Reverse Bits/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,36 @@

## Solutions

### Solution 1
### Solution 1: Bit Manipulation

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`.

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.

The time complexity is $O(\log n)$, and the space complexity is $O(1)$.

<!-- tabs:start -->

```python
class Solution:
def reverseBits(self, n: int) -> int:
res = 0
ans = 0
for i in range(32):
res |= (n & 1) << (31 - i)
ans |= (n & 1) << (31 - i)
n >>= 1
return res
return ans
```

```java
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int res = 0;
int ans = 0;
for (int i = 0; i < 32 && n != 0; ++i) {
res |= ((n & 1) << (31 - i));
ans |= (n & 1) << (31 - i);
n >>>= 1;
}
return res;
return ans;
}
}
```
Expand All @@ -76,36 +82,35 @@ public class Solution {
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t res = 0;
for (int i = 0; i < 32; ++i) {
res |= ((n & 1) << (31 - i));
uint32_t ans = 0;
for (int i = 0; i < 32 && n; ++i) {
ans |= (n & 1) << (31 - i);
n >>= 1;
}
return res;
return ans;
}
};
```

```go
func reverseBits(num uint32) uint32 {
var ans uint32 = 0
func reverseBits(n uint32) (ans uint32) {
for i := 0; i < 32; i++ {
ans |= (num & 1) << (31 - i)
num >>= 1
ans |= (n & 1) << (31 - i)
n >>= 1
}
return ans
return
}
```

```rust
impl Solution {
pub fn reverse_bits(mut x: u32) -> u32 {
let mut res = 0;
for _ in 0..32 {
res = (res << 1) | (x & 1);
x >>= 1;
pub fn reverse_bits(mut n: u32) -> u32 {
let mut ans = 0;
for i in 0..32 {
ans |= (n & 1) << (31 - i);
n >>= 1;
}
res
ans
}
}
```
Expand All @@ -116,13 +121,12 @@ impl Solution {
* @return {number} - a positive integer
*/
var reverseBits = function (n) {
let res = 0;
for (let i = 0; i < 32 && n > 0; ++i) {
res |= (n & 1) << (31 - i);
n >>>= 1;
let ans = 0;
for (let i = 0; i < 32 && n; ++i) {
ans |= (n & 1) << (31 - i);
n >>= 1;
}
// 无符号右移
return res >>> 0;
return ans >>> 0;
};
```

Expand Down
8 changes: 4 additions & 4 deletions solution/0100-0199/0190.Reverse Bits/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t res = 0;
for (int i = 0; i < 32; ++i) {
res |= ((n & 1) << (31 - i));
uint32_t ans = 0;
for (int i = 0; i < 32 && n; ++i) {
ans |= (n & 1) << (31 - i);
n >>= 1;
}
return res;
return ans;
}
};
9 changes: 4 additions & 5 deletions solution/0100-0199/0190.Reverse Bits/Solution.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
func reverseBits(num uint32) uint32 {
var ans uint32 = 0
func reverseBits(n uint32) (ans uint32) {
for i := 0; i < 32; i++ {
ans |= (num & 1) << (31 - i)
num >>= 1
ans |= (n & 1) << (31 - i)
n >>= 1
}
return ans
return
}
6 changes: 3 additions & 3 deletions solution/0100-0199/0190.Reverse Bits/Solution.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int res = 0;
int ans = 0;
for (int i = 0; i < 32 && n != 0; ++i) {
res |= ((n & 1) << (31 - i));
ans |= (n & 1) << (31 - i);
n >>>= 1;
}
return res;
return ans;
}
}
11 changes: 5 additions & 6 deletions solution/0100-0199/0190.Reverse Bits/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
* @return {number} - a positive integer
*/
var reverseBits = function (n) {
let res = 0;
for (let i = 0; i < 32 && n > 0; ++i) {
res |= (n & 1) << (31 - i);
n >>>= 1;
let ans = 0;
for (let i = 0; i < 32 && n; ++i) {
ans |= (n & 1) << (31 - i);
n >>= 1;
}
// 无符号右移
return res >>> 0;
return ans >>> 0;
};
6 changes: 3 additions & 3 deletions solution/0100-0199/0190.Reverse Bits/Solution.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Solution:
def reverseBits(self, n: int) -> int:
res = 0
ans = 0
for i in range(32):
res |= (n & 1) << (31 - i)
ans |= (n & 1) << (31 - i)
n >>= 1
return res
return ans
12 changes: 6 additions & 6 deletions solution/0100-0199/0190.Reverse Bits/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
impl Solution {
pub fn reverse_bits(mut x: u32) -> u32 {
let mut res = 0;
for _ in 0..32 {
res = (res << 1) | (x & 1);
x >>= 1;
pub fn reverse_bits(mut n: u32) -> u32 {
let mut ans = 0;
for i in 0..32 {
ans |= (n & 1) << (31 - i);
n >>= 1;
}
res
ans
}
}
11 changes: 11 additions & 0 deletions solution/0100-0199/0192.Word Frequency/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,15 @@ day 1

## 解法

### 方法一:awk

<!-- tabs:start -->

```bash
# Read from the file words.txt and output the word frequency list to stdout.
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}'
```

<!-- tabs:end -->

<!-- end -->
11 changes: 11 additions & 0 deletions solution/0100-0199/0192.Word Frequency/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,15 @@ day 1

## Solutions

### Solution 1: awk

<!-- tabs:start -->

```bash
# Read from the file words.txt and output the word frequency list to stdout.
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}'
```

<!-- tabs:end -->

<!-- end -->
11 changes: 11 additions & 0 deletions solution/0100-0199/0193.Valid Phone Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,15 @@

## 解法

### 方法一:awk

<!-- tabs:start -->

```bash
# Read from the file file.txt and output all valid phone numbers to stdout.
awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt
```

<!-- tabs:end -->

<!-- end -->
11 changes: 11 additions & 0 deletions solution/0100-0199/0193.Valid Phone Numbers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@

## Solutions

### Solution 1: awk

<!-- tabs:start -->

```bash
# Read from the file file.txt and output all valid phone numbers to stdout.
awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt
```

<!-- tabs:end -->

<!-- end -->
Loading