AI量化知识树

test

由bqnqc390创建,最终由bqnqc390 被浏览 57 用户

#102
def func(a):
    count = 0  # 当前元素的出现次数
    candidate = None  # 当前出现次数最多的元素
    for num in a:
        if count == 0:  # 当前元素第一次出现
            candidate = num
        count += (1 if num == candidate else -1)  # 如果当前元素和候选元素相同,则计数加一;否则减一
    return candidate
assert func([1, 1, 2, 3, 4, 5, 5, 5, 6]) == 5





#103
def func(a):  
    res = []  
    for i in a:  
        if i not in res:  
            res.append(i)  
    return res
assert func([1, 1, 2, 3, 4, 5, 5, 5, 6]) == [1, 2, 3, 4, 5, 6]


#104
def func(a, b):  
    left, right = 0, len(a) - 1  
    count = 0  
    while left < right:  
        if a[left] + a[right] == b:  
            count += 1  
            left += 1  
            right -= 1  
        elif a[left] + a[right] < b:  
            left += 1  
        else:  
            right -= 1  
    return count
assert func([1, 2], 4) == 0    # 不存在和为4的可能
assert func([1, 2, 3, 4, 5, 6, 9], 8) == 2    # 2+6, 3+5
    
    

\

{link}