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.

方法1:

利用排序,把数字按照lexicographical order 排序。但是最后通不过。

 public class Solution {
public List<Integer> lexicalOrder(int n) {
List<Integer> list = new ArrayList<>();
if (n < ) return list;
for (int i = ; i <= n; i++) {
list.add(i);
} Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
return String.valueOf(i1).compareTo(String.valueOf(i2));
}
});
return list;
}
}

方法2:来自leetcode discuss  https://discuss.leetcode.com/topic/55377/simple-java-dfs-solution

The idea is pretty simple. If we look at the order we can find out we just keep adding digit from 0 to 9 to every digit and make it a tree. Then we visit every node in pre-order.

    1        2        3    ...
/\ /\ /\
10 ...19 20...29 30...39 ....
 public class Solution {
public List<Integer> lexicalOrder(int n) {
List<Integer> res = new ArrayList<>();
for (int i = ; i < ; ++i) {
dfs(i, n, res);
}
return res;
} public void dfs(int cur, int n, List<Integer> res) {
if (cur > n) return;
res.add(cur);
for (int i = ; i < ; ++i) {
dfs( * cur + i, n, res);
}
}
}

Lexicographical Numbers的更多相关文章

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

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

  2. [LeetCode] Lexicographical Numbers 字典顺序的数字

    Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...

  3. Leetcode: Lexicographical Numbers

    Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...

  4. [Swift]LeetCode386. 字典序排数 | Lexicographical Numbers

    Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...

  5. 386. Lexicographical Numbers 输出1到n之间按lexico排列的数字序列

    [抄题]: Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,1 ...

  6. LeetCode - 386. Lexicographical Numbers

    Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...

  7. Leetcode算法比赛---- Lexicographical Numbers

    问题描述 Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10 ...

  8. 386. Lexicographical Numbers

    用DFS来做,先弄开头是1的,再弄开头是1的里面开头是1的,再开头是1的里面开头是1的里的开头是1的,再... 是吧-- 比N大了BREAK就行. 注意第一个循环是1-9,往后的循环是0-9. pub ...

  9. 386. Lexicographical Numbers 把1--n按字典序排序

    https://leetcode.com/problems/lexicographical-numbers/description/ 前20个是 1, 10, 11, 12, 13, 14, .... ...

随机推荐

  1. mysql rr 查询出现的事务情况

    select * from INFORMATION_SCHEMA.INNODB_TRX\G The INNODB_TRX table contains information about every ...

  2. js设置datagriad的行移动

    //                    ,//                formatter: function(value,row,index){//                    ...

  3. java之内部类与匿名内部类

    Java 内部类 分四种:成员内部类.局部内部类.静态内部类和匿名内部类. 1.成员内部类: 即作为外部类的一个成员存在,与外部类的属性.方法并列. 注意:成员内部类中不能定义静态变量,但可以访问外部 ...

  4. vijos1603迷宫

    这题的构思太巧妙了: 经典题目8 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值    把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j. ...

  5. Java [leetcode 9] Palindrome Number

    问题描述: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could n ...

  6. hdu 1712 ACboy needs your help

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. RMAN备份与恢复深入解<一>

    一 数据库版本 SQL> select *from v$version; BANNER ----------------------------------------------------- ...

  8. HDU 4393 Throw nails

    Throw nails Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. jdbc三种常见用法

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...

  10. Initializing nested object properties z

    public class Employee { public Employee() { this.Insurance = new Insurance(); } // Perhaps another c ...