首页 > 其他 > 详细

Leetcode 03 Longest Substring Without Repeating Characters

时间:2015-06-23 23:05:26      阅读:272      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!