LeetCode 342. Power of Four (Java版; Easy)

绝地灬酷狼 2023-07-04 06:09 80阅读 0赞

welcome to my blog

LeetCode 342. Power of Four (Java版; Easy)

题目描述

  1. Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
  2. Example 1:
  3. Input: 16
  4. Output: true
  5. Example 2:
  6. Input: 5
  7. Output: false
  8. Follow up: Could you solve it without loops/recursion?

第一次做; 核心: 如果x是4的幂, 那么x满足(Math.log(num)/Math.log(2) % 2 ==0)

  1. class Solution {
  2. public boolean isPowerOfFour(int num) {
  3. return num>0 && (Math.log(num)/Math.log(2) % 2 ==0);
  4. }
  5. }
  6. // x是否是2的幂, x>0 && x&(x-1)==0;

发表评论

表情:
评论列表 (有 0 条评论,80人围观)

还没有评论,来说两句吧...

相关阅读