Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters

  Total Accepted: 23601  Total Submissions: 105691

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.

//Longest Substring Without Repeating Characters 

#include
#include
using namespace std;

class Solution {
public:
	int check(int s,int e,string str)
	{
		for(int i=s; i<=e-1; ++i)
		{
			if(str[i]==str[e])
			{
				return i;
			}
		}
		
		return -1;
	}
	
    int lengthOfLongestSubstring(string s) {
		int len = s.length();
		if(len<=1)//0-1 
		{
			return len;
		}
		
		int max_len=0;
		
		int start=0,end=1;
		
		while(end(end-start)?max_len:(end-start);
				start=fg+1;//优化 
				++end;
			}			
			else
			{
				++end;
			}		
		}
		max_len = max_len>(end-start)?max_len:(end-start);//末尾情况 
		return max_len;       
    }
};

int
main()
{
	Solution sn;
	string str;
	cin>>str;
	cout<

你可能感兴趣的:(LEETCODE)