Skip to content

Commit

Permalink
feat: add rust solution to lcci problem: No.05.07
Browse files Browse the repository at this point in the history
No.05.07.Exchange
  • Loading branch information
YangFong committed Apr 16, 2022
1 parent bcc5be2 commit 8720418
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lcci/05.07.Exchange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ class Solution {
}
```

### **Rust**

```rust
impl Solution {
pub fn exchange_bits(mut num: i32) -> i32 {
let mut res = 0;
let mut i = 0;
while num != 0 {
let a = num & 1;
num >>= 1;
let b = num & 1;
num >>= 1;
res |= a << i + 1;
res |= b << i;
i += 2;
}
res
}
}
```

### **...**

```
Expand Down
21 changes: 21 additions & 0 deletions lcci/05.07.Exchange/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ class Solution {
}
```

### **Rust**

```rust
impl Solution {
pub fn exchange_bits(mut num: i32) -> i32 {
let mut res = 0;
let mut i = 0;
while num != 0 {
let a = num & 1;
num >>= 1;
let b = num & 1;
num >>= 1;
res |= a << i + 1;
res |= b << i;
i += 2;
}
res
}
}
```

### **...**

```
Expand Down
16 changes: 16 additions & 0 deletions lcci/05.07.Exchange/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
impl Solution {
pub fn exchange_bits(mut num: i32) -> i32 {
let mut res = 0;
let mut i = 0;
while num != 0 {
let a = num & 1;
num >>= 1;
let b = num & 1;
num >>= 1;
res |= a << i + 1;
res |= b << i;
i += 2;
}
res
}
}

0 comments on commit 8720418

Please sign in to comment.