[leetcode][C] 231번 - Power of Two

numango·2024년 9월 6일

leetcode

목록 보기
3/12

문제
Given an integer n, return true if it is a power of two. Otherwise, return false.

An integer n is a power of two, if there exists an integer x such that n == 2x.

Example 1:

Input: n = 1
Output: true
Explanation: 20 = 1

Example 2:

Input: n = 3
Output: false

Constraints:

-231 <= n <= 231 - 1


n이 2의 거듭제곱일때 true를 반환, 그외에는 false 반환하면 된다.

bool isPowerOfTwo(int n) {
    if(n <= 0){return false;}
    while(n>1){
        if(n%2 > 0){return false;}
        else{
            n = n/2;
        }
    }
    return true;

0개의 댓글