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

时间:2017-01-21 03:46:55

标签: java depth-first-search lexicographic-ordering

EG。 <p><span>Unchanged text</span> Changed text</p>

以下解决方案实际上非常有效。经过一段时间的努力我找到了它,我无法理解它是如何工作的。这似乎是纯粹的魔力。当我们进行递归调用时,在第二次递归之后,For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. 参数在世界中仍然是10

start

我不相信魔法,所以有人可以解释这是如何工作的吗?第一次迭代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); } } 为1,start为9。然后开始是10和19,正如我在第二次迭代中所期望的那样。然后,我期望开始为100,在我们进行下一次递归调用后结束为109;然而,它们与之前的递归一样:10和19。

2 个答案:

答案 0 :(得分:1)

这很简单。你有递归和循环。因此,第一次调用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).

只有对dfs(10,19,13)的调用实际上做了什么,因为在所有其他情况下前两个参数都大于第三个。

对dfs(10,19,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,只是第二组递归调用终止并返回到封闭循环的结果。

答案 1 :(得分:1)

基本上它做的是去一些数字,并将数字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}
相关问题