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

3 Longest Substring Without Repeating Characters

时间:2015-07-10 09:34:17      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:leetcode

3 Longest Substring Without Repeating Characters

链接:https://leetcode.com/problems/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.

Hide Tags Hash Table Two Pointers String

求给出字符串中的不重复的最大子串长度。

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int result=0,n;
        string temp="";
        for(int i=0;i<s.length();i++)
        {
            n=temp.find(s[i]);
            if(n<0)
              temp+=s[i];
            else
           {
               if(result<temp.size())
                   result=temp.size();

               temp=temp.substr(n+1)+s[i];
           }
        }
         if(result<temp.size())
                   result=temp.size();
        return result;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

3 Longest Substring Without Repeating Characters

标签:leetcode

原文地址:http://blog.csdn.net/efergrehbtrj/article/details/46822335

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