Given a string, find the length of the longest substring without
repeating characters.Example 1:
Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with
the length of 3. Example 2:Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the
length of 1. Example 3:Input: "pwwkew" Output: 3 Explanation: The answer is "wke", with the
length of 3.Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
对xxx串,它的最长不重复子串情况可以完全由xx可以决定,确认是dp问题
确定状态转移方程,定义dp[i]为与当前串构成不重复串的index
dp[i]=Math.max(dp[i-1],count[s.charAt(i-1)]+1);
public int lengthOfLongestSubstring(String s) {
int ret=0;
int l=s.length();
int[] dp=new int[l+1];
int[] count=new int[128];
for(int i=1;i<=l;i++){
dp[i]=Math.max(dp[i-1],count[s.charAt(i-1)]+1);
ret=Math.max(ret,i+1-dp[i]);
count[s.charAt(i-1)]=i;
}
return ret;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。