3Longest Substring Without Repeating Characters

来源:互联网 发布:中国的网络发展历程 编辑:程序博客网 时间:2024/06/06 20:42

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;    }};
0 0
原创粉丝点击