Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-in Event, click to viewactivity details。
The 75th day of daily brushing 2021.03.28
693. Alternate Bit Binary Numbers
- leetcode original title link:leetcode-cn.com/problems/bi…
- Difficulty: easy
- Method: Bitwise Operations
Topic description
- Given a positive integer, check that its binary representation always alternates 0s and 1s: in other words, the two adjacent digits in the binary representation are never the same.
Example
- Example 1
输入: n = 5
输出: true
解释: 5 的二进制表示是:101
复制代码
- Example 2
输入: n = 7
输出: false
解释: 7 的二进制表示是:111.
复制代码
Problem solving ideas
similar topics
regular simulation
- After converting the current decimal number into a binary number, separate the last digit and the previous digit for comparison. If the two bits are always the same, it is the matching alternate bit binary number, and return
true
; otherwise, return directlyfalse
- The original idea: Modulo operation obtains the last digit, and no two bits are compared as a group. If there is a group that does not meet the requirements, then return
false
; otherwise, if no incompatibility is found, returntrue
- Then you only need to use the XOR operator to determine whether it matches
- XOR, 1
- same XOR, 0
post-optimization
- Each time you get the last digit value (
0或者1
), you don't need to use the modulo operation, you can use&
the bitwise AND operation - Example:
111 & 1
= 1110 & 1
= 0
- Summary: Bitwise AND is used to determine whether the last bit is
0还是1
yes - In terms of operation speed, bit operation is better than modulo operation.
Another bitwise method
n
After the binary representation of the input is shifted to the right by one bit, the resulting number is XORed withn
the bitwise ORa
. If and only if the inputn
is an alternate-bit binary number,a
the binary representation is all1
(excluding leading0
). Here is a simple proof:a
when a bit of1
is , if and only ifn
the corresponding bit of is different from its previous bit.a
When each bit of1
is , if and onlyn
if all adjacent bits of are different, isn
an alternating-bit binary number.- Bitwise AND the and if and only if the
a
binary representation of is all , the result is . Here is a simple proof: if and only if the binary representation of is all , you can carry out, and the original highest position is , and the result of the bitwise AND is . Otherwise, no carry will be generated, the two most significant bits are both , and the phase and result are not . Combining the above two steps, it can be judged whether the input is an alternate bit binary number.a + 1
a
1
0
a
1
a + 1
0
0
1
0
AC
code
var hasAlternatingBits = function(n) {
let contra = n % 2;
n = n >> 1;
while(n) {
let wei = n % 2;
if(!((wei) ^ contra)){
return false;
}
// console.log('contra:', contra)
contra = wei;
n = n >> 1;
}
return true;
};
复制代码
New bit operation solution
var hasAlternatingBits = function(n) {
const a = n ^ (n >> 1);
return (a & (a + 1)) === 0;
};
复制代码
Summarize
- Shift operators are inherently faster
n / 2
than- shift operator =
parseInt(n / 2)
- shift operator =