Reverse Vowels of a String 仅翻转元音字符
来源:互联网 发布:高程测量记录表的算法 编辑:程序博客网 时间:2024/05/21 04:43
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".
这道题跟先前的翻转字符串的唯一区别,就是限制了只能是元音字符。
所以,我们只要增加代码判断首尾字符是不是aeiou AEIOU。
要注意的问题就是不要让数组越界。
运行时间:
代码:
public class ReverseVowelsofaString { public String reverseVowels(String s) { char[] word = s.toCharArray(); int i = 0; int j = word.length - 1; while (i < j) { while (i < j && word[i] != 'a' && word[i] != 'e' && word[i] != 'i' && word[i] != 'o' && word[i] != 'u' && i < j && word[i] != 'A' && word[i] != 'E' && word[i] != 'I' && word[i] != 'O' && word[i] != 'U') { i++; } while (i < j && word[j] != 'a' && word[j] != 'e' && word[j] != 'i' && word[j] != 'o' && word[j] != 'u' && i < j && word[j] != 'A' && word[j] != 'E' && word[j] != 'I' && word[j] != 'O' && word[j] != 'U') { j--; } if (i < j) { char temp = word[i]; word[i] = word[j]; word[j] = temp; } i++; j--; } return new String(word); }}
1 0
- Reverse Vowels of a String 仅翻转元音字符
- LeetCode 345. Reverse Vowels of a String(翻转元音字母)
- 【leetcode】 Reverse Vowels of a String(翻转字符串中出现的元音字母)
- LeetCode Reverse Vowels of a String(字符串中元音字符反转)
- leetcode 345. Reverse Vowels of a String 反转字符串元音字符
- Reverse Vowels of a String -- 颠倒字符串中的元音
- 逆转字符串中的元音字母 Reverse Vowels of a String
- Reverse Vowels of a String 交换元音字母
- 【leetcode80】Reverse Vowels of a String(元音字母倒叙)
- leetcode 345. Reverse Vowels(元音字母) of a String
- leetcode_345. Reverse Vowels of a String 转换字符串中的元音字符,list类型数据转换成str类型,join()函数
- Reverse Vowels of a String
- Reverse Vowels of a String
- Reverse Vowels of a String
- Reverse Vowels of a String
- Reverse Vowels of a String
- Reverse Vowels of a String
- Reverse Vowels of a String
- 正负数读入优化模板
- 网页开发基础知识 HTML + CSS
- Struts1 增删改查
- IO-同步,异步,阻塞,非阻塞
- (OK)(OK)(All in CLI) Fedora23 + Docker(busybox) + NS3 + MANETs - testing
- Reverse Vowels of a String 仅翻转元音字符
- mongodb 编译
- itertools实例与生成器代码
- 最小的K个数31
- EAS跳过权限检查
- 172. Factorial Trailing Zeroes
- 使用quartyz2D画圆,并将其三等分
- MS Chart Control - X轴 项目超9项时 Label显示不完全(已解决)
- Hbase-0.98.6源码分析--Put写操作Client端流程