[Leetcode] 3.Longest Substring Without Repeating Characters(unordered_map)

来源:互联网 发布:coach淘宝代购 编辑:程序博客网 时间:2024/06/02 05:35

通过把未访问的结点放到unordered_map中来判断是否重复,代码如下:

 1 class Solution { 2 public: 3     int lengthOfLongestSubstring(string s) { 4         if(s == "") 5             return 0; 6         unordered_map<char,int> hash; 7         int len = 0; 8         int res = 0; 9         for(int i = 0;i < s.length();i ++)10         {11             if(hash.find(s[i]) != hash.end())12             {13                 i = hash[s[i]];14                 hash.clear();15                 len = 0;16             }17             else18             {19                 hash[s[i]] = i;20                 len ++;21                 res = max(len, res);22             }23         }24         return res;25     }26 };

 

阅读全文
0 0