【leetcode】3. Longest Substring Without Repeating Characters(Python & C++)

51 篇文章 1 订阅
50 篇文章 28 订阅

3. Longest Substring Without Repeating Characters

题目链接

3.1 题目描述:

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given “abcabcbb”, the answer is “abc”, which the length is 3.

Given “bbbbb”, the answer is “b”, with the length of 1.

Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

3.2 解题思路:

  1. 思路一:首先记录字符串第一个元素为要找的最长无重复子串sub和初始长度count为1,并这是长度临时变量temp。然后依次遍历字符串后面的元素,判断此元素是否在子串sub中,如果不在,则直接将该字符append到sub后面,temp++;否则,获取该元素在sub中的位置p,erase子串sub中前p+1个元素,然后将该元素append到sub后面,temp-p。然后比较count与temp的大小,进行count更新。

  2. 思路二:利用vector,因为此次考虑的字符串中只包含字母,所以可以创建一个256大小,初始化为-1的vector,负责记录字符串中每个字符所在的坐标。并初始化一个first为-1,记录最长无重复子串的起始坐标。在每次遍历字符串时,如果vector中记录的该元素的坐标大于first,说明之前子串已经用到过相同的元素值,发现重复,则更新first为该元素坐标。然后将该元素坐标记录到vector中。最后更新一下count,根据当前遍历的坐标i-first与count比较。

  3. 思路三:(Python)使用字典,跟思路二相同,只不过代码看起来更加简洁。

3.3 C++代码:

1、思路一代码(16ms):

class Solution92 {
public:
    int lengthOfLongestSubstring(string s) {
        if (s.length() == 0)
            return 0;
        if (s.length() == 1)
            return 1;               
        string sub = "";
        sub.append(1, s[0]);
        int in = 0;
        int count = 1;
        int temp = 1;
        int i = 1;
        while (i<s.length())
        {
            if (sub.find(s[i])==-1)
            {
                sub.append(1, s[i]);
                temp++;
            }
            else
            {
                in = sub.find(s[i]) + 1;
                sub.erase(0, in);
                sub.append(1, s[i]);
                temp = temp - in  + 1;
            }
            if (temp>count)
            {
                count = temp;
            }
            i++;
        }
        return count;
    }
};

2、思路二代码(22ms):

class Solution92_1 {
public:
    int lengthOfLongestSubstring(string s) {
        vector<int>c(256, -1);
        int count = 0;
        int first = -1;
        for (int i = 0; i < s.length();i++)
        {
            if (c[s[i]]>first)
                first = c[s[i]];
            c[s[i]] = i;
            count = count > (i - first) ? count : (i - first);
        }
        return count;
    }
};

3.4 Python代码:

1、思路一代码(112ms):

class Solution1(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        if len(s)==0:
            return 0
        if len(s)==1:
            return 1
        count=1
        temp=1
        sub=""+s[0]
        f=0
        for i in range(1,len(s)):
            if sub.find(s[i])==-1:
                sub=sub+s[i]
                temp+=1
            else:
                f=sub.find(s[i])+1
                sub=sub[f::]+s[i]
                temp=temp-f+1
            if temp>count:
                count=temp
        return count    

2、思路二代码(96ms):

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        a=[-1]*256
        count=0
        first=-1
        for i in range(len(s)):
            if a[ord(s[i])]>first:
                first=a[ord(s[i])]
            a[ord(s[i])]=i
            count=max(count,(i-first))
        return count

3、思路三代码(102ms):

class Solution2(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        a={}
        count=0
        first=-1
        for i in range(len(s)):
            if s[i] in a and a[s[i]]>first:
                first=a[s[i]]
            a[s[i]]=i
            count=max(count,(i-first))
        return count

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值