LeetCode # Longest Substring Without Repeating Characters #,

Source: Internet
Author: User

LeetCode # Longest Substring Without Repeating Characters #,

LeetCode # Longest Substring Without Repeating Characters #





After a fight, the first thing I felt was that I didn't feel it... this kind of unskillful words of violence was unrealistic. Then I found that I didn't really understand the meaning of the question.


Have you thought about it? What is the longest substring?

There is a terrible concept here, sub-string. Don't underestimate this guy. It is the key to solving the problem.

"Abcduiwe" is a string that does not have repeated characters, so the oldest string is itself.


But! Once repeated characters appear, the current maximum substring must be smaller.

For example, if the original string is "abcd", the maximum substring is itself.

However, a character B is added at this time.

"Abcdb", then B is the end position of the sub-string abcd! This makes our largest substring (itself) smaller.

RemovedBBefore

Again, if we add different characters

"Abcdbefgh" at this time, the longest substring is "befgh". The key point is to calculate the new substring from the repeated substring!


On the way back, I was still bored. "sub-string... sub-string"


"""Programmer:EOFe-mail:jasonleaster@gmailDate:2015.04.02File:lswrc.py"""class Solution:    # @return an integer    def lengthOfLongestSubstring(self, s):    Table = [-1 for i in range(0, 256)]    maxLen = 0    lastRepeatPos = -1    """     We will use @ord() function to translate the character     into the number of it's ascii code.    """       for i in range(0, len(s)):    if Table[ord(s[i])] != -1 and lastRepeatPos < Table[ord(s[i])]:    lastRepeatPos = Table[ord(s[i])]        if i - lastRepeatPos > maxLen :    maxLen = i - lastRepeatPos    Table[ord(s[i])] = i    return maxLen#---------- just for testing ----------s = Solution()print s.lengthOfLongestSubstring("c")print s.lengthOfLongestSubstring("abcdababcde")



The following is the java Implementation of @ Kaixuan chongfeng.


"""Programmer:EOFe-mail:jasonleaster@gmailDate:2015.04.02File:lswrc.py"""class Solution:    # @return an integer    def lengthOfLongestSubstring(self, s):    Table = [-1 for i in range(0, 256)]    maxLen = 0    lastRepeatPos = -1    """     We will use @ord() function to translate the character     into the number of it's ascii code.    """       for i in range(0, len(s)):    if Table[ord(s[i])] != -1 and lastRepeatPos < Table[ord(s[i])]:    lastRepeatPos = Table[ord(s[i])]        if i - lastRepeatPos > maxLen :    maxLen = i - lastRepeatPos    Table[ord(s[i])] = i    return maxLen#---------- just for testing ----------s = Solution()print s.lengthOfLongestSubstring("c")print s.lengthOfLongestSubstring("abcdababcde")



The C ++ Implementation of Hao Shen:

// Source : https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/// Author : Hao Chen// Date   : 2014-07-19/********************************************************************************** * * 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.*               **********************************************************************************/#include <string.h>#include <iostream>#include <string>#include <map>using namespace std;/* * Idea: *  * Using a map store each char's index. *  * So, we can be easy to know the when duplication and the previous duplicated char's index. *  * Then we can take out the previous duplicated char, and keep tracking the maxiumn length.  *  */int lengthOfLongestSubstring1(string s) {    map<char, int> m;    int maxLen = 0;    int lastRepeatPos = -1;    for(int i=0; i<s.size(); i++){        if (m.find(s[i])!=m.end() && lastRepeatPos < m[s[i]]) {            lastRepeatPos = m[s[i]];        }        if ( i - lastRepeatPos > maxLen ){            maxLen = i - lastRepeatPos;        }        m[s[i]] = i;    }    return maxLen;}//don't use <map>int lengthOfLongestSubstring(string s) {    const int MAX_CHARS = 256;    int m[MAX_CHARS];    memset(m, -1, sizeof(m));    int maxLen = 0;    int lastRepeatPos = -1;    for(int i=0; i<s.size(); i++){        if (m[s[i]]!=-1 && lastRepeatPos < m[s[i]]) {            lastRepeatPos = m[s[i]];        }        if ( i - lastRepeatPos > maxLen ){            maxLen = i - lastRepeatPos;        }        m[s[i]] = i;    }    return maxLen;}int main(int argc, char** argv){    string s = "abcabcbb";    cout << s << " : " << lengthOfLongestSubstring(s) << endl;    s = "bbbbb";    cout << s << " : " << lengthOfLongestSubstring(s) << endl;    s = "bbabcdb";    cout << s << " : " << lengthOfLongestSubstring(s) << endl;    if (argc>1){        s = argv[1];        cout << s << " : " << lengthOfLongestSubstring(s) << endl;    }    return 0;}






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.