[LeetCode][JavaScript]Power of Two

Power of Two

Given an integer, write a function to determine if it is a power of two.

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



二分。

2的整数次幂,要么开方后是整数(这个数也是2的整数次幂),要么除以2之后再开方后是整数。继续递归判断开方后的结果直到碰到1或者2。

比如1024(2^10),是由两个32(2^5)相乘;2048(2^11),是由两个32(2^5)相乘,再乘以2。

 1 /**
 2  * @param {number} n
 3  * @return {boolean}
 4  */
 5 var isPowerOfTwo = function(n) {
 6     if(n < 1){
 7         return false;
 8     }else if(n === 2 || n === 1){
 9         return true;
10     }else{
11         var sqrt = Math.sqrt(n);
12         if(sqrt === parseInt(sqrt)){
13             return isPowerOfTwo(sqrt);
14         }else{
15             n = n / 2;
16             sqrt = Math.sqrt(n);
17             if(sqrt === parseInt(sqrt)){
18                 return isPowerOfTwo(sqrt);
19             }else{
20                 return false;
21             }
22         }
23     }
24 };
时间: 2024-10-15 22:26:11

[LeetCode][JavaScript]Power of Two的相关文章

[LeetCode][JavaScript]Power of Three

Power of Three Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it without using any loop / recursion? https://leetcode.com/problems/power-of-three/ 要求判断一个数是不是3的幂次方. 最直观的做法是不停除以3,看余数是否为零. 题目要求不能用递归和循环,

[LeetCode][JavaScript]Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] https://leetcode.com/problems/pascals-triangle/ 杨辉三角. 每行第一个和最后一个是1,其余是pre[i - 1] +

LeetCode 231 Power of Two(2的幂)

翻译 给定一个整型数,写一个函数来决定它是否是2的幂. 原文 Given an integer, write a function to determine if it is a power of two. 分析 详情请看这篇文章:LeetCode 326 Power of Three(3的幂)(递归.Log函数) 看题号,326是本题的加强版,326是要求不能用循环或递归的--大家可以去看看上面那篇文章. 本题就直接贴我的代码了-- 代码 class Solution { public: bo

[LeetCode][JavaScript]Insert Interval

https://leetcode.com/problems/insert-interval/ Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start t

【Leetcode】Power of Four

题目链接:https://leetcode.com/problems/power-of-four/ 题目: 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 lo

[LeetCode] Reordered Power of 2 重新排序为2的倍数

Starting with a positive integer?N, we reorder the digits in any order (including the original order) such that the leading digit is not zero. Return?true?if and only if we can do this in a way such that the resulting number is a power of 2. Example

leetcode 231. Power of Two

Given an integer, write a function to determine if it is a power of two. class Solution { public: bool isPowerOfTwo(int n) { int num = 0; while (n > 0) { if (n &1) num++; n/=2; } if (num == 1) return true; return false; } };

[LeetCode][JavaScript]Maximum Subarray

Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. https://leetcod

[LeetCode][JavaScript]Coin Change

Coin Change You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination o