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. 职责链模式,chain of responsibility

    定义: 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这个对象连城一条链,并沿着这条链传递该请求,知道有一个对象处理它为止. 客户端并不知道哪个对象会最终处理这个请求,这样 ...

  2. jQuery 预习视频

    1.事件补充 <input type="button" onclick="CheckAll('#edit_mode','#tb1');" value=&q ...

  3. javascript练习----复选框全选,全不选,反选

    第一种方式: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  4. iOS - AVPlayer 音视频播放

    前言 NS_CLASS_AVAILABLE(10_7, 4_0) @interface AVPlayer : NSObject @available(iOS 4.0, *) public class ...

  5. new road

    9月底进入到了新的公司,有些类似实习的时候的路程. 新公司的数据业务基本是一种幻想状态,完全没有什么数据方面的积淀.

  6. linux 命令 第一波

    man 命令名字 查看命令详细解释 q退出cd 切换目录cd .. 回到上级目录su 切换用户pwd 当前目录mkdir cmy 创建cmy文件夹[目录]rm cmy 删除cmy文件夹[如果cmy里面 ...

  7. ORA-12705: Cannot access NLS data files or invalid environment specified

    ASM实例无法启动 [grid@data ~]$ sqlplus / as sysasm SQL*Plus: Release 11.2.0.4.0 Production on Fri Sep 11 0 ...

  8. spring 配置触发器

    原转发的博文 http://blog.csdn.net/liaq325/article/details/8269439 在Spring中配置Quartz 前面介绍过了Timer在Spring中的实现, ...

  9. c++程序编码

    c++程序中涉及到中文字符的输入输出以及其他操作经常会出现乱码.乱码主要是由于程序的源文件编码.可执行文件编码以及程序运行环境的编码不匹配导致.比如,c++源程序文件编码为GB18030, 在源程序中 ...

  10. smarty 学习记录

    smarty模版是比较大众化的一个模版,在php开发过程当中被很多开发者视为最友好的模版之一,学习smarty课程对于很多培训机构来说也是列入了培训课程之一,那么很多方面就需要我们学习了一. 安装首先 ...