LeetCode之位操作题java
191. Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011
, so the function should return 3.
- public class Solution {
- // you need to treat n as an unsigned value
- //使用按位与操作
- public int hammingWeight(int n) {
- int count = 0;
- while(n!=0){
- n = n&(n-1);
- ++count;
- }
- return count;
- }
- }
190. Reverse Bits
Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
- public class Solution {
- // you need treat n as an unsigned value
- //结果往右移,而n值往左移动,以此来匹配
- public int reverseBits(int n) {
- if(n==0)
- return 0;
- int result = 0;
- for(int i=0;i<32;i++){
- result <<= 1;
- if((n&1)==1)
- result++;
- n >>= 1;
- }
- return result;
- }
- }
338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.
Example:
For num = 5
you should return [0,1,1,2,1,2]
.
Follow up:
- It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
- Space complexity should be O(n).
- Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
使用动态规划的思想;
- public class Solution {
- public int[] countBits(int num) {
- int[] arr = new int[num+1];
- arr[0] = 0;
- for(int i=1;i<=num;i++){
- arr[i] = arr[i&(i-1)]+1;///数i中1的位数,与前面的i&(i-1)中1的位数有关,为i&(i-1)中1的位数+1;
- }
- return arr;
- }
- }
371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator +
and -
.
Example:
Given a = 1 and b = 2, return 3
不使用加减法对两数进行相加:
解题思路:使用位操作,异或(^)操作(进行不进位的加法),位与(&)操作进行标记待进位的位置,如a=5=0101,b=7=0111,不进位的加法的结果为a^b=0010,待进位的位置是a&b=0101,初次进位0101<<1=1010,与a^b进行相加又产生进位....如此循环,直到进位为0
- public class Solution {
- public int getSum(int a, int b) {
- int sum = 0;
- int carry = 0;
- do{
- sum = a^b;//相加不进位
- carry = (a&b)<<1;//进位
- a = sum;
- b = carry;
- }while(b!=0);
- return a;
- }
- }
201. Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.
For example, given the range [5, 7], you should return 4.
思路:使用n&(n-1),将n最右边的第一个位数为1的经过该操作后变为0,可减少很多运算;
1)当n&(n-1)=0时,直接返回n=0;
2)当n&(n-1)=m时,直接返回m;
3)其他情况,n&(n-1)<m且不为0,最终的结果就是n&(n-1);
- public class Solution {
- public int rangeBitwiseAnd(int m, int n) {
- while(n>m){
- n = n&(n-1);
- }
- return n;
- }
- }
- public class Solution {
- public int rangeBitwiseAnd(int m, int n) {
- int step = 0;
- while(m!=n){
- m >>= 1;
- n >>= 1;
- step ++;
- }
- return m<<step;
- }
- }
LeetCode之位操作题java的更多相关文章
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- LeetCode第[15]题(Java):3Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c ...
- LeetCode第[16]题(Java):3Sum Closest 标签:Array
题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...
随机推荐
- CH1801 括号画家
题意 Candela是一名漫画家,她有一个奇特的爱好,就是在纸上画括号.这一天,刚刚起床的Candela画了一排括号序列,其中包含小括号( ).中括号[ ]和大括号{ },总长度为N.这排随意绘制的括 ...
- Mysql-Proxy 读写分离的各种坑,特别是复制延迟时
延迟问题读写分离不能回避的问题之一就是延迟,可以考虑Google提供的SemiSyncReplicationDesign补丁. 端口问题MySQL-Proxy缺省使用的是4040端口,如果你想透明的把 ...
- C#中的依赖注入那些事儿
目录 目录 1 IGame游戏公司的故事 1.1 讨论会 1.2 实习生小李的实现方法 1.3 架构师的建议 1.4 小李的小结 2 探究依赖注入 2.1 故事的启迪 2.2 正式定义依赖注入 3 依 ...
- web 前端 html
1,什么是web 在网络中,大量的数据需要有一个载体,而很多人都能够访问这个载体,利用浏览器的这个窗口链接一个有一个载体,这个载体就是网站也就是web的前身. 1,web标准:结构标准,表现标准,行为 ...
- bzoj 4010 [HNOI2015]菜肴制作——贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4010 和 bzoj 2535 差不多.因为当前怎么决策与该点后面连的点的标号情况有关,所以按 ...
- input type=number 禁止输入字符“e”的办法
输入框input,的type设置为number,本想只输入数字,但是字符“e”却能通过, 首先科普一下, <body> <input onkeypress="getCode ...
- linux 下apache 停止、重启等操作
基本的操作方法:本文假设你的apahce安装目录为/usr/local/apache2,这些方法适合任何情况 apahce启动命令:推荐/usr/local/apache2/bin/apachectl ...
- Web API 路由访问设置
前段时间一直致力于MVC webapi 技术的研究,中途也遇到过好多阻碍,特别是api路由的设置和URL的访问形式,所以针对这个问题,特意做出了记录,以供日后有同样困惑的大虾们借鉴: 在Mvc WEB ...
- MSYS2环境搭建
本机环境:Windows XP 32位MSYS2地址:http://sourceforge.net/projects/msys2/ 下载32位版本,地址:http://sourceforge.net/ ...
- ROS HTB限速失败原因分析和需注意事项
要想做限速,必须要知道以下几点: 首先要知道自己要限制什么的速度,谁的速度,于是需要用的标记,即Mangle. 其次要知道怎么限速,是限制上传,还是下载? 最后要知道所做的限速是否成功,即需要知道如何 ...