剑指offer、牛客-打印1到最大的n位数

思路:直接使用int进行遍历,int有边界(使用数组模拟,或者使用全排列)

1 数组模拟法


public class Solution {
    public void printToMaxOfNDedits(int n){
        if(n < 0)
            return;
        if (n ==0 ){
            System.out.println("0");
        }
        char [] num = new char[n];
        //初始化
        for (int i = 0; i < num.length; i++) {
            num[i] = '0';
        }
        while(!increment(num)){
            print(num);
        }
    }
    private static boolean increment(char [] num){
        boolean isEnd = false;
        int len = num.length;
        int nTakeOver = 0;//进位
        for (int i = len - 1; i >= 0 ; i--) {
            int nSum = num[i] - '0' + nTakeOver;//当前值
            //只有是个位时,数值加1
            if(i == len - 1)
                nSum++;
            //满10进1
            if (nSum > 9){
                if(i == 0){
                    //超过n位
                    isEnd = true;
                }else {
                    nTakeOver = 1;
                    num[i] = '0';
                }
            }else {
                num[i] = (char) ('0' + nSum);
                break;
            }
        }
        return isEnd;
    }
    private static void print(char [] num){
        StringBuilder str = new StringBuilder();
        boolean isBegin = false;
        int len = num.length;
        for (int i = 0; i < len; i++) {
            if(num[i] != '0' && !isBegin){
                isBegin = true;
            }
            if (isBegin){
                str.append(String.valueOf(num[i]));
            }
        }

        if(str.toString().matches("^9+$")){
            System.out.println(str.toString()+" ");
        } else {
            System.out.print(str.toString()+" ");
        }
    }

}

2 全排列代替法

public class Solution {
    public void printToMaxOfNDedits(int n){
        if(n < 0)
            return;
        if (n == 0 ){
            System.out.println("0");
            return;
        }
        char [] num = new char[n];
        //初始化
        for (int i = 0; i < num.length; i++) {
            num[i] = '0';
        }
        for (int i = 0; i < 10; i++) {
            num[0] = (char) ('0' + i);
            printToMaxOfNDeditsRecurstively(num, n, 0);
        }
    }
    private static void printToMaxOfNDeditsRecurstively(char [] num,int length, int index){
        if(index == length - 1){
            print(num);
            return;
        }
        for (int i = 0; i < 10; i++) {
            num[index + 1] = (char)(i + '0');
            printToMaxOfNDeditsRecurstively(num, length, index + 1);
        }
    }
    private static void print(char [] num){
        StringBuilder str = new StringBuilder();
        boolean isBegin = false;
        int len = num.length;
        for (int i = 0; i < len; i++) {
            if(num[i] != '0' && !isBegin){
                isBegin = true;
            }
            if (isBegin){
                str.append(String.valueOf(num[i]));
            }
        }
        if(str.toString().matches("^9+$")){
            System.out.println(str.toString()+" ");
        } else {
            System.out.print(str.toString()+" ");
        }
    }
}

扩展:char 最多能标识256个字符,十进制数:只用到10个,没有充分利用内存,有更高效的方式吗?有改char为byte

public class Solution {
    public void printToMaxOfNDedits(int n){
        if(n < 0)
            return;
        if (n ==0 ){
            System.out.println("0");
        }
        byte [] num = new byte[n];
        //初始化
        for (int i = 0; i < num.length; i++) {
            num[i] = 0;
        }
        while(!increment(num)){
            print(num);
        }
    }
    private static boolean increment(byte [] num){
        boolean isEnd = false;
        int len = num.length;
        byte nTakeOver = 0;//进位
        for (int i = len - 1; i >= 0 ; i--) {
            byte nSum = (byte) (num[i]  + nTakeOver);//byte相加,需要强转
            //只有是个位时,数值加1
            if(i == len - 1)
                nSum++;
            //满10进1
            if (nSum > 9){
                if(i == 0){
                    //超过n位
                    isEnd = true;
                }else {
                    nTakeOver = 1;
                    num[i] = 0;
                }
            }else {
                num[i] = nSum;
                break;
            }
        }
        return isEnd;
    }
    private static void print(byte [] num){
        StringBuilder str = new StringBuilder();
        boolean isBegin = false;
        int len = num.length;
        for (int i = 0; i < len; i++) {
            if(num[i] != 0 && !isBegin){
                isBegin = true;
            }
            if (isBegin){
                str.append(String.valueOf(num[i]));
            }
        }

        if(str.toString().matches("^9+$")){
            System.out.println(str.toString()+" ");
        } else {
            System.out.print(str.toString()+" ");
        }
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值