191.number-of-1-bits

Spread the love

190 会做了 就是一秒钟的事情

思路:判断器:n&1 判断这一位是不是1

然后右移一位

答案似乎有更简洁的写法:

n=n &(n-1 )可以把n里的1进行降维打击,原因是n的每一位,不是0就是1

如果尾数是1,减去1会消去0

如果尾数是0,就势必要去问前面借一个1,就又干掉了一个1

This entry was posted in leetcode. Bookmark the permalink.

Leave a Reply

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