问题描述

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的更多相关文章

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

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

  2. 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 ...

  3. Leetcode算法比赛----Longest Absolute File Path

    问题描述 Suppose we abstract our file system by a string in the following manner: The string "dir\n ...

  4. LeetCode算法题-Self Dividing Numbers(Java实现)

    这是悦乐书的第305次更新,第324篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第173题(顺位题号是728).自分割数是一个可被其包含的每个数字整除的数字.例如,12 ...

  5. LeetCode算法题-Sum of Square Numbers(Java实现)

    这是悦乐书的第276次更新,第292篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第144题(顺位题号是633).给定一个非负整数c,判断是否存在两个整数a和b,使得a的 ...

  6. LeetCode算法题-Maximum Product of Three Numbers(Java实现)

    这是悦乐书的第275次更新,第291篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第143题(顺位题号是628).给定一个整数数组,从其中找出三个数,使得乘积最大.例如: ...

  7. LeetCode算法题-Find All Numbers Disappeared in an Array(Java实现)

    这是悦乐书的第232次更新,第245篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第99题(顺位题号是448).给定一个整数数组,其中1≤a[i]≤n(n =数组的大小) ...

  8. LeetCode算法题-Heaters(Java实现)

    这是悦乐书的第239次更新,第252篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第106题(顺位题号是475).冬天来了!您在比赛期间的第一份工作是设计一个固定温暖半径 ...

  9. LeetCode算法题-Two Sum II - Input array is sorted

    这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...

随机推荐

  1. 1091 N-自守数 (15 分)

    // 建一个判断函数,接受两个整形的变量,再通过循环按位判断相等与否,主体函数中调用被调函数,建立一个判断变量.#include <iostream> using namespace st ...

  2. python学习,day4:装饰器的使用示例

    ---恢复内容开始--- 装饰器:本质是函数,(装饰其他函数)就是为其他函数添加附加功能 装饰器有其独特的原则:1.不能修改被装饰的函数的源代码 2.不能修改被装饰的函数的调用方式 例子 import ...

  3. windows 下加载执行hta文件的方法

    首先编写这么一个hta的文件: <html> <head> <script> s = new ActiveXObject("WScript.Shell&q ...

  4. C# 文件读写系列二

    读取文件原则上非常简单,但它不是通过FileInfo和DirectoryInfo来完成的,关于FileInfo和DirectoryInfo请参考C# 文件操作系列一,在.Net Framework4. ...

  5. javascript中的抽象相等==与严格相等===

    1.数据类型:String,Number,Boolean,Object,Null,Undefined 2.抽象相等:x==y A.两者数据类型相同:typeof x == typeof y a.Str ...

  6. js小技巧--摘录1

    原文地址https://github.com/loverajoel/jstips 1.数组中插入元素 a.尾部追加 var arr = [1,2,3,4,5]; var arr2 = []; arr. ...

  7. Hive 外部表新增字段或者修改字段类型等不生效

    标题比较笼统,实际情况是: 对于Hive 的分区外部表的已有分区,在对表新增或者修改字段后,相关分区不生效. 原因是:表元数据虽然修改成功,但是分区也会对应列的元数据,这个地方不会随表的元数据修改而修 ...

  8. JavaScript数据结构-6.优先队列

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. apollo应用配置集成及使用

    apollo应用配置集成及使用 1. 开发环境Apollo地址     用户名:apollo 密码:admin     开发环境Apollo管理台地址:http://localhost:8070/   ...

  10. 【原】jQuery easyUI 快速搭建前端框架

    jQueryEasyUI jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不需要 ...