题目简述:

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.

解题思路:

这有一种很简单的思路:

我们无非是要判断哪个串放在哪个的前面或者后面,这其实就是两个数的比较问题,只不过大小的比较方式不是通常的形式。当然通过字符串的处理有很多的方式,不过都略显复杂了,反正两个数的比较就两种情况,所以我们不妨列拿出来比较下得出结果就行。

#coding=utf-8
class Solution:
def cmp(self,x,y):
if x*(10**len(str(y)))+y < y*(10**len(str(x)))+x:
return 1
elif x*(10**len(str(y)))+y == y*(10**len(str(x)))+x:
return 0
else:
return -1
# @param num, a list of integers
# @return a string
def largestNumber(self, num):
num.sort(self.cmp)
return str(int(''.join(map(lambda x: str(x),num))))

【leetcode】Largest Number的更多相关文章

  1. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  2. 【leetcode】Largest Number ★

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

  3. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  4. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  5. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  6. 【LeetCode】476. Number Complement (java实现)

    原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...

  7. 【leetcode】Largest Plus Sign

    题目如下: In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the giv ...

  8. 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...

  9. 【LeetCode】1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 代码 复杂度分析 日期 题目地址:http ...

随机推荐

  1. 使用sublime一键格式化XML文件

    1 sublime简介 sublime是一款代码编辑和阅读软件,体积小,运行快,界面非常简洁漂亮.官方地址:https://www.sublimetext.com/ 2 在sublime上安装插件 使 ...

  2. HTML插入FLASH

    <object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" code ...

  3. SQL Server需要监控哪些计数器

    常规计数器 收集操作系统服务器的服务器性能信息,包括Processor.磁盘.网络.内存 Processor 处理器 1.1 % Processor Time指处理器用来执行非闲置线程时间的百分比.通 ...

  4. pywin32 创建一个窗口

    import win32con,win32gui class MyWindow(): def __init__(self): #注册一个窗口类 wc = win32gui.WNDCLASS() wc. ...

  5. js的click事件传递参数方法

    参考链接:http://www.cnblogs.com/shytong/p/5005704.html 由于是回调函数,事先就需要先把数据储存在event上,否则只能用全局变量做为参数传递,建议用bin ...

  6. Netty里的设计模式

    最近在撸 Netty 源码,发现了一些模式,顺手做个笔记. 分析版本是4.0 1. 构造器模式 ServerBootstrap 和 Bootstrap 的构建 2. 责任链设计模式 pipeline ...

  7. Volley框架使用笔记

    1.初始化请求队列 RequestQueue RequestQueue queue= Volley.newRequestQueue(context); 2.StringRequest 网络请求 Get ...

  8. 【Kubernetes】K8S 网络隔离 方案

    参考资料: K8S-网络隔离参考 OpenContrail is an open source network virtualization platform for the cloud. – Kub ...

  9. SecureCRT 常用命令

    常用命令:一.ls 只列出文件名 (相当于dir,dir也可以使用) -A:列出所有文件,包含隐藏文件. -l:列表形式,包含文件的绝大部分属性. -R:递归显示. --help:此命令的帮助. 二. ...

  10. day21

    1.文件上传     a. Html Form表单提交     b. Ajax提交         原生XMLHttpRequest             XmlHttpReqeust() 类    ...