标签:

位运算

描述:

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. php表单 - 验证邮件和URL

    PHP - 验证名称 以下代码将通过简单的方式来检测 name 字段是否包含字母和空格,如果 name 字段值不合法,将输出错误信息: $name = test_input($_POST[" ...

  2. c语言学习笔记 关于double

    今天做了个简单的例子,由于没有使用正确的数据类型导致出错,下面是记录 #include <stdio.h> int main(void){ int i; double sum; doubl ...

  3. php 怎样判断一段字符 有没有经过 urlencode 处理

    有没有百分号 判断字符串 执行urldecode 之前和之后是否一致 一致就是没有经过urlencode 不一致就是经过urlencode的 自己方法:判断是否所有: if(strpos($cooki ...

  4. 《DSP using MATLAB》Problem 7.32

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  5. ongl与Struts标签

    一.ONGL OGNL 的全称是“Object-Graph Navigation Language”,即对象图导航语言,它是一种功能强大的开源表达式语言.使用这种表达式语言可以通过某种表达式语法存取  ...

  6. day1(老男孩-Python3.5-S14期全栈开发)

    作者:赵俊            发布日期:2019/10/18 一.第一个python程序 1.在解释器下写hello world程序运行,与运行外部文件方法 运行外部文件,必须在相应位置创建一个p ...

  7. python函数当容器

    def func(): pass func2 = func func2() i = [func,func2] for a in i: a() 函数名就是内存地址,加()代表执行

  8. Python爬虫笔记【一】模拟用户访问之设置处理cookie(2)

    学习的课本为<python网络数据采集>,大部分代码来此此书. 做完请求头的处理,cookie的值也是区分用户和机器的一个方式.所以也要处理一下cookie,需要用requests模块,废 ...

  9. String、StringBuffer和StringBuilder源码解析

    1.String 1.1类的定义 public final class String implements java.io.Serializable, Comparable<String> ...

  10. Leetcode264. Ugly Number II丑数2

    编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 ...