LeetCode#345. Reverse Vowels of a String
来源:互联网 发布:图像领域的经典算法 编辑:程序博客网 时间:2024/06/13 05:41
- 题目:反转字符串中的元音字母
- 难度:Easy
- 思路:借鉴二分查找的思想,分别从前后进行查找,找到元音字母,就交换
- 代码:
public class Solution { public String reverseVowels(String s) { if(s == null || s.equals("")){ return s; } String vowels = "aeouiAEOUI"; StringBuilder sb = new StringBuilder(s); int len = s.length(); int left = 0; int right = len-1; while(left < right){ char ch1 = sb.charAt(left); char ch2 = sb.charAt(right); while(left < right && vowels.indexOf(ch1) == -1){ left++; ch1 = sb.charAt(left); } while(left < right && vowels.indexOf(ch2) == -1){ right--; ch2 = sb.charAt(right); } if(left < right){ sb.replace(left,left+1,String.valueOf(ch2)); sb.replace(right,right+1,String.valueOf(ch1)); } left++; right--; } return sb.toString(); }}
阅读全文
0 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
- Jenkins自动构建的方法
- Android Https相关完全解析 当OkHttp遇到Https
- centos7搭建webpack
- 数据结构-排序算法总结
- 牛客网-贪心-裁减网格纸
- LeetCode#345. Reverse Vowels of a String
- ActiveMQ实战(四)--ActiveMQ的通信方式之request-response(请求响应模式)
- 关于注解
- sql基础之表关联
- 学习Java越深入,感觉要学的越多,学到熟练的水平就很难了,学到精通得何时?
- idea maven jetty插件热部署
- 杂记(关于《js dom 编程艺术》)
- hdu 5289 ST表+尺取法
- poj 1426