|
| 1 | +// Source : https://leetcode.com/problems/lexicographical-numbers/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2016-08-23 |
| 4 | + |
| 5 | +/*************************************************************************************** |
| 6 | + * |
| 7 | + * Given an integer n, return 1 - n in lexicographical order. |
| 8 | + * |
| 9 | + * For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. |
| 10 | + * |
| 11 | + * Please optimize your algorithm to use less time and space. The input size may be as |
| 12 | + * large as 5,000,000. |
| 13 | + ***************************************************************************************/ |
| 14 | +class Solution { |
| 15 | + |
| 16 | +//Solution 1: convert the int to string for sort, Time complexity is high (Time Limited Error) |
| 17 | +public: |
| 18 | + vector<int> lexicalOrder01(int n) { |
| 19 | + vector<int> result; |
| 20 | + for (int i=1; i<=n; i++) { |
| 21 | + result.push_back(i); |
| 22 | + } |
| 23 | + sort(result.begin(), result.end(), this->myComp); |
| 24 | + return result; |
| 25 | + } |
| 26 | +private: |
| 27 | + static bool myComp(int i,int j) { |
| 28 | + static char si[32]={0}, sj[32]={0}; |
| 29 | + sprintf(si, "%d\0", i); |
| 30 | + sprintf(sj, "%d\0", j); |
| 31 | + return (strcmp(si, sj)<0); |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | +//Solution 2 : using recursive way to solution the problem, 540ms |
| 36 | +public: |
| 37 | + vector<int> lexicalOrder02(int n) { |
| 38 | + vector<int> result; |
| 39 | + for (int i=1; i<=n && i<=9; i++) { |
| 40 | + result.push_back(i); |
| 41 | + lexicalOrder_helper(i, n, result); |
| 42 | + } |
| 43 | + return result; |
| 44 | + } |
| 45 | + |
| 46 | +private: |
| 47 | + void lexicalOrder_helper(int num, int& n, vector<int>& result) { |
| 48 | + for (int i=0; i<=9; i++) { |
| 49 | + int tmp = num * 10 + i; |
| 50 | + if (tmp > n) { |
| 51 | + break; |
| 52 | + } |
| 53 | + result.push_back(tmp); |
| 54 | + lexicalOrder_helper(tmp, n, result); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | +//Solution 3: no recursive way, but the code is not easy to read |
| 59 | +public : |
| 60 | + vector<int> lexicalOrder03(int n) { |
| 61 | + vector<int> result; |
| 62 | + int curr = 1; |
| 63 | + while (result.size()<n) { |
| 64 | + // Step One |
| 65 | + // --------- |
| 66 | + //Adding all of the possible number which multiply 10 as much as possible |
| 67 | + // such as: curr = 1, then 1, 10, 100, 1000 ... |
| 68 | + // curr = 12, then 12, 120, 1200, ... |
| 69 | + for (; curr <= n; curr*=10 ) { |
| 70 | + result.push_back(curr); |
| 71 | + } |
| 72 | + |
| 73 | + // Step Two |
| 74 | + // --------- |
| 75 | + // After find the number which multiply 10 greater than `n`, then go back the previous one, |
| 76 | + // and keep adding 1 until it carry on to next number |
| 77 | + // for example: |
| 78 | + // curr = 100, then we need evalute: 11,12,13,14,15,16,17,18,19, but stop at 20 |
| 79 | + // curr = 230, then we need evaluate: 24,25,26,27,28,29, but stop at 30. |
| 80 | + curr = curr/10 + 1; |
| 81 | + for (; curr <= n && curr % 10 != 0; curr++) { |
| 82 | + result.push_back(curr); |
| 83 | + } |
| 84 | + |
| 85 | + // Step Three |
| 86 | + // ---------- |
| 87 | + // Now, we finished all of the number, we need go back for next number |
| 88 | + // Here is a bit tricky. |
| 89 | + // |
| 90 | + // Assuming the n is 234, and Step One evaluted 190, and Step Two, evaluted 191,192,...,199 |
| 91 | + // Now, the `curr` is 200, and we need start from 2 instead of 20, that's why need keep dividing 10 |
| 92 | + for (; curr%10 == 0; curr/=10); |
| 93 | + |
| 94 | + } |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | +//start point |
| 100 | +public: |
| 101 | + vector<int> lexicalOrder(int n) { |
| 102 | + //srand(time(NULL)); |
| 103 | + //if (rand()%2) |
| 104 | + // return lexicalOrder02(n); // recursive way 560ms |
| 105 | + //else |
| 106 | + return lexicalOrder03(n); // non-recursive way, 460ms |
| 107 | + } |
| 108 | + |
| 109 | +}; |
0 commit comments