345. 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”.
题目大意:交换字符串中的元音字母
思路:两点法
代码:
package String; /** * @Author OovEver * @Date 2017/12/4 11:13 */ public class LeetCode345 { public String reverseVowels(String s) { String vowels = "aeiouAEIOU"; int low = 0; int high = s.length() - 1; char[] sToChar = s.toCharArray(); while (low < high) { while (low < high && vowels.indexOf(s.charAt(low)) == -1) { low++; } while (low < high && vowels.indexOf(s.charAt(high)) == -1) { high--; } char temp = sToChar[low]; sToChar[low] = sToChar[high]; sToChar[high] = temp; low++; high--; } return String.valueOf(sToChar); } }
本文介绍了一种使用双指针技巧解决LeetCode 345题目的方法,该题要求反转字符串中的所有元音字母。通过遍历字符串并仅在遇到元音时进行交换,此算法有效地实现了题目要求。
456

被折叠的 条评论
为什么被折叠?



