LeetCode #Longest Substring Without Repeating Characters#

LeetCode #Longest Substring Without Repeating Characters#





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


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

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

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


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

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

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

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

变成了去除b的前面部分

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

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


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


"""
Programmer	:	EOF
e-mail		:	jasonleaster@gmail
Date		:	2015.04.02
File		:	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	:	EOF
e-mail		:	jasonleaster@gmail
Date		:	2015.04.02
File		:	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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值