Proble description:
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.
[Dynamic Programming] [Hashtable]
元旦刷了这题,算较复杂的dp题因为牵涉到hashtable。
开始使用O(n^2)但少用内存的方法被判定超时
后来用O(n)但多用了2KB的方法通过了
题目中保换的数据结构
int placeholder[256]; //用来存储当前最长连续非重复字符串,1表示在其中,0表示不在其中
int tailHolder[256]; //用来存储当前尾部最长连续非重复字符串, 也是0、1标示
int tailIndex[256]; //用来存储当前尾部最长连续非重复字符串中各个char在串中的index,从1开始
int maxLetter[length]; //length为输入string的长度,它用来存储长度从1~length的substring中最长连续非重复字符串的长度
int isTailed; //表明当前最长连续非重复字符串是否是尾部的substring
int tail_max;
Linear scan the string s
And parse every character s[i]
[1].check the isTailed
[1.1]True! check (tailHolder[s[i]]==1)?
[1.1.1]True!
updateTailHolder();
updatePlaceHolder();
tail_max = tail_max - tailIndex[s[i]] + 1;
updateIndex(tail_max_string);
isTailed = 0;
[1.1.2]False!
tail_max++;
tailIndex[s[i]] = tail_max;
placeHolder[s[i]] = 1;
tailHolder[s[i]] = 1;
[1.2].False! check (tailHolder[s[i]]==1)?
[1.2.1]True!
updateTailHolder();
updatePlaceHolder();
tail_max = tail_max - tailIndex[s[i]] + 1;
updateIndex(tail_max_string);
[1.2.2]False! Check (tail_max+1==maxLetter[i-1])?
[1.2.2.1]True
tailHolder[s[i]] = 1;
assign(placeHolder, tailHolder);
tail_max++;
tailIndex[s[i]] = tail_max;
[1.2.2.2]False
tailHolder[s[i]] = 1;
tail_max++;
tailIndex[s[i]] = tail_max;
END