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  又是阴天,和心情一样糟糕。

  • 相关阅读:
    Java NIO学习(Path接口、Paths和Files工具类的使用)
    Java 实现文件复制的不同方法
    Java NIO学习
    Properties工具类学习
    RandomAccessFile类学习
    Java管道流学习
    Java字符串流学习
    word里输入英文字母间距变宽,字体改变,怎么回事?
    js 和 css 中 不能使用 jsp 页面中一些 标签 和 java 代码等,应注意
    url地址 参数 带 参数 注意事项 , chain , redirect , redirectAction
  • 原文地址:https://www.cnblogs.com/wangyuxia/p/14602096.html
Copyright © 2011-2022 走看看