首页 > 其他 > 详细

3.Longest Substring Without Repeating Characters(unordered_map)

时间:2017-01-31 18:34:19      阅读:251      评论:0      收藏:0      [点我收藏+]

技术分享

通过把未访问的结点放到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             else
18             {
19                 hash[s[i]] = i;
20                 len ++;
21                 res = max(len, res);
22             }
23         }
24         return res;
25     }
26 };

 

3.Longest Substring Without Repeating Characters(unordered_map)

原文:http://www.cnblogs.com/lca1826/p/6358985.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!