leetcode - Longest Substring Without Repeating Characters

来源:互联网 发布:初学编程怎么学 编辑:程序博客网 时间:2024/03/29 15:10

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.

class Solution {public:    int lengthOfLongestSubstring(std::string s) {std::vector<int> vec(256,-1);int res = 0,t = 0;for (int i = 0; i < s.size(); i++){t = std::max(vec[s[i]] + 1, t);vec[s[i]] = i;res = std::max(res,i-t+1);}return res;    }};


0 0
原创粉丝点击