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 ...
随机推荐
- 揭示同步块索引(中):如何获得对象的HashCode
转自:http://www.cnblogs.com/yuyijq/archive/2009/08/13/1545617.html 题外话:为了尝鲜,也兴冲冲的安装了Win7,不过兴奋之余却郁闷不已,由 ...
- table样式的下拉框(angularjs)
前言 虽然使用的技术比较老了,但是思想却还是适用于现在的vue等框架. 一:实现的样式 二:实现包括的功能点 1:下拉框内容是表格,类似于一个弹窗 表格内容最多六行,超出的显示滚动条,表头固定,可滚动 ...
- php mysql apache字符集(二) (转)
1 MYSQL中的字符集概念 Mysql的字符集里有两个概念,一个是"Character set(字符集)",另一个是"Collations".1.1 Col ...
- 【备忘】mysql主从设置
主(master)192.168.1.10机器设置: [root@vm-vagrant mysql]# vi my.cnf [mysqld]节点下添加以下配置server-id=1log-bin=my ...
- 在同一服务器使用git分支建立线上 和 测试 项目
分支分配文件夹后 sourcetree 创建分支与合并 https://blog.csdn.net/qq_34975710/article/details/74469068 线上分支master 测试 ...
- MOSS 2013研究系列---Win2008R2 建立域控时候,报密码不符合要求解决办法
今天远程给Win2008R2装AD域控的时候,突然报如下的错误页面: 修改了密码,将密码强度设置复杂了,但是,仍然会弹出这个错误页面,估计是因为远程账号的关系,于是再网上搜下了一下,找到了一个解决方案 ...
- "==" 与 “equals”
“==”: “==”或等号操作在Java编程语言中是一个二元操作符,用于比较原生类型和对象.(操作符不支持重载overloading) “==”对比两个对象基于内存引用,如果两个对象的引用完全相同(指 ...
- 1006 Sign In and Sign Out (25)(25 分)思路:普通的时间比较题。。。
1006 Sign In and Sign Out (25)(25 分) At the beginning of every day, the first person who signs in th ...
- Wdatepicker日期控件的使用指南
示例2-3-1 起始日期简单应用 示例2-3-2 alwaysUseStartDate属性应用 示例2-3-3 使用内置参数 示例 2-4-1: 年月日时分秒 示例 2-4-2 时分秒 示例 2-4- ...
- 关于Eclipse中复制粘贴一个项目后的操作
今天在做一个小Demo,内容和之前的项目有些类似就直接复制过来了,项目名修改了,web.xml的项目名也修改了,可是部署到Tomcat之后,以这个新项目名进行访问就会出现404的错误,只可以使用复制之 ...