首页 > 代码库 > 345. 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".
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。