回答
商店字一個字符串變量和循環結束後返回的字符串。
您可以將每個句子分成(這個詞是一個提示)到List
的單詞中,並獲得最大元音量的單詞。
您將以每個句子的一個單詞(「最大」)結束,再次處理這個List
的單詞(您可以在這裏做一個簡潔的遞歸調用),並且您將獲得文字中含有最多元音的單詞。
我建議你看看由Collections提供的靜態方法,尤其是:
public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp)
,並最終
public static <T> void sort(List<T> list, Comparator<? super T> c)
所有你所要做的就是實現一個Comparator這之間確定兩個詞是哪一個元音的數量最多。這幾乎是兩行代碼,聽起來像是作業。
編輯:最後,因爲他到處都是劇透,那就是解決方案。隨着記憶化有一種方法,以商更好的性能,但是這不是重點我想
final static Comparator <String> vowelComparator = new Comparator<String>() {
@Override
public final int compare(final String word1, final String word2) {
return vowelCount(word1) - vowelCount(word2);
}
private final int vowelCount(final String word) {
int count = 0;
for (final char c : word.toCharArray()) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
count++;
}
return count;
}
};
public static void main(String...args) {
//Sentences
final List<String> sentences = new ArrayList<String>(3){{
add("This is my first line");
add("Hohoho is my second astonishing line");
add("And finally here is my last line");
}};
//Store the words with highest number of vowels/sentence
final List<String> interestingWords = new LinkedList<>();
for (final String sentence : sentences) {
interestingWords.add(Collections.max(Arrays.asList(sentence.split(" ")), vowelComparator));
}
System.out.println(Collections.max(interestingWords, vowelComparator));
}
String[] words = yourString.split(" ");
int max = 0;
for(String myStr: words)
max = Math.max(max,myStr.split("[aeiou]").length);
for(String myStr: words)
if(myStr.split("[aeiou]").length == max)
return myStr;
似乎是一個不必要的昂貴的數字計數方式......尤其是如果你這樣做了兩次。 – Dmitri
我想你可以跟蹤哪個字符串具有最大值,但它確實沒有必要,因爲編譯器應該優化它。 – AJMansfield
@AJMansfield不是很有建設性的答案。也許可以嘗試一個不太簡潔的代碼示例,它可能更具信息性和指導性? – travega
String myString = "Java is magical";
// 1. Split your string into an array of words.
String[] words = myString.split(" ");
// 2. Initialise a max vowel count and current max count string variable
int maxVowelCount = 0;
String wordWithMostVowels = "";
// 3. Iterate over your words array.
for (String word : words) {
// 4. Count the number of vowel in the current word
int currentVowelCount = word.split("[aeiou]", -1).length;
// 5. Check if it has the most vowels
if (currentVowelCount > maxVowelCount) {
// 6. Update your max count and current most vowel variables
wordWithMostVowels = word;
maxVowelCount = currentVowelCount;
}
}
// 6. Return the word with most vowels
return wordWithMostVowels;
你可能會想在一個方法來包裝這個功能,併到過你「的myString的價值方法。
這裏的東西我會考慮簡單,易讀,正確的是:
public static String findMaxVowels(Collection<String> text) {
String best = null;
int max = 0;
for (String line : text) {
// may need a better definition of "word"
for (String word : line.split("\\s+")) {
int count = countChars(word.toLowerCase(), "aeiou");
if (count > max) {
max = count;
best = word;
}
}
}
return best;
}
public static int countChars(String text, String chars) {
int count = 0;
for (char c : text.toCharArray())
if (chars.indexOf(c) >= 0)
count += 1;
return count;
}
- 1. 「檢查一個字母是否是元音」 - 函數返回false?
- 2. 功能:返回非元音字符串
- 3. AES返回字母數字
- 4. 程序打印最多元音字數
- 5. 返回每個元音
- 6. 最後在java中返回
- 7. 返回包含最後的元音音素和輔音後續音素(或多個)列表
- 8. 字符串 - 大寫元音,下一個字母的字母 - Javascript
- 9. JSON.parse()返回字母順序
- 10. 返回Erlang中的元音列表
- 11. 阿爾法指數不返回重音字母
- 12. 如何返回python中最重複的字母?
- 13. 元音字母邊界處的元音正則表達式
- 14. 從列表中返回元素考慮字母表規則
- 15. 返回三個字符串的最後一個字母大寫
- 16. 字母→下一個字母和大寫元音
- 17. 這是隻返回最大的字母詞,當我想要它返回每個字母詞的計數
- 18. 詞通過字母/元音翻譯
- 19. 扭轉和消除元音位置的字母在字符串中的Java
- 20. 查找字符串中的第一個字母和第一個元音java
- 21. 試圖返回字符串中元音的索引
- 22. 返回字符串中元音數量的函數
- 23. Excel - 返回最後一個特定字母結尾的單元格
- 24. Java/startsWith多個字母?
- 25. Java JtextArea多個字母
- 26. Python中的重音字母
- 27. 從用戶輸入中刪除元音的程序,並在java中按字母順序顯示其餘字母
- 28. 算法,吐出最小和最大的子字符串,以元音開始,並以字母串輔音結尾
- 29. 音譯C中的重音字母
- 30. 返回多少次出現在每個字母在字符串
商店當前詞最元音和當前元音變量數。當你循環你的單詞,如果你檢查的單詞有更多的元音,那麼當前的冠軍,然後用該單詞替換當前的冠軍,並計數你的檢查。 –
+1:沒想到對這樣一個簡單問題的答案如此多樣。 – Dmitri
可以有多個獨特的單詞與最多的元音數量,所以記得要返回設置。 –
Srimathi