leetcode_3 - Longest Substring Without Repeating Characters

leetcode_3 : Longest Substring Without Repeating Characters

标签(空格分隔): leetcode string twoPoints set


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.

译:给定字符串, 求出字符串的最长字串的长度

我最初思路写的Python代码:

"""
思路:定义i, j, i 从 0 ~ length 循环, j 从 i ~ length 开始循环, 对于每一个字符, 记录下以该字符为首字符的最长子串, 将其放入list中,当遇到相同的字符时,求出此时字串的长度,如果大于maxLen, 则用maxLen记录下, 如此反复。 

问题:该算法的时间复杂度为O(n * n), 空间复杂度为O(n), 最终在OJ上时间超时。
"""
class Solution(object):
    def lengthOfLongestSubstring(self, s):
        # 获取字符串的长度
        length = len(s)
        #字串长度
        maxLen = 0
        for i in range(0, length):
            sublen = 0
            temp = []
            for j in range(i, length):
                if s[j] in temp:
                    break
                else:
                    sublen += 1
                    temp.append(s[j])
            if sublen > maxLen:
                maxLen = sublen
        return maxLen
# 测试代码
str = Solution()
print('the maxSubStr : ' + str.lengthOfLongestSubstring('pwwkew'))

方法二:滑动窗口算法

Python:

"""
思路:定义两个指针i, j。初始i = j = 0, 此时判断j指向的字符是否在set里面, 如果不在则j滑向下一个字符,计算此时字串的长度,如果大于此时记录的最大字串长度, 则更新最长字串长度, 同时将刚刚j指向的字符添加进set中;如果字符在set中, 则将i滑向下一个字符,此时set中也需要滑动, 即删除掉i刚刚指向的字符,如此直到结束。
分析:时间复杂度为O(2n) = O(n), 空间复杂度为O(maxLen)。结果AC!
"""
class Solution(object):
    def lengthOfLongestSubstring(self, s):
        # 获取字符串的长度
        length = len(s)
        # 最长子串长度
        ans = 0
        # 设置保存字串字符的set
        sets = set()
        i = j  = 0
        while i < length and j < length:
            if s[j] not in sets:
                sets.add(s[j])
                j += 1
                ans = max(j - i, ans)
            else:
                sets.remove(s[i])
                i += 1
        return ans

Java:

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        Set<Character> set = new HashSet<>();
        int ans = 0, i = 0, j = 0;
        while (i < n && j < n) {
            // try to extend the range [i, j]
            if (!set.contains(s.charAt(j))){
                set.add(s.charAt(j++));
                ans = Math.max(ans, j - i);
            }
            else {
                set.remove(s.charAt(i++));
            }
        }
        return ans;
    }
}

方法三:优化的滑动窗口算法

Java:

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length(), ans = 0;
        Map<Character, Integer> map = new HashMap<>(); // current index of character
        // try to extend the range [i, j]
        for (int j = 0, i = 0; j < n; j++) {
            if (map.containsKey(s.charAt(j))) {
                i = Math.max(map.get(s.charAt(j)), i);
            }
            ans = Math.max(ans, j - i + 1);
            map.put(s.charAt(j), j + 1);
        }
        return ans;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值