Leetcode算法题-位运算

201. 数字范围按位与

力扣

public int rangeBitwiseAnd(int m, int n) {
        int shift = 0;
        while (m < n) {
            m >>= 1;
            n >>= 1;
            shift++;
        }
        return m <<= shift;
    }

338. 比特位计数

力扣

class Solution {
    public int[] countBits(int n) {
        int[] res = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            res[i] = Integer.bitCount(i);
        }
        return res;
    }
}

##############自己利用位运算实现bitCount方法##################
class Solution {
    public int[] countBits(int n) {
        int[] res = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            res[i] = bitCount(i);
        }
        return res;
    }
    
    public int bitCount(int i) {
        int res = 0;
        while (i > 0) {
            i &= i - 1;
            res++;
        }
        return res;
    }
}

####################动态规划思想加位运算####################
class Solution {
    public int[] countBits(int n) {
        int[] res = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            res[i] = res[i & (i - 1)] + 1;
        }
        return res;
    }
}

面试题 05.01. 插入

力扣

public int insertBits(int N, int M, int i, int j) {
        int mask = ((1 << (j - i + 1)) - 1) << i;
        mask = ~mask;
        N &= mask;
        M <<= i;
        return N | M;
    }

面试题 05.02. 二进制数转字符串

力扣

class Solution {
    public String printBin(double num) {
        StringBuilder sb = new StringBuilder("0.");
        for (int i = 1; i < 31 && num > 0; i++) {
            double n = Math.pow(0.5, i);
            if (num >= n) {
                num -= n;
                sb.append(1);
            }  else sb.append(0);
        }
        return num == 0 ? sb.toString() : "ERROR";
    }
}

面试题 05.03. 翻转数位

力扣

class Solution {
    public int reverseBits(int num) {
        if (num == -1) return 32;
        int cnt1 = 1;
        int pos = -1;
        int result = 0;
        for (int i = 0; i < 33; i++) {
            if ((num & 1) == 1) {
                cnt1++;
            } else {
                result = Math.max(result, cnt1);
                cnt1 = i - pos;
                pos = i;
            }
            num >>>= 1;
        }

        return result;
    }
}

面试题 05.06. 整数转换

力扣

class Solution {
    public int convertInteger(int A, int B) {
        return Integer.bitCount(A^B);
    }
}

面试题 05.07. 配对交换

力扣

class Solution {
    public int exchangeBits(int num) {
        int odd = num & 0x55555555;
        int even = num & 0xaaaaaaaa;
        odd <<= 1;even >>>= 1;
        return odd | even;
    }
}

面试题 08.01. 三步问题

力扣

class Solution {
    public int waysToStep(int n) {
        if (n  < 3) return n;
        int[] dp = new int[n + 1];
        dp[0] = 1;dp[1] = 1;dp[2] = 2;
        for (int i = 3; i <= n; i++) {
            dp[i] = ((dp[i - 1] + dp[i - 2]) % 1000000007 + dp[i - 3]) % 1000000007;
        }
        return dp[n];
    }
}

class Solution {
    public int waysToStep(int n) {
        int a = 1, b = 2, c = 4;
        int MAX = 1000000007;
        if (n == 1) return a;
        if (n == 2) return b;
        if (n == 3) return c;
        for (int i = 3; i < n; i++) {
            int temp = ((a + b) % 1000000007 + c) % 1000000007;
            a = b;
            b = c;
            c = temp;
        }
        return c;
    }
}

面试题 08.05. 递归乘法

力扣

class Solution {
    public int multiply(int A, int B) {
        int min = Math.min(A, B);
        int max = Math.max(A, B);
        int ans = 0;

        for (int i = 0; min != 0; i++) {
            if ((min & 1) == 1) {
                ans += max << i;
            }
            min >>= 1;
        }

        return ans;
    }
}

面试题 17.04. 消失的数字

力扣

public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] arr = {0};
        System.out.println(solution.missingNumber(arr));
    }

    //数学方法
    public int missingNumber(int[] nums) {
        int n = nums.length;
        Long sum = 0L;
        for (int i = 0; i < n; i++) {
             sum += nums[i];
        }
        return (int) (n * (n + 1) / 2 - sum);
    }

    //空间换时间
    public int missingNumber2(int[] nums) {
        boolean[] a=new boolean[nums.length+1];
        for(int i=0;i<nums.length;i++){
            a[nums[i]]=true;
        }
        for(int i=0;i<a.length;i++){
            if(a[i] == false){
                return i;
            }
        }
        return 0;
    }

    //异或运算a ^b ^ b = a, a ^ b ^a = b;
    public int missingNumber3(int[] nums) {
        int n = nums.length;
        int ans = 0;
        for (int i = 1; i <= n; i++) {
             ans ^= i;
             ans ^= nums[i - 1];
        }
        return ans;
    }
}

面试题 17.01. 不用加号的加法

力扣

class Solution {
    public int add(int a, int b) {
        int m = a ^ b; //不进位的结果
        int n = (a & b) << 1; //进位
        while (n != 0) {
            int temp = m ^ n;
            n = (m & n) << 1;
            m = temp;
        }
        return m;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值