Skip to content

Commit 8720418

Browse files
committed
feat: add rust solution to lcci problem: No.05.07
No.05.07.Exchange
1 parent bcc5be2 commit 8720418

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

lcci/05.07.Exchange/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,27 @@ class Solution {
5555
}
5656
```
5757

58+
### **Rust**
59+
60+
```rust
61+
impl Solution {
62+
pub fn exchange_bits(mut num: i32) -> i32 {
63+
let mut res = 0;
64+
let mut i = 0;
65+
while num != 0 {
66+
let a = num & 1;
67+
num >>= 1;
68+
let b = num & 1;
69+
num >>= 1;
70+
res |= a << i + 1;
71+
res |= b << i;
72+
i += 2;
73+
}
74+
res
75+
}
76+
}
77+
```
78+
5879
### **...**
5980

6081
```

lcci/05.07.Exchange/README_EN.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,27 @@ class Solution {
5555
}
5656
```
5757

58+
### **Rust**
59+
60+
```rust
61+
impl Solution {
62+
pub fn exchange_bits(mut num: i32) -> i32 {
63+
let mut res = 0;
64+
let mut i = 0;
65+
while num != 0 {
66+
let a = num & 1;
67+
num >>= 1;
68+
let b = num & 1;
69+
num >>= 1;
70+
res |= a << i + 1;
71+
res |= b << i;
72+
i += 2;
73+
}
74+
res
75+
}
76+
}
77+
```
78+
5879
### **...**
5980

6081
```

lcci/05.07.Exchange/Solution.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn exchange_bits(mut num: i32) -> i32 {
3+
let mut res = 0;
4+
let mut i = 0;
5+
while num != 0 {
6+
let a = num & 1;
7+
num >>= 1;
8+
let b = num & 1;
9+
num >>= 1;
10+
res |= a << i + 1;
11+
res |= b << i;
12+
i += 2;
13+
}
14+
res
15+
}
16+
}

0 commit comments

Comments
 (0)