基础篇(6)LeetCode--CHAPTER 5. BIT MANIPULATION

Unit 1 Practice I

LeetCode 136 Single Number

Given an array of integers, every element appears twice except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Use bitwise XOR to solve this problem :

First , the rule bitwise XOR in java is:

0 ^ N = N
N ^ N = 0

So, if N is the single number, then

N1 ^ N1 ^ N2 ^ N2 .............. Nx ^ Nx ^ N

= (N1^N1) ^ (N2^N2) .............. (Nx^Nx) ^ N

= 0 ^ 0 ^ ..........^ 0 ^ N

= N

public int singleNumber(int[] nums) {
    int singleNumber = 0;
    for (int i = 0; i < nums.length; i++) {
        singleNumber ^= nums[i];
    }
    return singleNumber;
}

Unit 2 Practice II

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

Follow up:
If this function is called many times, how would you optimize it?

Java提供的位运算符有:左移( << )、右移( >> ) 、无符号右移( >>> ) 、位与( & ) 、位或( | )、位非( ~ )、位异或( ^ ),除了位非( ~ )是一元操作符外,其它的都是二元操作符。
<< 左移,各二进位全部左移若干位,高位丢弃,低位补0
“>>” “>>>” 右移/无符号右移,各二进位全部右移若干位,对无符号数,高位补9,有符号数,各编译器处理方法不一样,有的补符号位(算术右移),有的补0(逻辑右移)

& 与,两个位都为1时,结果才为1
| 或,两个位都为0时,结果才为0
~ 非,取反,0变1,1变0
^ 异或两个位相同为0,相异为1

public class Solution {
    // you need treat n as an unsigned value
    public int reverseBits(int n) {
        int result = 0;
        for (int i = 0; i < 32; i++, n>>=1) {
            result += n & 1;
            if (i < 31) // CATCH: for last digit, don't shift!
                result <<= 1;
        }
        return result;
    }
}

Follow up: haven't finished.

Unit 3 Practice III

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

Iterate over 32 bits since it's a 32-bit integer.
This will be O(1) since it is in constant time.
Left shift the number by i to get the LSB(Least Significant Bit最低有效位) value
Do an AND of the number obtained from step 2 with 1. If the result of the AND is 1 then increment the count because the LSB value of that bit was 1.

public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        int count = 0;
        for(int i=0; i<32; i++){
            count += (n >> i & 1) == 1 ? 1: 0;
        }
        return count;
    }
}

Unit 4 Practice IV

LeetCode 231 Power of Two
Given an integer, write a function to determine if it is a power of two.

如果一个整数是2的幂,那么它的二进制形式最高位为1,其余各位为0
等价于:n & (n - 1) = 0,且n > 0

public class Solution {
    public boolean isPowerOfTwo(int n) {
        if(n<=0) {
            return false;
        }
        return (n & (n-1)) == 0;
    }
}

你可能感兴趣的:(基础篇(6)LeetCode--CHAPTER 5. BIT MANIPULATION)