精華區beta Marginalman 關於我們 聯絡資訊
※ 引述《ZooseWu (動物園 公告)》之銘言: : 2785. Sort Vowels in a String : 給你一個字串 : 子音不動母音自己排序之後回傳 : * 照著ASCII value排序 : * 母音 = a e i o u : Input: s = "lEetcOde" : Output: "lEOtcede" : Input: s = "lYmpH" : Output: "lYmpH" 思路: 1.第一眼覺得要用計數排序 然後提交ac99%可以去睡覺了 Java Code: ---------------------------------------------- class Solution { public String sortVowels(String s) { char[] res = s.toCharArray(); int[] count = new int[128]; for (char ch : res) { count[ch]++; } String vowels = "AEIOUaeiou"; int vIndex = 0; for (int i = 0; i < res.length; i++) { if (isVowels(res[i])) { while (count[vowels.charAt(vIndex)] == 0) { vIndex++; } res[i] = vowels.charAt(vIndex); count[vowels.charAt(vIndex)]--; } } return new String(res); } private boolean isVowels(char c) { switch (c) { case 'a' : case 'A' : case 'e' : case 'E' : case 'i' : case 'I' : case 'o' : case 'O' : case 'u' : case 'U' : return true; default: return false; } } } ---------------------------------------------- 晚安 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.100.73.13 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1699891836.A.BEE.html