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 ...
随机推荐
- java实验报告五
一.实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 二.实验基础: IP和端口:IP是用来标示计算机,而端口是用来标示某个计算机上面的特定应用.至于它们的 ...
- 第一Sprint阶段回复其他各组对我组提出的意见
组号 组名 组名 对我组提出的意见 对各组的回复 1 理财猫 1.虚拟机和手机端的交互是否能扩展到整个学校 2.每次都需要老师输入作业内容吗 3.操作过于繁琐 多谢你们的建议,老师可以选择输入 ...
- beta2
组员1:吴晓晖(组长) 过去两天完成了哪些任务 代码重构基本完成 展示GitHub当日代码/文档签入记录 接下来的计划 推荐算法 还剩下哪些任务 组员2:陈锦谋 过去两天完成了哪些任务 重新制作图标 ...
- java 封装,继承,多态基础
什么是封装? 1,对象数据和在.操作该对象的指令都是对象自身的一部分,能够实现尽可能对外部隐藏数据. 2,实际项目开发中,使用封装最多的就是实体类. 什么是继承? 1,继承是面向对象程序设计能提高效率 ...
- jsp数据库开发
完全卸载mysql数据库图文教程 https://jingyan.baidu.com/article/f96699bbaa8fc1894f3c1b5a.html MySQl:123456 JDBC概述 ...
- stylus-loader (copy)
https://blog.csdn.net/xqnode/article/details/59777793 "stylus-loader": "^2.5.0", ...
- SpringBoot 7.SpringBoot 结合 Thymeleaf
一.引入 Thymeleaf 依赖 <!-- Spring boot - thymeleaf --> <dependency> <groupId>org.sprin ...
- mysql 好用的sql语句
1.删除某个库里面全部的表 ,先在mysql库中执行: SELECT CONCAT('drop table ',table_name,';') FROM information_schema.`TA ...
- 阿里Java编码规范
详细,全面 很不错 阿里 Java编码规范
- Centos中安装和配置vsftp简明教程
一.vsftp安装篇 # 安装vsftpd yum -y install vsftpd # 启动 service vsftpd start # 开启启动 chkconfig vsftpd on 二.v ...