如何对排列进行排序

  • 本文关键字:排序 排列 java
  • 更新时间 :
  • 英文 :


>我已经编译了我的代码并且它工作正常,我目前正在尝试添加一个排序方法,以使我的排列具有降序排序函数。

我尝试了许多数组排序函数。我不知道我需要更改什么才能配置排序方法。

import java.util.*;
public class Permutation { 

public static void main(String[] args) 
{ 
String str;
Scanner in = new Scanner(System.in);
System.out.println("Enter details");
str = in.nextLine();
System.out.println("You entered " + str);
int n = str.length(); 
Permutation permutation = new Permutation(); 
permutation.permute(str, 0, n - 1); 
} 
/** 
* permutation function 
* @param str string to calculate permutation for 
* @param l starting index 
* @param r end index 
*/
private void permute(String str, int l, int r) 
{ 
if (l == r) 
System.out.println(str); 
else { 
for (int i = l; i <= r; i++) { 
str = swap(str, l, i); 
permute(str, l + 1, r); 
//str = swap(str, l, i);
}
} 
} 
/** 
* Swap Characters at position 
* @param a string value 
* @param i position 1 
* @param j position 2 
* @return swapped string 
*/
public String swap(String a, int i, int j) 
{ 
char temp; 
char charArray[] = a.toCharArray(); 
temp = charArray[i]; 
charArray[i] = charArray[j];
charArray[j] = temp; 
return String.valueOf(charArray);
}
} 

我的程序工作正常,我得到了结果。我只需要以降序格式对结果进行排序。我在完成此功能时遇到问题。

我认为您有两种方法可以解决问题。

  1. 您不是一次打印出排列,而是在permutate()的开头创建一个数组String[] permutatedStrings,并在现在打印它们的位置添加排列字符串。在方法结束时,执行Arrays.sort(permutatedStrings, Comparator<String>.comparingInt((String str) -> Integer.parseInt(str) * -1);
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Permutation {
public static void main(String[] args)
{
String str;
Scanner in = new Scanner(System.in);
System.out.println("Enter details");
str = in.nextLine();
in.close();
System.out.println("You entered " + str);
str = str.replaceAll("\D+", "");
// Eliminating excess character, leaving integers
try {
Integer.parseInt(str);
} catch (NumberFormatException e) {
System.out.println("No Valid Numbers");
}
// Error exception is thrown where there is no integer present
int n = str.length();
Permutation permutation = new Permutation();
ArrayList<String> permutatedStrings = new ArrayList<>();
ArrayList<String> resultList = permutation.permute(permutatedStrings, str, 0, n - 1);
Object[] result = resultList.toArray();
Arrays.sort(result, Comparator.comparingInt((Object string) -> Integer.parseInt((String) string) * -1));
for (Object object : result) {
System.out.println((String) object);
}
}
/**
* permutation function
* 
* @param str string to calculate permutation for
* @param l   starting index
* @param r   end index
*/
private ArrayList<String> permute(ArrayList<String> permutatedStrings, String str, int l, int r) {
if (l == r) {
permutatedStrings.add(str);
} else {
for (int i = l; i <= r; i++) {
str = swap(str, l, i);
permute(permutatedStrings, str, l + 1, r);
// str = swap(str, l, i);
}
}
return permutatedStrings;
}
/**
* Swap Characters at position
* 
* @param a string value
* @param i position 1
* @param j position 2
* @return swapped string
*/
public String swap(String a, int i, int j) {
char temp;
char charArray[] = a.toCharArray();
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return String.valueOf(charArray);
}
}
  1. 第二种方法是按顺序创建排列。首先,对数字进行排序。然后你按照Nayuki计划进行体育锻炼。

我有一个可读的解决方案给你。添加了一些评论。

import java.util.*;
public class Permutation {
public static void main(String[] args) {
String str;
Scanner in = new Scanner(System.in);
System.out.println("Enter details");
str = in.nextLine();
System.out.println("You entered " + str);

str = str.replaceAll("\D+", "");
//Eliminating excess character, leaving integers
try {
Integer.parseInt(str);
} catch (NumberFormatException e) {
System.out.println("No Valid Numbers");
}
// Error exception is thrown where there is no integer present
int n = str.length();
Permutation permutation = new Permutation();
// permutations get accumulated in here
Set<String> result = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
//reversed order - o2.compareTo(o1) - results in descending order out of the box -
// no need to cast to integer all call implicit sort().
// As a bonus: you don't care about possible duplicates.
return o2.compareTo(o1);
}
});
permutation.permute(str, 0, n - 1, result);
// listing is here now
result.forEach(res -> System.out.println(res));
}
/**
* permutation function
*
* @param str string to calculate permutation for
* @param l   starting index
* @param r   end index
*/
private Set<String> permute(String str, int l, int r, Set<String> result) {
if (l == r) {
result.add(str);
return result;
// too early to print!
//System.out.println(str);
} else {
for (int i = l; i <= r; i++) {
str = swap(str, l, i);
// result of the call can be ignored
permute(str, l + 1, r, result);
//str = swap(str, l, i);
}
}
// unreachable, it's OK
return null;
}
/**
* Swap Characters at position
*
* @param a string value
* @param i position 1
* @param j position 2
* @return swapped string
*/
public String swap(String a, int i, int j) {
char temp;
char charArray[] = a.toCharArray();
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return String.valueOf(charArray);
}
} 

最新更新