LeetCode:Reverse Vowels of a String
2016-05-02 18:57
316 查看
Reverse Vowels of a String
Total Accepted: 8150 Total
Submissions: 23213 Difficulty: Easy
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".
Subscribe to see which companies asked this question
Hide Tags
Two Pointers String
Hide Similar Problems
(E) Reverse String
code:
public class Solution {
public boolean isVowel(char c) {
if('A' <= c && c <= 'Z') c += 'a'-'A';
return c=='a' || c=='e' || c=='i' || c=='o' || c=='u';
}
public String reverseVowels(String s) {
int len = s.length();
char[] chs = s.toCharArray();
int i=0,j=len-1;
while(i<j) {
while(i<j && !isVowel(chs[i])) i++;
while(i<j && !isVowel(chs[j])) j--;
char t = chs[i];
chs[i] = chs[j];
chs[j] = t;
i++;
j--;
}
return new String(chs);
}
}
相关文章推荐
- mac安装Homebrew
- 多线程
- 联合线程
- 道格拉斯-普克 Douglas-Peuker(DP算法)-python实现
- eclipse https git
- php正则表达式和数组
- sprignmvc 中使用zyUpload 上传图片(批量)
- HDU 2795 Billboard
- HDU 2795 Billboard
- gitlab open ssl
- matlab usage: cellfun
- 搭建使用springmvc的web项目
- 从两地三中心到双活数据中心
- 基于bootstrap 模态框的警告框
- 5.简单的超链接的使用
- 回文串和镜像串和ctype.h
- Object-c------@property_and_@synthesize
- 判断一棵树是否是另一棵树的子树(C语言版)
- C++简单QQ程序服务器端的实现代码
- linux基础(5)--恢复误删除的文件--RHEL6.5