LeetCode(52) Power of Two
題目描述
Given an integer, write a function to determine if it is a power of two.
本題要求判斷一個數是否是2的指數。
題目解答
本題很簡單可以通過位運算進行判斷。首先如果一個數為負,則肯定不為2的指數,直接返回false;對於正數可以判斷該數字二進位制表達時1的個數,如果1的個數為只有一個則為2的指數,否則不為2的指數。
當然本題也可以通過不停的對待判斷數字除以2,若最終為1,則說明該數字是2的指數。
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n <= 0) return false;
int count = 0;
const int one = 1;
while(n)
{
if(n & one == one) count++;
n = n >> 1;
}
if(count == 1) return true;
else return false;
}
};
相關推薦
LeetCode(52) Power of Two
題目描述 Given an integer, write a function to determine if it is a power of two. 本題要求判斷一個數是否是2的指數。 題目解答 本題很簡單可以通過位運算進行判斷。首先如果一個數為負,則肯定不為2的指
LeetCode 231:Power of Two
data con int style 是否 false 循環 mod 二進制 ?? Given an integer, write a function to determine if it is a power of two. //題目要求:求一個數是否是2的冪次方
leetcode-231-Power of Two
dsm top margin n) line != 都是 ont bottom Power of Two Given an integer, write a funct
[LeetCode]231. Power of Two判斷是不是2的冪
post 判斷 一位 clas 左移 power col style lee /* 用位操作,乘2相當於左移1位,所以2的冪只有最高位是1 所以問題就是判斷你是不是只有最高位是1,怎判斷呢 這些數-1後形成的數,除了最高位,
leetcode 231. Power of Two
code urn n) == nbsp mine elf integer brush Given an integer, write a function to determine if it is a power of two. class Solution(objec
[leetcode]231.Power of Two
amp 其余 bool 其它 說明 write solution clas 題目 題目 Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1
LeetCode-231 power-of-two 2的冪
題目連結 https://leetcode-cn.com/problems/power-of-two/ 題意 中文題,題面意思,給一個整數判定是否為2的冪。 題解 有趣的一個題,就是太水了。直接暴力也能做其實,不過時間沒
leetcode 231 power of two 191 位1的個數 -------python
給定一個整數,編寫一個函式來判斷它是否是 2 的冪次方。 示例 1: 輸入: 1 輸出: true 解釋: 20 = 1 示例 2: 輸入: 16 輸出: true 解釋: 24 = 16 示例 3: 輸入: 218 輸出: false 思路:(2的n次冪) 與 (2的n 次
#Leetcode# 231. Power of Two
ftw urn n) integer cti put term cpp brush https://leetcode.com/problems/power-of-two/ Given an integer, write a function to determine i
LeetCode - 231. Power of Two
Given an integer, write a function to determine if it is a power of two. Example 1: Input: 16 Output: true Example 2: Input: 5 Output: false判斷一
Leetcode 231 Power of Two
Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: true Explanation: 20 = 1 Example 2
LeetCode 231. Power of Two (演算法,計算二進位制數中1的位數)
Given an integer, write a function to determine if it is a power of two. 輸入一個數,判斷其是否為2的冪。 思路:可以按照326題的思路,用換底公式計算。也可以根據2進位制的特點,2的冪一定是最高位是1
LeetCode 231. Power of Two --Easy
Given an integer, write a function to determine if it is a power of two. 方法一、 迭代判斷n/2是不是2的乘冪 <pr
LeetCode 第 231 題 (Power of Two)
i++ ret write data mark hid article views pop LeetCode 第 231 題 (Power of Two) Given an integer, write a function to determi
【LeetCode題解】231_2的冪(Power-of-Two)
文章目錄 描述 解法 1:判斷整數 $x$ 的二進位制表示中是否只有一位為1 實現方式 1:除以 2 Java 實現(非遞迴) Python 實現(非遞迴) Java 實現(遞迴) Python
leetcode Power of Two
Power of Two 題目:https://leetcode.com/problems/power-of-two/ Power of Two 判斷一個數是否是2的整數次冪 public static void main(String[] args) { int n=127
LeetCode演算法題-Power Of Two(Java實現)
這是悅樂書的第194次更新,第200篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第56題(順位題號是231)。給定一個整數,寫一個函式來確定它是否是2的冪。例如: 輸入:1 輸出:true 說明:2^0 = 1 輸入:16 輸出:true 說明:2^4 = 16
LeetCode-Power of Two
Description: Given an integer, write a function to determine if it is a power of two. Example 1: Inpu
LeetCode刷題EASY篇Power of Two
題目 Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: true Explanation: 20 = 1 Exampl
【leetcode】231.Power of Two
題目描述 求n是否是2的n次方。 思路 使用位運算。 假如n是2的n次方,則n&n-1一定等於0. 程式碼 class Solution: def isPowerOfTwo(self, n): """ :type n: int