小凉

3 0642700 3 0642770 5 34202 13942 4314 0624

Longest Substring Without Repeating Characters(Difficulty: Medium)

题目:

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

实现:

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4     vector<int> dict(256, -1);
 5         int maxLen = 0, start = -1;
 6         for (int i = 0; i != s.length(); i++) {
 7             if (dict[s[i]] > start)
 8                 start = dict[s[i]];
 9             dict[s[i]] = i;
10             maxLen = max(maxLen, i - start);
11         }
12         return maxLen;
13     }
14 };

 

posted on 2016-05-10 20:36  小凉  阅读(127)  评论(0编辑  收藏  举报

导航