作者Rushia (みけねこ的鼻屎)
看板Marginalman
標題Re: [閒聊] 每日LeetCode
時間Fri Nov 4 10:05:10 2022
345. Reverse Vowels of a String
給你一個字串要你把他的母音字母都反轉。
Example:
Input: s = "hello"
Output: "holle"
思路:
1.寫一個判斷字母是否是母音的函數。
2.雙指標找到左邊和右邊的第一個母音。
3.把兩個母音交換之後繼續往字串裡面緊縮直到 i == j。
JavaCode:
class Solution {
public String reverseVowels(String s) {
int n = s.length();
char[] chars = s.toCharArray();
int i = 0, j = n - 1;
while (i < j) {
while (i < j && !isVowel(chars[i])) i++;
while (i < j && !isVowel(chars[j])) j--;
char tmp = chars[i];
chars[i++] = chars[j];
chars[j--] = tmp;
}
return new String(chars);
}
private boolean isVowel(char c) {
switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
return true;
default:
return false;
}
}
}
☹☹☹
--
https://i.imgur.com/PIoxddO.jpg
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.231.26.112 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1667527512.A.F00.html
推 PyTorch: 這提男得我會耶 11/04 10:07
→ PyTorch: 話說你表情符號怎麼打的這麼小的 11/04 10:07
→ Rushia: ☹ = / smile 去空格 11/04 10:11
推 TNPSCG: 刷這種題目有在管效率的嗎?還是效率就純粹自我挑戰而已 11/04 10:16
→ Rushia: 簡單的我會想辦法優化 難的基本上以先解出來為主 11/04 10:18
→ Rushia: easy以上的有贏50%基本上都可以接受 11/04 10:19
推 TNPSCG: 原來看的到和其他人的比較結果 11/04 10:21
推 pandix: 大師 11/04 10:26
推 itoumashiro: ☹ 11/04 10:26
推 Pash97143: 大師 11/04 21:10