[CareerCup] 5.1 Insert Bits 插入位
5.1 You are given two 32-bit numbers, N and M, and two bit positions, land j. Write a method to insert M into N such that M starts at bit j and ends at bit i. 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.
EXAMPLE
Input: N = 10000000000, M = 10011, i = 2, j = 6
Output: N = 10001001100
这道题给了我们两个二进制数N和M,又给了我们两个位置i和j,让我们在把N中的[i,j]替换为M。我最先想到的方法比较笨,就是先把N的后i位取出来存入结果中,然后再把M按对应位置存入结果中,最后把j之后的N的位存入结果中,参见代码如下:
解法一:
class Solution {
public:
int insertBits(int n, int m, int i, int j) {
int res = ;
for (int k = ; k < i; ++k) {
if (n & ) res += << k;
n = n >> ;
}
for (int k = i; k <= j; ++k) {
if (m & ) res += << k;
m = m >> ;
n = n >> ;
}
res += n << (j + );
return res;
}
};
但是书上给出了一个更巧妙的解法,首先在N中把[i,j]对应的位清空,然后把M平移到对应的位置,然后用或来合并M和N即可,方法很巧妙,参见代码如下:
解法二:
class Solution {
public:
int insertBits(int n, int m, int i, int j) {
int allOnes = ~;
int left = allOnes << (j + );
int right = ( << i) - ;
int mask = left | right;
int n_cleared = n & mask;
int m_shifted = m << i;
return n_cleared | m_shifted;
}
};
[CareerCup] 5.1 Insert Bits 插入位的更多相关文章
- innodb insert buffer 插入缓冲区的理解
今天在做一个大业务的数据删除时,看到下面的性能曲线图 在删除动作开始之后,insert buffer 大小增加到140.对于这些状态参数的说明 InnoDB Insert Buffer 插入缓冲,并不 ...
- c++ insert iterators 插入型迭代器
insert iterators 插入型迭代器 (1)front inserters 前向插入迭代器 只适用于提供有push_front()成员函数的容器,在标准程序库中这样的容器是deque和lis ...
- oracle insert into 插入多组数据方法总结
网上好多oracle 的文章,多是以oracle开头,内容确实其他sql,一幅气死人不偿命的嘴脸着实让人难受. 今天就更新点oracle 使用insert into插入数据的方式: 1.oracle ...
- Leetcode#191. Number of 1 Bits(位1的个数)
题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...
- mysql数据库使用insert语句插入中文数据报错
在mysql的命令行模式中,通过insert语句插入中文数据的时候报错,类似于下面这样: Incorrect string value: '\xE7\x8F' for column 'name' at ...
- INSERT: 批量插入结果集方式
INSERT: 批量插入结果集 insert into table select x,y from A UNION select z,k from B ; insert into table sele ...
- 【java】[sql]使用Java程序向MySql数据库插入一千万条记录,各种方式的比较,最后发现insert批量插入方式对效率提升最明显
我的数据库环境是mysql Ver 14.14 Distrib 5.6.45, for Linux (x86_64) using EditLine wrapper 这个数据库是安装在T440p的虚拟机 ...
- ADO方式,VC调用Execute执行INSERT INTO插入变量SQL语句的写法
ADO方式,VC调用Execute执行INSERT INTO插入变量SQL语句的写法 有些情况下,SQL SERVER 2008r2中需要保存float,int类型的数据,当C 中的变量为double ...
- [LeetCode] Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
随机推荐
- Win10 下使用 ionic 框架开发 android 应用之搭载开发环境
转载请注明出处:http://www.cnblogs.com/titibili/p/5102035.html 谢谢~ 1.下载JDK并配置Java运行环境 http://www.oracle.com/ ...
- DEV主从表
1.主从表隐藏表格展开按钮. 当主表内容不包含子表时候隐藏,主从表加号图标.效果如下图. 实现代码 private void gvMain_CustomDrawCell(object sender, ...
- (转)为什么大公司青睐Java
转自 http://www.zhihu.com/question/25908953/answer/32119971 因为这是一个商业问题,不是技术问题. 我在面试时探讨过这个问题,对方创业期,问我如果 ...
- Effective Java 47 Know and use the libraries
Advantages of use the libraries By using a standard library, you take advantage of the knowledge of ...
- mac下 home-brew安装及php,nginx环境安装及配置
Homebrew官网 http://brew.sh/index_zh-cn.html Homebrew是神马 linux系统有个让人蛋疼的通病,软件包依赖,好在当前主流的两大发行版本都自带了解决方案, ...
- Python语言100例
Python版本:python 3.2.2 电脑系统:win7旗舰 实例来源:python菜鸟教程100例 #!/usr/bin/python # -*- coding: UTF-8 -*- impo ...
- C++ new(3)
转载自:http://www.builder.com.cn/2008/0104/696370.shtml “new”是C++的一个关键字,同时也是操作符.关于new的话题非常多,因为它确实比较复杂,也 ...
- mac下 ssh免密码登陆设置
由于mac os 是基于unix的操作系统终端和linux非常类似,所以不用借助类似于windows下的putty 和CRT工具即可远程登陆linux服务器,只需简单地3步即可免密码ssh远程. 1 ...
- 迅为iTOP-4418开发板兼容八核6818开发板介绍
核心板介绍 三星四核S5P4418与八核6818完美兼容 1GB内存/2GB内存可选 电源管理:AXP228,支持动态调频,超低功耗 核心板引出脚最全:四组连接器共320个PIN脚 核心板连接器高度仅 ...
- iTOP-4412开发板---Linux系统学习下载步骤
本文转自迅为论坛:http://www.topeetboard.com 1.cd /home/topeet/Linux-simple/console 下建立.c文件 2. 编译命令,就在此目录下 # ...