zoukankan      html  css  js  c++  java
  • Leetcode 179. Largest Number

    Description: Given a list of non-negative integers nums, arrange them such that they form the largest number.

    Note: The result may be very large, so you need to return a string instead of an integer.

    Link: 179. Largest Number

    Examples:

    Example 1:
    Input: nums = [10,2]
    Output: "210"
    
    Example 2:
    Input: nums = [3,30,34,5,9]
    Output: "9534330"
    
    Example 3:
    Input: nums = [1]
    Output: "1"
    
    Example 4:
    Input: nums = [10]
    Output: "10"

    思路: 重点在于如何比较两个数,在题目的设定下,什么样的数字应该是大的,应该排在前面。xy连接起来大于yx,那么x就是大的。所以任何排序算法都可以用,只是将平常的数字大小比较换成题目中的compare. 试一下冒泡:

    class Solution(object):
        def largestNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: str
            """
            if len(nums) <= 1:
                return str(nums[0])
            for i in range(len(nums)-1):
                for j in range(i+1, len(nums)):
                    if self.compare(nums[i], nums[j]):
                        nums[i], nums[j] = nums[j], nums[i]
            res = ''
            for s in nums[::-1]:
                res += str(s) 
            if res == len(res) * '0':
                return '0'
            return res     
                
        def compare(self, x, y):
            xy = int(str(x)+str(y))
            yx = int(str(y)+str(x))
            if xy >= yx:
                return True
            return False

    归并排序:

    class Solution(object):
        def largestNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: str
            """
            if len(nums) <= 1:
                return str(nums[0])
            sortnums = self.sortlist(nums)
            res = ''
            for s in sortnums:
                res += str(s) 
            if res == len(res) * '0':
                return '0'
            return res 
        
        def sortlist(self, nums):
            if len(nums) == 1:
                return nums
            mid = len(nums) // 2
            return self.merge(self.sortlist(nums[:mid]), self.sortlist(nums[mid:]))
        
        def merge(self, left, right):
            res = []
            while len(left) > 0 and len(right) > 0:
                if self.compare(left[0], right[0]):
                    res.append(left.pop(0))
                else:
                    res.append(right.pop(0))
            res += left
            res += right
            return res
            
            
            
        def compare(self, x, y):
            xy = int(str(x)+str(y))
            yx = int(str(y)+str(x))
            if xy >= yx:
                return True
            return False

    日期: 2021-03-31  又是阴天,和心情一样糟糕。

  • 相关阅读:
    数据库封装类使用
    C# Socket-TCP异步编程原理详解附源码
    Mysql 5.7安装与配置-默认密码
    Python-Django WebAPi基本使用方法
    C# 微信小程序获取openid sessionkey
    voith项目配置服务程序
    社区安防系统
    数据结构和算法-一元多项式运算算法(加法)
    数据结构和算法-贪婪算法
    Oracle分页SQL语句
  • 原文地址:https://www.cnblogs.com/wangyuxia/p/14602096.html
Copyright © 2011-2022 走看看