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的位存入结果中,参见代码如下:

解法一:

  1. class Solution {
  2. public:
  3. int insertBits(int n, int m, int i, int j) {
  4. int res = ;
  5. for (int k = ; k < i; ++k) {
  6. if (n & ) res += << k;
  7. n = n >> ;
  8. }
  9. for (int k = i; k <= j; ++k) {
  10. if (m & ) res += << k;
  11. m = m >> ;
  12. n = n >> ;
  13. }
  14. res += n << (j + );
  15. return res;
  16. }
  17. };

但是书上给出了一个更巧妙的解法,首先在N中把[i,j]对应的位清空,然后把M平移到对应的位置,然后用或来合并M和N即可,方法很巧妙,参见代码如下:

解法二:

  1. class Solution {
  2. public:
  3. int insertBits(int n, int m, int i, int j) {
  4. int allOnes = ~;
  5. int left = allOnes << (j + );
  6. int right = ( << i) - ;
  7. int mask = left | right;
  8. int n_cleared = n & mask;
  9. int m_shifted = m << i;
  10. return n_cleared | m_shifted;
  11. }
  12. };

[CareerCup] 5.1 Insert Bits 插入位的更多相关文章

  1. innodb insert buffer 插入缓冲区的理解

    今天在做一个大业务的数据删除时,看到下面的性能曲线图 在删除动作开始之后,insert buffer 大小增加到140.对于这些状态参数的说明 InnoDB Insert Buffer 插入缓冲,并不 ...

  2. c++ insert iterators 插入型迭代器

    insert iterators 插入型迭代器 (1)front inserters 前向插入迭代器 只适用于提供有push_front()成员函数的容器,在标准程序库中这样的容器是deque和lis ...

  3. oracle insert into 插入多组数据方法总结

    网上好多oracle 的文章,多是以oracle开头,内容确实其他sql,一幅气死人不偿命的嘴脸着实让人难受. 今天就更新点oracle 使用insert into插入数据的方式: 1.oracle ...

  4. Leetcode#191. Number of 1 Bits(位1的个数)

    题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...

  5. mysql数据库使用insert语句插入中文数据报错

    在mysql的命令行模式中,通过insert语句插入中文数据的时候报错,类似于下面这样: Incorrect string value: '\xE7\x8F' for column 'name' at ...

  6. INSERT: 批量插入结果集方式

    INSERT: 批量插入结果集 insert into table select x,y from A UNION select z,k from B ; insert into table sele ...

  7. 【java】[sql]使用Java程序向MySql数据库插入一千万条记录,各种方式的比较,最后发现insert批量插入方式对效率提升最明显

    我的数据库环境是mysql Ver 14.14 Distrib 5.6.45, for Linux (x86_64) using EditLine wrapper 这个数据库是安装在T440p的虚拟机 ...

  8. ADO方式,VC调用Execute执行INSERT INTO插入变量SQL语句的写法

    ADO方式,VC调用Execute执行INSERT INTO插入变量SQL语句的写法 有些情况下,SQL SERVER 2008r2中需要保存float,int类型的数据,当C 中的变量为double ...

  9. [LeetCode] Insert Interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

随机推荐

  1. IOS之--UI进阶--多控制器管理第一天

    01-项目中常见的文件(LaunchScreen) Xcode5 框架是苹果事先已经导入进去的.在项目的结构当中就能够看到导入的框架. Xcode6 会自动导入一些觉见的框架.在项目结构当中,看不到已 ...

  2. PHPExcel中open_basedir restriction in effect的解决方法

    用PHPExcel做导出execl的时候发现在本地没有问题,但是把网站传到租用的服务器的时候就报错,具体如下: Warning: realpath() [function.realpath]: ope ...

  3. Js中获取对象的所有key值

    假如现在有一个对象 var obj = { A:2 ,B:"Ray" ,C:true ,D:function(){} } 如果想遍历对象obj中的所有键值,一般是用以下方式 for ...

  4. CentOS下搭建SVN服务器

    1.安装SVN SVN数据存储有两种方式,BDB(事务安全表类型)和FSFS(一种不需要数据库的存储系统),为了避免在服务器连接中断时锁住数据,FSFS是一种更安全也更多人使用的方式.SVN的运行方式 ...

  5. Github学习之路-初出茅庐,接触Github,了解Github

     一.了解GitHub 作为一个在线协作网站,GitHub允许程序员们分享和协力于开源项目的工作. GitHub不仅是一个分享开源创作并与其它程序员合作的好地方,你也可以从GitHub上收到自己作品的 ...

  6. js清除缓存方法

    1.加入如下头描述 <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUI ...

  7. Java基础の乱弹琴二:break关键字

    Java中的break一般用于 跳出一个switch或者循环. 跳出switch基本不用赘述. break跳出循环一般是跳出当前一层循环. 如若需要跳出多层循环可以在break后加标签,然后把标签标注 ...

  8. 用UNIX消息队列实现IPC(以ATM为例)

    清明假期三天没出寝室的门,先是把独立的博客折腾好了.域名备案还没好.域名是ilovecpp.com,意为“我爱C++”,好羞涩,掩面,逃:).话说cnblogs.com的界面好丑 .其余大部分时间就是 ...

  9. Hadoop could not find or load main class

    Error: Could not find or load main class <class_name> 我在尝试使用hadoop definitive guide的代码做练习时,遇到一 ...

  10. Oracle Jdbc demo

    两种方式: thin是一种瘦客户端的连接方式,即采用这种连接方式不需要安装oracle客户端,只要求classpath中包含jdbc驱动的jar包就行.thin就是纯粹用Java写的ORACLE数据库 ...