Java 给定整数n,按字典顺序返回1-n

Java 给定整数n,按字典顺序返回1-n,java,depth-first-search,lexicographic-ordering,Java,Depth First Search,Lexicographic Ordering,例如,例如,给定13,返回:[1,10,11,12,13,2,3,4,5,6,7,8,9] 下面的解决方案实际上非常有效。我在挣扎了一段时间后找到了它,我不明白它到底是怎么工作的。这看起来像是纯粹的魔法。当我们进行递归调用时,start参数在第二次递归后仍然是10吗 public static ArrayList<Integer> lexicalOrder(int n) { ArrayList<Integer> result = new ArrayList<

例如,
例如,给定13,返回:[1,10,11,12,13,2,3,4,5,6,7,8,9]

下面的解决方案实际上非常有效。我在挣扎了一段时间后找到了它,我不明白它到底是怎么工作的。这看起来像是纯粹的魔法。当我们进行递归调用时,
start
参数在第二次递归后仍然是10吗

public static ArrayList<Integer> lexicalOrder(int n) {
    ArrayList<Integer> result = new ArrayList<>();
    dfs(1, 9, n, result);
    return result;
}
private static void dfs(int start, int end, int n, ArrayList<Integer> result){
    for (int i = start; i <= end && i <= n; i++){
        result.add(i);
       //Just printing out the end and start to try to understand
        System.out.println("Start : " + start + " End : " + end);
       //If i is 10, how does 10 * 10 end up as 10 again for several iterations???
        dfs(i * 10, i * 10 + 9, n, result);
    }
}
publicstaticarraylistlexicalorder(intn){
ArrayList结果=新建ArrayList();
dfs(1,9,n,结果);
返回结果;
}
私有静态void dfs(int start、int end、int n、ArrayList result){

对于(int i=start;i来说,它非常简单。既有递归又有循环。因此,对dfs(1,9,13)的第一次调用实际上是这样做的:

add 1 to result and call dfs (10,19,13), 
add 2 to result and call dfs (20,29,13)
... 
add 9 to result and call dfs (90,99,13).
add 10 to result and call dfs (100, 109, 13)
add 11 to result and call dfs (110, 119, 13)
add 12 to result and call dfs (120, 129, 13)
add 13 to result and call dfs (130, 139, 13)
实际上,只有对dfs(10,19,13)的调用才起作用,因为在所有其他情况下,前两个参数都大于第三个参数

对dfs(10,19,13)的调用执行以下操作:

add 1 to result and call dfs (10,19,13), 
add 2 to result and call dfs (20,29,13)
... 
add 9 to result and call dfs (90,99,13).
add 10 to result and call dfs (100, 109, 13)
add 11 to result and call dfs (110, 119, 13)
add 12 to result and call dfs (120, 129, 13)
add 13 to result and call dfs (130, 139, 13)
然后它终止,因为14大于13


您看到的开始和结束返回到10和13的行为,只是第二组递归调用终止并返回到封闭循环的结果。

非常简单。您既有递归又有循环。因此,对dfs(1,9,13)的第一次调用实际上是这样做的:

add 1 to result and call dfs (10,19,13), 
add 2 to result and call dfs (20,29,13)
... 
add 9 to result and call dfs (90,99,13).
add 10 to result and call dfs (100, 109, 13)
add 11 to result and call dfs (110, 119, 13)
add 12 to result and call dfs (120, 129, 13)
add 13 to result and call dfs (130, 139, 13)
实际上,只有对dfs(10,19,13)的调用才起作用,因为在所有其他情况下,前两个参数都大于第三个参数

对dfs(10,19,13)的调用执行以下操作:

add 1 to result and call dfs (10,19,13), 
add 2 to result and call dfs (20,29,13)
... 
add 9 to result and call dfs (90,99,13).
add 10 to result and call dfs (100, 109, 13)
add 11 to result and call dfs (110, 119, 13)
add 12 to result and call dfs (120, 129, 13)
add 13 to result and call dfs (130, 139, 13)
然后它终止,因为14大于13


您看到的开始和结束返回到10和13的行为,仅仅是第二组递归调用终止并返回到封闭循环的结果。

基本上它所做的是转到某个数字,并将数字0到9附加到该数字,然后转到这些数字,并将0到9附加到该数字,跳过over大于N的数字(本例中为13)

这里有几个步骤

通过查看以左为中心的“i”,可能更容易看到发生了什么

"i"                                         return
1   //Add to list                           {1}
10  //Add to list                           {1,10}
100 //Bigger than n! (n = 13)               {1,10}
11  //Add to list                           {1,10,11}
110 //Bigger than n! (n = 13)               {1,10,11}
12  //Add to list                           {1,10,11,12}
120 //Bigger than n! (n = 13)               {1,10,11,12}
13  //Add to list                           {1,10,11,12,13}
130 //Bigger than n! (n = 13)               {1,10,11,12,13}
14  //Bigger than n! (n = 13)               {1,10,11,12,13}
2   //Add to list                           {1,10,11,12,13,2}
20  //Bigger than n! (n = 13)               {1,10,11,12,13,2}
3   //Add to list                           {1,10,11,12,13,2,3}
30  //Bigger than n! (n = 13)               {1,10,11,12,13,2,3}
4   //Add to list                           {1,10,11,12,13,2,3,4}
40  //Bigger than n! (n = 13)               {1,10,11,12,13,2,3,4}
5   //Add to list                           {1,10,11,12,13,2,3,4,5}
50  //Bigger than n! (n = 13)               {1,10,11,12,13,2,3,4,5}
6   //Add to list                           {1,10,11,12,13,2,3,4,5,6}
60  //Bigger than n! (n = 13)               {1,10,11,12,13,2,3,4,5,6}
7   //Add to list                           {1,10,11,12,13,2,3,4,5,6,7}
70  //Bigger than n! (n = 13)               {1,10,11,12,13,2,3,4,5,6,7}
8   //Add to list                           {1,10,11,12,13,2,3,4,5,6,7,8}
80  //Bigger than n! (n = 13)               {1,10,11,12,13,2,3,4,5,6,7,8}
9   //Add to list                           {1,10,11,12,13,2,3,4,5,6,7,8,9}
90  //Bigger than n! (n = 13)               {1,10,11,12,13,2,3,4,5,6,7,8,9}
10  //Bigger than end! (end = 9)            {1,10,11,12,13,2,3,4,5,6,7,8,9}
正在发生的事情的更完整版本:

lexicalOrder(13)
    result = {}
    dfs(1,9,13,result)  //1 is the smallest digit, 9 is the largest digit,
                        //13 is the largest possible value,
                        //Passed in "result" array to be edited.
        i = start
        //i = 1
        Enter Loop
        result.add(1)
        //result = {1}
        dfs(10,19,13,result)
            i = start
            //i = 10
            Enter Loop
            result.add(10)
            //result = {1,10}
            dfs(100,109,13,result)
                i = start
                //i = 100
                Enter Loop
                Whoops! "i" is greater than "n"! //n = 13
                Exit Loop
            i++
            //i = 11
            result.add(11)
            //result = {1,10,11}
            dfs(110,119,13,result)
                i = start
                //i = 110
                Enter Loop
                Whoops! "i" is greater than "n"! //n = 13
                Exit Loop
            i++
            //i = 12
            result.add(12)
            //result = {1,10,11,12}
            dfs(120,129,13,result)
                i = start
                //i = 120
                Enter Loop
                Whoops! "i" is greater than "n"! //n = 13
                Exit Loop
            i++
            //i = 13
            result.add(13)
            //result = {1,10,11,12,13}
            dfs(130,139,13,result)
                i = start
                //i = 130
                Enter Loop
                Whoops! "i" is greater than "n"! //n = 13
                Exit Loop
            i++
            //i = 14
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 2
        result.add(i)
        //result = {1,10,11,12,13,2}
        dfs(20,29,13,result)
            i = start
            //i = 20
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 3
        result.add(i)
        //result = {1,10,11,12,13,2,3}
        dfs(30,39,13,result)
            i = start
            //i = 30
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 4
        result.add(i)
        //result = {1,10,11,12,13,2,3,4}
        dfs(40,49,13,result)
            i = start
            //i = 40
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 5
        result.add(i)
        //result = {1,10,11,12,13,2,3,4,5}
        dfs(50,59,13,result)
            i = start
            //i = 50
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 6
        result.add(i)
        //result = {1,10,11,12,13,2,3,4,5,6}
        dfs(60,69,13,result)
            i = start
            //i = 60
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 7
        result.add(i)
        //result = {1,10,11,12,13,2,3,4,5,6,7}
        dfs(70,79,13,result)
            i = start
            //i = 70
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 8
        result.add(i)
        //result = {1,10,11,12,13,2,3,4,5,6,7,8}
        dfs(80,89,13,result)
            i = start
            //i = 80
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 9
        result.add(i)
        //result = {1,10,11,12,13,2,3,4,5,6,7,8,9}
        dfs(90,99,13,result)
            i = start
            //i = 90
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 10
        Whoops! "i" is greater than "end"! //end = 9
    return result // result = {1,10,11,12,13,2,3,4,5,6,7,8,9}

基本上,它所做的是转到某个数字,并在其上附加数字0到9,然后转到这些数字,并在其上附加0到9,跳过大于N的数字(本例中为13)

这里有几个步骤

通过查看以左为中心的“i”,可能更容易看到发生了什么

"i"                                         return
1   //Add to list                           {1}
10  //Add to list                           {1,10}
100 //Bigger than n! (n = 13)               {1,10}
11  //Add to list                           {1,10,11}
110 //Bigger than n! (n = 13)               {1,10,11}
12  //Add to list                           {1,10,11,12}
120 //Bigger than n! (n = 13)               {1,10,11,12}
13  //Add to list                           {1,10,11,12,13}
130 //Bigger than n! (n = 13)               {1,10,11,12,13}
14  //Bigger than n! (n = 13)               {1,10,11,12,13}
2   //Add to list                           {1,10,11,12,13,2}
20  //Bigger than n! (n = 13)               {1,10,11,12,13,2}
3   //Add to list                           {1,10,11,12,13,2,3}
30  //Bigger than n! (n = 13)               {1,10,11,12,13,2,3}
4   //Add to list                           {1,10,11,12,13,2,3,4}
40  //Bigger than n! (n = 13)               {1,10,11,12,13,2,3,4}
5   //Add to list                           {1,10,11,12,13,2,3,4,5}
50  //Bigger than n! (n = 13)               {1,10,11,12,13,2,3,4,5}
6   //Add to list                           {1,10,11,12,13,2,3,4,5,6}
60  //Bigger than n! (n = 13)               {1,10,11,12,13,2,3,4,5,6}
7   //Add to list                           {1,10,11,12,13,2,3,4,5,6,7}
70  //Bigger than n! (n = 13)               {1,10,11,12,13,2,3,4,5,6,7}
8   //Add to list                           {1,10,11,12,13,2,3,4,5,6,7,8}
80  //Bigger than n! (n = 13)               {1,10,11,12,13,2,3,4,5,6,7,8}
9   //Add to list                           {1,10,11,12,13,2,3,4,5,6,7,8,9}
90  //Bigger than n! (n = 13)               {1,10,11,12,13,2,3,4,5,6,7,8,9}
10  //Bigger than end! (end = 9)            {1,10,11,12,13,2,3,4,5,6,7,8,9}
正在发生的事情的更完整版本:

lexicalOrder(13)
    result = {}
    dfs(1,9,13,result)  //1 is the smallest digit, 9 is the largest digit,
                        //13 is the largest possible value,
                        //Passed in "result" array to be edited.
        i = start
        //i = 1
        Enter Loop
        result.add(1)
        //result = {1}
        dfs(10,19,13,result)
            i = start
            //i = 10
            Enter Loop
            result.add(10)
            //result = {1,10}
            dfs(100,109,13,result)
                i = start
                //i = 100
                Enter Loop
                Whoops! "i" is greater than "n"! //n = 13
                Exit Loop
            i++
            //i = 11
            result.add(11)
            //result = {1,10,11}
            dfs(110,119,13,result)
                i = start
                //i = 110
                Enter Loop
                Whoops! "i" is greater than "n"! //n = 13
                Exit Loop
            i++
            //i = 12
            result.add(12)
            //result = {1,10,11,12}
            dfs(120,129,13,result)
                i = start
                //i = 120
                Enter Loop
                Whoops! "i" is greater than "n"! //n = 13
                Exit Loop
            i++
            //i = 13
            result.add(13)
            //result = {1,10,11,12,13}
            dfs(130,139,13,result)
                i = start
                //i = 130
                Enter Loop
                Whoops! "i" is greater than "n"! //n = 13
                Exit Loop
            i++
            //i = 14
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 2
        result.add(i)
        //result = {1,10,11,12,13,2}
        dfs(20,29,13,result)
            i = start
            //i = 20
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 3
        result.add(i)
        //result = {1,10,11,12,13,2,3}
        dfs(30,39,13,result)
            i = start
            //i = 30
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 4
        result.add(i)
        //result = {1,10,11,12,13,2,3,4}
        dfs(40,49,13,result)
            i = start
            //i = 40
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 5
        result.add(i)
        //result = {1,10,11,12,13,2,3,4,5}
        dfs(50,59,13,result)
            i = start
            //i = 50
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 6
        result.add(i)
        //result = {1,10,11,12,13,2,3,4,5,6}
        dfs(60,69,13,result)
            i = start
            //i = 60
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 7
        result.add(i)
        //result = {1,10,11,12,13,2,3,4,5,6,7}
        dfs(70,79,13,result)
            i = start
            //i = 70
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 8
        result.add(i)
        //result = {1,10,11,12,13,2,3,4,5,6,7,8}
        dfs(80,89,13,result)
            i = start
            //i = 80
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 9
        result.add(i)
        //result = {1,10,11,12,13,2,3,4,5,6,7,8,9}
        dfs(90,99,13,result)
            i = start
            //i = 90
            Enter Loop
            Whoops! "i" is greater than "n"! //n = 13
            Exit Loop
        i++
        //i = 10
        Whoops! "i" is greater than "end"! //end = 9
    return result // result = {1,10,11,12,13,2,3,4,5,6,7,8,9}

使用调试器逐步完成。此外,
返回IntStream.rangeClosed(1,n).boxed().sorted((a,b)->String.valueOf(a).compareTo(String.valueOf(b)).collector(Collectors.toList())
我通过调试器看到了发生的事情。开始和结束实际上变成了100和109,然后while循环中的条件失败,因为我不再小于n。但是,我想我仍然有点困惑,我们如何回到10和19。@ElroyJetson
dfs(10,19,n,result)
通过调试器多次被调用。此外,
返回IntStream.rangeClosed(1,n).boxed().sorted((a,b)->String.valueOf(a).compareTo(String.valueOf(b)).collect(collector.toList())
我通过调试器,看到了发生了什么。开始和结束实际上变成了100和109,然后while循环中的条件失败,因为我不再小于n。但是,我想我仍然有点困惑,我们如何回到10和19。@ElroyJetson
dfs(10,19,n,result)
被多次调用