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. Vertica环境安装R-Lang包提示缺少libgfortran.so.1

    环境:RHEL 6.4 + Vertica 7.0.0-11.最终确认安装compat-libgfortran-41-4.1.2-39.el6.x86_64.rpm即可解决. # rpm -ivh v ...

  2. HTML5-video标签-实现点击预览图播放或暂停视频

    HTML5-video标签-实现点击预览图播放或暂停视频 刚刚参加工作,开始更多的接触到一些新的知识,促使我开始了解html5和css3的新特性.这时我才真的发现到html5和css3的强大. 之前关 ...

  3. ThreadLocal 工作原理、部分源码分析

    1.大概去哪里看 ThreadLocal 其根本实现方法,是在Thread里面,有一个ThreadLocal.ThreadLocalMap属性 ThreadLocal.ThreadLocalMap t ...

  4. 在thinkphp中,写的博文标签多对多关系的标签频率统计算法

    常常看到别人的博客里面,或者网站里面有这样随机颜色,但字体大小与标签出现频率有关的标签云,于是自己就想写一个.至于颜色的随机显示,那就很简单了,这里就不列代码. 因为正在学thinkphp,所以数据查 ...

  5. 深入理解javascript原生拖放

    × 目录 [1]拖放源 [2]拖放目标 [3]dataTransfer对象[4]改变光标 前面的话 拖放(drag-and-drop,DnD)其实是两个动作——拖和放.所以,它涉及到两个元素.一个是被 ...

  6. .Net语言 APP开发平台——Smobiler学习日志:如何快速实现类似于微信的悬浮显示二维码效果

    最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 样式一 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的&qu ...

  7. 扩展JS Date对象时间格式化功能

    在自己JS代码中引入一下代码: Date.prototype.format =function(format) { var o = { "M+" : this.getMonth() ...

  8. BroadcastReceiver几种常见监听

    1.BroadcastReceiver监听拨号 <intent-filter android:priority="1000" > <action android: ...

  9. Java程序员应该掌握的10项技能

    这篇文章主要介绍了作为Java程序员应该掌握的10项技能,包括java的知识点与相关的技能,对于java的学习有不错的参考借鉴价值,需要的朋友可以参考下   1.语法:必须比较熟悉,在写代码的时候ID ...

  10. spring springMVC

    spring是一个开源框架,是为了解决企业应用程序开发,功能如下 目的:解决企业应用开发的复杂性 功能:使用基本的javabean代替EJB,并提供了更多的企业应用功能 范围:任何java应用 总之: ...