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

Leetcode 03 Longest Substring Without Repeating Characters

时间:2015-06-23 23:07:53      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

Leetcode 03 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

字符串处理

class Solution 
{
public:
    int lengthOfLongestSubstring(string s) 
    {
        vector<int> bar(200, -1);
        int N = s.size();
        int result = 0;
        int l = 0, r = 0;
        while (r < N)
        {
            if (bar[s[r]]>=l)
            {
                result = max(result, r - l);
                l = bar[s[r]] + 1;
            }
                bar[s[r] ] = r;
            r++;
        }
        result = max(result, r - l);
        vector<int>().swap(bar);
        return result;
    }
};

 

Leetcode 03 Longest Substring Without Repeating Characters

标签:

原文地址:http://www.cnblogs.com/chenwanqq-leetcode/p/4596352.html

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