博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lintcode: Update Bits
阅读量:5888 次
发布时间:2019-06-19

本文共 1094 字,大约阅读时间需要 3 分钟。

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? YesExampleGiven N=(10000000000)2, M=(10101)2, i=2, j=6return N=(10001010100)2NoteIn the function, the numbers N and M will given in decimal, you should also return a decimal number.ChallengeMinimum number of operations?ClarificationYou 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位,按位或

1 class Solution { 2     /** 3      *@param n, m: Two integer 4      *@param i, j: Two bit positions 5      *return: An integer 6      */ 7     public int updateBits(int n, int m, int i, int j) { 8         // write your code here 9         int len = j-i+1;10         int temp = 0;11         for (int x=0; x

 

转载地址:http://zngix.baihongyu.com/

你可能感兴趣的文章
OSChina 周四乱弹 ——解读揭秘动弹惨案
查看>>
Java程序员集合框架面试题
查看>>
双飞翼布局和常见三角
查看>>
[10秒学会] - iOS 网络检测(如需要更详细 请用Reachability)
查看>>
理解Golang包导入
查看>>
category && extension 介绍
查看>>
Keil5可以打开Keil4的Project
查看>>
IntelliJ IDEA 2018.1.3 + jdk1.8 安装教程
查看>>
命令行输出java版本与环境变量配置的不一样问题解决
查看>>
你们终于喊出自己就是公有云了?
查看>>
帝国的征程——一个国家如何获得五大流氓的地位[转]
查看>>
Spring Security教程之自定义Spring Security默认的403页面
查看>>
WebMagic使用说明-基本的爬虫
查看>>
前嗅网络的ForeLib数据库介绍
查看>>
exchange 中继
查看>>
电脑蓝屏修复过程
查看>>
vmware ESXI 之重新注册虚拟机
查看>>
Apache Zeppelin安装及使用
查看>>
外网无法访问云主机HDFS文件系统
查看>>
hovercard头像悬浮卡效果
查看>>