179. Largest Number -- 数字字符串比较大小

时间:2022-03-06 09:53:37

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&period; 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&period; Largest Number)

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

  4. &lbrack;leetcode sort&rsqb;179&period; Largest Number

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

  5. 【Leetcode】179&period; Largest Number

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

  6. &lbrack;LeetCode&rsqb; 179&period; Largest Number 最大组合数

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

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

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

  8. 179&period; Largest Number&lpar;INT&comma; String&rpar;

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

  9. &lbrack;leed code 179&rsqb; Largest Number

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

随机推荐

  1. Ubuntu Server 安装桌面untiy

    1.安装 sudo apt-get install ubuntu-desktop sudo apt-get install unity sudo apt-get install unity-commo ...

  2. PHP中phar包的使用

    PHP5.3之后支持了类似Java的jar包,名为phar.用来将多个PHP文件打包为一个文件. 首先需要修改php.ini配置将phar的readonly关闭,默认是不能写phar包的,includ ...

  3. android中include 的使用讲解

    include的作用就是重复使用同一段代码,提高代码的重用性.具体说就是,通过include 在 某布局 a.xml 中引用 B.xml布局文件,这个b.xml可同时被多个布局同时使用,所以达到了同一 ...

  4. 关于macOS Sierra无法使用gdb进行调试的解决方案

    1.对gdb进行签名,签名过程详见:http://jingyan.baidu.com/article/d169e1864dc24d436611d839.html: 2.重新启动系统,同时按住键盘上的c ...

  5. Ubuntu的LTS版本

    Ubuntu的LTS版本什么意思 LTS是长期支持(Long Term Support)的缩写. 我们每六个月制作一个新的Ubuntu桌面和服务器的版本,这意味着你总能拥有开源世界提供的最新最好的应用 ...

  6. STL学习系列五:Queue容器

    Queue简介 queue是队列容器,是一种“先进先出”的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> 1.queue对象的默认构造 ...

  7. 一步步学习ASP&period;NET MVC3 &lpar;5&rpar;——View从Action中获得数据

    请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们把Razor的模板技术给大家介绍了一下,当然模板中还有其他的知识点,这个以后我们还会继续讲解.本章我们主要讨论 ...

  8. 离线消息如何实现?-- ESFramework 4&period;0 快速上手(02)

    在ESFramework 4.0 快速上手一文中,主要介绍了如何使用ESPlus.Rapid命名空间中的引擎来快速地构建基于TCP的网络通信系统,即使是使用ESPlus.Rapid来进行ESFrame ...

  9. 网页的居中显示,使用了margin、clear&colon;both

    很久没写过页面了,现在写起来也觉得捡起来还是挺快的. 当时遇到了这样的问题,我有一个大的div包涵了整个网站,有网页头部,中部还有底部.头部就是一个标题,中部就是几张图片跟文字排版,结果左右两边的图片 ...

  10. Ubuntu下errno值

    Ubuntu下errno值             每当一个Unix函数(如socket函数)中错误发生时,全局变量errno将被设置成一个指示错误类型的正整数,假设函数不返回错误,errno的值就未 ...