基础篇(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;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,788评论 0 33
  • 身处洁极思浊念 心摆安息意难牵 佛号除声魂临境 果腹未足精满填 — 默白抒
    默白抒阅读 232评论 0 1
  • 刚下了雨,空气变得非常清新,夜幕已落下,我漫步在一个村庄的街道上。迎面而来一个,西装笔挺,非常富态的一个中年人。仔...
    李冠华阅读 393评论 0 0
  • 有时,我们总是 坐在一段时光里 静静的看着另一段时光 时光吞噬了年华 青春淹没了等待 那里,也由最初的静默 悄悄的...
    猫的解忧杂货店阅读 319评论 0 0
  • 在这周开始连续在简书发文之前,我写了N年的日记,以及N年没什么人看的博客和公众号。日记是写给自己看的,完全不牵涉到...
    紫苑阅读 181评论 4 5