关闭

Lexicographical Numbers

标签: leetcode
746人阅读 评论(2) 收藏 举报
分类:

题目描述:

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.

这个题主要是要想清楚下一个数究竟是多少,已经最简单的循环终止条件是什么。

public List<Integer> lexicalOrder(int n) {
	List<Integer> list=new ArrayList<Integer>();
	if(n<1)
		return list;
	list.add(1);
	//list包含了n个数就结束咯
	while(list.size()<n){
		int lastNum=list.get(list.size()-1);
		if(lastNum*10<=n){
			list.add(lastNum*10);
		}else{
			//如果是10的倍数就除以10的n次方
			if((lastNum+1)%10==0){
				list.add(divideNTen(lastNum+1));
			}else{
				if(lastNum+1<=n){
					list.add(lastNum+1);
				}else{
					list.add(lastNum/10+1);
				}
			}
		}
	}
	return list;
}

public int divideNTen(int n){
	while(n%10==0){
		n/=10;
	}
	return n;
}


0
1
查看评论

LeetCode—386. Lexicographical Numbers

Lexicographical Numbers思路:最直观的思路就是分治,但是太无趣了。直接上计数。 把每个数当做11进制来看,先确定好数的范围,根据n可以知道最大为k位数,然后把每个数都转成11进制,不足k位的后面补x补齐。 每个位置上面,x表示0,其他10个数字表示自身+1,这是一个...
  • corpsepiges
  • corpsepiges
  • 2016-08-22 15:05
  • 1505

386. Lexicographical Numbers

ProblemGiven 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 al...
  • lqcsp
  • lqcsp
  • 2016-08-21 16:28
  • 1003

LeetCode笔记:386. Lexicographical Numbers

将n个数字在限定时间内排序成词典顺序
  • Cloudox_
  • Cloudox_
  • 2017-04-18 09:16
  • 801

字典序—lexicographical order

字典序简介  英文名叫lexicographical order,数字可以作为特别的字符串,这种情况下,如果我们用字典序进行比较,就有可能会出现下面这种情况:   ”100”<”1000”(加引号的目的是为了区别数字与数字串),事实上,在计算机里,我们会这么看?   和之前一样,我们会首先...
  • oNever_say_love
  • oNever_say_love
  • 2015-11-03 16:05
  • 3831

C++之lexicographical_compare

转载地址:http://hi.baidu.com/nicker2010/item/8ca1a371c7d3984eee1e53ea
  • lijun5635
  • lijun5635
  • 2014-04-29 20:39
  • 1731

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 ...
  • yeshiwu
  • yeshiwu
  • 2016-08-30 10:43
  • 746

【Leetcode】386. Lexicographical Numbers

题目链接:https://leetcode.com/problems/lexicographical-numbers/ 题目: Given an integer n, return 1 - n in lexicographical order.For example, given 13, ret...
  • yeqiuzs
  • yeqiuzs
  • 2016-09-15 16:29
  • 1545

LeetCode 386 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...
  • niuooniuoo
  • niuooniuoo
  • 2016-08-26 21:09
  • 666

leetcode oj java 386. 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,...
  • u011060119
  • u011060119
  • 2017-01-14 10:26
  • 106

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 algorith...
  • u012319493
  • u012319493
  • 2016-08-27 19:39
  • 408
    个人资料
    • 访问:63009次
    • 积分:2761
    • 等级:
    • 排名:第15350名
    • 原创:215篇
    • 转载:37篇
    • 译文:0篇
    • 评论:6条
    最新评论