leetcode笔记:Power of two

一. 题目描述

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

二.题目分析

该题要求简单,给定一个整数,判断其是不是2的整数次幂,这道题的解题关键是找到一个规律:如果一个数字是2的整数次幂,若将该数写为二进制数,这个二进制数中有且仅有一位为1,其余均为0。根据这一性质,不难给出以下给出两种解决方法。

三.示例代码

// 将n不停右移,比较二进制数中1出现的个数,count = 1时判定为ture
bool isPowerOfTwo(int n) {
    int count = 0;
    while (n > 0)
    {
        count+=(n&0x01);
        n>>=1;
    }
    if(sum==1)
        return true;
    else
        return false;
}

以下方法同样利用了一个2的整数次幂的二进制写法中有且仅有一位为1的性质。假设该数为n,根据这一性质,则(n - 1)必然是将n的最高位10,然后其余二进制位均置1,当且仅当这种情况下,有(n&(n-1)) == 0,一个例子:

n = 8 -> 1000

n - 1 = 7 -> 0111

则有:1000 & 0111 = 0000

class Solution {
public:
    bool isPowerOfTwo(int n) {
        return (n>0) && (!(n&(n-1)));
    }
};  

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-26 04:41:50

leetcode笔记:Power of two的相关文章

leetcode笔记

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a

[leetcode笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&

leetcode笔记:Pascal's Triangle

一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 二. 题目分析 关于帕斯卡三角形的定义,可參考:http://baike.baidu.com/link?url=qk_-urYQnO4v6v3P4BuMtCa0tMNUqJUk

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

[leetcode笔记] Longest Consecutive Sequence

随机挑选一题试试手气.   问题描述: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your al

[LeetCode][JavaScript]Power of Three

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? https://leetcode.com/problems/power-of-three/ 要求判断一个数是不是3的幂次方. 最直观的做法是不停除以3,看余数是否为零. 题目要求不能用递归和循环,

【Leetcode】Power of Four

题目链接:https://leetcode.com/problems/power-of-four/ 题目: 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 lo

[LeetCode][JavaScript]Power of Two

Power of Two Given an integer, write a function to determine if it is a power of two. https://leetcode.com/problems/power-of-two/ 二分. 2的整数次幂,要么开方后是整数(这个数也是2的整数次幂),要么除以2之后再开方后是整数.继续递归判断开方后的结果直到碰到1或者2. 比如1024(2^10),是由两个32(2^5)相乘:2048(2^11),是由两个32(2^5)相

[LeetCode] Reordered Power of 2 重新排序为2的倍数

Starting with a positive integer?N, we reorder the digits in any order (including the original order) such that the leading digit is not zero. Return?true?if and only if we can do this in a way such that the resulting number is a power of 2. Example