LeetCode #Longest Substring Without Repeating Characters#

来源:互联网 发布:淘宝信欣美妆靠谱吗 编辑:程序博客网 时间:2024/05/18 03:39

LeetCode #Longest Substring Without Repeating Characters#





折腾了一回,第一感觉就是没有感觉...这种没技巧的话暴力是不现实的..而后发现其实没有很好理解题目的意


考过么?究竟什么是最长的子字符串

这里有个很要命的概念, 子字符串,别小看这家伙.是搞定题目的关键.

"abcduiwe" 这串字符串是没有重复字符的,那么最长子串就是自己.


但是!一旦出现重复的字符了,当前最大子字符串必然变小.

比方说,原字符串为"abcd",最大子字符串就是自身.

但这个时候加上了一个字符b

"abcdb"那么这个时候b就作为子串abcd的结束位置了!这就让我们的最大子串(自身),变小了

变成了去除b的前面部分

再看,如果我们加的不同的字符更多

"abcdbefgh"这个时候,最长的子串就是"befgh",要点就在于,从重复的那个字符串开始算新的子字符串


刚回来的路上还一直闷神,,"子字符串..子字符串"


"""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")



下面是 @凯旋冲锋 的java的实现


"""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")



皓神的C++实现:

// 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;}






0 0
原创粉丝点击