345. Reverse Vowels of a String
来源:互联网 发布:服务器网络拓扑图图片 编辑:程序博客网 时间:2024/06/10 20:21
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 class Solution {
public String reverseVowels(String s) {
char[] total=s.toCharArray();
StringBuilder sb=new StringBuilder();
StringBuilder t=new StringBuilder();
if(s==null)
return null;
int vowel=0;
for(char e:total){
vowel=e-'A';
if(vowel==0||vowel==4||vowel==8||vowel==14||vowel==20||vowel==32||vowel==36||vowel==40||vowel==46||vowel==52)
t.append(e);
}
int count=t.length()-1;
for(int i=0;i<s.length();i++){
vowel=s.charAt(i)-'A';
if(vowel==0||vowel==4||vowel==8||vowel==14||vowel==20||vowel==32||vowel==36||vowel==40||vowel==46||vowel==52){
sb.append(t.charAt(count--));
}
else{
sb.append(s.charAt(i));
}
}
return sb.toString();
}
}
直接将首尾两端的元音字符交换位置。
public class Solution {
static final String vowels = "aeiouAEIOU";public String reverseVowels(String s) {
int first = 0, last = s.length() - 1;
char[] array = s.toCharArray();
while(first < last){
while(first < last && vowels.indexOf(array[first]) == -1){
first++;
}
while(first < last && vowels.indexOf(array[last]) == -1){
last--;
}
char temp = array[first];
array[first] = array[last];
array[last] = temp;
first++;
last--;
}
return new String(array);
}
}
- String:345. Reverse Vowels of a String
- 345. Reverse Vowels of a String 【E】
- LeetCode 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- leetcode-345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- #leetcode#345. Reverse Vowels of a String
- [leetcode] 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- Leetcode:345. Reverse Vowels of a String
- LeetCode *** 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- LeetCode.345. Reverse Vowels of a String
- 345. Reverse Vowels of a String
- LeetCode 345. Reverse Vowels of a String
- 判断是否包含全角
- Netty学习总结(2)——Netty的高性能架构之道
- 伪函数与多线程
- android中的:scaleType属性
- org.apache.commons.mail来发送邮件
- 345. Reverse Vowels of a String
- python 中的%s和%r、str.format()函数
- Oracle trunc()函数的用法
- Cordova的简单使用
- Windows Server 2003域和集群
- GObject 学习笔记汇总---6
- hibernate,执行原生SQL并将结果转换为Map
- js判断某个数字是否为小数 并且有几位小数
- posix pthread 使用笔记