前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode-345-Reverse Vowels of a String

leetcode-345-Reverse Vowels of a String

作者头像
chenjx85
发布2018-05-21 18:43:10
5310
发布2018-05-21 18:43:10
举报

题目描述:

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)?

说明:

1、题目不难看懂,从字符串首部开始找,找到的第一个元音和,从字符串尾部开始找,找到的第一个元音交换位置。接着继续找第二个……直到所有元音都反转完成。

2、元音是“a”、“e“、“i”、“o”、“u”以及它们的大写形式。

3、令i=0,j=s.size()-1,不断查找并且交换位置,最终退出循环条件是i>=j。

代码如下:

代码语言:javascript
复制
    string reverseVowels(string s) 
    {
        int i=0,j=s.size()-1;
        while(i<j)
        {
            if(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')
            {
                while(i<j)
                {
                    if(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')
                    {
                        swap(s[i],s[j]);
                        j--;
                        break;
                    }
                    j--;
                }
            }
            i++;
        }
        return s;
    }

上述代码实测12ms,beats 86.45% of cpp submissions。

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-04-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述:
  • 要完成的函数:
  • 说明:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com