首页 > 代码库 > problem 3. Longest Substring Without Repeating Characters
problem 3. Longest Substring Without Repeating Characters
1 class Solution { 2 public: 3 int lengthOfLongestSubstring(string s) { 4 vector<int> signs(256, -1); 5 int max_length = 0; 6 int last_valid = -1; 7 for (size_t i = 0; i < s.size(); ++i){ 8 char c = s[i]; 9 int last_occ = signs[c];10 if (last_occ < last_valid){11 last_occ = last_valid;12 }13 if (last_occ > last_valid){14 last_valid = last_occ;15 }16 int length = i - last_occ;17 if (length > max_length){18 max_length = length;19 }20 21 signs[c] = i;22 }23 if (max_length == 0){24 max_length = s.size();25 }26 return max_length;27 }28 };
problem 3. Longest Substring Without Repeating Characters
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。