leetcode-Longest Substring Without Repeating Characters

来源:互联网 发布:深圳市软件产业基地5栋 编辑:程序博客网 时间:2024/06/08 19:41

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.


思路:

1,用一个hash记录字符出现过的位置

2,从头开始遍历字符串,记录无重复子串的长度,如果扫描到重复字符,则从重复字符处的下一个开始扫描。

如:str[]="abcabcbb"

a,str[0]==str[3] 最长子串长度3,从str[1]开始继续扫描

b,str[1]==str[4]最长子串长度3,从str[2]继续扫描

c,str[2]==str[5]最长子串长度3,从str[3]继续扫描

d,str[4]==str[6]最长子串长度3,从str[5]继续扫描

e,str[6]==str[7]最长子串长度3,扫描结束

0 0
原创粉丝点击