Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def rangeBitwiseAnd(self, left: int, right: int) -> int:
- steps=0
- while left!=right:
- steps+=1
- left=left>>1
- right=right>>1
- return left<<steps
- ```
- The idea is very simple:
- 1.last bit of (odd number & even number) is 0.
- 2.when m != n, There is at least an odd number and an even number, so the last bit position result is 0.
- 3.Move m and n rigth a position.
- Keep doing step 1,2,3 until m equal to n, use a factor to record the iteration time.
- return left<<steps because left reaches to 0 early then right while doing right shift operations
- ```
Add Comment
Please, Sign In to add comment