呓语Beta 2.0 首页 镜头后 灶台前 捐赠者名单 有趣的小站

leetcode 506 Relative Ranks

文档信息

leetcode 506 Relative Ranks

解法一

class Solution(object):
    def findRelativeRanks(self, nums):
    """
    :type nums: List[int]
    :rtype: List[str]
    """
    sorted_nums = sorted(nums, reverse=True)
                                                   
    for k, v in enumerate(sorted_nums):
        nums[nums.index(v)] = str(k+1)
                                                                             
    if len(nums)>0:
        nums[nums.index("1")] = "Gold Medal"
    if len(nums)>1:
        nums[nums.index("2")] = "Silver Medal"
    if len(nums)>2:
        nums[nums.index("3")] = "Bronze Medal"

    return nums

note

解法二

def findRelativeRanks(self, nums):
    sort = sorted(nums)[::-1]
    rank = ["Gold Medal", "Silver Medal", "Bronze Medal"] + map(str, range(4, len(nums) + 1))
    return map(dict(zip(sort, rank)).get, nums)

note

捐赠支持

可以使用支付宝转帐给richard.ma.19850509#gmail.com(请将#替换为@),谢谢!

Fork me on GitHub