日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

345. Reverse Vowels of a String

發布時間:2023/12/9 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 345. Reverse Vowels of a String 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目:

Given a string?s, reverse only all the vowels in the string and return it.

The vowels are?'a',?'e',?'i',?'o', and?'u', and they can appear in both cases.

Example 1:

Input: s = "hello" Output: "holle"

Example 2:

Input: s = "leetcode" Output: "leotcede"

Constraints:

  • 1 <= s.length <= 3 * 105
  • s?consist of?printable ASCII?characters.

思路:

要交換位置,明顯的雙指針。先用哈希set記錄下元音,可以只記錄全大寫或者全小寫,在判斷的時候多寫一些語句也行,不過這里偷懶就把大小寫元音都記錄下來了。兩個指針分別index = 0 和 n - 1,只要左指針的當前字母不在哈希set內,右移;同理右指針的當前字母不在哈希set內就左移,如果當前左指針index小于右指針index,則交換元素并且再次移動指針。最后返回字符串即可。

代碼:

class Solution {
public:
? ? string reverseVowels(string s) {
? ? ? ? unordered_set<char> mp= {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
? ? ? ? int n = s.size(), i = 0, j = n - 1;
? ? ? ? while (i < j) {
? ? ? ? ? ? while (i < n && !mp.count(s[i]))
? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? while ( j > 0 && !mp.count(s[j]))
? ? ? ? ? ? ? ? j--;
? ? ? ? ? ? if (i < j) {
? ? ? ? ? ? ? ? swap(s[i], s[j]);
? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? ? ? j--;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return s;
? ? }
};

總結

以上是生活随笔為你收集整理的345. Reverse Vowels of a String的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。