当前位置:网站首页>Dictionary sort of array
Dictionary sort of array
2022-06-11 00:38:00 【Alexon Xu】
It's interesting to meet a problem in front-end time , Share it :
subject : Give you a non repeating array of unordered integers nums, Require dictionary sort of output array
Dictionary sorting is not an ordinary sort , He needs to sort each digit of the number ,
Such as unordered array :nums=[12, 14, 11, 39, 339, 345, 1, 2, 4, 555, 501, 45, 29, 23, 25]
The output dictionary order is :[1, 11, 12, 14, 2, 23, 25, 29, 339, 345, 39, 4, 45, 501, 555]
according to leetcode Provided by the great God above 1-n The numerical code sorting solution of Gongshui three leaf great God To transform , You can write a suitable solution 
Pictured above , The main idea is to recurse the multitree , The first level root node is 0, It can be omitted ; The second level is :1、2、3…9, The third level is two digits :10,11,12,....19, 21,22,23...29,......91,92,...99; The fourth level is three digits ,100,101,....109,110,111,112,...119,...190,191,192,...199; The fifth level is four digits , By analogy
The detailed code is as follows :
public class ArrayToLexicalOrder {
public static void main(String[] args) {
System.out.println(arrayToLexicalOrder(new int[]{
12, 14, 11, 39, 339, 345, 1, 2, 4, 555, 501, 45, 29, 23, 25}));
}
public static List<Integer> arrayToLexicalOrder(int[] nums) {
// Get the maximum number
int max = getMax(nums);
List<Integer> resList = new ArrayList<>();
// Array to List, It is convenient for later judgment
List<Integer> targetList = Arrays.stream(nums).boxed().collect(Collectors.toList());
// Recursive bit start
for (int i = 1; i <= 9; i++) {
dfs(i, max, targetList, resList);
}
return resList;
}
private static int getMax(int[] nums) {
if (nums == null || nums.length == 0) return Integer.MIN_VALUE;
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
if (max < nums[i]) max = nums[i];
}
return max;
}
private static void dfs(Integer cur, Integer max, List<Integer> targetList, List<Integer> resList) {
// Set maximum , More than the , It means that recursion can end
if (cur > max) return;
// If the current number is in the list , Add to result list
if (targetList.contains(cur)) resList.add(cur);
// dfs With cur The first values ( Such as cur=2, The recursive range here is :20、21、22、23....29)
for (int i = 0; i <= 9; i++) {
dfs(cur * 10 + i, max, targetList, resList);
}
}
}
Output results :
边栏推荐
猜你喜欢

How word removes the header line

Blog recommendation | building IOT applications -- Introduction to flip technology stack

JVM 垃圾回收机制和常见的垃圾回收器
![[JVM] thread](/img/fc/c1f2eeaca639f319a4ef33f5fa6658.png)
[JVM] thread

【JVM】线程

452. detonate the balloon with the minimum number of arrows
![[network planning] 2.2.3 user server interaction: cookies](/img/a8/74a1b44ce4d8b0b1a85043a091a91d.jpg)
[network planning] 2.2.3 user server interaction: cookies

MESI cache consistency protocol for concurrent programming

动态规划经典题目三角形最短路径

【JVM】内存模型
随机推荐
学习笔记:插件化Activity之Hook点位
SQL statement -- enter the month, query the date (month, year, day), and output the month
763. dividing alphabetic intervals
文件缓存和session怎么处理?
LeetCode 1673. 找出最具竞争力的子序列**
What is thread in concurrent programming
【无标题】
[JVM] memory model
How word inserts a guide (dot before page number) into a table of contents
富文本活动测试1
测试下吧先
VTK例子--三個相交的平面
Unable to return to the default page after page Jump
[untitled]
Download Google gcr IO image
unity 网格面片生成抛物线,折线
With a market value of 21.5 billion yuan, will the post-80s generation in Sichuan make TV history?
Mysql database table backup
Synchronized keyword for concurrent programming
Go language channel understanding and use