leetcode-5.25[190. 顛倒二進制位](python實現)

題目1

在這裏插入圖片描述

題解1

class Solution:
    def reverseBits(self, n: int) -> int:
        ans = 0
        i = 32
        # 循環移動32次
        while i > 0:
            # 向左移動
            ans <<= 1
            # 移動後將末尾加n的尾部值
            ans += n&1
            # 跟新n值,以取到最後的值
            n >>= 1
            i -= 1
        return ans

附上題目鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章