题目:

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

代码:

  1. class Solution {
  2. public:
  3. bool isPowerOfTwo(int n) {
  4. if ( n<= ) return false;
  5. int countOne = ;
  6. for ( int i=; i<sizeof(n)* && countOne<=; ++i )
  7. {
  8. countOne += (n>>i) & ;
  9. }
  10. return countOne==;
  11. }
  12. };

tips:

首先的思路是bit-munipulation

检查int中有多少个1

学习了一个更简洁的(https://leetcode.com/discuss/45017/5-lines-o-1-space%26time-c-solution-no-hash

自己优化了一下 一行代码AC。

  1. class Solution {
  2. public:
  3. bool isPowerOfTwo(int n) {
  4. return n<= ? false : !(n & (n-));
  5. }
  6. };

【Power of Two】cpp的更多相关文章

  1. hdu 4740【模拟+深搜】.cpp

    题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...

  2. 【Search Insert Position 】cpp

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

  3. 【First Missing Positive】cpp

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  4. 【Insertion Sorted List】cpp

    题目: Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct L ...

  5. 【Merge Sorted Array】cpp

    题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...

  6. 【Path Sum II】cpp

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  7. 【Longest Common Prefix】cpp

    题目: Write a function to find the longest common prefix string amongst an array of strings. 代码: class ...

  8. 【 Regular Expression Matching 】cpp

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  9. 【Longest Palindromic Substring】cpp

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

随机推荐

  1. java研发常见问题总结 1

    1.java中所有类的父类是什么?他都有什么方法? Object类是所有类的直接或间接基类,如果一个类在声明时未继承基类,Java就默认其基类是Object,故Object被称为根类.该类位于java ...

  2. VUE在页面没加载完的时候会显示原代码的处理方法

    CSS: [v-cloak] { display: none; } HTML : <div v-cloak> {{ message }} </div> 其中 v-cloak官方 ...

  3. poj 2057 树形DP,数学期望

    题目链接:http://poj.org/problem?id=2057 题意:有一只蜗牛爬上树睡着之后从树上掉下来,发现后面的"房子"却丢在了树上面, 现在这只蜗牛要求寻找它的房子 ...

  4. Linux 初学者:移动文件

    你学习了有关目录和访问目录的权限是如何工作的.你在这些文章中学习的大多数内容都可应用于文件 -- Paul Brown 在之前的该系列的部分中, 你学习了有关目录 和 访问目录 的权限 是如何工作的. ...

  5. 0001-BUGIFX-Magento-Zend-Framework-1-PHP5.6.patch

    It is from the full Github-Gist: Bugfix for Zend Framework 1 in Magento (>= 1.7..) + PHP 5.6 http ...

  6. sublime text 3 python 控制台输出中文乱码解决方案

    自建的python运行环境如下:python3 找到python3.sublime-build文件打开,在文件中加入"env": { "PYTHONIOENCODING& ...

  7. 安裝 PHP 時出現undefined reference to `libiconv_open’ 之類的錯誤訊息

    在安裝 PHP 到系統中時要是發生「undefined reference to `libiconv_open'」之類的錯誤訊息,那表示在「./configure 」沒抓好一些環境變數值.錯誤發生點在 ...

  8. js日期转换Fri Oct 31 18:00:00 UTC+0800 2008转换为yyyy-mm-dd

    Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d ...

  9. loss 和accuracy的关系梳理

    最近打算总结一下这部分东西,先记录留个脚印.

  10. 开源项目托管github步骤

    一.在github新建项目,复制到本地更改之后命令提交. 1.进入github主页新建项目:https://github.com/ccyinghua 2.复制项目地址 3.打开git Bash 命令行 ...