LeetCode_231. Power of Two

231. Power of Two

Easy

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:

Input: 16
Output: true
Explanation: 24 = 16

Example 3:

Input: 218
Output: false
package leetcode.easy;

public class PowerOfTwo {
	public boolean isPowerOfTwo(int n) {
		if (n <= 0) {
			return false;
		}
		if (n == 1) {
			return true;
		}
		if ((n & (n - 1)) == 0) {
			return true;
		}
		return false;
	}

	@org.junit.Test
	public void test() {
		System.out.println(isPowerOfTwo(1));
		System.out.println(isPowerOfTwo(16));
		System.out.println(isPowerOfTwo(218));
	}
}

原文地址:https://www.cnblogs.com/denggelin/p/11737225.html

时间: 2024-11-07 09:35:57

LeetCode_231. Power of Two的相关文章

leetcode_231 Power Of Two(Bit Manipulation)

Given an integer, write a function to determine if it is a power of two. public class Solution { public boolean isPowerOfTwo(int n) { return ((n&(n-1))==0&&n>0); } } 强大的位运算!!!

leetcode_231题——Power of Two (位运算)

Power of Two Total Accepted: 11027 Total Submissions: 36750My Submissions Question Solution 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 a

[LeetCode] 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? 判断一个数是否是3的幂,则这个数a的所有的约数都是3的幂,如果一个数b小于这个数a且是3的幂,则这个数b一定是a的约数.所以找出3的最大的幂,然后用这个数对n取余即可. class Solution { public: boo

poj 3134 Power Calculus(迭代加深dfs+强剪枝)

Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications: x2 = x × x, x3 = x2 × x, x4 = x3 × x, …, x31 = x30 × x. The operation of squaring can be appreciably shorten the sequence of multiplications.

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

Power BI教程_Power BI数据分析快速上手及案例实战

Power BI数据分析快速上手及案例实战 课程学习地址:http://www.xuetuwuyou.com/course/194 课程出自学途无忧网:http://www.xuetuwuyou.com 课程简介 本课程在<Power BI 数据分析快速上手>基础上结合大量的实例,深入讲解PowerBI中看似难懂的各种概念.操作, 并结合行业中的典型案例贯穿了从初级的数据透视表工具.数据透视表选项.数据透视表的刷新.数据透视表中的排序,到中级的动 态数据透视表的创建.数据透视表函数 GETPI

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] Power of Two

Given an integer, write a function to determine if it is a power of two. 判断一个整数是否是2的幂.如果一个数是2的幂,则这个数的二进制表示中,最高位是1,其他位都是0.这个数-1的二进制位全是1.所以我们可以利用这个规律对两个数取与进行判断. class Solution { public: bool isPowerOfTwo(int n) { if (n <= 0) return false; return !(n &

POWER(x,y)

POWER(x,y) 用于返回 x 的 y 次方的结果 mysql> SELECT POWER(2,4), POWER(2,-4); +------------+-------------+ | POWER(2,4) | POWER(2,-4) | +------------+-------------+ | 16 | 0.0625 | +------------+-------------+