386. Lexicographical Numbers

2024-08-23 20:38
文章标签 numbers 386 lexicographical

本文主要是介绍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 optimize your algorithm to use less time and space. The input size may be as large as 5,000,000.


给定一个int,以字典序返回小于这个int的所有值

从1开始 不断的深入递归,(1)-(10-(100-109)-   判断条件也很简单只要小于n就放到list中



public class Solution {public List<Integer> lexicalOrder(int n) {List<Integer> res=new ArrayList<Integer>();for(int i=1;i<=9;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=0;i<=9;i++){dfs(cur*10+i,n,res);}}
}


这篇关于386. Lexicographical Numbers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1100444

相关文章

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

高精度打表-Factoring Large Numbers

求斐波那契数,不打表的话会超时,打表的话普通的高精度开不出来那么大的数组,不如一个int存8位,特殊处理一下,具体看代码 #include<stdio.h>#include<string.h>#define MAX_SIZE 5005#define LEN 150#define to 100000000/*一个int存8位*/int num[MAX_SIZE][LEN];void

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

【codeforces】55D. Beautiful numbers 数位DP

传送门:【codeforces】55D. Beautiful numbers 题目分析:被每一位整除则用二进制记录已经包括的数字的个数,以及对2520取模后的状态。由于对5整除当且仅当最后一个数为0或5,对2整除当且仅当最后一个数为偶数,且1~9的最小公倍数为2520,不包括2,5后的最小公倍数为252,所以除最后一层对2520取模,其余时候都对252取模即可。由于整除的状态有限,最多只有

【SGU】113. Nearly prime numbers 合数分解

传送门:【SGU】113. Nearly prime numbers 题目分析:O(sqrt(N))。。 代码如下: #include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std ;#define rep( i , a , b ) for

USACO Section 3.1 Humble Numbers

题意: 已知一个集合S  由S的任意子集作为因子  可构造出一个数字  求  这些构造出的数字中第k大的数字是多少 思路: 拿到这题就被“数字不是很多而且比较连续暴力枚举就好”这个思路迷惑了  果断TLE… 跪了一次后想到通过bfs构造可取  这时用了queue维护bfs  用priority_queue维护答案(大顶堆  内部最多k个数字)  用set判重复(5*2=2*5)  写

Sum of Square Numbers

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. Example 1: Input: 5Output: TrueExplanation: 1 * 1 + 2 * 2 = 5 Example 2: Inpu

JavaScript - First step - Numbers and operators

Types of numbers Integers 整数Floating point numbers 单精度浮点数Doubles 双精度浮点数Binary 二进制Octal 八进制Hexadecimal 十六进制 Arithmetic operators 算术运算符 + 加法- 减法* 乘法/ 除法% 求余** 指数 (次方 5 ** 5 = 5 * 5 * 5 * 5 * 5) Oper

leetcode 902. Numbers At Most N Given Digit Set

题目链接 Given an array of digits which is sorted in non-decreasing order. You can write numbers using each digits[i] as many times as we want. For example, if digits = ['1','3','5'], we may write number