标签:

位运算

描述:

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. mysql备份时的快照原理

    实际上实验的结果表明,这里的 the first such read指的是:对同一个表或者不同表进行的第一次select语句建立了该事务中一致性读的snapshot. 其它update, delete ...

  2. vue项目 环境搭建

    1.前端安装 安装项目:vue init webpack docvote 进入docvote里:cd docvote 安装脚手架:cnpm i 运行:npm run dev 2.异步加载 const ...

  3. Java中"str1.equals(str2)"和"str1==str2"的区别

    大家好,这是我的第一篇博客,作为即将入职的学生,我现在的心情是既好奇又兴奋,对未知的职场生活充满了无限的憧憬,也想赶紧对大学生活say goodbye,因为自己的能力现在还比较有限,我想通过博客这个平 ...

  4. mysql知识点回顾与梳理

    一.sql语句执行顺序 from join on where group by avg,sum,count等各种函数 having select distinct order by(asc(升序),d ...

  5. 冒泡排序算法[C++]

    冒泡排序应该是最容易实现的一种排序算法了.其基本思想是:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后.然后比较第2个数和第3个数,将小 ...

  6. Uva10795 A Different Task

    A Different Task https://vjudge.net/problem/UVA-10795 题目大意:给定一个汉诺塔初末状态,求从初状态到末状态最少需要多少步. 考虑最大的一个初末不同 ...

  7. Leetcode506.Relative Ranks相对名次

    给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 "金牌","银牌" 和" 铜牌"(" ...

  8. 搭建单向HTTPS

    //参考网址:https://blog.csdn.net/haolipengzhanshen/article/details/51278262 //打开CMD窗口 cd C:\wamp\bin\apa ...

  9. url映射 ccf (Java正则表达式80分解法)

    问题描述 试题编号: 201803-3 试题名称: URL映射 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 URL 映射是诸如 Django.Ruby on Rails 等 ...

  10. 【AHOI2013复仇】从一道题来看DFS及其优化的一般步骤和数组分层问题【转】

    http://www.cppblog.com/MatoNo1/archive/2012/09/23/191708.html —————————————————————————————————————— ...