leetcode 231: Power of Two

原创 2015年07月07日 05:01:24

Power of Two

Total Accepted: 3596 Total Submissions: 11779

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

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

[思路]

如果是power of two, 则2进制表达中,有且仅有一个1.  可以通过移位来数1的个数, 这里用了一个巧妙的办法, 即判断   N & (N-1) 是否为0.

[CODE]

public class Solution {
    public boolean isPowerOfTwo(int n) {
       return n > 0 && ((n & (n - 1)) == 0 );
    }
}


相关文章推荐

leetcode 231. Power of Two 判断是否为2的幂 Java

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

[LeetCode] Power of Three | Power of Two

Given an integer, write a function to determine if it is a power of three.Follow up: Could you do i...

LeetCode 231: 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

 题目链接:https://leetcode.com/problems/power-of-two/ 大意是给一个整数,判断它是不是2的N(N>=0,整数)次方。 一开始我的想法是将它转换成2...

[LeetCode-231] Power of Two(判断一个数是不是2的若干次幂)

Given an integ这里写代码片er, write a function to determine if it is a power of two.【分析】如果一个数是2的若干次幂,那么二进制...

231. Power of Two [easy] (Python)

题目链接https://leetcode.com/problems/power-of-two/题目原文 Given an integer, write a function to determin...

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

Given an integer, write a function to determine if it is a power of two. 判断一个整数是不是2的次方: class Solu...

LeetCode 231. Power of Two 解题报告

LeetCode 231. Power of Two 解题报告

[231] Power of Two

求一个整数是不是2的n次方
  • camlot_
  • camlot_
  • 2016年06月25日 19:50
  • 162

Leetcode 231. Power of Two & 326. Power of Three & 342. Power of Four

231. Power of Two   Total Accepted: 69528 Total Submissions: 190898 Difficulty: Easy Given an ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 231: Power of Two
举报原因:
原因补充:

(最多只允许输入30个字)