英文:
JAVA Sort String Array Add Missing Numbers
问题
在Java中,我有一个字符串数组。它包含了不同编号的字符串。例如:x1,x3,x5,x9,y1,y3,y6,y9,z14,z17,z22,z50。我该如何按照字母和数字对这个数组进行排序,并按顺序添加缺失的字符串元素(x2等)?
英文:
In java, I have a string array. It contains strings with different numbers. For example: x1,x3,x5,x9,y1,y3,y6,y9,z14,z17,z22,z50. How can I sort this array by both letters and numbers, and add the missing string elements (x2...) in order?
答案1
得分: 1
这是解决方案的翻译:
1. 对数组进行排序。
2. 获取每个字母的最小和最大数字,并将它们保存在不同的映射中。
3. 对于每个字母,从其保存的范围内添加到列表中。
以下是解决方案:
public static void main(String[] args) {
String[] arr = new String[] {"x1","x3","x5","x9","y1","y3","y6","y9","z14","z17","z22","z50"};
System.out.println(Arrays.toString(addMissing(arr)));
}
private static String[] addMissing(String[] arr) {
Arrays.sort(arr);
Map<String,Integer> min = new HashMap<>();
Map<String,Integer> max = new HashMap<>();
for(int i = 0; i < arr.length; i++) {
String str = arr[i];
int number = getNum(str);
String letter = getChar(str);
if(min.containsKey(letter)) {
if(min.get(letter) > number)
min.put(letter, number);
}else {
min.put(letter, number);
}
if(max.containsKey(letter)) {
if(max.get(letter) < number)
max.put(letter, number);
}else {
max.put(letter, number);
}
}
List
for(String key : min.keySet()) {
int minNumber = min.get(key), maxNumber = max.get(key);
for(int i = minNumber; i <= maxNumber; i++)
list.add(key+i);
}
String[] res = new String[list.size()];
for(int i = 0; i < list.size(); i++) res[i] = list.get(i);
return res;
}
private static String getChar(String str) {
return str.replaceAll("\d", "");
}
private static int getNum(String str) {
return Integer.parseInt(str.replaceAll("\D+", ""));
}
输出:
[x1, x2, x3, x4, x5, x6, x7, x8, x9, y1, y2, y3, y4, y5, y6, y7, y8, y9, z14, z15, z16, z17, z18, z19, z20, z21, z22, z23, z24, z25, z26, z27, z28, z29, z30, z31, z32, z33, z34, z35, z36, z37, z38, z39, z40, z41, z42, z43, z44, z45, z46, z47, z48, z49, z50]
希望这有助于您理解代码和解决方案。
英文:
A simple way to do this would be the following:
- Sort the array.
- Get the minimum and the maximum number of each letter and save them in separate maps.
- For each letter, add to a list from the specified saved range of it.
Here is the solution:
public static void main(String[] args) {
String[] arr = new String[] {"x1","x3","x5","x9","y1","y3","y6","y9","z14","z17","z22","z50"};
System.out.println(Arrays.toString(addMissing(arr)));
}
private static String[] addMissing(String[] arr) {
Arrays.sort(arr);
Map<String,Integer> min = new HashMap<>();
Map<String,Integer> max = new HashMap<>();
for(int i = 0; i < arr.length; i++) {
String str = arr[i];
int number = getNum(str);
String letter = getChar(str);
if(min.containsKey(letter)) {
if(min.get(letter) > number)
min.put(letter, number);
}else {
min.put(letter, number);
}
if(max.containsKey(letter)) {
if(max.get(letter) < number)
max.put(letter, number);
}else {
max.put(letter, number);
}
}
List<String> list = new ArrayList<>();
for(String key : min.keySet()) {
int minNumber = min.get(key), maxNumber = max.get(key);
for(int i = minNumber; i <= maxNumber; i++)
list.add(key+i);
}
String[] res = new String[list.size()];
for(int i = 0; i < list.size(); i++) res[i] = list.get(i);
return res;
}
private static String getChar(String str) {
return str.replaceAll("\\d", "");
}
private static int getNum(String str) {
return Integer.parseInt(str.replaceAll("\\D+",""));
}
Output:
[x1, x2, x3, x4, x5, x6, x7, x8, x9, y1, y2, y3, y4, y5, y6, y7, y8, y9, z14, z15, z16, z17, z18, z19, z20, z21, z22, z23, z24, z25, z26, z27, z28, z29, z30, z31, z32, z33, z34, z35, z36, z37, z38, z39, z40, z41, z42, z43, z44, z45, z46, z47, z48, z49, z50]
答案2
得分: 0
尝试一下这个。
String[] arr = new String[] {"x1","x3","x5","x9","y1","y3","y6","y9","z14","z17","z22","z50"};
List<String> result = Arrays.stream(arr)
.collect(Collectors.groupingBy(s -> s.substring(0, 1),
Collectors.mapping(s -> Integer.parseInt(s.substring(1)), Collectors.toList())))
.entrySet().stream()
.flatMap(e -> IntStream.rangeClosed(
e.getValue().stream().mapToInt(Integer::intValue).min().getAsInt(),
e.getValue().stream().mapToInt(Integer::intValue).max().getAsInt())
.mapToObj(i -> e.getKey() + i))
.sorted()
.collect(Collectors.toList());
System.out.println(result);
输出结果
[x1, x2, x3, x4, x5, x6, x7, x8, x9, y1, y2, y3, y4, y5, y6, y7, y8, y9, z14, z15, z16, z17, z18, z19, z20, z21, z22, z23, z24, z25, z26, z27, z28, z29, z30, z31, z32, z33, z34, z35, z36, z37, z38, z39, z40, z41, z42, z43, z44, z45, z46, z47, z48, z49, z50]
groupingBy
的中间结果
{x=[1, 3, 5, 9], y=[1, 3, 6, 9], z=[14, 17, 22, 50]}
英文:
Try this.
String[] arr = new String[] {"x1","x3","x5","x9","y1","y3","y6","y9","z14","z17","z22","z50"};
List<String> result = Arrays.stream(arr)
.collect(Collectors.groupingBy(s -> s.substring(0, 1),
Collectors.mapping(s -> Integer.parseInt(s.substring(1)), Collectors.toList())))
.entrySet().stream()
.flatMap(e -> IntStream.rangeClosed(
e.getValue().stream().mapToInt(Integer::intValue).min().getAsInt(),
e.getValue().stream().mapToInt(Integer::intValue).max().getAsInt())
.mapToObj(i -> e.getKey() + i))
.sorted()
.collect(Collectors.toList());
System.out.println(result);
output
[x1, x2, x3, x4, x5, x6, x7, x8, x9, y1, y2, y3, y4, y5, y6, y7, y8, y9, z14, z15, z16, z17, z18, z19, z20, z21, z22, z23, z24, z25, z26, z27, z28, z29, z30, z31, z32, z33, z34, z35, z36, z37, z38, z39, z40, z41, z42, z43, z44, z45, z46, z47, z48, z49, z50]
Intermediate result of groupingBy
{x=[1, 3, 5, 9], y=[1, 3, 6, 9], z=[14, 17, 22, 50]}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论