Question

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/lexicographical-numbers/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Ideas
1、Answer( Java )
解法思路:数字字典序排序
⚡️Arrays.sort()
⚡️字典序,就是按照字典中出现的先后顺序进行排序。
⚡️数字字典序算法 简而言之,就是根据数字的前缀进行排序
//Arrays.sort(String[] a) String 类型数组会按照字典序排序
String[] string = new String[13];
for (int i = 0; i < 13; i++) {
string[i] = String.valueOf(i + 1);
}
Arrays.sort(string);
for (String s : string) {
System.out.print(s + " ");//输出 1 10 11 12 13 2 3 4 5 6 7 8 9
}
⚡️整个字典序排列也就是对 十叉树 进行 先序遍历。
每一个节点都拥有 10 个子节点,因为作为一个前缀,它后面可以接 0~9 这十个数字

👍Arrays.sort() 对整型数组默认是从小到大排序
@Test
public void test4() {
int[] res = new int[]{1, 2, 7, 8, 9, 10, 3, 4, 5, 6, 11, 12, 13};
Arrays.sort(res);
for (int i = 0; i < res.length; i++) {
System.out.print(res[i] + " ");//输出 1 2 3 4 5 6 7 8 9 10 11 12 13
}
}
Code
/**
* @author Listen 1024
* @description 386. 字典序排数(数字字典序排序)
* @date 2022-04-18 8:21
*/
class Solution {
public List<Integer> lexicalOrder(int n) {
String[] string = new String[n];
for (int i = 0; i < n; i++) {
string[i] = String.valueOf(i + 1);
}
Arrays.sort(string);
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < string.length; i++) {
res.add(Integer.parseInt(string[i]));
}
return res;
}
}
1775

被折叠的 条评论
为什么被折叠?



