【问题标题】:Sorting characters in a string using selection sort使用选择排序对字符串中的字符进行排序
【发布时间】:2021-03-17 19:38:16
【问题描述】:
我的代码有什么错误?
给定一个由小写字母组成的字符串,将其所有字母按升序排列。
输入:输入的第一行包含 T,表示测试用例的数量。然后是每个测试用例的描述。测试用例的第一行包含正整数 N,表示字符串的长度。第二行包含字符串。
输出:对于每个测试用例,输出排序后的字符串。
约束:
1 <= T <= 100
1 <= N <= 100
import java.util.*;
class GFG {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 1; i <= t; i++) {
int n = sc.nextInt();
sc.nextLine();
String S = sc.nextLine();
String sor = "";
for (int j = 0; j < n; j++) {
int min = j;
for (int k = j + 1; k < n; k++) {
if (S.charAt(k) > S.charAt(min)) {
min = k;
}
}
sor += S.substring(min, min + 1);
}
System.out.println(sor);
}
}
}
对于输入:
1
5
edcab
输出:
edcbb
预期输出:
abcde
【问题讨论】:
标签:
java
string
sorting
selection-sort
【解决方案1】:
import java.util.*;
class GFG {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 1; i <= t; i++) {
int n = sc.nextInt();
sc.nextLine();
String S = sc.nextLine();
System.out.println("S: "+S);
String sor = "";
for (int j = 0; n > 0; j++) {
int min = 0;
for (int k = 0; k < n; k++) {
if (S.charAt(k) < S.charAt(min)) {
min = k;
}
}
sor += S.substring(min, min + 1);
S = S.substring(0, min) + S.substring(min + 1);
n--;
}
System.out.println(sor);
}
}
}
此代码执行您想要的操作。我将 > 更改为
【解决方案2】:
找到min 字符后,您不会交换它的位置。但是java中的Strings 是不可变的,所以你不能交换其中字符的位置。我建议您将您的字符串转换为char[],以便您可以交换字符:
public static void main (String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i=1; i<=t; i++){
int n = sc.nextInt();
sc.nextLine();
String S = sc.nextLine().toCharArray(); // convert it to char array
char[] sor = new char[S.length];
for(int j=0; j<n; j++){
int min = j;
for(int k =j+1; k<n; k++){
if(S[k]<S[min]){
min = k;
}
}
swap(S, min, j);
sor[j] = S[min]
}
System.out.println(new String(sor));// reconvert to string
}
}
public static void swap(char[] c,int x,int y){
char temp= c[x];
c[x] = c[y];
c[y] = temp;
}
【解决方案3】:
您可以使用String.toCharArray 方法遍历此字符串的字符数组char[],对其十进制值进行排序并返回包含排序数组字符的字符串:
public static void main(String[] args) {
String str = "edcab";
String sorted = selectionSort(str.toCharArray());
System.out.println(sorted); // abcde
}
public static String selectionSort(char[] arr) {
// iterate over all subsets of the array
// (0-last, 1-last, 2-last, 3-last, ...)
for (int i = 0; i < arr.length; i++) {
// assume the min is
// the first element
char min = arr[i];
// index of the
// min element
int min_i = i;
// check the elements
// after i to find
// the smallest
for (int j = i + 1; j < arr.length; j++) {
// if this element
// is less, then it
// is the new min
if (arr[j] < min) {
min = arr[j];
min_i = j;
}
}
// if min element is not
// equal to the current
// one, then swap them
if (i != min_i) {
char temp = arr[i];
arr[i] = arr[min_i];
arr[min_i] = temp;
}
}
return String.valueOf(arr);
}
您可以使用String.codePoints 方法迭代此字符串的字符的int 值,对它们进行排序并收集另一个排序后的字符串:
String str = "edcab";
String sorted = str.codePoints()
.sorted()
.mapToObj(Character::toString)
.collect(Collectors.joining());
System.out.println(sorted); // abcde
另见:
• How do I not take Special Characters in my Password Validation (without Regex)?
• Java Selection Sort