打印从1到n位数(防止数据溢出)

 1 package LeetCode;
 2 
 3 /*打印N位最大数据*/
 4 public class PrintToMaxOfDigits {
 5 
 6     public static void main(String[] args) throws Exception {
 7         // TODO Auto-generated method stub
 8         PrintToMaxData(0);
 9     }
10     public static void PrintToMaxData(int n) throws Exception
11     {
12         if (n<=0)
13         {
14             System.out.println("输入的值不符合要求");
15             throw new Exception("输入值不符合题意");
16             
17         }
18         char numbel[]=new char[n+1];
19         
20         numbel[n]='';
21         
22         for(int i=0;i<10;i++)
23         {
24             numbel[0]=(char) ('0'+i);
25             PrintTo(numbel,n,0);
26         }
27     }
28     
29     public static void PrintTo(char array[],int length,int index)
30     {
31         //进行每一位的全排列
32         if(index==length-1)
33         {
34             PrintNum(array);
35             return;
36         }
37         
38         for(int i=0;i<10;i++)
39         {
40             array[index+1]=(char) (i+'0');
41             PrintTo(array,length,index+1);
42         }
43     }
44     public static void PrintNum(char numbel[])
45     {
46         boolean  isBegining=true;
47         int nLength=numbel.length;
48         
49         //循环找到非零的位置进行打印
50         for(int i=0;i<nLength;i++)
51         {
52             //通过
53             if(isBegining && numbel[i]!='0')
54                 isBegining=false;
55             if(isBegining==false)
56                 System.out.print(numbel[i]);
57         }
58     }
59 }

对全排列数组进行递归法

原文地址:https://www.cnblogs.com/woainifanfan/p/6567697.html