题目:
解题思路:
代码:
import java.util.HashMap;
import java.util.Map;
public class LC146 {
public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> dic = new HashMap<>();
int res = 0, tmp = 0;
for(int j = 0; j < s.length(); j++) {
int i = dic.getOrDefault(s.charAt(j), -1); // 获取索引 i
dic.put(s.charAt(j), j); // 更新哈希表
tmp = tmp < j - i ? tmp + 1 : j - i; // dp[j - 1] -> dp[j]
res = Math.max(res, tmp); // max(dp[j - 1], dp[j])
}
return res;
}
public static void main(String[] args) {
LC146 lc = new LC146();
System.out.println(lc.lengthOfLongestSubstring("abcdaebcd"));
}
}
来源:oschina
链接:https://my.oschina.net/janeroad/blog/4952819