[LeetCode] Lexicographical Numbers 字典順序的數字


 

Given an integer n, return 1 - n in lexicographical order.

For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9].

Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000.

 

這道題給了我們一個整數n,讓我們把區間[1,n]的所有數字按照字典順序來排列,題目中也給了我們字典順序的例子。那么我們需要重新排序,我最開始想到的方法是重寫sort方法的comparator,思路是把所有數字都轉為字符串,然后兩個字符串按位相比,然后排好序后再轉回數字,這種方法通過不了OJ的大集合,說明本題不是想考我們這種方法。我在論壇里看到大家普遍使用的是下面這種方法,學習了一下,感覺思路十分巧妙,估計我自己肯定想不出來。這種思路是按個位數遍歷,在遍歷下一個個位數之前,先遍歷十位數,十位數的高位為之前的個位數,只要這個多位數並沒有超過n,就可以一直往后遍歷,如果超過了,我們除以10,然后再加1,如果加1后末尾形成了很多0,那么我們要用個while循環把0都去掉,然后繼續運算,參見代碼如下:

 

解法一:

class Solution {
public:
    vector<int> lexicalOrder(int n) {
        vector<int> res(n);
        int cur = 1;
        for (int i = 0; i < n; ++i) {
            res[i] = cur;
            if (cur * 10 <= n) {
                cur *= 10;
            } else {
                if (cur >= n) cur /= 10;
                cur += 1;
                while (cur % 10 == 0) cur /= 10;
            }
        }
        return res;
    }
};

 

下面這種方法是上面解法的遞歸形式,思路並沒有什么不同,參見代碼如下:

 

解法二:

class Solution {
public:
    vector<int> lexicalOrder(int n) {
        vector<int> res;
        for (int i = 1; i <= 9; ++i) {
            helper(i, n, res);
        }
        return res;
    }
    void helper(int cur, int n, vector<int>& res) {
        if (cur > n) return;
        res.push_back(cur);
        for (int i = 0; i <= 9; ++i) {
            if (cur * 10 + i <= n) {
                helper(cur * 10 + i, n, res);
            } else break;
        }
    }
};

 

參考資料:

https://discuss.leetcode.com/topic/55131/ac-240ms-c-solution

https://discuss.leetcode.com/topic/55091/java-recursion-backtracking-with-explanation

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM