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

强大的位运算!!!

时间: 2024-10-19 17:48:14

leetcode_231 Power Of Two(Bit Manipulation)的相关文章

leetcode_326 Power Of Three(Binary Manipulation)

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? public class Solution { public boolean isPowerOfThree(int n) { return (Math.log10(n)/Math.log10(3))%1==0; } }

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 le

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

Jan 12 - Power of Two; Integer; Bit Manipulation;

Two's complement of integer: https://zh.wikipedia.org/wiki/%E4%BA%8C%E8%A3%9C%E6%95%B8 Bit Manipulation: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html 代码: public class Solution { public boolean isPowerOfTwo(int n) { int count = 0

Lintcode: O(1) Check Power of 2

Using O(1) time to check whether an integer n is a power of 2. Example For n=4, return true For n=5, return false Challenge O(1) time Tags Expand 这道题考察bit manipulation. 1的个数只能有1个才是power of 2. 主要是要注意Integer.MIN_VALUE,这个只有一个1,但是是false 1 class Solution

LeetCode 342. Power of Four (4的次方)

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 loops/recursion? 题目标签:Bit Manipulation 这道题目让我们判断一个数字是不是4的

C#.NET开源项目、机器学习、Power BI (转载)

.NET技术, 开源项目, 数据挖掘, 机器学习, 微软Power BI, 足球赛事分析, Matlab与C#编程 博客园 管理 本站首页 头条推荐 Power BI .NET开源 机器学习 博客美化 X组件 Matlab 随笔 - 189  文章 - 15  评论 - 4316 [翻译]Awesome R资源大全中文版来了,全球最火的R工具包一网打尽,超过300+工具,还在等什么? 阅读目录 0.前言 1.集成开发环境 2.语法 3.数据操作 4.图形显示 5.HTML部件 6.复用组件研究

[Lintcode]142. O(1) Check Power of 2

142. O(1) Check Power of 2 本题难度: Easy Topic: Math&Bit Manipulation Description Using O(1) time to check whether an integer n is a power of 2. Example Example 1: Input: 4 Output: true Example 2: Input: 5 Output: false Challenge O(1) time 别人的代码 参考野球拳刷刷

[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