标签:

位运算

描述:

Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e g , M becomes a substring of N located at i and starting at j)

 解题思路:
1.这道题给出了四个int型的整数,其中m,n是两个给定的整数,而i与j则是前后两位的区间,例如i =2 , j =6 就是将n中的第二位到第六位变换为m
2.此题引入了一个掩码的概念(mask):
   (1) 初始的时候所有位都为1:ones = ~0
   (2) left :将ones 所有的位都向左移动j位+1 left = (ones<< j) + 1,形成mask左边部分(之所以加1是因为第一位是符号位1)
     (3)  right : 将1同时也向左移动i位,再减去1,right = (1<<i)-1(之所以减去1是因为,要空出第i位,从i-1位变成1)
   (4)将left和right来做或的运算,可以得到mask,正好得到前面所有的1和后面所有的1,空出中间的0,从而得到mask
 (5)对于mask其实存在一个corner case就是如果j>31 的情况, 在此left方向就会溢出,此时只需要计算right部分即可,因为题目中保证存在足够的空间进行位移,所以不考虑溢出。 
3. 将mask 与 n来做与运算,从而可以将mask其他的位置上的数字变为与n相同的
4. 将m向前移动i位,正好可以与mask中预留出来的所有的0对齐,再与第三步得到的结果进行或运算,将m中的内容插入n中的指定位置。
5. 最后的表达形式为:(n&mask) | (m<<i) 
参考代码:
http://www.kancloud.cn/kancloud/data-structure-and-algorithm-notes/72988

LintCode刷题笔记-- Update Bits的更多相关文章

  1. LintCode刷题笔记--Flip Bits

    Flip Bits: 标签:位运算 题目:Determine the number of bits required to flip if you want to convert integer n  ...

  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. python中defaultdict方法的使用

    默认值可以很方便 众所周知,在Python中如果访问字典中不存在的键,会引发KeyError异常(JavaScript中如果对象中不存在某个属性,则返回undefined).但是有时候,字典中的每个键 ...

  2. EasyUI Tree与Datagrid联动

      效果图 这是一个简单的solr检索的例子   输入关键词,显示树   选择一个节点,得到该节点下文档信息   代码: JSP: 重点是标红的URL传递 <body>     <d ...

  3. js 设置缓存

    长期存储    localStorage.getItem("key");       //获取键的值    localStorage.setItem("key" ...

  4. 加载框(loading)

    一般在用户提交数据或者新加载页面时,请求服务器的过程,页面没有响应,但是用户并不知道,此时在发生什么.这时,就需要loading框给用户提示,增加用户体验. 1.引入loading.css. html ...

  5. javascript基础:bom

    一.BOM 1.概念:Browser Object Model  浏览器对象模型 *  将浏览器的各个组成部分封装成对象 2.组成: *  Window:窗口对象                  1 ...

  6. python打包成为exe文件

    pyinstaller 库的使用 PyInstaller是一个十分有用的第三方库,它能够在Windows.Linux.Mac OS X 等操作系统下将 Python 源文件打包,通过对源文件打包,Py ...

  7. Django项目:CRM(客户关系管理系统)--40--32PerfectCRM实现King_admin添加不进行限制

    # forms.py # ————————19PerfectCRM实现King_admin数据修改———————— from django import forms from crm import m ...

  8. 前端(Node.js)(3)-- Node.js实战项目开发:“技术问答”

    1.Web 与 Node.js 相关技术介绍 1.1.Web应用的基本组件 web应用的三大部分 brower(GUI)<==>webserver(business logic.data ...

  9. 使用yarn代替npm

    npm node module package,是nodeJs的包管理工具,最初是有 Isaac Z. Schlueter 开发的,这个让全世界的人都可以很快的运用互相开发的package的工具使no ...

  10. python 升级后正确安装 pip

    由于服务器的python 版本是2.6.6 , 为了使用 twisted 升级至 2.7.13 , 如果此时直接用 yum install python-pip 安装 pip, 则实际pip 会安装在 ...