leetcode--Reserve Vowels of a String
2016-04-26 12:44
423 查看
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”.
反转元音字母,只需要做判断就行了,还是使用从两端向中间逼近的方法。
Example 1:
Given s = “hello”, return “holle”.
Example 2:
Given s = “leetcode”, return “leotcede”.
反转元音字母,只需要做判断就行了,还是使用从两端向中间逼近的方法。
package test; public class StringReserve2 { public static String reverseVowels(String s) { char[] c1 = s.toCharArray(); int i = 0, j = c1.length - 1; while (i < j) { if (isVowel(c1[j]) && isVowel(c1[i])) { char tmp = c1[i]; c1[i] = c1[j]; c1[j] = tmp; i++; j--; } else if (!isVowel(c1[j]) && isVowel(c1[i])) j--; else if (isVowel(c1[j]) && !isVowel(c1[i])) i++; else{ i++; j--; } } return new String(c1); } public static boolean isVowel(char c) { switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': return true; } return false; } public static void main(String[] args) { String s = "leetcode"; String reString = reverseVowels(s); System.out.print(reString); } }
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- android上改变listView的选中颜色
- String.intern
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- PostgreSQL ERROR: invalid escape string 解决办法
- 浅谈C++中的string 类型占几个字节
- 标准C++类string的Copy-On-Write技术
- C++实现string存取二进制数据的方法
- C#中string和StingBuilder内存中的区别实例分析
- 详解C++中实现继承string类的MyString类的步骤
- PHP STRING 陷阱原理说明
- c#中 String和string的区别介绍
- C#实现的图片、string相互转换类分享
- asp.net String.IsNullOrEmpty 方法
- JavaScript中字符串(string)转json的2种方法
- C#中string用法实例详解
- C#中的DataSet、string、DataTable、对象转换成Json的实现代码