forked from aylei/leetcode-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp0693_binary_number_with_alternating_bits.rs
65 lines (59 loc) · 1.41 KB
/
p0693_binary_number_with_alternating_bits.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* [693] Binary Number with Alternating Bits
*
* Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
*
* Example 1:
*
* Input: n = 5
* Output: true
* Explanation: The binary representation of 5 is: 101
*
* Example 2:
*
* Input: n = 7
* Output: false
* Explanation: The binary representation of 7 is: 111.
* Example 3:
*
* Input: n = 11
* Output: false
* Explanation: The binary representation of 11 is: 1011.
* Example 4:
*
* Input: n = 10
* Output: true
* Explanation: The binary representation of 10 is: 1010.
* Example 5:
*
* Input: n = 3
* Output: false
*
*
* Constraints:
*
* 1 <= n <= 2^31 - 1
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/binary-number-with-alternating-bits/
// discuss: https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn has_alternating_bits(n: i32) -> bool {
let n = n ^ n >> 1;
(n & (n+1)) == 0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_693() {
assert!(Solution::has_alternating_bits(5));
assert!(!Solution::has_alternating_bits(7));
assert!(!Solution::has_alternating_bits(11));
assert!(Solution::has_alternating_bits(10));
}
}