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

345. Reverse Vowels of a String

时间:2016-09-25 06:16:19      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

 

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

 

public string ReverseVowels(string s) {
        if(s == "") return s;
        var ss = s. ToCharArray();
        int left=0;
        int right = s.Length-1;
        var vowels = new List<char>(){a,o,e,u,i,A,O,E,I,U};
        while(left< right)
        {
            if(vowels.Contains(s[left]) && vowels.Contains(s[right]))  Swap(ss, left++,right--);
            else if(!vowels.Contains(s[left])) left++;
            if(!vowels.Contains(s[right])) right--;
        }
        return new string(ss);
    }
    private void Swap(char[] ss, int i, int j)
    {
        char temp = ss[i];
        ss[i] = ss[j];
        ss[j] = temp;
    }

 

345. Reverse Vowels of a String

标签:

原文地址:http://www.cnblogs.com/renyualbert/p/5904828.html

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