[leedcode 231] Power of Two

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

public class Solution {
    //注意0和负数都返回false!!!

    /*public boolean isPowerOfTwo(int n) {
        if(n<=0) return false;//此方法关键是使用Integer.toBinaryString(n),将整数转化为二进制的字符串
       String m=Integer.toBinaryString(n);
       for(int i=1;i<m.length();i++){
           if(m.charAt(i)==‘1‘) return false;
       }
       return true;
    }*/

    /* public boolean isPowerOfTwo(int n) {
       if(n<=0) return false;
       while(n>0){
           if(n!=1&&n%2==1) return false;
           n=n>>1;
       }
       return true;
    }*/
    public boolean isPowerOfTwo(int n) {
        if(n<=0) return false;
       return (n&(n-1))==0;
    }
}
时间: 2024-08-29 10:42:29

[leedcode 231] Power of Two的相关文章

231. Power of Two 342. Power of Four -- 判断是否为2、4的整数次幂

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) { return n > 0 ? (n & (n-1)) == 0 : false; } }; 342. Power of Four Given an integer (signed 32 bits), write

Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

1. 232 Implement Queue using Stacks 1.1 问题描述 使用栈模拟实现队列.模拟实现如下操作: push(x). 将元素x放入队尾. pop(). 移除队首元素. peek(). 获取队首元素. empty(). 判断队列是否为空. 注意:只能使用栈的标准操作,push,pop,size和empty函数. 1.2 方法与思路 本题和用队列实现栈思路一样,设两个辅助栈stk1和stk2. push(x): 将x入栈stk1. pop(): 依次将stk1中的元素p

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; } };

231. Power of Two

题目: Given an integer, write a function to determine if it is a power of two. 答案: 2的幂有两个特点: 1)大于0 2)二进制形式都是首位为1,其余位为0,因此n&(n-1)等于0 1 class Solution { 2 public: 3 bool isPowerOfTwo(int n) { 4 if( n>0&&(!(n&(n-1))) ) 5 return true; 6 else

【LeetCode】231 - Power of Two

Given an integer, write a function to determine if it is a power of two. Solution:一个整数如果是2的整数次方,那么它的二进制表示中有且只有一位是1,而其他所有位都是0.把这个整数与这个整数减去1之后进行与运算,那么这个整数当中唯一的1会变为0,这个整数也变为0 1 class Solution { 2 public: 3 bool isPowerOfTwo(int n) { 4 if(n<=0)return fal

python leetcode 日记--231. Power of Two

题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): def isPowerOfTwo(self, n): # """ :type n: int :rtype: bool """ 方法:分析2的幂次方的特点,发现2的任意次方的数,化成二进制时只有首位为1其余位为0,因此我的解决方法如下: class

Java [Leetcode 231]Power of Two

题目描述: Given an integer, write a function to determine if it is a power of two. 解题思路: 判断方法主要依据2的N次幂的特点:仅有首位为1,其余各位都为0. 代码如下: public class Solution { public boolean isPowerOfTwo(int n) { return (n > 0) && ( n & (n - 1)) == 0; } }

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

Java for LeetCode 231 Power of Two

public boolean isPowerOfTwo(int n) { if(n<1) return false; while(n!=1){ if(n%2!=0) return false; n>>=1; } return true; }