[每日一题] 12.26 - 无重复字符的最长子串

A+B Problem

[每日一题] 12.26 - 无重复字符的最长子串_第1张图片我的:

s = input().split()
print(int(s[0]) + int(s[1]))

无重复字符的最长子串

[每日一题] 12.26 - 无重复字符的最长子串_第2张图片

def lengthOfLongestSubstring(s):
    list = []
    max_length = 0
    for end in range(len(s)):
        while s[end] in list:
            list.pop(0)
        list.append(s[end])
        max_length = max(max_length,len(list))
    return max_length

def lengthOfLongestSubstring(s: str):
    max_length = 0
    start = 0
    sett = set()
    for end ,c in enumerate(s):
        while c in sett:
            sett.remove(s[start])
            start += 1
        sett.add(c)
        max_length = max(max_length,end - start + 1)
    return max_length

def lengthOfLongestSubstring(s: str):
    max_len = 0
    hashmap = {}

    start = 0

    for end in range(len(s)):
        # 这一行将哈希表中键为 s[end] 的值加一,并更新字典中的键值对。如果 s[end] 在哈希表中不存在,就将其添加到哈希表中,值为 1。
        hashmap[s[end]] = hashmap.get(s[end],0) + 1 # 用于获取指定键 s[end] 对应的值。如果键不存在,则返回默认值 0
        if len(hashmap) == end - start + 1:
            max_len = max(max_len, end - start + 1)

        while end - start + 1 > len(hashmap):
            head = s[start]
            hashmap[head] -= 1
            if hashmap[head] == 0:
                del hashmap[head]
            start += 1
    return max_len

子数组最大平均数

[每日一题] 12.26 - 无重复字符的最长子串_第3张图片

def findMaxAverage(nums: list[int], k: int) -> float:
    sum = 0
    largest = float('-inf')
    start = 0 # 定义窗口首尾
    for end in range(len(nums)):
        sum += nums[end]
        if end >= k:
            sum -= nums[start]
            start += 1
        if end >= k - 1:
            largest = max(sum, largest)
    return largest / k

任何一个伟大的思想,都有一个微不足道的开始。

你可能感兴趣的:(算法,python,python,算法)