标签:

位运算

题目:

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的更多相关文章

  1. LintCode刷题笔记-- Count1 binary

    标签: 位运算 描述: Count how many 1 in binary representation of a 32-bit integer. 解题思路: 统计一个int型的数的二进制表现形式中 ...

  2. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  3. LintCode刷题笔记-- LongestCommonSquence

    标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...

  4. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  5. LintCode刷题笔记-- Maximum Product Subarray

    标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...

  6. LintCode刷题笔记-- Maximal Square

    标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...

  7. LintCode刷题笔记-- Edit distance

    标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...

  8. LintCode刷题笔记-- Distinct Subsequences

    标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...

  9. LintCode刷题笔记-- BackpackIV

    标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...

随机推荐

  1. PostgreSQL的架构

    是最先进的数据库.他的第一个版本在1989年发布,从那时开始,他得到了很多扩展.根据db-enginers上的排名情况,PostgreSQL目前在数据库领域排名第四. 本篇博客,我们来讨论一下Post ...

  2. Python-购物车系统

    # coding=utf-8 import os, pickle class color: def echo_error(self, red): print(f"\033[31;1m {re ...

  3. python 连接mssql数据库

    1.目标数据sql2008 R2 ComPrject=>TestModel 2.安装python 连接mssql 模块 运行 pip install pymssql-2.2.0.dev0-cp3 ...

  4. 使用C#反射实现用户控件调用父页面方法

    using System.Reflection; MethodInfo mi = this.Page.GetType().GetMethod("GetUserName"); //该 ...

  5. python生成VOC2007的类库

    VOCAnnotation.py: # -*-coding:utf-8-*- from lxml import etree import os class VOCAnnotation(object): ...

  6. jquery ajax跨越

    JSONP是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问 1.jsonp之$.ajax js $.ajax({ ...

  7. python 基础(while 循环、格式化输出、运算符、编码初识)

    while循环 break 终止当前循环 count = 1 while count < 3: print(count) count += 1 break # while循环中一旦代码执行到br ...

  8. Javascript模块化编程(一)模块的写法最佳实践

    Javascript模块化编程,已经成为一个迫切的需求.理想情况下,开发者只需要实现核心的业务逻辑,其他都可以加载别人已经写好的模块但是,Javascript不是一种模块化编程语言,它不支持类clas ...

  9. 轮播图js版&jQ版

    JS版轮播图 html部分和css部分自己任意定 主要构成: 1,一个固定的框 超出框的部分隐藏 2,几张图片float:left 3,下部下原点,点击切换,切换到不同的张都有红色显示 4,左右两个大 ...

  10. 2019-3-1-WPF-从零开始开发-dotnet-Remoting-程序

    title author date CreateTime categories WPF 从零开始开发 dotnet Remoting 程序 lindexi 2019-03-01 09:30:45 +0 ...