[Lintcode]184. Largest Number/[Leetcode]179. Largest Number

184. Largest Number/179. Largest Number

  • 本题难度: Medium
  • Topic: Greedy

Description

  1. Largest Number

    中文English

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

Example

Given [1, 20, 23, 4, 8], the largest formed number is 8423201.

Challenge

Do it in O(nlogn) time complexity.

Notice

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

别人的代码

参考1

from functools import cmp_to_key

class Solution:
    def largestNumber(self, nums):
        def cmp(a, b):
            return int(b + a) - int(a + b)

        nums = list(map(str, nums))
        nums.sort(key=cmp_to_key(cmp))
        return ‘‘.join(nums).lstrip(‘0‘) or ‘0‘‘

参考2

# build-in function
def largestNumber1(self, nums):
    if not any(nums):
        return "0"
    return "".join(sorted(map(str, nums), cmp=lambda n1, n2: -1 if n1+n2>n2+n1 else (1 if n1+n2<n2+n1 else 0)))

# bubble sort
def largestNumber2(self, nums):
    for i in xrange(len(nums), 0, -1):
        for j in xrange(i-1):
            if not self.compare(nums[j], nums[j+1]):
                nums[j], nums[j+1] = nums[j+1], nums[j]
    return str(int("".join(map(str, nums))))

def compare(self, n1, n2):
    return str(n1) + str(n2) > str(n2) + str(n1)

# selection sort
def largestNumber3(self, nums):
    for i in xrange(len(nums), 0, -1):
        tmp = 0
        for j in xrange(i):
            if not self.compare(nums[j], nums[tmp]):
                tmp = j
        nums[tmp], nums[i-1] = nums[i-1], nums[tmp]
    return str(int("".join(map(str, nums))))

# insertion sort
def largestNumber4(self, nums):
    for i in xrange(len(nums)):
        pos, cur = i, nums[i]
        while pos > 0 and not self.compare(nums[pos-1], cur):
            nums[pos] = nums[pos-1]  # move one-step forward
            pos -= 1
        nums[pos] = cur
    return str(int("".join(map(str, nums))))

# merge sort
def largestNumber5(self, nums):
    nums = self.mergeSort(nums, 0, len(nums)-1)
    return str(int("".join(map(str, nums))))

def mergeSort(self, nums, l, r):
    if l > r:
        return
    if l == r:
        return [nums[l]]
    mid = l + (r-l)//2
    left = self.mergeSort(nums, l, mid)
    right = self.mergeSort(nums, mid+1, r)
    return self.merge(left, right)

def merge(self, l1, l2):
    res, i, j = [], 0, 0
    while i < len(l1) and j < len(l2):
        if not self.compare(l1[i], l2[j]):
            res.append(l2[j])
            j += 1
        else:
            res.append(l1[i])
            i += 1
    res.extend(l1[i:] or l2[j:])
    return res

# quick sort, in-place
def largestNumber(self, nums):
    self.quickSort(nums, 0, len(nums)-1)
    return str(int("".join(map(str, nums)))) 

def quickSort(self, nums, l, r):
    if l >= r:
        return
    pos = self.partition(nums, l, r)
    self.quickSort(nums, l, pos-1)
    self.quickSort(nums, pos+1, r)

def partition(self, nums, l, r):
    low = l
    while l < r:
        if self.compare(nums[l], nums[r]):
            nums[l], nums[low] = nums[low], nums[l]
            low += 1
        l += 1
    nums[low], nums[r] = nums[r], nums[low]
    return low

思路

  • 时间复杂度
  • 出错

原文地址:https://www.cnblogs.com/siriusli/p/10360070.html

时间: 2024-12-18 03:20:28

[Lintcode]184. Largest Number/[Leetcode]179. Largest Number的相关文章

8.15 [LeetCode] 179 Largest Number

[LeetCode 179] Largest Number | COMMENTS Question link Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be ver

leetCode 179. Largest Number 字符串排序 | Medium

179. Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a st

[LeetCode] 179. Largest Number 解题思路

Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of an i

leetcode[179]Largest Number

Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of an i

Java for LeetCode 179 Largest Number

Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of an i

[LeetCode] 179. Largest Number Java

题目: Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of

Java 特定规则排序-LeetCode 179 Largest Number

Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of an i

LeetCode 179. Largest Number(排序)

题目 题意:给你一个数组,让你把数组里的元素组合起来,组合成的新数字最大 题解:把数字给排个序,但是排序的标准是啥呢?两个数字孰大孰小呢?判断标准就是两个数字分别前后组合,得出的数字哪个大,则前面的那个数字就大. 一开始我的思路错了,根据数字每个位上的数字判断,这样反而是变得更加复杂了! class Solution { public: string largestNumber(vector<int>& nums) { vector<string> strs; for(in

LeetCode:Largest Number

1.题目名称 Largest Number(求整型数组中各元素可拼合成的最大数字) 2.题目地址 https://leetcode.com/problems/largest-number/ 3.题目内容 英文:Given a list of non negative integers, arrange them such that they form the largest number. 中文:给出一组非负整数,求这些非负整数可以拼接出的最大数字 说明:例如,给出数组 [3, 30, 34,