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.
Java算法实现
public class Solution {
public List<Integer> lexicalOrder(int n) {
List<Integer>list=new ArrayList<Integer>(n);
if(n<=9){
for(int i=1;i<=n;i++){
list.add(i);
}
}
else{
for(int i=1;i<=9;i++){
list.add(i);
doAdd(list, i, n);//每次doAdd都是把以i开头的数字都加入List中
}
}
return list;
}
public static void doAdd(List<Integer>list,int start,int n){
int moreBit=start*10;
if(moreBit<=n){
int ceil=n-moreBit;
int ans;
ceil=ceil>9?9:ceil;
for(int i=0;i<=ceil;i++){
ans=moreBit+i;
list.add(ans);
doAdd(list, ans, n);
}
}
}
}
Leetcode算法比赛---- Lexicographical Numbers的更多相关文章
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- Leetcode算法比赛----First Unique Character in a String
问题描述 Given a string, find the first non-repeating character in it and return it's index. If it doesn ...
- Leetcode算法比赛----Longest Absolute File Path
问题描述 Suppose we abstract our file system by a string in the following manner: The string "dir\n ...
- LeetCode算法题-Self Dividing Numbers(Java实现)
这是悦乐书的第305次更新,第324篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第173题(顺位题号是728).自分割数是一个可被其包含的每个数字整除的数字.例如,12 ...
- LeetCode算法题-Sum of Square Numbers(Java实现)
这是悦乐书的第276次更新,第292篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第144题(顺位题号是633).给定一个非负整数c,判断是否存在两个整数a和b,使得a的 ...
- LeetCode算法题-Maximum Product of Three Numbers(Java实现)
这是悦乐书的第275次更新,第291篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第143题(顺位题号是628).给定一个整数数组,从其中找出三个数,使得乘积最大.例如: ...
- LeetCode算法题-Find All Numbers Disappeared in an Array(Java实现)
这是悦乐书的第232次更新,第245篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第99题(顺位题号是448).给定一个整数数组,其中1≤a[i]≤n(n =数组的大小) ...
- LeetCode算法题-Heaters(Java实现)
这是悦乐书的第239次更新,第252篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第106题(顺位题号是475).冬天来了!您在比赛期间的第一份工作是设计一个固定温暖半径 ...
- LeetCode算法题-Two Sum II - Input array is sorted
这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...
随机推荐
- [黑科技]跑的比fread还快的cin挂和cout挂
CCPC赛后摸鱼搞了个新的奇怪外挂 这里贴上利用sgetn和sputn来实现的读入读出挂,理论上比fread更优 期望在赛中TLE的代码能强行卡过去hhh 利用小规模的Codeforces - 103 ...
- Calibre 3.4版中,为epub书籍嵌入中文字体
1. 先把原版书籍epub文件添加到Calibre书库: 2. 书名上右键,选则 转换书籍 -> 逐个转换: 3. 在 界面外观 -> 字体 中,选择嵌入字体,在列表中选择中文字体,并勾选 ...
- 毫秒查询9位数qq号码是否存在-BitMap算法应用
实现详情请查看博客园 https://www.cnblogs.com/caoke/p/10793885.html 随机注册10万个放入BitMap,然后查询qq号码是否已存在,算法复杂度O(1). / ...
- 分享:Android浏览器,用NDK C++做底层开发的Android浏览器,纯免费,无广告
分享:用 NDK C++做底层开发的Android 浏览器,纯免费,无广告 分享:用 NDK C++做底层开发的Android 浏览器,纯免费,无广告 操作简单,傻瓜一看就会 无毒.无广告.无负作用, ...
- 100道C#面试题(.net开发人员必备)
1. .NET和C#有什么区别 答:.NET一般指 .NET FrameWork框架,它是一种平台,一种技术. C#是一种编程语言,可以基于.NET平台的应用. 2.一列数的规则如下: 1.1.2.3 ...
- css hack 汇整
针对Chrome和Safari等Webkit核心浏览器的CSS hack代码: @media screen and (-webkit-min-device-pixel-ratio:0) { …… } ...
- CentOS和Ubuntu系统下安装 HttpFS (助推Hue部署搭建)
不多说,直接上干货! 我的集群机器情况是 bigdatamaster(192.168.80.10).bigdataslave1(192.168.80.11)和bigdataslave2(192.168 ...
- JavaScript设计模式-18.享元模式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- FocusBI:《DW/BI项目管理》之SSIS执行情况
微信公众号:FocusBI关注可了解更多的商业智能.数据仓库.数据库开发.爬虫知识及沪深股市数据推送.问题或建议,请关注公众号发送消息留言;如果你觉得FocusBI对你有帮助,欢迎转发朋友圈或在文章末 ...
- Elasticsearch集群和索引常用命令
https://www.cnblogs.com/pilihaotian/p/5846173.html REST API用途 ES提供了很多全面的API,大致可以分成如下几种: 1 检查集群.节点.索引 ...