[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 = ;
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 字典顺序的数字的更多相关文章
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)
1. 题目名称 Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...
- Leetcode: Lexicographical Numbers
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- [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 ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- [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 ...
随机推荐
- Oracle逻辑迁移某业务用户及数据
1.确定基本信息 2.源数据库导出 3.目的数据库导入 4.逻辑迁移注意事项 1.确定基本信息 确定基本信息: 源数据库所在系统类型:________ 源数据库地址:__.__.__.__ 源数据库版 ...
- 【分布式】Zookeeper在大型分布式系统中的应用
一.前言 上一篇博文讲解了Zookeeper的典型应用场景,在大数据时代,各种分布式系统层出不穷,其中,有很多系统都直接或间接使用了Zookeeper,用来解决诸如配置管理.分布式通知/协调.集群管理 ...
- 【分布式】Zookeeper使用--Java API
一.前言 上一篇博客我们通过命令行来操作Zookeper的客户端和服务端并进行相应的操作,这篇主要介绍如何通过API(JAVA)来操作Zookeeper. 二.开发环境配置 首先打开Zookeeper ...
- IOS学习之-私人通讯录
通过一段时间IOS的学习完成了一个简单的应用,"私人通讯录". 运行效果如下图: 1.登录页 2.通讯录列表 3.添加 4.编辑 5.删除 6.注销 总视图结构如下图: 总结本程序 ...
- .NET框架解决的问题
面向对象开发环境 自动垃圾收集 互操作性 不需要COM 简化部署 类型安全 基类库
- C#开发微信门户及应用(21)-微信企业号的消息和事件的接收处理及解密
在上篇随笔<C#开发微信门户及应用(19)-微信企业号的消息发送(文本.图片.文件.语音.视频.图文消息等)>介绍了有关企业号的消息发送,官方特别声明消息是不用加密发送的.但是在回调的服务 ...
- Yii2.X 多语言-类图
- mysql技术点1.-----------查询当天的所有数据
select xs.* from xn_supervision xs where xs.task_type="+taskType+" and xs.create_time&g ...
- Flexible 弹性盒子模型之flex
实例 让所有弹性盒模型对象的子元素都有相同的长度,忽略它们内部的内容: #main div { flex:1; } 复制 效果预览 浏览器支持 表格中的数字表示支持该属性的第一个浏览器的版本号. 紧跟 ...
- Fresco从配置到使用(最高效的图片加载框架)
Frescoj说明: facebook开源的针对android应用的图片加载框架,高效和功能齐全. 支持加载网络,本地存储和资源图片: 提供三级缓存(二级memory和一级internal ...