簡體   English   中英

給定數字N和N個字符串數組,請基於每個字符串中的元音數量以降序對字符串進行排序

[英]Given a number N and an array of N strings, sort the strings based on number of vowels in each of the strings in the descending order

我已經成功地計算了字符串數組中每個元素的元音數量。 但是我無法比較它們並打印出元音數量最少的數組元素。 請幫幫我。 這是我到目前為止編寫的代碼。

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int N = s.nextInt();
    int vowelcount = 0;
    int maxcount = 0, sum = 0;
    String a[] = new String[N];

    for(int i = 0; i < N; i++) {
        a[i] = s.next();
    }

    for(int i = 0; i < N; i++) {
        String str = a[i];
        for(int j = 0; j < str.length(); j++) {
            if(str.charAt(j) == 'a' || str.charAt(j) == 'e' || str.charAt(j) == 'i'
                    || str.charAt(j) == 'o' || str.charAt(j) == 'u') {
                vowelcount = vowelcount + 1;
            }
        }

        System.out.println(vowelcount);
        vowelcount = 0;
    }
}

我已經修改了您的代碼。請嘗試運行以下代碼

  public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    int N=s.nextInt();
    int vowelcount=0;
    int maxcount=0,sum=0;
    String a[]=new String[N];
    for(int i=0;i<N;i++){
        a[i]=s.next();
    }
    //added
    int minCount = Integer.MAX_VALUE;
    int minCountIndex = Integer.MAX_VALUE;
    //till here
    for(int i=0;i<N;i++){
        String str=a[i];
        for(int j=0;j<str.length();j++){
            if(str.charAt(j)=='a'||str.charAt(j)=='e'||str.charAt(j)=='i'||str.charAt(j)=='o'||str.charAt(j)=='u'){
                vowelcount=vowelcount+1;

            }

        } //Add below lines
        if(vowelcount < minCount) {
            minCount = vovelcount;
            minCountIndex = i;
        } //till here
        System.out.println(vowelcount);
        vowelcount=0;
    }
    System.out.println("String with Minimum Vovels :" + a[minCountIndex]);  // Added this

}

創建一個適合您的自定義編譯器類,如下所示:

static class SortDescendingByNumberOfVowels implements Comparator<String> {
    private int getNumberOfVowels(String str) {
        int counter = 0;
        for(int i = 0; i < str.length(); i++) {
            if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
                    || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
                counter += 1;
            }
        }
        return counter;
    }

    @Override
    public int compare(String str1, String str2) {
        return getNumberOfVowels(str2) - getNumberOfVowels(str1);
    }
}

然后使用上面的編譯器通過使用另一個Arrays.sort()簽名對數組進行排序,該簽名將Arrays.sort()器作為第二個參數:

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int N = Integer.parseInt(s.nextLine());
    String a[] = new String[N];

    for(int i = 0; i < N; i++) {
        a[i] = s.nextLine();
    }

    System.out.println("Initial array: " + Arrays.toString(a));

    Arrays.sort(a, new SortDescendingByNumberOfVowels());
    System.out.println("Sorted array : " + Arrays.toString(a));

    System.out.println("Item with less vowels: " + a[N - 1]);
}

您可以使用正則表達式,而不是比較各個字符以獲得元音計數

Pattern vowels = Pattern.compile("[aeiou]", Pattern.CASE_INSENSITIVE);
int numberOfVowels = value.length() - vowels.matcher(value).replaceAll("").length();

您還可以使用流進行排序。

Pair fewest = Arrays.stream(values)
        .map(value -> new Pair(value.length() - vowels.matcher(value).replaceAll("").length(), value))
        .sorted((v1, v2) -> Integer.compare(v1.length, v2.length))
        .findFirst().orElse(null);

System.out.println(fewest.value);

這依賴於擁有長度和值的Pair類

private class Pair {

    int length;
    String value;

    Pair(int length, String value) {
        this.length = length;
        this.value = value;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM