Offer fluent wins the series - to print a maximum number of bits n

Interview questions 16: 1 to print the maximum number of bits n

I. Description of the problem

Enter the number n, in order to print out a decimal from 1 to the maximum n-bit number. For example, input 3, then print out up to a maximum of 1, 2, 3 digits, ie 999.

Second, the problem analysis

A look at this problem is very simple thing, many people do not go through careful analysis, a write up, wrong. If in the interview, made such a mistake, not rigorous attitude will leave an impression.

n did not specify how many digits, indicating there may be a phenomenon of the presence of large numbers, that time can not simply use int or long data output. We have to consider the use of characters.
Considering the array of characters is more flexible than the string, it may be a character array as the starting point for analysis. Our ultimate goal is an array of char by the character [] to digital output.

Easier way is to think of

  1. The recursive arrangement each bit from 0 to 9
  2. The digital representation of the printed string

But some friends should contact 在字符串上模拟数字加法face questions, solving the problem in detail can be used here

  1. In the digital representation of the analog addition string;
  2. The digital representation of the string printed.

Here are two ways to achieve it are carried out.

Third, questions

method one:

    public void printToMaxOfNDights1s(int n) {
        
        if(n<=0) {
            return;
        }
        char[] digit = new char[n];
        for(int i=0;i<n;i++) {
            digit[i]='0';
        }
        for(int i=n-1;i>=0;i--) {
            while(digit[i]!='9') {
                int m=0;
                digit[m]++;
                while(m<n-1 && digit[m]>'9') {
                    digit[m]='0';
                    digit[m+1]++;
                    m++;
                }
                printdigits(digit);
            }
        }
    }

    private void printdigits(char[] digit) {
        int m = digit.length-1;
        while(digit[m]=='0') {
            m--;
        }
        
        for(int i=m;i>=0;i--) {
            System.out.print(digit[i]);
        }
        System.out.println();
    }

Method Two:

    public void printToMaxOfNDigits(int n) {
       
        if (n <= 0) {
            return;
        }
        char[] number = new char[n];
        // 不能用foreach方法对nuber[]赋值
        // for (char c : number) {
        // c = '0';
        // }
        for (int k = 0; k < number.length; k++) {
            number[k] = '0';
        }
        while (!increment(number)) {
            printCharNumber(number);
        }
    }

    /**
     * 对字符串进行加一操作,number达到最大值后返回true
     * 最低位加一;所有位如果超过10,则进位
     */
    private boolean increment(char[] number) {
        int nTakeOver = 0; // 代表进位
        for (int i = number.length - 1; i >= 0; i--) {
            int nSum = (number[i] - '0') + nTakeOver; // 当前位置数字
            // number[i]-'0'是把char转化为int,nTakeOver代表进位
            if (i == number.length - 1) {
                nSum++;
            }
            if (nSum >= 10) {
                if (i == 0) {
                    return true; // 超出范围了
                }
                nTakeOver = 1;
                nSum -= 10;
                number[i] = (char) (nSum + '0');
            } else {
                number[i] = (char) (nSum + '0');
                break; // 高位不变,可以直接跳出循环了
            }
        }
        return false;
    }

    /**
     * 打印字符数组形成的数字
     * 书中方法:利用布尔变量isBeginning0来从第一个非零字符打印
     */
    private void printCharNumber(char[] number) {
        boolean isBeginning0 = true;
        for (int i = 0; i < number.length; i++) {
            if (isBeginning0 && (number[i] - '0') != 0) {
                isBeginning0 = false;
            }
            if (!isBeginning0) {
                // System.out.print(number[i] - '0');
                System.out.print(number[i]);
            }
        }
        System.out.println();
    }
Published 134 original articles · won praise 3084 · Views 360,000 +

Guess you like

Origin blog.csdn.net/qq_42322103/article/details/104040155