【每日一题1017】两个最大数字

:mage:t2: 给定一个数字列表,请编写一个函数,找出其中最大的两个不同数字,并且返回结果按照从大到小排列。

示例:
输入: [4, 10, 10, 9],输出:[10, 9]
题目难度:简单
题目来源:codewars: Return Two Highest Values in List

def two_highest(nums: list) -> list:
    pass

assert two_highest([4, 10, 10, 9]) == [10, 9]
assert two_highest([15, 20, 20, 17]) == [20, 17]
def two_highest(nums: list) -> list:
    nums = list(set(nums))
    nums.sort(reverse=True)
    return nums[:2]


assert two_highest([4, 10, 10, 9]) == [10, 9]
assert two_highest([15, 20, 20, 17]) == [20, 17]

排序,切片[:2]

def two_highest(nums: list) -> list:
    # 定义最大值变量,初始值为空
    max_num = None
    # 定义第二大值变量,初始值为空
    second_num = None
    # 遍历列表中的每个数字
    for num in nums:
        # 判断最大值变量为空,则进行赋值操作
        # 或者该数字大于最大值,则进行赋值操作
        if max_num is None or num > max_num:
            max_num = num

        # 判断该数字小于最大值,则进入第二次判断
        if num < max_num:
            # 判断第二大值变量为空,则进行赋值操作
            # 或者该数字大于第二大值,则进行赋值操作
            if second_num is None or num > second_num:
                second_num = num

    # 最大值或者第二大值为空,则剔除掉
    # 然后返回列表
    result = [v for v in [max_num, second_num] if v]
    return result


assert two_highest([4, 10, 10, 9]) == [10, 9]
assert two_highest([15, 20, 20, 17]) == [20, 17]
assert two_highest([1, 1, 1]) == [1]
assert two_highest([]) == []
public List<Integer> twoHighest(List<Integer> arrayList) {
        return arrayList.stream().sorted(Comparator.reverseOrder()).distinct().limit(2).collect(Collectors.toList());
    }

    @Test
    public void testTwoHighest() {
        twoHighest(Arrays.asList(4, 10, 10, 9)).stream().forEach(System.out::println);
        twoHighest(Arrays.asList(15, 20, 20, 17)).stream().forEach(System.out::println);
    }

第一种

def sort_list(list_num: List[int]) -> List[int]:
    list_tmp = list(set(list_num))
    list_tmp.sort(reverse=True)
    return list_tmp[:2]

第二种

def sort_list(list_num: List[int]) -> List[int]:
    set_dict = set(list_num)
    list_tmp = list()
    for num in set_dict:
        if num in list_tmp: // 这个判断在使用set时,可以不写
            continue
        if not list_tmp:
            list_tmp.append(num)
            continue
        for t in list_tmp:
            if t < num:
                list_tmp.insert(list_tmp.index(t), num)
                break
    return list_tmp[:2]

def two_highest(nums: list) -> list:
    nums.sort(reverse=True)
    two_highest_list = []
    for n in nums:
        if n not in two_highest_list:
            two_highest_list.append(n)
        if len(two_highest_list) == 2:
            return two_highest_list


assert two_highest([4, 10, 10, 9]) == [10, 9]
assert two_highest([15, 20, 20, 17]) == [20, 17]
def two_highest(nums: list) -> list:
    L = list(set(nums))
    L.sort(reverse=True)
    return L[:2]

assert two_highest([4, 10, 10, 9]) == [10, 9]
assert two_highest([15, 20, 20, 17]) == [20, 17]
def two_highest(nums: list) -> list:
    nums = sorted(list(set(nums)))
    nums.reverse()
    return nums[:2]


assert two_highest([4, 10, 10, 9]) == [10, 9]
assert two_highest([15, 20, 20, 17]) == [20, 17]

Python参考题解:

import heapq

def two_highest(nums: list) -> list:
    return heapq.nlargest(2, list(set(nums))) 
    
assert two_highest([4, 10, 10, 9]) == [10, 9]
assert two_highest([15, 20, 20, 17]) == [20, 17]

思路:利用标准库heapq进行堆排序,获取最大的两个数。

def two_highest(nums: list) -> list:
    set1 = set(nums)
    list1 = list(set1)
    list1.sort(reverse=True)
    return list1[0:2]


assert two_highest([4, 10, 10, 9]) == [10, 9]
assert two_highest([15, 20, 20, 17]) == [20, 17]
def two_highest(nums: list) -> list:
    return sorted(list(set(nums)), reverse=True)[:2]


assert two_highest([4, 10, 10, 9]) == [10, 9]
assert two_highest([15, 20, 20, 17]) == [20, 17]
def two_highest(nums: list) -> list:
    return sorted(set(nums),reverse=True)[:2]


assert two_highest([4, 10, 10, 9]) == [10, 9]
assert two_highest([15, 20, 20, 17]) == [20, 17]