[LeetCode]231.Power of Two

题目

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

代码

/*---------------------------------------
*   日期:2015-08-02
*   作者:SJF0115
*   题目: 231.Power of Two
*   网址:https://leetcode.com/problems/power-of-two/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
#include <vector>
#include <stack>
using namespace std;

class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n <= 0){
            return false;
        }//if
        while((n & 1) == 0 && n > 0) {
            n = n >> 1;
        }//while
        return n == 1;
    }
};

int main(){
    Solution s;
    int n = 7;
    bool result = s.isPowerOfTwo(n);
    // 输出
    cout<<result<<endl;
    return 0;
}
时间: 2024-09-30 21:59:11

[LeetCode]231.Power of Two的相关文章

[LeetCode]--326. 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? Credits: Special thanks to @dietpepsi for adding this problem and creating all test cases. 前提学习: Java Math的floor

[LeetCode]--342. 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 loops/recursion? Credits: Special thanks to @yukuairoy f

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

[LeetCode] 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 loops/recursion? 解题思路 如果一个数为4n,则它等于n个4相乘.乘以4可以化为左移2位的位运算

LeetCode:Permutation Sequence

题目链接:http://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): "123" "132&q

[LeetCode] Number of 1 Bits &amp;amp; Reverse Integer - 整数问题系列

目录:1.Number of 1 Bits  - 计算二进制1的个数 [与运算] 2.Contains Duplicate - 是否存在重复数字 [遍历]3.Reverse Integer - 翻转整数 [int边界问题]4.Excel Sheet Column Number - Excel字符串转整数 [简单]5.Power of Two & Happy Number - 计算各个位数字 [%10 /10] 一.Number of 1 Bits  题目概述:Write a function t

[LeetCode]135.Candy

[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can

《Power Designer系统分析与建模实战》——2.2 建立需求模型

2.2 建立需求模型 在Power Designer中生成需求模型的方式多种多样,主要有如下几种形式:1)新建RQM.2)从已有的RQM生成新的RQM.3)从其他模型导入生成的RQM.4)从Word文档导入生成的RQM.本节主要讲解在Power Designer中直接新建RQM的方法.2.2.1 创建RQM选择"File"→"New"菜单项,从弹出的新建模型对话框(见图2-3)中选择"Model types"→"Requirements

[LeetCode] Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和

Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. Example 1: Input: s1 = "sea", s2 = "eat" Output: 231 Explanation: Deleting "s" from "sea" adds the ASCII value of