[leetcode sort]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 integer.
对一组数字排序,使其组成的数字最大
思路:排序,不过重新定义排序规则,比如字符串x和字符串y,比较x+y和y+x的大小,如果x+y>y+x,则x>y
class Solution:
def largestNumber(self, nums):
nums = [str(n) for n in nums]
nums.sort(cmp=lambda y,x:cmp(x+y,y+x))
return ''.join(nums).lstrip('') or ''
[leetcode sort]179. Largest Number的更多相关文章
- 【Leetcode】179. Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 【刷题-LeetCode】179 Largest Number
Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...
- leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数
这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...
- [LeetCode] 179. Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. Example ...
- leetcode 179. Largest Number 求最大组合数 ---------- java
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Java for LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [LeetCode] 179. Largest Number 解题思路
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [leetcode]179. Largest Number最大数
Given a list of non negative integers, arrange them such that they form the largest number. Input: [ ...
随机推荐
- oracle常见错误对应代码与含义
本篇文章是对oracle错误代码进行了详细的总结与分析,需要的朋友参考下 ORA-00001: 违反唯一约束条件 (.) ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会 ...
- POJ 2449 Remmarguts' Date (K短路 A*算法)
题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...
- A - ACM Computer Factory(网络流)
题目链接:https://cn.vjudge.net/contest/68128#problem/A 反思:注意拆点,否则的话节点就没用了,还有注意源点和汇点的赋值. AC代码: #include&l ...
- Understanding the Space Used by ZFS -- (转)
Understanding the Space Used by ZFS By Brian Leonard on Sep 28, 2010 Until recently, I've been confu ...
- 洛谷 P3307: bzoj 3202: [SDOI2013] 项链
题目传送门:洛谷P3307.这题在bzoj上是权限题. 题意简述: 这题分为两个部分: ① 有一些珠子,每个珠子可以看成一个无序三元组.三元组要满足三个数都在$1$到$m$之间,并且三个数互质,两个珠 ...
- 源码安装postgresql数据库
一般情况下,postgresql由非root用户启动. 1.创建postgres用户 groupadd postgres useradd -g postgres postgres 下面的操作都在pos ...
- Java关于网络编程回顾
一.Java网络编程三要素:1.IP地址:是要确定发送的地址,IP地址一般分为5类. 2.端口:要确定发送的程序是哪一个,端口的范围是0--65535,其中0-1024是系统使用或保留端口 3.协议: ...
- 一、springboot入门
构建spring boot工程一般采用两种方式 gradle .maven maven方式 pom.xml spring-boot-starter:核心模块,包括自动配置支持.日志和YAML spri ...
- Android studio 安装过程中遇到的问题
之前用eclipse,想换下studio试试,安装时遇到问题,参考:http://www.cnblogs.com/csulennon/p/4178404.html
- HDU 5115 Dire Wolf (区间DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5115 题目大意:有一些狼,从左到右排列,每只狼有一个伤害A,还有一个伤害B.杀死一只狼的时候,会受到这 ...