"title"
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "ABCABCBB" are "abc", which the length is 3. For "bbbbb" the longest substring are "B", with the length of 1.
"Analysis"
From left to right, when a repeating letter is encountered, the index+1 of the previous repeating letter is used as the starting position of the new search until the last letter.
"Code"
/********************************** Date: 2015-01-20* sjf0115* title: 3.Longest Substring without repeating characters* Website: https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/* Result: AC* Source: LeetCode* Time complexity: O ( N) * Spatial complexity: O (1) * Blog: **********************************/#include <iostream> #include <cstring>using Namespace Std;class Solution {Public:int lengthoflongestsubstring (string s) {int len = s.length (); if (len <= 1) {return len; }//if BOOL visited[256]; Initialize memset (visited,false,sizeof (visited)); computes int max = 0,cur = 0,start = 0; for (int i = 0;i < Len;) {//no element and the element repeats if (!visited[s[i]]) {Visited[s[i]] = true; ++cur; ++i; }//if else{//update maximum length if (cur >= max) {max = cur; }//if cur = 0; memset (visited,false,sizeof (visited)); int index = 0; Look for the subscript for the next element of the previous repeating element for (int j = Start;j < I;++j) {if (s[i] = = S[j]) { index = j+1; Break }//if}//for start = index; i = index; }}//for if (cur > max) {max = cur; }//if return Max; }};int Main () {solution solution; String str ("ABBACDAFG"); int result = solution.lengthoflongestsubstring (str); Output cout<<result<<endl; return 0;}
[Leetcode] 3.Longest Substring without repeating characters