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 = ;
for (int i = ; i < n; ++i) {
res[i] = cur;
if (cur * <= n) {
cur *= ;
} else {
if (cur >= n) cur /= ;
cur += ;
while (cur % == ) cur /= ;
}
}
return res;
}
};

下面这种方法是上面解法的递归形式,思路并没有什么不同,参见代码如下:

解法二:

class Solution {
public:
vector<int> lexicalOrder(int n) {
vector<int> res;
for (int i = ; i <= ; ++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 = ; i <= ; ++i) {
if (cur * + i <= n) {
helper(cur * + 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 题目讲解汇总(持续更新中...)

[LeetCode] Lexicographical Numbers 字典顺序的数字的更多相关文章

  1. [LeetCode] Consecutive Numbers 连续的数字

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

  2. [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)

    1. 题目名称   Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...

  3. Leetcode: Lexicographical Numbers

    Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...

  4. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  5. [LeetCode] K-th Smallest in Lexicographical Order 字典顺序的第K小数字

    Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. N ...

  6. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  7. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  8. [LeetCode] 2. Add Two Numbers 两个数字相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  9. [CareerCup] 2.5 Add Two Numbers 两个数字相加

    2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...

随机推荐

  1. C#面试题汇总(未完成)

    泛型的好处: 1.可以保证类型安全以及避免装箱和拆箱操作,泛型类会在编译时由具体的类型去取代. 2.我们就拿一个ArrayList来说吧,ArrayList要进行拆箱操作,也就是ArrayList传入 ...

  2. ionic之$ionicGesture手势(大坑)

    鄙人来本公司前未用过ionic框架,但由于ionic是基于angularjs封装的,正好我用过angularjs,很荣幸的面试就过了,然后通过该网站http://www.ionic.wang(后面简称 ...

  3. Google地图开发总结

    我们经常使用地图查位置.看公交.看街景,同时地图还开放第三方的API给开发者.利用这些API进行地图的个性化的展示和控制,例如北京被水淹了,开发一个网页显示北京被淹的地图,地图上面标志被水淹的位置.严 ...

  4. C#-#define条件编译

    本文导读: C#的预处理器指令从来不会转化为可执行代码的命令,但是会影响编译过程的各个方面,常用的预处理器指令有#define.#undef.#if,#elif,#else和#endif等等,下面介绍 ...

  5. StackExchange.Redis帮助类解决方案RedisRepository封装(散列Hash类型数据操作)

    本文版权归博客园和作者本人共同所有,转载和爬虫请注明本系列分享地址:http://www.cnblogs.com/tdws/p/5815735.html 上一篇文章的不合理之处,已经有所修改. 今天分 ...

  6. C语言实现2个大数相加。

    #include<stdio.h>#include<string.h>int main(){    char s1[100],s2[100];    int num1[31], ...

  7. 分享一个php邮件库——swiftmailer

    最近看到一个好的php邮件库,与phpmailer作用一样,但性能比phpmailer好,尤其是在处理附件的能力上,发送邮件成功的几率也高. github地址:https://github.com/s ...

  8. 来玩Play框架03 模板

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在上一章节中,我把字符串通过ok()返回给客户.我可以把一个完整的html页面放入 ...

  9. centos 域名硬解析(linux)

    centos做硬解析跟Windows一样修改一个文件. 具体文件为:/etc/hosts.修改命令: vi /etc/hosts 格式个Windows也一样的.

  10. 初探物联网 - 基于Arduino的气象站和View and Data API的结合实例

    如果你参加了上个月在北京的Autodesk 开发者日,你应该看到了我做的关于Arduino的物联网实例演示,如果你没看到,欢迎参加14号在上海的开发者日,到时候我会再演(xian)示(bai)一下. ...