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 ...
随机推荐
- 微信小程序之组件的集合(六)
这个将是最后一篇关于小程序的记录了,课程接近尾声,最后一个是关于用户的page页面,看看这个页面中有哪些值得学习的地方! 一.page中my开发 这个主要是展示用户喜欢的杂志,以及用户的信息,需要创建 ...
- Ionic cordova-plugin-splashscreen
1.添加插件 cordova plugin add https://github.com/apache/cordova-plugin-splashscreen.git 2.设置启动画面 在根目录下面r ...
- linux学习(三)-----linux用户管理、实用指令
用户管理 基本介绍 说明: 1.Linux 系统是一个多用户多任务的操作系统,任何一个要使用系统资源的用户,都必须首先向 系统管理员申请一个账号,然后以这个账号的身份进入系统. 2.Linux 的用户 ...
- 【DM642学习笔记八】色度重采样
TI文档"TMS320C64x DSP Video Port_VCXO Interpolated Control (VIC)Port.pdf"第3.5.2 Chrominance ...
- 解决hive无法传参问题思路
由于hive执行过程中参数必须写死,无法进行传递参数.利用shell脚本和java编程进行解决 #!/bin/sh#date = `date -d -1days '+%Y-%m-%d'`if [ $# ...
- service network restart 报错重启失败
Job for network.service failed because the control process exited with error code. See “systemctl st ...
- Kotlin 委托(1)类委托、变量委托注意事项
1.官方文档 英文: https://kotlinlang.org/docs/reference/delegation.html https://kotlinlang.org/docs/referen ...
- Django项目:CRM(客户关系管理系统)--54--45PerfectCRM实现账号快速重置密码
# gbacc_ajax_urls.py # ————————42PerfectCRM实现AJAX全局账号注册———————— from django.conf.urls import url fro ...
- 如何获取SpringBoot项目的applicationContext对象
ApplicationContext对象是Spring开源框架的上下文对象实例,在项目运行时自动装载Handler内的所有信息到内存.传统的获取方式有很多种,不过随着Spring版本的不断迭代,官方也 ...
- SQL SERVER 自增列的方法
declare @Table_name varchar(60) set @Table_name = ''; if Exists(Select top 1 1 from sysobjects ...