[LeetCode] Longest Substring Without Repeating Characters

来源:互联网 发布:色诱诱导充值源码 编辑:程序博客网 时间:2024/04/29 10:56
class Solution {public:    int lengthOfLongestSubstring(string s) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        vector<int> v(256, -1);                int pos = 0;        int ret = 0;                for (int i = 0; i < s.size(); i++) {            char ch = s[i];                        if (pos <= v[ch]) {                ret = max(ret, i - pos);                pos = v[ch] + 1;            }                        v[ch] = i;        }                ret = max(ret, (int)s.size() - pos);                return ret;    }};


Small Case: 0ms

Large Case: 48ms


Time: O(n)

Space: O(n)