179-更新二进制位

给出两个32位的整数N和M,以及两个二进制位的位置i和j。写一个方法来使得N中的第i到j位等于M(M会是N中从第i为开始到第j位的子串)

注意事项

In the function, the numbers N and M will given in decimal, you should also return a decimal number.

说明

You can assume that the bits j through i have enough space to fit all of M. That is, if M=10011, you can assume that there are at least 5 bits between j and i. You would not, for example, have j=3 and i=2, because M could not fully fit between bit 3 and bit 2.

样例

给出N = (10000000000)2,M = (10101)2, i = 2, j = 6

返回 N = (10001010100)2

挑战

最少的操作次数是多少?

标签

比特位操作 Cracking The Coding Interview

思路

先将 N 的 i 到 j 位清零,把 M 向右移动 i 位,然后将 N 与上 M

code

class Solution {
public:
/**
*@param n, m: Two integer
*@param i, j: Two bit positions
*return: An integer
*/
int updateBits(int n, int m, int i, int j) {
// write your code here
for (int pos = i; pos <= j; ++pos) {
n &= ~(1 << pos);
}
m <<= i;
return n | m;
}
};

lintcode-179-更新二进制位的更多相关文章

  1. LintCode题解

    JAVA代码:https://github.com/tw1996/LintCode 持续更新

  2. poj3311 Hie with the Pie (状态压缩dp,旅行商)

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3160   Accepted: 1613 ...

  3. oracle从入门到精通复习笔记

    为方便大家跟着我的笔记练习,为此提供数据库表文件给大家下载:点我下载 描述一个表用 desc employees过滤重复的部门 select distinct department_id from e ...

  4. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  5. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  6. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  7. 【刷题笔记】--lintcode木头加工(java)

    木头加工 题目描述 有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k.当然,我们希望得到的小段越长越好,你需要计算能够得到的小段木头的最大长度. 注意事项 木头 ...

  8. lintcode最长回文子串(Manacher算法)

    题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...

  9. topcoder-srm701-div2-900 博弈\计算二进制位1的个数\dp\状态压缩

    借用一下qls翻译过来的题面  现在有 n 个石子,A 和 B 轮流取石子,A先,每次最多可以取 m 个石子,取到最后一个石子的人获胜,但是某个人如果取完石子时候剩余石子数的二进制表示中有奇数个1,这 ...

  10. LintCode A + B Problem

    原题链接在这里:http://www.lintcode.com/en/problem/a-b-problem/ 不让用 数学运算符,就用位运算符. a的对应位 ^ b的对应位 ^ carry 就是re ...

随机推荐

  1. Python学习 :集合

    集合 Set 集合的创建 集合的创建只有一种方式 集合中的元素必须是不可变的数据类型 集合是无序的,可以通过 for 循环来遍历或者迭代器进行筛选 s=set('xiaoming') s1=['ale ...

  2. 在线升级python3.3版本

    1.sudo su 2.wget -P /usr/local  http://www.python.org/ftp/python/3.3.0/Python-3.3.0.tgz    #指定下载到目录/ ...

  3. gem install tiny_tds失败

    解决: brew install freetds gem install tiny_tds -v '2.1.0'

  4. Go中处理文本格式

    首先是xml 解析xml package main import ( "encoding/xml" //xml标准库 "fmt" "io/ioutil ...

  5. ACM1004:Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  6. linux 通过 openconnect 来连接学校内网

    参考 http://xingda1989.iteye.com/blog/1969908 https://blog.csdn.net/edin_blackpoint/article/details/70 ...

  7. [HDU6321]Dynamic Graph Matching(DP)

    题意:给定一个n个点的无向图,开始没有边,然后m个操作,每次加边或者删边,每次操作后输出正好k个边的匹配数k=1,2,3,...n/2,n<=10,m<=30000 可以发现,n<= ...

  8. [原创]python高可用程序设计方法

    有时候程序上的bug会导致程序引发诸如段错误的情况而导致程序异常退出,这时用crond服务来检测,就会有一段时间程序处于不可用的情况,为了增强程序的可用性,我们可以让子进程处理业务,而让主进程检测子进 ...

  9. 北京Uber优步司机奖励政策(高峰期5倍奖励)(12月7日)

    用户组:人民优步及电动车(适用于12月7日) 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http ...

  10. 长沙Uber优步司机奖励政策(1月4日~1月10日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...