File size: 884 Bytes
8be366a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// Created by yaning on 17-6-1.
//
#include <iostream>

using namespace std;

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int left = 0;
        int right = 0;
        int maxLength = 0;
        int length = 0;
        int check[255] = { 0, };
        for (; right < s.length(); right++) {
            if (check[s[right]] == 1) {
                for (; s[left] != s[right]; left++) {
                    check[s[left]] = 0;
                    length--;
                }
                left++;
            } else {
                check[s[right]] = 1;
                length++;
                if (length > maxLength) {
                    maxLength = length;
                }
            }
        }

        return maxLength;
    }
};

int main() {

    Solution sol;

    cout << sol.lengthOfLongestSubstring("12342543921");

    return 0;
}