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 ...
随机推荐
- WPF 使用 WindowChrome,在自定义窗口标题栏的同时最大程度保留原生窗口样式(类似 UWP/Chrome)
WPF 自定义窗口样式有多种方式,不过基本核心实现都是在修改 Win32 窗口样式.然而,Windows 上的应用就应该有 Windows 应用的样子嘛,在保证自定义的同时也能与其他窗口样式保持一致当 ...
- js中怎么去掉数组的空值
for(var i = 0 ;i<array.length;i++) { if(array[i] == "" || typeof(array[i] ...
- 如何将angular-ui-bootstrap的图片轮播组件封装成一个指令
在项目开发中我们经常会遇到图片轮播的功能点: 如果我们开发人员自己原生手写,将会花费很多的时间,最终得不偿失. 接下来就详细说说如何使用angular-ui发热图片轮播模块,并且将它写成一个指令(便于 ...
- 关于ng的过滤器的详细解释angular-filter
在使用ng框架做项目的时候,我们可能会使用到ng自带的过滤器,来进行数据的筛选 一:ng自带的过滤器:currency ,date,limitTo,lowercase,uppercase,number ...
- 【备忘录】Sublime Text编辑器如何在选中的多行行首增加字符串
如题:上面的代码,想在每一行的开头加上一个字符 * 如下: 操作步骤如下: 1.选中要操作的行(我这里Ctrl+A) 2.Ctrl+Shift+L (待操作状态) 3.方向键← (操作这步骤后,可 ...
- 解决Oracle的http://localhost:1158/em页面打不开的问题
https://localhost:1158/em 无法显示页面,在网上查阅资料以后发现这个页面时由服务:OracleDBConsoleoracl控制的,所以到管理界面打开服务:OracleDBCon ...
- 同步机制之--java CyclicBarrier 循环栅栏
CyclicBarrier介绍一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待 ...
- ubuntu16.04编译QT5.6所依赖的库
首先在QT的根目录下,阅读README文件! 里面介绍了ubuntu环境下,编译该版本的QT需要安装的包 New dependencies in Qt 5 ------------------- ...
- 关于Eclipse中复制粘贴一个项目后的操作
今天在做一个小Demo,内容和之前的项目有些类似就直接复制过来了,项目名修改了,web.xml的项目名也修改了,可是部署到Tomcat之后,以这个新项目名进行访问就会出现404的错误,只可以使用复制之 ...
- PHP中的traits简单理解
Traits可以理解为一组能被不同的类都能调用到的方法集合,但Traits不是类!不能被实例化.先来例子看下语法: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...