3.jpeg

    代码 :

    1. class Solution {
    2. public:
    3. int lengthOfLongestSubstring(string s) {
    4. if(s.size() == 0) return 0;
    5. unordered_map<char, int> hash;
    6. int res = 0;
    7. for(int i = 0, j = 0; j < s.size(); j ++ ) {
    8. hash[s[j]] ++;
    9. while(hash[s[j]] > 1) {
    10. hash[s[i++]]--;
    11. }
    12. res = max(res, j - i + 1);
    13. }
    14. return res;
    15. }
    16. };