"Prove safety offer" face questions 1 to 12 prints maximum number of bits n Java version

The book Methods: This question is a trap that can not use int or long to store the number you want to print, and then use the print function to print, because this number may be quite large. If adding n-1 exceeds the maximum number of digits, do not print. Carry with the highest level is to determine whether the end, when printing careful not to print out 0 front that may arise.

    public void print(int n){
        if(n<=0){
            return;
        }
        //必须要用字符数组防止大数
        char[] c = new char[n];
        for(int i=0; i<n; i++){
            c[i] = '0';
        }
        
        while(!increment(c)){
            digitsPrint(c);
            
        }
    }
    
    private boolean increment(char[] c){
        boolean overflow = false;
        //因为是加1,当作从小数进位了1.
        int carry = 1;
        //从个位开始计算
        for(int i=c.length-1; i>=0; i--){
            int sum = c[i] - '0' + carry;
            //如果进位了
            if(sum == 10){
                if(i == 0){
                    overflow = true;
                    break;
                }
                carry = 1;
                c[i] = '0';
            }else{//如果没进位
                c[i] = (char)('0' + sum);
                break;
            }
        }
        return overflow;
    }
    
    private void digitsPrint(char[] c){
        int index = 0;
        while(c[index] == '0'){
            index++;
        }
        for(int i=index; i<c.length; i++){
            System.out.print(c[i]);
        }
        System.out.println();
    }

My approach: see "all" and "print" this keyword, it is easy to think of methods to do with backtracking, because the essence of all combinations of the above method is determined to be able to traverse all combinations. And seeking the whole string of different permutation and combination (Subject 28), where characters can be reused (also think within the n-bit digital printing will not be repeated here ... integer not expand, for later). Recall that when we print the full array of strings, because each character can only be used once, so we need to find ways to ensure that each character is selected only once (with an additional array or exchange). Now we simply need to select the possible values ​​on each seat, then recursively go on the line. Outer control loop is a length, an inner layer selected for every digital, each bit of the '0' - '9' may be selected character (except the first one).

    public void print2(int n){
        if(n <= 0)return;
        
        List<String> result = new ArrayList<String>();
        String line = "";
        
        //用n控制位数
        for(int i=1; i<=n; i++){
            find(result, line, 0, i);
        }
        
        for(String s : result){
            System.out.println(s);
        }
        
        
    }
    
    private void find(List<String> result, String line, int level, int border){
        //每一位添加完毕后保存
        if(level >= border){
            result.add(new String(line));
            return;
        }
                //用户保护原始的line,即保护现场(可百度这个关键词)
        String temp = new String(line);
        for(int i=0; i<=9; i++){
            //第一位不能为0
            if(level == 0 && i == 0){
                continue;
            }else{
                                //当前位添加
                line += i;
                                //继续添加下一位
                find(result, line, level+1, border);
                //让line恢复,进入下次for循环
                                line = temp;
            }
        }
    }

Guess you like

Origin www.cnblogs.com/czjk/p/11611395.html