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”.
字母问题注意大小写
public class Solution { public String reverseVowels(String s) { if (s == null || s.length() == 0) return s; char[] ss = s.toCharArray(); int head = 0; int tail = s.length()-1; String vowels = "AEIOUaeiou"; while (head < tail) { char ch = ss[head]; char ct = ss[tail]; if(vowels.indexOf(ch) != -1 && vowels.indexOf(ct) != -1){ char tmp = ch; ss[head] = ss[tail]; ss[tail] = tmp; head++; tail--; } else { if (vowels.indexOf(ch) == -1) head++; if (vowels.indexOf(ct) == -1) tail--; } } return new String(ss); }}新闻热点
疑难解答