Skip to content

Commit 3ed0ed8

Browse files
committed
feat: add rust solution to lc problem: No.0693
No.0693.Binary Number with Alternating Bits
1 parent dc631e9 commit 3ed0ed8

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

solution/0600-0699/0693.Binary Number with Alternating Bits/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,35 @@ public:
8686
};
8787
```
8888
89+
### **Rust**
90+
91+
```rust
92+
impl Solution {
93+
pub fn has_alternating_bits(mut n: i32) -> bool {
94+
let u = n & 3;
95+
if u != 1 && u != 2 {
96+
return false;
97+
}
98+
while n != 0 {
99+
if (n & 3) != u {
100+
return false
101+
}
102+
n >>= 2;
103+
}
104+
true
105+
}
106+
}
107+
```
108+
109+
```rust
110+
impl Solution {
111+
pub fn has_alternating_bits(n: i32) -> bool {
112+
let t = n ^ (n >> 1);
113+
(t & (t + 1)) == 0
114+
}
115+
}
116+
```
117+
89118
### **...**
90119

91120
```

solution/0600-0699/0693.Binary Number with Alternating Bits/README_EN.md

+29
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,35 @@ public:
7272
};
7373
```
7474
75+
### **Rust**
76+
77+
```rust
78+
impl Solution {
79+
pub fn has_alternating_bits(mut n: i32) -> bool {
80+
let u = n & 3;
81+
if u != 1 && u != 2 {
82+
return false;
83+
}
84+
while n != 0 {
85+
if (n & 3) != u {
86+
return false
87+
}
88+
n >>= 2;
89+
}
90+
true
91+
}
92+
}
93+
```
94+
95+
```rust
96+
impl Solution {
97+
pub fn has_alternating_bits(n: i32) -> bool {
98+
let t = n ^ (n >> 1);
99+
(t & (t + 1)) == 0
100+
}
101+
}
102+
```
103+
75104
### **...**
76105

77106
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
impl Solution {
2+
pub fn has_alternating_bits(n: i32) -> bool {
3+
let t = n ^ (n >> 1);
4+
(t & (t + 1)) == 0
5+
}
6+
}

0 commit comments

Comments
 (0)