您的位置:首页 > 其它

3、Longest Substring Without Repeating Characters

2015-01-21 15:08 197 查看
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.

Hash
Table Two Pointers String

int lengthOfLongestSubstring(string s) {
int place[128];
for (int i = 0; i < 128; ++i)
place[i] = -1;
int length = s.size();
int maxLength = 0, maxIndex = -1;
for (int i = 0; i < length; ++i)
{
//此方法比较取巧,可能一眼难以理解,此题中关于哈希表的应用也是不大清楚
maxIndex = max(place[s[i]], maxIndex);
maxLength = max(i - maxIndex, maxLength);
place[s[i]] = i;
}
return maxLength;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: