您的位置:首页 > 职场人生

面试题12:字符串无重复子串的最大长度

2017-05-14 17:26 323 查看
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.

1 class Solution {
2 public:
3     int lengthOfLongestSubstring(string s) {
4         int* m = new int[256];
5         for (int i = 0; i < 256; i++) {
6             m[i] = -1;
7         }
8         int res = 0;
9         int start = -1;
10         int n = s.length();
11         for (int i = 0; i < n; i++) {
12             start = max(start, m[s[i]]);
13             res = max(res, i - start);
14             m[s[i]] = i;
15         }
16         return res;
17     }
18
19 };
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐