Bit Manipulation 位操作

注意的几点:

  1. java中计算一个数表示成二进制后中1的个数 调用函数 Integer.countBit(n)
  2. 位操作包括:与,或,非,同或,异或,左移位,右移位
  3. 位操作是将数转换为二进制后分别对每一位做的操作,不是对单独的一个数(int)做的操作,在做题的时候不要被混淆蒙蔽了。位操作中只有0和1。
  4. 位操作的优先级较低,比如说要小于 == ,因此,使用的时候尽量加括号使用
  5. a&0=0; a&a=a;  a|0=a;  a|a=a;  a^a=0;   a^0=a; 
  6. 与是有0就取0,或是有1就取1

190. Reverse Bits

Reverse bits of a given 32 bits unsigned integer.

Example:

Input: 43261596
Output: 964176192
Explanation: 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?


public class Solution {
    // you need treat n as an unsigned value
    public int reverseBits(int n) {
        int res=0;
        for(int i=0;i<32;i++){
            //如果最后一位是1,那么将res右移一位加一
            //如果是0,将res左移一位
            //将n右移一位
            if((n&1)==1){
                //在java中,==的优先级要高于位运算符(&,|,^,~)
                res=(res<<1)+1;
            }
            else{
                res=res<<1;
            }
            n=n>>1;
        }
        return res;
    }
}


136. Single Number

Given a non-empty 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?

Example 1:

Input: [2,2,1]
Output: 1

Example 2:

Input: [4,1,2,1,2]
Output: 4

题目要求找出数组中只出现过一次的数,首先想到的是将整体进行排序,排序之后出现一次或两次一目了然。

另一种操作是通过位操作来得到答案。

异或:相同为0,相异为1

class Solution {
    public int singleNumber(int[] nums) {
        //sort
        // Arrays.sort(nums);
        // for(int i=0;i<nums.length-1;i=i+2){
        //     if((nums[i]^nums[i+1])!=0)
        //         return nums[i];
        // }
        // //如果前面的排序之后都是相等的的话,那么就返回最后一个的值
        // return nums[nums.length-1];
        
        //bit manipulation
        int ans=0;
        for(int i=0;i<nums.length;i++){
            ans=(ans^nums[i]);
        }
        return ans;
    }
}


78. Subsets

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

一个类似排列组合的内容,很容易联想到是这么长的数字的二进制的表现形式,出现为1,不出现为0

class Solution {
    public List<Integer> getSubset(int n,int[] nums){
        List res=new ArrayList<Integer>();
        int i=nums.length-1;
        while(i>=0){
            if((n&1)==1){//表示末位为1
                //int pos=nums.length-i-1;
                res.add(nums[i]);
            }
            i--;
            n=n>>1;
        }
        return res;
    }
    public List<List<Integer>> subsets(int[] nums) {
        int len=nums.length;
        List<List<Integer>> ans=new ArrayList<List<Integer>>();
        for(int j=0;j<Math.pow(2,len);j++){
            ans.add(getSubset(j,nums));
        }
        return ans;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值