LeetCode 345. Reverse Vowels of a String(翻转元音字母)
来源:互联网 发布:teamviewer 12 mac版 编辑:程序博客网 时间:2024/05/10 15:28
原题网址:https://leetcode.com/problems/reverse-vowels-of-a-string/
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 { boolean[] vowel = { true, false, false, false, true, false, false, false, true, false, false, false, false, false, true, false, false, false, false, false, true, false, false, false, false, false }; public String reverseVowels(String s) { if (s == null) return null; char[] sa = s.toCharArray(); int i=0, j=sa.length-1; while (i<j) { while (i<j && (!Character.isLetter(sa[i]) || (Character.isLowerCase(sa[i]) && !vowel[sa[i]-'a']) || (Character.isUpperCase(sa[i]) && !vowel[sa[i]-'A']))) i++; while (i<j && (!Character.isLetter(sa[j]) || (Character.isLowerCase(sa[j]) && !vowel[sa[j]-'a']) || (Character.isUpperCase(sa[j]) && !vowel[sa[j]-'A']))) j--; if (i>=j) break; char c = sa[i]; sa[i] = sa[j]; sa[j] = c; i++; j--; } return new String(sa); }}
另一种实现:
public class Solution { public String reverseVowels(String s) { char[] sa = s.toCharArray(); int left = 0, right = sa.length-1; while (left < right) { while (left < right && !vowel(sa[left])) left ++; while (left < right && !vowel(sa[right])) right --; if (left < right) { char ch = sa[left]; sa[left] = sa[right]; sa[right] = ch; left ++; right --; } } return new String(sa); } private boolean vowel(char ch) { if (ch >= 'a') ch -= 'a'-'A'; return ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'; }}
0 0
- LeetCode 345. Reverse Vowels of a String(翻转元音字母)
- 【leetcode】 Reverse Vowels of a String(翻转字符串中出现的元音字母)
- leetcode 345. Reverse Vowels(元音字母) of a String
- 【leetcode80】Reverse Vowels of a String(元音字母倒叙)
- Leetcode #345. Reverse Vowels of a String 逆转元音字母 解题报告
- 345. Reverse Vowels of a String (转置字符串中的元音字母)
- Reverse Vowels of a String 仅翻转元音字符
- 逆转字符串中的元音字母 Reverse Vowels of a String
- 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
- leetcode-345. Reverse Vowels of a String
- #leetcode#345. Reverse Vowels of a String
- [leetcode] 345. Reverse Vowels of a String
- Leetcode:345. Reverse Vowels of a String
- LeetCode *** 345. Reverse Vowels of a String
- LeetCode.345. Reverse Vowels of a String
- 一些在线工具集(图形处理、开发工具集、cdn服务)
- TWeaver Quick-Start
- lua面向对象封装及元表(metatable)性能测试
- python实现欧拉计划18题
- Scala学习笔记-基本数据类型
- LeetCode 345. Reverse Vowels of a String(翻转元音字母)
- PHP数据库操作:从MySQL原生API到PDO
- 代码训练营——StringBuffer
- 基础篇 _练习 小练习1~4
- 常用的android 框架收藏
- ffmpeg接收g723音频流
- Swift 闭包(block)传值
- 【NOIP2013普及组P2】表达式求值(NKOJ2500)题解
- 15年编程生涯,资深架构师总结的7条经验