Longest Substring Without Repeating Characters,longestrepeating
分享于 点击 42058 次 点评:199
Longest Substring Without Repeating Characters,longestrepeating
Longest Substring Without Repeating Characters
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.
Method:自己写的代码太渣渣了,今天直接上大神的代码吧:https://leetcode.com/discuss/25051/my-o-n-solution-runtime-5ms
int lengthOfLongestSubstring(char *s) {
int m[129] = {0};
int i, j;
int cnt = 0, pre = 0;
int max = 0;
int c;
for (i = 0; c = s[i]; i++) {
if (pre < m[c]) {
if (max < cnt)
max = cnt;
cnt = i-m[c];
pre = m[c];
}
cnt++;
m[c] = i+1;
}
return max > cnt ? max : cnt;
}
相关文章
- 暂无相关文章
用户点评