标签:

位运算

描述:

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. HZOI20190828模拟32题解

    题面:https://www.cnblogs.com/Juve/articles/11428730.html chinese: 考虑$\sum\limits_{i=0}^{n*m}i*f_i$的意义: ...

  2. html常用标签6-表单标签

    1.表单的初始化标签 <form action="#" method="get"><!--表单的开始--> </form> ...

  3. python 运行python -m pip list 出现错误

    1.出现如下错误提示 2解决方案 解决方法:(windows)在C:\Users{用户名}\ 目录下创建名称为pip的文件夹,里面创建文本文件,内容为 [list] format=columns 保存 ...

  4. poj 2288

    传送门 解题思路 状压dp,记录路径条数,dp[S][i][j]表示状态为S,前一个点是i,再前一个点是j的最大值,然后在开个一样的数组记录方案数,时间复杂度O(2^n*n^2),注意要用long l ...

  5. service network restart 报错重启失败

    Job for network.service failed because the control process exited with error code. See “systemctl st ...

  6. python时间处理,datetime中的strftime/strptime

    python中datetime模块非常好用,提供了日期格式和字符串格式相互转化的函数strftime/strptime 1.由日期格式转化为字符串格式的函数为: datetime.datetime.s ...

  7. Leetcode476.Number Complement数字的补数

    给定一个正整数,输出它的补数.补数是对该数的二进制表示取反. 注意: 给定的整数保证在32位带符号整数的范围内. 你可以假定二进制数不包含前导零位. 示例 1: 输入: 5 输出: 2 解释: 5的二 ...

  8. 时序数据库连载系列: 时序数据库一哥InfluxDB之存储机制解析

    InfluxDB 的存储机制解析 本文介绍了InfluxDB对于时序数据的存储/索引的设计.由于InfluxDB的集群版已在0.12版就不再开源,因此如无特殊说明,本文的介绍对象都是指 InfluxD ...

  9. TZ_11_Spring-Boot的入门

    1.SpringBoot的特点 为基于Spring的开发提供更快的入门体验 开箱即用,没有代码生成,也无需XML配置.同时也可以修改默认值来满足特定的需求 提供了一些大型项目中常见的非功能性特性,如嵌 ...

  10. gomod

    package main import "mycore" func main(){ mycore.ShowName() } main.go module mytest requir ...