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;
    }
}
posted @ 2021-07-15 23:32  吴丹阳-V  阅读(43)  评论(0编辑  收藏  举报