leetcode-345. 反转字符串中的元音字母
题目
https://leetcode-cn.com/problems/reverse-vowels-of-a-string/
解法
双指针 666
来全场跟我左边画条龙,右边跟我画一道彩虹
class Solution {
const VOWELS = ['a' => 1, 'e' => 1, 'i' => 1, 'o' => 1, 'u' => 1];
function reverseVowels($s) {
if (empty($s)) {
return $s;
}
$len = strlen($s);
$left = 0;
$right = $len - 1;
while ($left < $right) {
while (!array_key_exists(strtolower($s[$left]), self::VOWELS) && $left < $right) {
$left++;
continue;
}
$leftChar = $s[$left];
while (!array_key_exists(strtolower($s[$right]), self::VOWELS) && $left < $right) {
$right--;
continue;
}
$rightChar = $s[$right];
$s[$left] = $rightChar;
$s[$right] = $leftChar;
$left++;
$right--;
}
return $s;
}
}
本文来自博客园,作者:吴丹阳-V,转载请注明原文链接:https://www.cnblogs.com/wudanyang/p/15018084.html