[leetcode]345. Reverse Vowels of a String
来源:互联网 发布:王健林 鲁豫 知乎 编辑:程序博客网 时间:2024/06/05 17:01
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) { HashMap<Character, Integer> map=new HashMap<>(); StringBuffer stringBuffer=new StringBuffer(); char[] res=s.toCharArray(); char temp; map.put('a', 0); map.put('e', 1); map.put('i', 2); map.put('o', 3); map.put('u', 4); map.put('A', 0); map.put('E', 1); map.put('I', 2); map.put('O', 3); map.put('U', 4); if(s.length()==0){ return ""; } int left=0; int right=s.length()-1; while(left<right){ if(!map.containsKey(s.charAt(left))){ left++; } if(!map.containsKey(s.charAt(right))){ right--; } if(map.containsKey(s.charAt(right))&&map.containsKey(s.charAt(left))){ temp=s.charAt(left); res[left]=res[right]; res[right]=temp; left++; right--; } } for(int i=0;i<res.length;i++){ stringBuffer.append(res[i]); } return stringBuffer.toString(); }}
1 0
- 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
- Leetcode 345. Reverse Vowels of a String
- LeetCode 345. Reverse Vowels of a String
- 345.[LeetCode]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
- Linux终端得到root权限
- Android设备的蓝牙通信
- User-Agent详解
- Unity 配置窍门:内存,音频和纹理
- 24种设计模式与7大原则
- [leetcode]345. Reverse Vowels of a String
- react使用小记1
- iOS利用Xcode制作静态库和动态库
- Mysql 执行外部脚本
- Java经典算法——窃贼问题
- 两个host上的qemu VM使用bridge互通
- 文章标题
- gdb调试-dump
- ps动作储存覆盖原文件的原因