Leetcode 中等二 无重复字符的最长子串

无重复字符的最长子串:

PHP 56ms:

利用哈希表,理解的不是太明白,后期重构:

class Solution {

    /**
     * @param String $s
     * @return Integer
     */
    function lengthOfLongestSubstring($s) {
        $re = 0;
        $hash = [];
        for($i = 0,$j = 0;$i < strlen($s);$i++){
            if(array_key_exists($s[$i],$hash)){
                $j = max($hash[$s[$i]],$j);
            }
            $re = max($re,$i - $j + 1);
            $hash[$s[$i]] = $i + 1;
        }
        return $re;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36688622/article/details/88122926