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 ...
随机推荐
- json-server 和mock.js生成大量json数据
JSON-server和mock.jsmock文件夹下 db.json db.jsjson-sever使用 安装:npm install json-server -g/mock 目录下执行json-s ...
- Ionic 图片预览ion-slide-box,ion-slide,ion-scroll实现
1.index.html 代码 <body ng-app="starter"> <ion-pane> <ion-header-bar class=&q ...
- 499 单词计数 (Map Reduce版本)
原题网址:https://www.lintcode.com/problem/word-count-map-reduce/description 描述 使用 map reduce 来计算单词频率http ...
- PAT甲级——A1023 Have Fun with Numbers
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...
- qml获取实际渲染的字体
当设置qml的Text元素的字体时,如果系统中不存在设置的字体,qml会根据匹配算法自动选取系统中存在的一种字体.比如:设置font.family: "微软雅黑",但系统中根本没有 ...
- 机器学习实战之k-近邻算法(3)---如何可视化数据
关于可视化: <机器学习实战>书中的一个小错误,P22的datingTestSet.txt这个文件,根据网上的源代码,应该选择datingTestSet2.txt这个文件.主要的区别是最后 ...
- ECMAScript 5 严格模式
1. 变量必须使用var声明,杜绝不小心将本地变量声明成一个全局变量 在常规模式下,如果我们声明一个变量时省略了var关键字,解析引擎会自动将其声明为全局变量,但在严格模式下,会直接抛出异常,不会为我 ...
- 详解Spring MVC 4常用的那些注解
Spring从2.5版本开始在编程中引入注解,用户可以使用@RequestMapping, @RequestParam, @ModelAttribute等等这样类似的注解.到目前为止,Spring的版 ...
- Redis 核心
一.Redis单机多实例原理 每个实例对应不同的配置文件,配置文件对应不同的端口.数据库文件位置.日志位置. 二.Redis单实例多数据库 每个Redis实例都有16个数据库,下标从0-15,当 se ...
- Docker(四)安装Redis
1.安装redis https://blog.csdn.net/qq_29917523/article/details/78019511 2.测试连接