반응형

https://leetcode.com/problems/power-of-two/

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

2의 제곱 수라면 비트가 1개만 켜져있으므로, n & (n-1)의 결과는 0이 됩니다.

class Solution {
public:
    bool isPowerOfTwo(int n) {
        return n > 0 && !(n & (n - 1));
    }
};
반응형

+ Recent posts