leetcode 179. 最大数 解题报告
给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。
示例 1:
输入: [10,2]
输出: 210
示例 2:
输入: [3,30,34,5,9]
输出: 9534330
说明: 输出结果可能非常大,所以你需要返回一个字符串而不是整数。
这道题目非常有意思。读完题之后,我们可以得到一个初步的方案:由于无论怎么排,最佳的方案肯定是会把所有的数字全部用上。所以,如果字典序越大的,得到的数值也会越大。只需要尽可能的让字典序大的数字处于尽可能高的位置即可。那么一个初步的实现方案如下
class Solution:
def largestNumber(self, nums):
"""
:type nums: List[int]
:rtype: str
"""
result = ''.join(sorted(map(str, nums), reverse=True))
return result
看起来不错,但是却无法通过该题的所有测试。下面给出一个反例,['30', '3'],排序之后得到的是’303‘而不是’330‘。受此启发,我们假设有两个字符串a,b。我们只要判断a+b大还是b+a大,大的排在前面即可。那么刚刚我们的反例可以顺利通过了。
于是,一个可以通过所有测试样例的代码便有了
from functools import cmp_to_key
def cmp(a, b):
if a + b > b + a:
return 1
elif a + b < b + a:
return -1
else:
return 0
class Solution:
def largestNumber(self, nums):
"""
:type nums: List[int]
:rtype: str
"""
result = ''.join(sorted(map(str, nums), key=cmp_to_key(cmp), reverse=True))
return result[:-1].lstrip('0') + result[-1]
这里涉及到了Python3 sorted函数自定义比较函数的知识点,在Python3中,sorted方法撤销掉了原有的cmp参数,所以要进行这样的排序操作更加复杂了。有关此知识点的更加详尽的说明请查阅相关文档。
leetcode 179. 最大数 解题报告的更多相关文章
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- LeetCode 179. 最大数(Largest Number) 21
179. 最大数 179. Largest Number 题目描述 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 每日一算法2019/5/24Day 21LeetCode179. La ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- Java实现 LeetCode 179 最大数
179. 最大数 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 示例 1: 输入: [10,2] 输出: 210 示例 2: 输入: [3,30,34,5,9] 输出: 9534330 ...
- 力扣Leetcode 179. 最大数 EOJ 和你在一起 字符串拼接 组成最大数
最大数 力扣 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 示例 1: 输入: [10,2] 输出: 210 示例 2: 输入: [3,30,34,5,9] 输出: 9534330 说 ...
- LeetCode 179. 最大数(Largest Number)
题目描述 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 示例 1: 输入: [10,2] 输出: 210 示例 2: 输入: [3,30,34,5,9] 输出: 9534330 说明: ...
- LeetCode: Permutation Sequence 解题报告
Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...
随机推荐
- 多重网格方法(Multigridmethod)
原文链接 多重网格方法是解微分方程的方法.这个方法的好处是在利用迭代法收敛结果的时候速度特别快.并且,不管是否对称,是否线性都无所谓.它的值要思想是在粗糙结果和精细结果之间插值. 前面介绍了Gauss ...
- axios 二进制流导出
axios 二进制流导出 axios({ url: 'http://xxx', method:'get', data:{}, headers:{ 'ContentType': 'application ...
- javascript入门笔记2-window
1.JavaScript-输出内容(document.write) <script type="text/javascript"> document.write(&qu ...
- 使用免费公开的api接口示例(iOS)
做项目难免需要测试,要测试就需要一些接口,现在网上的很多接口都是需要收费的. 以下是目前找到的免费 JSON API免费接口 云聚数据 网吧数据 其中选取了一个百度百科的接口 百度接口 百度百科接口: ...
- LeetCode567. Permutation in String
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- Servlet学习笔记02——什么是http协议?
1.http协议 (了解) (1)什么是http协议? 是一种网络应用层协议,规定了浏览器与web服务器之间 如何通信以及相应的数据包的结构. 注: a.tcp/ip: 网络层协议,可以保证数据可靠的 ...
- java动态返回一个大对象里多个小对象map返回,el表达式用c:set拼接
基于堆内存,先把map放到返回值里 Map _map=new HashMap(); modelAndView.addObject("pledgeInsurance",_map);/ ...
- Windows下如何安装composer
相对 来说并不难直接将此文件下载安装即可 1 https://getcomposer.org/Composer-Setup.exe 文件地址由官方提供 https://getcomposer.org/ ...
- MyFirstDay_答案_1.**猫(自己整理)
1>***猫: python基础类: 字符串反转的常用处理方式: # 方法一:使用字符串切片 s = "hello python" result = s[::-1] prin ...
- python正则表达式02--findall()和search()方法区别,group()方法
import re st = 'asxxixxsaefxxlovexxsdwdxxyouxxde' #search()和 findall()的区别 a = re.search('xx(.*?)xxsa ...