Update Bits
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)
Notice
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 jand i. You would not, for example, have j=3 and i=2, because M could not fully fit between bit 3 and bit 2.
Given N=(10000000000)2, M=(10101)2, i=2, j=6
return N=(10001010100)2
分析:http://www.kancloud.cn/kancloud/data-structure-and-algorithm-notes/72988
大致步骤如下:
- 得到第
i位到第j位的比特位为0,而其他位均为1的掩码mask。 - 使用
mask与 N 进行按位与,清零 N 的第i位到第j位。 - 对 M 右移
i位,将 M 放到 N 中指定的位置。 - 返回 N | M 按位或的结果。
获得掩码mask的过程可参考 CTCI 书中的方法,先获得掩码(1111...000...111)的左边部分,然后获得掩码的右半部分,最后左右按位或即为最终结果。
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
int ones = ~;
int mask = ;
if (j < ) {
int left = ones << (j + );
int right = (( << i) - );
mask = left | right;
} else {
mask = ( << i) - ;
}
return (n & mask) | (m << i);
}
};
Update Bits的更多相关文章
- Lintcode: Update Bits
Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits be ...
- LintCode刷题笔记-- Update Bits
标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 解决win10无法完成更新 正在撤销更改
删除Windows 更新缓存文件按Windows+X,选择“命令提示符(管理员)”:输入:net stop wuauserv,回车(此处会提醒服务停止):输入: %windir%\SoftwareDi ...
- MD5加密详解
MD5加密详解 引言: 我在百度百科上查找到了关于MD5的介绍,我从中摘要一些重要信息: Message Digest Algorithm MD5(中文名为信息摘要算法第五版)为计算机安全领域广泛使用 ...
- C、C++的Makefile的编写以及动、静态库的制作调用(包括MAC地址的获取及MD5加密)
一.C代码 静态库 四个.h.c文件 add.h #ifndef ADD_H #define ADD_H int add(int a,int b); #endif add.c #include < ...
- MD5 32位加密算法源码(测试通过)(系转载 飞扬天下)
供自己学习使用 md5.h文件 #ifndef MD5_H #define MD5_H #include <string> #include <fstream> /* Type ...
- win10更新时遇到错误0x80070002的正确处理方法
win10更新Flash Player.或者在 “启用或关闭windows功能” 经常出现提示错误0x80070002,这要怎么解决呢?这里介绍下正确的错误代码0x80070002解决办法. 严肃提 ...
- Win10更新补丁失败后出现无法更新正在撤销 解决办法
系统更新失败,反复重启还是不行,那是不是下载下来的补丁没用了呢??所以我们先要删除Windows更新的缓存文件!在做以下操作之前,首先我们要确认系统内的windows update & BIT ...
随机推荐
- 第一个sprint与第二个sprint阶段总结
总体: 在第一个sprint中,团队里的小伙伴都在积极努力的配合,基本按照流程做了一次Sprint,大家一块进行计划会议,一块估计任务工时,但是还是有一些意外的事情,这段时间大家都没什么精力放在这门上 ...
- java词频统计——web版支持
需求概要: 1.把程序迁移到web平台,通过用户上传TXT的方式接收文件. 2.用户直接输入要统计的文本,服务器返回结果 3.在页面上给出链接 (如果有封皮.作者.字数.页数等信息更佳)或表格,展示经 ...
- ejabberd与XMPP
ejabberd支持XMPP协议. worktile用ejabberd来做了实时消息推送: https://worktile.com/tech/basic/worktile-real-time-not ...
- 用send_keys输入文本的方法
我们使用app时,输入文字都是调用软键盘.在自动化测试中当然也可以调用软键盘,但是由于输入法设计上的差异,有时候不能达到很好的效果. 例如,搜狗拼音输入法: 选择4-咖啡,然而多打几次,输入法就把“咖 ...
- 如何设置Java虚拟机JVM启动内存参数
Tomcat默认的Java虚拟机JVM启动内存参数大约只有64MB或者128MB,非常小,远远没有利用现在服务器的强大内存,所以要设置Java虚拟机JVM启动内存参数.具体设置方法为: Tomcat修 ...
- 【BZOJ1032】[JSOI2007]祖玛(动态规划)
[BZOJ1032][JSOI2007]祖玛(动态规划) 题面 BZOJ 洛谷 题解 听说是道假题,假的原因是因为出题人可能没有考虑到祖玛的骚套路,比如可以先打几个球进去再一波消掉.也就是出题人基本默 ...
- 2:jquery.cookie用法详细解析
一个轻量级的cookie 插件,可以读取.写入.删除 cookie. jquery.cookie.js 的配置 首先包含jQuery的库文件,在后面包含 jquery.cookie.js 的库文件. ...
- linux shell重定向
http://note.youdao.com/noteshare?id=e944e6315d1566b3417e6f59305ddedc
- 实战:使用SVN+apache搭建一个版本控制服务器
今天讲的内容: 实战:使用SVN+apache搭建一个版本控制服务器 每天: 10:00 晚上:21:00 服务端:xuegod63.cn IP:192.168.10.63 服务概述: SVN(s ...
- 配置使用 NTP
1. 安装chrony(时间同步客户端) ubuntu/debian: apt-get install chrony Centos/redhat/alios: yum install chrony 2 ...