欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

【LeetCode】马三来刷题之 Reverse Vowels of a Stringm

发布时间:2023/12/16 42 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【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的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。