Leetcode 345: Reverse Vowels of a String
生活随笔
收集整理的這篇文章主要介紹了
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方便交換字符串中的元素
時間復雜度:O(n)
總結
以上是生活随笔為你收集整理的Leetcode 345: Reverse Vowels of a String的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 惠普m1005连接电脑步骤_电脑连接电视
- 下一篇: 完美抠图王冰冰!字节实习生实现4K60帧