标签:

位运算

描述:

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. __name__变量

    再写项目过程中,将功能函数写在 if  __name=="__main__":之外,执行代码写在之中,如下:如果在foo模块打印__name__为__main__,而如果在bin模 ...

  2. 模板:有旋treap

    有旋转的treap,其实rotate比较难理解 没关系,上板子: 题目还是普通平衡树 #include<iostream> #include<cstdio> #include& ...

  3. 【bzoj2120】数颜色 带修莫队

    数颜色 题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画 ...

  4. 19-10-17-T

    真的T了.(滑稽 Final 35 Miemeng 100 03:12:51 0 03:12:54 15 03:12:55 115 03:12:55 幸好$T1$还能要,不然就…… 前言 中午$\sc ...

  5. python 获取公网 ip

    from urllib2 import urlopen my_ip = urlopen('http://ip.42.pl/raw').read() print 'ip.42.pl', my_ip fr ...

  6. JS 中 Tween 的使用

    JavaScript Tween算法及缓动效果   Flash做动画时会用到Tween类,利用它可以做很多动画效果,例如缓动.弹簧等等.我这里要教大家的是怎么利用flash的Tween类的算法,来做j ...

  7. PAT甲级——A1021 Deepest Root

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  8. 使用ssh时报错:Service对象空指针异常

    有可能是spring容器不能自动生成service对象,导致空指针异常,常见的情况可能是在service前面加@Service注释

  9. Python3实用编程技巧进阶

    Python3实用编程技巧进阶  整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看的时候可以 ...

  10. Spring Cloud Security&Eureka安全认证(Greenwich版本)

    Spring Cloud Security&Eureka安全认证(Greenwich版本) 一·安全 Spring Cloud支持多种安全认证方式,比如OAuth等.而默认是可以直接添加spr ...