zoukankan      html  css  js  c++  java
  • 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".

    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;
        }
  • 相关阅读:
    一些开发中用到的注解
    ios下设置user-scalable=no无效
    git的使用
    mongoose操作
    mongodb常用命令
    node express安装
    弹窗
    css实现全图滚动
    前端小技巧
    实现移动端轮播图
  • 原文地址:https://www.cnblogs.com/renyualbert/p/5904828.html
Copyright © 2011-2022 走看看