剑指Offer(Java实现):剪绳子 + 二进制中1的个数

package com.dengzm.jianzhioffer;

/**
 * @Description 014 剪绳子
 * 给你一根长度为n的绳子,请把绳子剪成m段(m,n都是整数,n>1并且m>1),每段绳子的长度记为k[0],k[1],...,k[m]。
 * 请问k[0]*k[1]*...*k[m]可能的最大乘积是多少?
 * Created by deng on 2018/12/20.
 */
public class Jianzhi014 {
    public static void main(String[] args) {
        System.out.println(maxProductAfterCut1(8));
        System.out.println(maxProductAfterCut1(18));
        System.out.println(maxProductAfterCut2(8));
        System.out.println(maxProductAfterCut2(18));
    }

    //方法1:动态规划
    //在剪一刀的时候,我们有n-1种选择,即f(n)=max(f(i)*f(n-i))。
    //我们从下至上进行计算,算出每个长度对应的能达到的最大乘积是多少,最后计算出结果
    public static int maxProductAfterCut1(int length) {
        if (length < 2) {
            return 0;
        }
        if (length == 2) {
            return 1;
        }
        if (length == 3) {
            return 2;
        }
        int[] products = new int[length + 1];
        products[0] = 0;
        products[1] = 1;
        products[2] = 2;
        products[3] = 3;

        int max;

        for (int i = 4; i < length + 1; i ++) {
            max = 0;
            for (int j = 1; j <= i / 2; j ++) {
                int product = products[j] * products[i - j];
                if (max < product) {
                    max = product;
                }
            }
            products[i] = max;
        }
        return products[length];
    }

    //方法2:贪婪算法
    //当n>=5时,我们尽可能的多剪长度为3的绳子;当剩下的绳子长度为4时,把绳子剪成两段长度为2的绳子。
    //证明:当n>=5时,2(n-2)>n,3(n-3)>n,且3(n-3)>=2(n-2),因此应该尽可能剪长度为3的绳子。
    //而长度为4时,2*2>3*1,应剪为两段为2的绳子。
    public static int maxProductAfterCut2(int length) {
        if (length < 2) {
            return 0;
        }
        if (length == 2) {
            return 1;
        }
        if (length == 3) {
            return 2;
        }
        //尽可能多的剪去长度为3的绳子
        int timesOf3 = length / 3;
        //当绳子最后长度为4的时候,剪为2*2
        if (length - timesOf3 * 3 == 1) {
            timesOf3 --;
        }
        int timesOf2 = (length - timesOf3 * 3) / 2;
        return (int) (Math.pow(3, timesOf3) * Math.pow(2, timesOf2));
    }
}

package com.dengzm.jianzhioffer;

/**
 * @Description 015 二进制中1的个数
 * 请实现一个函数,输入一个整数,输出该数二进制表示中1的个数。
 * Created by deng on 2018/12/20.
 */
public class Jianzhi015 {
    public static void main(String[] args) {
        System.out.println(numberOf1_1(100));
        System.out.println(numberOf1_1(-100));
        System.out.println(numberOf1_2(100));
        System.out.println(numberOf1_2(-100));
        System.out.println(Integer.toBinaryString(-100));
    }

    //解法1:考虑到有负数的情况,如果对原数字进行右移,会在高位产生1;故使用flag=1并左移,共进行32次循环
    public static int numberOf1_1(int num) {
        int count = 0;
        int flag = 1;
        while (flag != 0) {
            if ((num & flag) != 0) {
                count ++;
            }
            flag = flag << 1;
        }
        return count;
    }

    //解法2:低位为1时,减1变为0;低位为0时,减1使得最近的一个为1的高位变为0,此位以下的低位均变为1。
    //此时进行&操作,可以消去原数中的一个1
    public static int numberOf1_2(int num) {
        int count = 0;
        while (num != 0) {
            count ++;
            num = num & (num - 1);
        }
        return count;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值