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.

class Solution {
static bool cmp(string s1, string s2) //字符串比较 GOOD!
{
return s1+s2 > s2+s1;
} public:
string largestNumber(vector<int>& nums) {
vector<string> vs;
for(int i = ; i < nums.size(); i++)
vs.push_back(to_string(nums[i])); //int转string
sort(vs.begin(), vs.end(), cmp);
string s = "";
for(int i = ; i < vs.size(); i++)
s += vs[i];
if(s[] == '')
return "";
return s;
}
};

179. Largest Number -- 数字字符串比较大小的更多相关文章

  1. leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数

    这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...

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

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

  3. JavaScript中sort方法的一个坑(leetcode 179. Largest Number)

    在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...

  4. [leetcode sort]179. Largest Number

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

  5. 【Leetcode】179. Largest Number

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

  6. [LeetCode] 179. Largest Number 最大组合数

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

  7. leetcode 179. Largest Number 求最大组合数 ---------- java

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

  8. 179. Largest Number(INT, String)

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

  9. [leed code 179] Largest Number

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

随机推荐

  1. COM/ATL 资料收集

    COM/ATL COM基础知识 COM技术分类

  2. [转]or cad drc 错误

    本文转自 恋上姐的博客 http://blog.sina.com.cn/u/1750715103 用“取缔”一词,是源自<嘻哈四重奏>里面卢导的口头禅,哈哈借用一下!大多数DRC warn ...

  3. 多命令顺序执行、管道符 ; && || |

    多命令顺序执行:

  4. Java——Image 图片合并

    1.合并图片 package com.tb.image; import java.awt.Image; import java.awt.image.BufferedImage; import java ...

  5. .net 浏览器请求过程(图)

    大致: 细节: (信息来源于传智播客教学视频)

  6. linux 查看是否安装perl模块

    这里介绍两种linux中查看perl模块是否安装的方法,一种是对于单体的模块,一种是对于群体的. 单体验证: [root@root ~]# perl -MShell -e "print\&q ...

  7. HTML5探索之datalist研究

    最近一个项目需要用到类似淘宝,百度搜索时的自动检索方案.自然想到了使用datalist标签.但是遇到一个bug,经过两天研究.小有结果给大家分享下~~ 首先看bug吧!~ 因为项目最开始测试就是用36 ...

  8. hibernate配置文件中的catalog属性

    在hibernate表的映射文件中 <hibernate-mapping>    <class name="com.sooyie.hibernate.orm.Link&qu ...

  9. ffmpeg,X264编码结果I帧QP比P帧还大

    enc_ctx->profile =FF_PROFILE_H264_MAIN ; enc_ctx->time_base.den = 24; enc_ctx->time_base.nu ...

  10. Mysql 组合查询 UNION 与 UNION ALL