190.reverse-bits

Spread the love

https://www.geeksforgeeks.org/python-bitwise-operators/

因为是unsigned bit,和普通int不一样

思路,看答案后:

双指针,一个指针读n的末位和1做&逻辑运算(if both 1 return 1 else 0),因为1只有1位,所以默认和末尾pk;比较完后得到的结果,加到新队伍(某变量,初始值0)里,新队伍要保证加进来的结果在尾部,因此在做加法前,用位移把已有的部分往前移动一格,在bit运算里,移动向左移动一格用<<表示。完毕后,把n指针指向倒数第二格,准备进入下一轮循环。指向倒数第二格也要用到>>的方法

This entry was posted in leetcode. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *