Участник:Ковальков Антон М05-903б/Bitwise AND of Numbers Range

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/bitwise-and-of-numbers-range/

Python3

class Solution:
    def rangeBitwiseAnd(self, m: int, n: int) -> int:
        k = n - m
        i = 0
        r = n
        for _ in range (k):
            if r == 0:
                return 0
            if r <= m:
                return r
            while (r >> i) & 1 != 1:
                i += 1
            r = (r >> (i+1)) << (i+1)
            i += 1
        return r