您的位置:首页 > 其它

leetcode substring without repeating characters

2014-07-28 18:06 197 查看
本来应该一次过的,结果没想到leetcode放了一堆奇怪的字符。。

做题就该思路清晰啊。

class Solution {
public:
int lengthOfLongestSubstring(string s) {
int loc[255] = {0};
memset(loc,-1,sizeof(loc));
int i = 0, n = s.length(), ind, maxsize = 0, tmpsize = 0, start = 0;
for (i = 0; i < n; i ++)
{
ind = s[i];
if(loc[ind] == -1){
loc[ind] = i;
continue;
}

int oldloc = loc[ind];
tmpsize = i - start;
if (tmpsize > maxsize)maxsize = tmpsize;

i = oldloc;
start = i+1;
memset(loc,-1,sizeof(loc));
}
tmpsize = n - start;
maxsize = tmpsize > maxsize? tmpsize : maxsize;
return maxsize;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: