LintCode刷题笔记-- O(1) Check Power of 2
标签:
位运算
题目:
Using O(1) time to check whether an integer n is a power of 2
.
解题思路:
这道题是利用位运算判断一个数是不是2 的整数次方
思路:
1. 如果一个数是2的整数次方的话,那么他的二进制表现形式上只有一位是1,其余的都会是0
2. 如果将n-1那么在原先所在的那一位就会变为0,其余之后的位数都会变成1,例如:
16: 10000 ,15:01111
3. 将两者进行与运算,num&(num-1), 则所有的位上都会为0,这一特点可以判断一个数是不是2的整数次方。
4. 2的整数次方一定是正数所以此处不讨论负数。
参考代码:
http://www.code123.cc/docs/leetcode-notes/math_and_bit_manipulation/o1_check_power_of_2.html
LintCode刷题笔记-- O(1) Check Power of 2的更多相关文章
- LintCode刷题笔记-- Count1 binary
标签: 位运算 描述: Count how many 1 in binary representation of a 32-bit integer. 解题思路: 统计一个int型的数的二进制表现形式中 ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- LintCode刷题笔记-- Maximum Product Subarray
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...
- LintCode刷题笔记-- Maximal Square
标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...
- LintCode刷题笔记-- Edit distance
标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...
- LintCode刷题笔记-- Distinct Subsequences
标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...
- LintCode刷题笔记-- BackpackIV
标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...
随机推荐
- JZOJ5857 【NOIP提高组模拟A组2018.9.8】没有上司的舞会
题目 Description "那么真的有果尔德施坦因这样一个人?"他问道. "是啊,有这样一个人,他还活着.至于在哪里,我就不知道了." "那么那个 ...
- Opencv Mat矩阵操作注意事项
矩阵操作通常不会进行元素复制,应注意: Mat a=Mat(100,100,CV_32S); Mat b=Mat(100,100,CV_32S); b=a.col(8);//此时并未进行元素赋值,而只 ...
- webpack处理字体文件
1. 安装 file-loader npm install file-loader --save-dev 2. 在webpack.config.js中配置 module.exports={ //... ...
- 详解如何挑战4秒内百万级数据导入SQL Server(转)
对于大数据量的导入,是DBA们经常会碰到的问题,在这里我们讨论的是SQL Server环境下百万级数据量的导入,希望对大家有所帮助.51CTO编辑向您推荐<SQL Server入门到精通&g ...
- Android中Activity和AppcompatActivity的区别(详细解析)
转载 https://blog.csdn.net/today_work/article/details/79300181 继承AppCompatActivity的界面. 如下图所示: copy界面代码 ...
- 解决 Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.DarkActionBar'
If you are looking for the solution in Android Studio : Right click on your app Open Module Settings ...
- hashMap 源码解读理解实现原理和hash冲突
hashMap 怎么说呢. 我的理解是 外表是一个set 数组,无序不重复 . 每个set元素是一个bean ,存着一对key value 看看代码吧 package test; import jav ...
- day2(老男孩-Python3.5-S14期全栈开发)
作者:赵俊 发布日期:2019/11/5 一.模块初识 import sys '''默认在当前目录下找模块,找不到再去python环境变量中寻找''' print(sys.pat ...
- 使用Python Requests上传表单数据和文件
在Python环境下写一个HTTP客户端,发送POST请求,同时上传表单数据和文件,我们可以使用Requests模块来实现.代码如下: data = { 'name': 'nginx' } files ...
- js移动端判断上下左右划屏
$(function(){ (function(){ var LSwiperMaker = function(o){ var that = this; this.config = o; this.co ...