Sunday, March 16, 2014

LeetCode:Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

Solution:
今天做的两道题都是关于双指针问题
刚开始尝试用DP,无果,后来参考解释,有了这个思路
这个题的解法稍微容易一些,用一个数组保存已经出现的字符信息,下标是某个letter的ASCII码值
重点是发现重复字符后的处理:1.比较是否找到更长的符合条件的字符串,2,suppose, s[j]==s[i] (j>i), then set letterExist[start...i-1] to false, the next potential substring starts from i+1; 下面这个图不错,转过来

Code:

第一百个题目完成! 继续加油

No comments:

Post a Comment