java—使用选择排序对字符串中的字符进行排序

qyswt5oh  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(326)

我的代码有什么错误?
给定一个由小写字母组成的字符串,将其所有字母按升序排列。
输入:输入的第一行包含t,表示测试用例的数量。然后遵循每个测试用例的描述。testcase的第一行包含表示字符串长度的正整数n。第二行包含字符串。
输出:对于每个testcase,输出排序的字符串。
约束条件:

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
avwztpqn

avwztpqn1#

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);
        }
    }
}

这个代码做你想做的。我将>改为<,并从未排序的字符串中删除了添加到已排序字符串的每个字符。这样我们就不需要一次又一次地处理同一个字符。

fhg3lkii

fhg3lkii2#

你可以用 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

另请参见:
•如何在密码验证中不使用特殊字符(没有regex)?
•java选择排序

6qftjkof

6qftjkof3#

你不能交换房间的位置 min 找到它之后的角色。但是 String java中的字符是不可变的,因此不能交换其中字符的位置。我建议你把你的字符串转换成 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;
}

相关问题