leetCode(32):Power of Two
Given an integer, write a function to determine if it is a power of two.
2的幂的二进制表示中,必定仅仅有一个“1”,且不可能为负数。
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n<0)
{//若为负数则直接返回
return false;
}
int num=0;
while(n)
{//统计1的个数
n=n&(n-1);
num++;
}
if(num==1)
return true;
return false;
}
};
leetCode(32):Power of Two的更多相关文章
- [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:Giv ...
- leetcode 326. Power of Three(不用循环或递归)
leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...
- [LeetCode] 231. Power of Two 2的次方数
Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- 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:Giv ...
- LeetCode 342. Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- 【LeetCode】Power of Two
问题描写叙述 Given an integer, write a function to determine if it is a power of two. 意:推断一个数是否是2的n次幂 算法思想 ...
- leetcode:Power of Two
Given an integer, write a function to determine if it is a power of two. 分析:这道题让我们判断一个数是否为2的次方数(而且要求 ...
- Python [Leetcode 342]Power of Four
题目描述: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Examp ...
随机推荐
- Shell编程中Shift的用法【转】
本文转载自:http://www.cnblogs.com/image-eye/archive/2011/08/20/2147153.html Shell编程中Shift的用法 位置参数可以用shift ...
- STEM教育是什么?
STEM教育是什么? STEM 是Science科学.Technology技术.Engineering工程.Math数学,这4个词的开头字母的组合.所以STEM教育就是结合科学.技术.工程.数学的跨领 ...
- BPM中字段查重,C#Ajac调用示例
BPM中字段查重记录: 这也算是一个C#调用Ajax的示例吧,如果是异步加载的话async: false去掉就可以了. 需求:比如现在要录入一些信息,但是,有一个字段不能重复,BPM表都是自己生成的, ...
- FileCopy文件复制
package cn.com.filecopy; import java.io.FileInputStream; import java.io.FileNotFoundException; impor ...
- lua lfs库
lfs.attributes(filepath [, aname]) 获取路径指定属性 lfs.chdir(path) 改变当前工作目录,成功返回true,失败返回nil加上错误信息 l ...
- vue2.0.js
数据的渲染.数据同步 组件化.模块化 路由 ajax 数据流 Vue.js学习资源 中文官网:http://cn.vuejs.org/ 源码:http ...
- 移动端布局 rem,和px
1.rem布局,根据屏幕来计算rem,也就是意义上的适应屏幕,但是一些字体大小转换和计算有些复杂. // rem 布局重定义 (function(){ $('html').css('font-size ...
- Nginx代码风格图示
Nginx代码风格图示 (100%) 一.基本原则 K&R编码风格(偏BSD子类). 每行不能超过80列. 不用TAB对齐,用空格. 默认对齐单元是4个空格. 除宏定义外,字母均为小写,单词间 ...
- Java中的自动转换
特点: 1. 系统自动完成的,不需要程序员手动修改代码 2.将 取值范围小的类型 自动提升为 取值范围大的类型 注意: 整数类型直接写会默认为int 小数类型直接写默认为double 类型的范围大小 ...
- SQL增删改
USE sqlxx CREATE TABLE ygb( sid INT, sname VARCHAR(20), sgender VARCHAR(2), sbirthday DATE, semail V ...