1算法基础

二进制

package algorithm.basics;

/**
 * 十进制转32位二进制
 */
public class NumTo32bit {

    private static void to32bit(int num){
        for (int i = 31; i >= 0 ; i--) {
            System.out.print((num & (1 << i)) == 0 ? "0" : "1");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        // 32位
        to32bit(1);
        to32bit(7);
        // 负数:取反~+1
        to32bit(-1); // 11111111111111111111111111111111

        // 右移 >>(带符号位)、>>>
        to32bit(Integer.MIN_VALUE); // 10000000000000000000000000000000
        to32bit(Integer.MIN_VALUE>>1); // 11000000000000000000000000000000
        to32bit(Integer.MIN_VALUE>>>1); //01000000000000000000000000000000

        // a=-b等价于a=~b+1
        int b = 123;
        to32bit(b);  // 00000000000000000000000001111011
        to32bit(-b); // 11111111111111111111111110000101
        to32bit(~b+1); // 11111111111111111111111110000101

        // 0、Integer.MIN_VALUE 取反+1 不变
        to32bit(~Integer.MIN_VALUE+1);
    }
}

位运算

| 或
& 与
^ 异或
~ 非
<< 左移
>> 带符号右移
>>> 不带符号右移
-a=~a+1

数据结构

数组:易寻址,不易增删

链表:易增删,不易寻址

// 前缀和
package algorithm.basics;

/**
 * 前缀和:求数组指定区间的和
 */
public class PreSum {

    /**
     * 遍历
     */
    public static class RangeSum1{
        private int[] arr;

        public RangeSum1(int[] array){
            arr = array;
        }

        public int rangeSum(int l, int r){
            int sum = 0;
            for (int i=l; i<=r; i++){
                sum += arr[i];
            }
            return sum;
        }

    }

    /**
     * 前缀和
     */
    public static class RangeSum2{
        // 前缀和数组
        private int[] preSum;

        public RangeSum2(int[] array){
            int len = array.length;
            preSum = new int[len];
            preSum[0] = array[0];
            for (int i = 1; i < len; i++) {
                preSum[i] = preSum[i-1] + array[i];
            }
        }

        public int rangeSum(int l, int r){
            return l == 0 ? preSum[r] : preSum[r] - preSum[l-1];
        }
    }

}

随机函数

package algorithm.basics;

public class Random {

    public static void main(String[] args) {
        int num = 1000000;
        // Math.random() -> double -> [0,1)
        double random = Math.random();

        // x^2
        Math.max(Math.random(), Math.random());
        // 1-(1-x)^2 = 2x-x^2
        Math.min(Math.random(), Math.random());

        // 不修改f1(), 1-5等概率随机 -> 1-7等概率随机
        g();

        // a-b等概率随机 -> c-d等概率随机


        // 01不等概率x() -> 01等概率y()
        int count = 0;
        for (int i = 0; i < 1000000; i++) {
            if (x()==1){
                count++;
            }
        }
        System.out.println((double)count/(double)num);
        System.out.println("==========");
        count = 0;
        for (int i = 0; i < 1000000; i++) {
            if (y1()==1){
                count++;
            }
        }
        System.out.println((double)count/(double)num);
    }

    // 1-5等概率随机,不可修改
    public static int f1(){
        return (int)(Math.random()*5 +1);
    }

    // 0-1等概率随机
    public static int f2(){
        int ans = 0;
        do {
            ans = f1();
        }while(f1() == 3);
        return ans > 3 ? 1 : 0;
    }

    // 0-7等概率随机
    public static int f3(){
        return (f2()>>2) + (f2()>>1) + f2();
    }

    // 1-7等概率随机
    public static int g(){
        int ans = 0;
        do {
            ans = f3();
        }while (ans == 0);
        return ans;
    }

    // 01不等概率
    public static int x(){
        return Math.random() > 0.8 ? 1 : 0;
    }

    // 01等概率
    public static int y1(){
        int i = 0;
        do {
            i = (x()<<1) + x();
        }while (i==3 || i==0);
        return i == 1 ? 0 : 1;
    }
    // 01等概率,更优解
    public static int y2(){
        int i = 0;
        do {
            i = x();
        }while (i==x());
        // i 0 1
        // i 1 0
        return i;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值