首页 > 学院 > 开发设计 > 正文

345. Reverse Vowels of a String

2019-11-06 07:58:56
字体:
来源:转载
供稿:网友

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); }}
上一篇:[DP]挖地雷

下一篇:makefile参数

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表