无重复字符的最长子串:
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;
}
}