Leetcode:Longest Substring Without Repeating Characters

Problem:Longest Substring Without Repeating Characters

  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.

Analysis:

  这道题要求求出一段字符串中最长的不重复的字符串的长度。

Solution:

  设置下标i,i从零开始往后走,j动态的表示从i往后最长的不重复数组的长度。当s[j]在s[i:(i + j)]中是,找到重复的字母s[i],从字母重复的位置到s[i+j]为止这么长的字符串,继续往后找重复的字母。将最长的j记录下来。
  空间复杂度:
  

O(1)

  时间复杂度:
  
O(n)

#pyhton code
class Solution:
    def lengthOfLongestSubstring(self, s):
        if s == '':
            return 0
        longString = 1
        i,j = 0, 1
        lenOfString = len(s) - 1
        while i + j <= lenOfString :
            while (i + j ) <= lenOfString:
                if s[i + j] not in s[ i : ( i + j ) ]:
                    j += 1
                    if j >= longString:
                        longString = j
                else:
                    i, j = s[i:( i + j)].index(s[i + j]) + i + 1,j - s[i:( i + j)].index(s[i + j])
                    break
        return longString

下面这段代码是我第一次写的解决方案,能够运行,但是时间超时了。

class Solution:
    def lengthOfLongestSubstring(self, s):
        if s == '':
            return 0
        longString = 1
        i = 0
        lenOfString = len(s)
        while( i < (lenOfString - 1) ):
            j, c = 1, 1
            while(( i + j) != lenOfString):
                if s[i+j] not in s[i:(i + j)]:
                    c += 1
                    j += 1
                else:
                    break
            if c >= longString:
                longString = c
            i =  i + 1
        return longString

其他人的解决方案:

#python code
class Solution:
    # @param {string} s
    # @return {integer}
    def lengthOfLongestSubstring(self, s):
        start_pos = 0
        current_pos = -1    # consider blank string case
        max_length = 0
        char_pos = {}
        for current_pos in range(len(s)):
            if s[current_pos] in char_pos and char_pos[s[current_pos]] >= start_pos :
                max_length = max(max_length, current_pos - start_pos)
                //这个写法以后借鉴一下
                start_pos = char_pos[s[current_pos]] + 1
            char_pos[s[current_pos]] = current_pos
        return max(max_length, current_pos - start_pos + 1)
//c语言
int lengthOfLongestSubstring(char *s) {
    int maxLen = 0;
    int pre = -1;
    int hash[128];
    memset(hash, -1, sizeof(hash));

    int i,c;
    for(i=0; c=s[i]; i++)
    {
        if(pre < hash[c] && hash[c]!=-1)
            pre = hash[c];
        maxLen = maxLen>(i-pre)? maxLen : (i-pre);

        hash[c] = i;
    }

    return maxLen;
}
//c++
int lengthOfLongestSubstring(string s) {
    if(s.empty())
    {
        return 0;
    }

    int maxLen = 0;
    int start = 0;
    vector<int> registry;
    registry.resize(256, -1); // char can be the index here
    int j = start;
    while(j < s.length())
    {
        if(registry[s[j]] != -1)
        {
            maxLen = max(maxLen, j - start);
            // clean up the registry up to the previous occurence of the current char, inclusively
            for(int k = start; k < registry[s[j]]; ++k)
            {
                registry[s[k]] = -1;
            }
            start = registry[s[j]] + 1;                
        }
        registry[s[j]] = j;
        ++j;
    }
    return max(maxLen, j - start);
}
//java
public int lengthOfLongestSubstring(String s) {
    HashMap<Character, Integer> map = new HashMap<Character, Integer>();
    if (s == null || s.length() == 0) return 0;
    if (s.length() == 1) return 1;
    int rightPointer = 0, leftPointer = rightPointer - 1, answer = 0;
    while (rightPointer != s.length()) {
        Integer previousOccurrence = map.put(s.charAt(rightPointer), rightPointer);
        if (previousOccurrence != null) {
            leftPointer = Math.max(leftPointer, previousOccurrence);
        }
        answer = Math.max(answer, rightPointer - leftPointer);
        rightPointer++;
    }
    return answer;
}

上面四种解法,使用hash函数记录不同字符的位置,速度很快。
注:
void *memset(void *s, int ch, size_t n);
  函数解释:将s中前n个字节 (typedef unsigned int size_t )用 ch 替换并返回 s 。
  memset:作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法 。
  

Summarizes:

错误一:一定要注意关键点,<, >, <= ,>=

Reference:

《python核心编程》
python中的string操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值