码迷,mamicode.com
首页 > 其他好文 > 详细

<LeetCode OJ> 345. Reverse Vowels of a String

时间:2016-04-29 19:53:09      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

Total Accepted: 537 Total Submissions: 1488 Difficulty: Easy

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".

Subscribe to see which companies asked this question

Hide Tags
 Two Pointers String
Hide Similar Problems
 (E) Reverse String

分析:

典型的双指针问题,注意边界条件即可!

class Solution {
public:
    void swapchar(string &str,int pos1,int pos2)  
    {  
        char tmpch=str[pos2];  
        str[pos2]=str[pos1];  
        str[pos1]=tmpch;  
    }  
    string reverseVowels(string s) {
        char base[10]={'a','e','i','o','u','A','E','I','O','U'};
        set<char> seting(base,base+10);
        int startpos=0,endpos=s.size()-1;
        while(startpos < endpos )
        {
            while( startpos < endpos && seting.find(s[startpos]) == seting.end())
                startpos++;
            while( startpos < endpos && seting.find(s[endpos]) == seting.end())
                endpos--;
            if(startpos < endpos)    
                swapchar(s,startpos++,endpos--);    
        }
        return s;
    }
};



注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/51228095

原作者博客:http://blog.csdn.net/ebowtang

本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895

<LeetCode OJ> 345. Reverse Vowels of a String

标签:

原文地址:http://blog.csdn.net/ebowtang/article/details/51228095

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!