您的位置:首页 > 其它

每日一题(day3)

2016-07-09 16:44 267 查看

Leetcode003

Longest Substring Without Repeating Characters问题

int lengthOfLongestSubstring(string s) {
int table[256];
int curlen = 0, maxlen = 0, curhead = 0, maxhead = 0;
memset(table, -1, sizeof(table));
for (int i = 0; i < s.length(); i++)
{
if (table[s[i]]<0)
{
table[s[i]] = i;
if (++curlen > maxlen)
{
maxlen = curlen;
maxhead = curhead;
}

}
else
{
curlen -= table[s[i]] - curhead;
for (int j = curhead; j < table[s[i]]; j++)
table[s[j]] = -1;
curhead = table[s[i]] + 1;
table[s[i]] = i;
}
}
return maxlen;
}


采用技巧:利用数组记录重复字符出现的最终位置,-1表示未出现

注意问题:长度更新与数组内容替换
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode substring