[Leetcode] 3.Longest Substring without repeating characters

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.