LeetCode之位操作题java

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;
    }
}

 

posted @ 2016-04-26 15:46  sunny_2015  阅读(309)  评论(0编辑  收藏  举报