Home 231. Power of Two
Post
Cancel

231. Power of Two

정리 코드

Recursion Function(재귀함수) : 자기 자신을 재참조, 종료 조건으로 종료 시점을 제어한다.


leetcode : 231. Power of Two 문제


  • 조건
    • -231 <= n <= 231 - 1


1
2
3
4
5
6
7
8
9
10
class Solution {
  public boolean isPowerOfTwo(int n) {
    if (n <= 0) return false;
    if (n == 1) return true;
    if (n % 2 != 0) {
      return false;
    }
    return isPowerOfTwo(n / 2);
  }
}
This post is licensed under CC BY 4.0 by the author.