|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | import java.util.ArrayList;
|
4 |
| -import java.util.Collections; |
5 |
| -import java.util.Comparator; |
6 |
| -import java.util.Date; |
7 | 4 | import java.util.List;
|
8 | 5 |
|
9 | 6 | /**
|
| 7 | + * 386. Lexicographical Numbers |
| 8 | + * |
10 | 9 | * Given an integer n, return 1 - n in lexicographical order.
|
11 |
| -
|
12 |
| -For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. |
13 |
| -
|
14 |
| -Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000.*/ |
| 10 | + * |
| 11 | + * For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. |
| 12 | + * |
| 13 | + * Please optimize your algorithm to use less time and space. The input size may be as large as |
| 14 | + * 5,000,000. |
| 15 | + */ |
15 | 16 | public class _386 {
|
16 |
| - //Radix sort doesn't apply here! Don't confuse myself! |
17 |
| - |
18 |
| - //rewrote their solution from Python to Java:https://discuss.leetcode.com/topic/54986/python-memory-limit-exceeded-for-problem-386/17 |
19 |
| - public static List<Integer> lexicalOrder(int n) { |
20 |
| - List<Integer> result = new ArrayList(); |
21 |
| - int i = 1; |
22 |
| - while (true) { |
23 |
| - result.add(i); |
24 |
| - if (i * 10 <= n) { |
25 |
| - i *= 10; |
26 |
| - } else { |
27 |
| - while (i % 10 == 9 || i == n) { |
28 |
| - i /= 10; |
29 |
| - } |
30 |
| - if (i == 0) { |
31 |
| - return result; |
32 |
| - } |
33 |
| - i++; |
34 |
| - } |
35 |
| - } |
36 |
| - } |
37 |
| - |
38 |
| - //someone on Discuss hinted that you could use recursion to solve it in Java |
39 |
| - //then I wrote the following method, eventually, got all test cases produce the right output, but unfortunately TLE'ed by OJ |
40 |
| - public static List<Integer> lexicalOrder_LTE_by_10458(int n) { |
41 |
| - List<Integer> result = new ArrayList(); |
42 |
| - int insertPosition = 0; |
43 |
| - return addNumbers(result, 1, insertPosition, n); |
44 |
| - } |
45 |
| - |
46 |
| - private static List<Integer> addNumbers(List<Integer> result, int insertNumber, int insertPosition, int n) { |
47 |
| - int i; |
48 |
| - for (i = 0; i < 9; i++) { |
49 |
| - if (insertNumber + i > n) { |
50 |
| - return result; |
51 |
| - } |
52 |
| - result.add(insertPosition + i, insertNumber + i); |
53 |
| - if ((insertNumber + i) % 10 == 0 && (insertNumber + i) == (insertNumber + 10)) { |
54 |
| - break; |
55 |
| - } |
56 |
| - } |
57 |
| - while ((insertNumber + i) % 10 != 0 && (insertNumber + i) <= n) { |
58 |
| - result.add(insertPosition + i, insertNumber + i); |
59 |
| - i++; |
60 |
| - } |
61 |
| - //find next insert position: |
62 |
| - insertPosition = result.indexOf((insertNumber + i) / 10); |
63 |
| - return addNumbers(result, insertNumber + i, insertPosition + 1, n); |
64 |
| - } |
65 |
| - |
66 |
| - public static void main(String... strings) { |
67 |
| - long lStartTime = new Date().getTime(); |
| 17 | + public static class Solution1 { |
| 18 | + //Radix sort doesn't apply here! Don't confuse yourself! |
68 | 19 |
|
69 |
| -// CommonUtils.printList(lexicalOrder_TLE_by_23489(23489)); |
70 |
| -// List<Integer> result = lexicalOrder(1);//right |
71 |
| -// List<Integer> result = lexicalOrder(13);//right |
72 |
| -// List<Integer> result = lexicalOrder_LTE_by_10458(58); |
73 |
| -// List<Integer> result = lexicalOrder(120);//right |
74 |
| -// List<Integer> result = lexicalOrder(1200); |
75 |
| -// List<Integer> result = lexicalOrder(10); |
76 |
| -// List<Integer> result = lexicalOrder(5000000); |
77 |
| -// List<Integer> result = lexicalOrder_LTE_by_10458(50000);//this can finish in 183 ms |
78 |
| - List<Integer> result = lexicalOrder_LTE_by_10458(500000); |
79 |
| -// List<Integer> result = lexicalOrder_LTE_by_10458(14959); |
80 |
| - long lEndTime = new Date().getTime(); |
81 |
| - long difference = lEndTime - lStartTime; |
82 |
| - System.out.println("Elapsed milliseconds: " + difference); |
83 |
| - System.out.println("result size is: " + result.size()); |
84 |
| -// CommonUtils.printList(result); |
85 |
| - } |
86 |
| - |
87 |
| - /** |
88 |
| - * The most naive way is to generate this list, sort it using a customized comparator and then return it. |
89 |
| - * Unfortunately, this results in TLE with this input: 23489 |
90 |
| - */ |
91 |
| - public static List<Integer> lexicalOrder_TLE_by_23489(int n) { |
92 |
| - List<Integer> result = new ArrayList(); |
93 |
| - for (int i = 1; i <= n; i++) { |
94 |
| - result.add(i); |
| 20 | + //rewrote their solution from Python to Java:https://discuss.leetcode.com/topic/54986/python-memory-limit-exceeded-for-problem-386/17 |
| 21 | + public List<Integer> lexicalOrder(int n) { |
| 22 | + List<Integer> result = new ArrayList(); |
| 23 | + int i = 1; |
| 24 | + while (true) { |
| 25 | + result.add(i); |
| 26 | + if (i * 10 <= n) { |
| 27 | + i *= 10; |
| 28 | + } else { |
| 29 | + while (i % 10 == 9 || i == n) { |
| 30 | + i /= 10; |
| 31 | + } |
| 32 | + if (i == 0) { |
| 33 | + return result; |
| 34 | + } |
| 35 | + i++; |
95 | 36 | }
|
96 |
| - Collections.sort(result, new Comparator<Integer>() { |
97 |
| - @Override |
98 |
| - public int compare(Integer o1, Integer o2) { |
99 |
| - String s1 = String.valueOf(o1); |
100 |
| - String s2 = String.valueOf(o2); |
101 |
| - return s1.compareTo(s2); |
102 |
| - } |
103 |
| - }); |
104 |
| - return result; |
| 37 | + } |
105 | 38 | }
|
106 |
| - |
| 39 | + } |
107 | 40 | }
|
0 commit comments