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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Leetcode 345: Reverse Vowels of a String

發布時間:2023/12/9 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Leetcode 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.
s consist of printable ASCII characters
將原字符串中的元音字母倒序
注意審題,A與a均為元音,所以要考慮大小寫

思路:同樣是雙指針夾擊,當左側指針遇到元音字母時,停住并設立stopSign標記,否則前進;同理右側指針。若在某一時刻兩側指針均被stopSign標記,這時需要交換,交換后解除stopSign,并繼續靠近。

遇到的問題:沒審清題,未考慮元音字母存在的大小寫問題

代碼如下:
注:用stringbuilder方便交換字符串中的元素

public String reverseVowels(String s) {int left=0;int right=s.length()-1;char temp = ' ';StringBuilder ans = new StringBuilder(s);boolean stopSign_left = false;boolean stopSign_right = false;//when any pointer detects a vowel, stop, and do not move until the other pointer detects a vowelwhile(left<right){if (isVowel(ans.charAt(left))){stopSign_left = true;}else{left++;}if (isVowel(ans.charAt(right))){stopSign_right = true;}else{right--;}if ((stopSign_left)&&(stopSign_right)){temp = ans.charAt(left);ans.setCharAt(left, s.charAt(right));ans.setCharAt(right, temp);stopSign_left = false;stopSign_right = false;left++;right--;}}return ans.toString();}public boolean isVowel(char something){if ((something=='a')||(something=='e')||(something=='i')||(something=='o')||(something=='u')||(something=='A')||(something=='E')||(something=='I')||(something=='O')||(something=='U')){return true;}return false;} }

時間復雜度:O(n)

總結

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

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