【LeetCode】马三来刷题之 Reverse Vowels of a Stringm
生活随笔
收集整理的這篇文章主要介紹了
【LeetCode】马三来刷题之 Reverse Vowels of a Stringm
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:https://leetcode.com/problems/reverse-vowels-of-a-string/
345. Reverse Vowels of a String
?Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
Note:
The vowels does not include the letter "y".
題目很簡單,找到字符串中的元音字母,然后一一交換即可。
string reverseVowels(string s) {int i=0,j=s.length()-1;if(i>j)return s;while(i<j){while((s[j]!='a'&&s[j]!='e'&&s[j]!='i'&&s[j]!='o'&&s[j]!='u'&&s[j]!='A'&&s[j]!='E'&&s[j]!='I'&&s[j]!='O'&&s[j]!='U')&&i<j){j--;}while((s[i]!='a'&&s[i]!='e'&&s[i]!='i'&&s[i]!='o'&&s[i]!='u'&&s[i]!='A'&&s[i]!='E'&&s[i]!='I'&&s[i]!='O'&&s[i]!='U')&&i<j){i++;}if(i!=j){char c=s[i];s[i]=s[j];s[j]=c;i++;j--;}}return s; }每天一道題,保持新鮮感,就這樣~
總結
以上是生活随笔為你收集整理的【LeetCode】马三来刷题之 Reverse Vowels of a Stringm的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .bash_profile和.bashr
- 下一篇: 【精品软件】鼠标右键菜单设置管理工具