码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode – Refresh – Longest Substring Without Repeating Characters

时间:2015-03-20 09:09:30      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:

For this problem, we are OK to use hash set, since no numbers are needed.

 

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         int len = s.size(), result = 0;
 5         if (len < 2) return len;
 6         unordered_set<char> sets;
 7         for (int left = 0, right = 0; right < len; right++) {
 8             if (sets.find(s[right]) == sets.end()) {
 9                 result = max(result, right - left + 1);
10                 sets.insert(s[right]);
11             } else {
12                 sets.erase(s[left++]);
13                 right--;
14             }
15         }
16         return result;
17     }
18 };

 

LeetCode – Refresh – Longest Substring Without Repeating Characters

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4352702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!