191. Number of 1 Bits

Total Accepted: 87985 Total Submissions: 234407 Difficulty: Easy

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

Total Accepted: 60957 Total Submissions: 208165 Difficulty: Easy

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

Total Accepted: 17636 Total Submissions: 31865 Difficulty: Medium

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的更多相关文章

  1. 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 + ...

  2. 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 ...

  3. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  4. 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 ...

  5. 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 ...

  6. LeetCode第[29]题(Java):Divide Two Integers

    题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...

  7. 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 ...

  8. 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  ...

  9. 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 ...

随机推荐

  1. (1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序

    每次使用 Visual Studio 的模板创建一个 UWP 程序,我们会在项目中发现大量的项目文件.配置.应用启动流程代码和界面代码.然而这些文件在 UWP 程序中到底是如何工作起来的? 我从零开始 ...

  2. Tornado之实例和扩展

    1.Tornado文件的结构: 1.Controllers控制器 2.Models数据库操作 3.Views前端显示 2.样例 #!/usr/bin/env python # -*- coding: ...

  3. Gridview实现突出显示某一单元格的方法

    GridView突出显示某一单元格(例如金额低于多少,分数不及格等) 效果图: 解决方案:主要是绑定后过滤 GridView1.DataBind();        for (int i = 0; i ...

  4. CH1802 表达式计算4

    题意 给出一个表达式,其中运算符仅包含+,-,*,/,^(加 减 乘 整除 乘方)要求求出表达式的最终值 数据可能会出现括号情况,还有可能出现多余括号情况 数据保证不会出现>=2^31的答案 数 ...

  5. 使用distillery 实现版本的动态升级&& 动态降级

    备注: distillery  使用很棒的elixir 打包构建工具,下面演示的是升级以及降级   1. 参考项目 https://github.com/rongfengliang/phoenix-r ...

  6. 【转】Linux export 命令

    原文网址:http://mymobile.iteye.com/blog/1407601 Linux export 命令 功能说明: 设置或显示环境变量.(比如我们要用一个命令,但这个命令的执行文件不在 ...

  7. jdk1.8新特性应用之Iterable

    我们继续看lambda表达式的应用: public void urlExcuAspect(RpcController controller, Message request, RpcCallback ...

  8. 本地Office Project计划表同步到SharePoint2013任务列表的权限问题

    使用SharePoint做项目管理时,项目任务列表往往比较重要,通常使用任务列表体现项目计划,而这个任务列表经常会根据项目计划的变更而进行调整,但更多时候项目管理者会习惯在本地Project中维护这份 ...

  9. VB.NET实现32位、64位远线程运行ASM,注入非托管、托管DLL

    这是一个老话题,远线程函数给我们提供了机会在其他进程中启动一个新线程,所以我们可以做很多事情.但事情远远没有结束,如果我们要做的事情非常复杂,那么将面临编写大量的ASM代码,虽然我们可以用VC之类的工 ...

  10. Android ListView的item背景色设置

    1.如何改变item的背景色和按下颜色 listview默认情况下,item的背景色是黑色,在用户点击时是黄色的.如果需要修改为自定义的背景颜色,一般情况下有三种方法: 1)设置listSelecto ...