leetcode-top100滑动窗口专题

第一题:无重复字符的最长字串

题目链接:

无重复字符的最长子串

解题思路:

解题代码:

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        n = len(s)
        if n <= 1:
            return n
        left = 0
        right = 0
        temp = []
        result = 0
        temp.append(s[left])
        while right < n-1 and right >= left:
            right += 1
            while s[right] in temp:
                temp.remove(s[left])
                left += 1
            temp.append(s[right])
            result = max(result,(right-left+1))
        return result

第二题:找到字符串中所有字母异位词

题目链接:

438. 找到字符串中所有字母异位词 - 力扣(LeetCode)

解题思路:

class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        result =[]
        n1 = len(s)
        n2 = len(p)
        left = 0
        right = n2
        p = ''.join(sorted(p))
        while right <= n1:
            temp = s[left:right]
            #print(temp)
            right += 1
            left += 1
            temp = sorted(temp)
            temp = ''.join(temp)
            #print(temp,p)
            if temp == p:
                result.append(left-1)
        return result

你可能感兴趣的:(leetcode,算法,职场和发展)