386. Lexicographical Numbers 把1--n按字典序排序
https://leetcode.com/problems/lexicographical-numbers/description/
前20个是
1, 10, 11, 12, 13, 14, .....19 2, 20, 3, 4, 5, 6, ....9
class Solution {
public:
vector<int> lexicalOrder(int n) {
vector<int> ans;
int cur = 1;
for (int i = 1; i <= n; ++i) {
ans.push_back(cur);
if (cur * 10 <= n) {
cur *= 10;
} else if (cur % 10 != 9 && cur + 1 <= n) {
cur++;
} else {
cur += 10;
cur -= cur % 10;
while (cur % 10 == 0) cur /= 10;
}
}
return ans;
}
};
386. Lexicographical Numbers 把1--n按字典序排序的更多相关文章
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 386 Lexicographical Numbers 字典序排数
给定一个整数 n, 返回从 1 到 n 的字典顺序.例如,给定 n =1 3,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] .请尽可能的优化算法的时间复杂度和空间复杂度. 输入 ...
- LeetCode - 386. Lexicographical Numbers
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
- 386. Lexicographical Numbers 输出1到n之间按lexico排列的数字序列
[抄题]: Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,1 ...
- 386. Lexicographical Numbers
用DFS来做,先弄开头是1的,再弄开头是1的里面开头是1的,再开头是1的里面开头是1的里的开头是1的,再... 是吧-- 比N大了BREAK就行. 注意第一个循环是1-9,往后的循环是0-9. pub ...
- 【java】java反射机制,动态获取对象的属性和对应的参数值,并属性按照字典序排序,Field.setAccessible()方法的说明【可用于微信支付 签名生成】
方法1:通过get()方法获取属性值 package com.sxd.test.controller; public class FirstCa{ private Integer num; priva ...
- Leetcode 385.字典序排序
字典序排序 给定一个整数 n, 返回从 1 到 n 的字典顺序. 例如, 给定 n =1 3,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] . 请尽可能的优化算法的时间复杂度和 ...
- 微信支付MD5签名算法C#版,ASCII码字典序排序0,A,B,a,b
/// <summary> /// 微信支付MD5签名算法,ASCII码字典序排序0,A,B,a,b /// </summary> /// <param name=&qu ...
- [Swift]LeetCode386. 字典序排数 | Lexicographical Numbers
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
随机推荐
- python使用基础(win10)
1.安装 官网下载:https://www.python.org/ 请选择2.X版本 2.从命令提示符打开python 直接输入python点enter即可 查看python版本输入python -V ...
- linux下top命令参数解释
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法. top - 01:06:48 up 1:22, 1 ...
- Spring-访问静态资源文件的方法
转自: http://blog.163.com/zhangmihuo_2007/blog/static/27011075201453044959574?suggestedreading 如果你的Di ...
- 在控制台使用MySQL数据库
本篇内容介绍的是如何在控制台下使用MySQL数据库.首先需要安装MySQL数据库应用程序,然后找到MySql的Command Line Client进入之后你会看到,此处需要正确输入密码,否则会直接退 ...
- GridView控件点击单元格如何获取该列的列标题
本博文Insus.NET教你实现在GridView控件中,用mouse点击某单元格之后,希望能获取到该列的列标题. 创建一个网页,创建一个GridView控件: 去cs绑定数据给GridView控件: ...
- myql 服务启动不了怎么办
今天,不小心手动将mysql 服务停掉后,怎么也启动不了,后面查了半天 ,终于知道要先将任务管理器里的mysql.exe 先Kill掉,然后可以启动了,记录一下
- 《图解HTTP》阅读笔记---第一章网络基础
第一章.网络基础TCP/IP:----------传输控制协议Transmission Control Protocol / 网络协议Internet Protocol是一种用于Internet(因特 ...
- 给花_Q
- ERROR (UnicodeEncodeError): 'ascii' codec can't encode character u'\uff08' in position 9: ordinal not in range(128)
环境win10+anaconda2 在安装labelme时遇到了这个问题,其实跟labelme没啥关系,主要是python2读取中文路径时报错,因为默认编码是ASCII,不认识中文,看到有一个一次性解 ...
- matplotlib学习笔记(三)
柱状图 柱状图用其每根柱子的长度表示值的大小,它们通常用来比较两组或多组值.下面的程序从文件中读入中国人口的年龄分布数据,并使用柱状图比较男性和女性的年龄分布. import numpy as np ...