杂项

记录一下一些可能会用到的比较有趣的技巧和神仙思路。
1.2的幂
通过n&(n-1)可以快速判断一个数(>=0)是不是2的幂。如果n&(n-1)==0,那么这个数就是2的幂。以LeetCode231题为例:[https://leetcode-cn.com/problems/power-of-two/]

1
2
3
4
5
6
7
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n<=0) return false;
return !(n&(n-1));
}
};

to be continue…