File tree 3 files changed +64
-0
lines changed
solution/0600-0699/0693.Binary Number with Alternating Bits
3 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -86,6 +86,35 @@ public:
86
86
};
87
87
```
88
88
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
+
89
118
### ** ...**
90
119
91
120
```
Original file line number Diff line number Diff line change @@ -72,6 +72,35 @@ public:
72
72
};
73
73
```
74
74
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
+
75
104
### ** ...**
76
105
77
106
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments