题目:
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".
题目的意思大概是这样的
给定一个字符串,然后兑换里面元音字母对应的位置
先写下实现的代码(javaScript版)
var reverseVowels = function(s) {
if(s === null || s.length === 0) {
return s;
}
var chars = s.split('');
var low = 0;
var high = s.length - 1;
var vowels = "aeiouAEIOU";
var tmp;
while(low < high) {
while(low < high && vowels.indexOf(chars[low]) === -1) {
low++;
}
while(low < high && vowels.indexOf(chars[high]) === -1) {
high--;
}
tmp = chars[high];
chars[high] = chars[low];
chars[low] = tmp;
low++;
high--;
}
return chars.join('');
};
实现思路
只要设置一个头部变量和尾部变量一直向对方逼近就可以了,直接看代码就可以看懂啦~~
ps:这是我的第一篇文章,以后会坚持更新的~~