日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Leetcode 345: Reverse Vowels of a String

發(fā)布時間:2023/12/9 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Leetcode 345: Reverse Vowels of a String 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

問題描述:

Given a string s, reverse only all the vowels in the string and return it.
The vowels are ‘a(chǎn)’, ‘e’, ‘i’, ‘o’, and ‘u’, and they can appear in both cases.
s consist of printable ASCII characters
將原字符串中的元音字母倒序
注意審題,A與a均為元音,所以要考慮大小寫

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

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

代碼如下:
注:用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;} }

時間復(fù)雜度:O(n)

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。