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 ...
随机推荐
- 升级gitk后,Error in startup script: unknown color name "lime"
$ gitkError in startup script: unknown color name "greeen" (processing "-fore" o ...
- 取消 ios 上下滑动
- PAT甲级——A1031 Hello World for U
Given any string of N (≥) characters, you are asked to form the characters into the shape of U. For ...
- Django-rest Framework(五)
把十大接口做完了才能更好的了解后面的视图类 1.(重点)二次封装Response;自定义APIResponse继承Response,重写 ____init____方法 from rest_framew ...
- Hibernate-概述-搭建-测试-配置详解
一 hibernate概述 1.1 框架是什么 1.框架是用来提高开发效率的 2.封装了好了一些功能.我们需要使用这些功能时,调用即可.不需要再手动实现. 3.所以框架可以理解成是一个半成品的项目.只 ...
- python生成VOC2007的类库
VOCAnnotation.py: # -*-coding:utf-8-*- from lxml import etree import os class VOCAnnotation(object): ...
- Vuejs实战项目五:数据列表
1.在EasyMock 中添加数据列表模拟接口 请求url:/suyuan/list 请求方式:get 描述:数据列表 mock.js配置: 例: { "code": 2000, ...
- 【Java爬虫】爬取南通大学教务处成绩
没使用自动登录,所以获取是比较麻烦.. 1.http://jwgl.ntu.edu.cn/cjcx 进入官网,进行账号密码登录 2.点击全部成绩查询(也一定要点进去,不然cookie不会返回值) ...
- Odoo 在 Ubuntu 环境下性能调优
一.首先我们要分析影响odoo 服务器 性能的因素 CPU 目前大部分CPU在同一时间只能运行一个线程,超线程的处理器可以在同一时间处理多个线程,因此可以利用超线程特性提高系统性能. 在linux系统 ...
- 用js实现base64编码器
base-64作为常见的编码函数,在基本认证.摘要认证以及一些HTTP扩展中得到了大量应用.在前端领域,也常常把图片转换为base-64编码在网络中传输.本文将详细介绍base64的原理及用js实现b ...