【Power of Two】cpp
题目:
Given an integer, write a function to determine if it is a power of two.
代码:
class Solution {
public:
bool isPowerOfTwo(int n) {
if ( n<= ) return false;
int countOne = ;
for ( int i=; i<sizeof(n)* && countOne<=; ++i )
{
countOne += (n>>i) & ;
}
return countOne==;
}
};
tips:
首先的思路是bit-munipulation
检查int中有多少个1
学习了一个更简洁的(https://leetcode.com/discuss/45017/5-lines-o-1-space%26time-c-solution-no-hash)
自己优化了一下 一行代码AC。
class Solution {
public:
bool isPowerOfTwo(int n) {
return n<= ? false : !(n & (n-));
}
};
【Power of Two】cpp的更多相关文章
- hdu 4740【模拟+深搜】.cpp
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
- 【Search Insert Position 】cpp
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 【First Missing Positive】cpp
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- 【Insertion Sorted List】cpp
题目: Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct L ...
- 【Merge Sorted Array】cpp
题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...
- 【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 ...
- 【Longest Common Prefix】cpp
题目: Write a function to find the longest common prefix string amongst an array of strings. 代码: class ...
- 【 Regular Expression Matching 】cpp
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- 【Longest Palindromic Substring】cpp
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
随机推荐
- May 04th 2017 Week 18th Thursday
No matter how far you may fly, never forget where you come from. 无论你能飞多远,都别忘了你来自何方. I never forget w ...
- Altium_Designer-PCB中布局元器件时的翻转问题
这个问题是我在第一次对PCB元器件布局时发现的,当时我绘制好原理图生成PCB后,出现了这样一个情况: 在我反复尝试走线后,走好线发现很困难,最后我才想到如果能把这个器件反转一下问题不就都解决了吗!自己 ...
- 2017.9.21 HTML学习总结---多媒体播放系统设计
1.题目:整个页面被划分三个子窗口,上面窗口为页面功能提示区, 下左部分为不同类型播放的功能选项,下右部分为播放系统显示播放信息窗口. (1)网页设计框架: <html> <head ...
- css3阴影 box-shadow
语法 box-shadow:X轴偏移量 y轴偏移量 [阴影模糊半径] [阴影扩展半径] [阴影颜色] [投影方式] 参数介绍: 注:inset 可以写在参数的第一个或最后一个,其它位置是无效的. 阴影 ...
- 【转】使用webmagic搭建一个简单的爬虫
[转]使用webmagic搭建一个简单的爬虫 刚刚接触爬虫,听说webmagic很不错,于是就了解了一下. webmagic的是一个无须配置.便于二次开发的爬虫框架,它提供简单灵活的API,只需少量代 ...
- 【洛谷P1525】[NOIP2010]关押罪犯
关押罪犯 题目链接 思路: 二分图或并查集 这里讲并查集算法: 1.将每对罪犯的冲突关系按影响从大到小排序 2.将集合与(i+n)合并表示编号为i的罪犯不能在该集合内 3.依次从大到小处理冲突关系: ...
- _default_ VirtualHost overlap on port 80, the first has precedence
去掉#NameVirtualHost *:80,然后重启httpd
- phpmyadmin高级功能尚未完全设置部分功能未激活
1.登录phpmyadmin,点击导入,选择/var/ww/html/phpmyadmin/examples/create_tables.sql并执行 完成后可以看到多出了一个库phpmyadmin. ...
- android动画解析(初级)
效果图: ObjectAnimator继承自ValueAnimator的,底层的动画实现机制也是基于ValueAnimator来完成的,因此ValueAnimator仍然是整个属性动画当中最核心的一个 ...
- iOS圆角view的Swift实现(利用Core Graphics绘制)
iOS圆角view的Swift实现(利用Core Graphics绘制) 因为app的列表用用到了圆形图片的头像,所以去探究并思考了一下这个问题.首先这个问题有两个方向的解决方案: 把图片弄成圆形的. ...