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 between i and j in N equal to M (e g , M becomes a substring of N located at i and starting at j) Have you met this question in a real interview? Yes
Example
Given N=(10000000000)2, M=(10101)2, i=2, j=6 return N=(10001010100)2 Note
In the function, the numbers N and M will given in decimal, you should also return a decimal number. Challenge
Minimum number of operations? Clarification
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.
以题中例子为例,做一个滤波器在i,j之间:11110000011,来跟N按位与,再把M左移i位,按位或
class Solution {
/**
*@param n, m: Two integer
*@param i, j: Two bit positions
*return: An integer
*/
public int updateBits(int n, int m, int i, int j) {
// write your code here
int len = j-i+1;
int temp = 0;
for (int x=0; x<len; x++) {
temp |= 1<<x;
}
temp = ~(temp<<i);
return (n&temp) | (m<<i);
}
}
Lintcode: 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 ...
- Lintcode: Flip Bits
Determine the number of bits required to flip if you want to convert integer n to integer m. Have yo ...
- 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]——目录
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解决办法. 严肃提 ...
随机推荐
- XPath学习:轴(14)——总结
原文地址:http://www.cnblogs.com/zhaozhan/archive/2009/09/10/1564396.html XPath 是一门在 XML 文档中查找信息的语言.XPath ...
- laravel paginate动态分页
1.router Route::get('product', function(){ $products = App\Product::paginate(10); return view('produ ...
- phone number is not known @w@ have no phone, and thus no phone number
http://dev.mysql.com/doc/refman/5.7/en/problems-with-null.html B.5.4.3 Problems with NULL Values The ...
- Python之 continue继续循环和多重循环
Python之 continue继续循环 在循环过程中,可以用break退出当前循环,还可以用continue跳过后续循环代码,继续下一次循环. 假设我们已经写好了利用for循环计算平均分的代码: L ...
- 关于pom.xml的一些问题的理解
最近在pom上出了一些问题,搞了一天才理解了一些问题,记录一下. 当在覆盖本地repository包之后,pom.xml上面出现了一个x. 当mvn->update project之后,还是有许 ...
- iOS开发教程之:iPhone开发环境搭建
安装条件: 硬件:一台拥有支持虚拟技术的64位双核处理器和2GB以上内存的PC. 注意:运行MAC OS,需要电脑支持虚拟技术(VT),安装时,需要将VT启动,在BIOS中开启. 软件: Window ...
- [LeetCode]题解(python):089 Gray Code
题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...
- iOS UPYUN(又拍云)使用总结
UPYUN,原来没用过,上个周用了一次,觉得蛮方便的,对于个人开发者,且没有服务器的,上传图片和文件,是个不二选择. 首先,先明白原理: 1.又拍云有一个上传空间,在这个空间里,有空间名称.密钥,其他 ...
- C# base和this
• 是否可以在静态方法中使用base和this,为什么? • base常用于哪些方面?this常用于哪些方面? • 可以base访问基类的一切成员吗? • 如果有三层或者更多继承,那么最下级派生类的b ...
- LeetCode Course Schedule II
原题链接在这里:https://leetcode.com/problems/course-schedule-ii/ 题目: There are a total of n courses you hav ...