Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? 我的解法极其垃圾,建议不要看. public class Solution {…
Given an integer, write a function to determine if it is a power of three. Example 1: Input: 27 Output: true Example 2: Input: 0 Output: false Example 3: Input: 9 Output: true Example 4: Input: 45 Output: false Follow up:Could you do it without using…
计算X的n次幂,有多种算法 例子:计算2的62次方. method 1 :time = 1527 纳秒. 常规思路,进行61次的乘法! private static long mi(long X, long n) { long start = System.nanoTime(); long result = 1; for (long k = 0; k < n; k++) { result *= X; } System.out.println(System.nanoTime()-start); r…
位运算相关 三道题 231. Power of Two Given an integer, write a function to determine if it is a power of two. (Easy) 分析: 数字相关题有的可以考虑用位运算,例如&可以作为筛选器. 比如n & (n - 1) 可以将n的最低位1删除,所以判断n是否为2的幂,即判断(n & (n - 1) == 0) 代码: class Solution { public: bool isPowerOf…