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.

分析,刚开始的时候想着用基数排序来做,看到网上的分析,直接应用STL中的排序函数,只要重新定义一个符合特殊要求的比较函数就行了,顿时觉得简单多了·····················

转自:http://www.cnblogs.com/ganganloveu/p/4228832.html

首先考虑两个数字拼在一起会有越界的可能性,所以,要用字符串来存储。(这个特别重要,许多的题都需要这样来做)

这次关键在于排序compare,也就是怎样比较a和b的拼接顺序。

这样来考虑:

如果完全相同,那么返回false。

如果在相同位数下已经比较出结果,比如12 vs. 131,由于2<3,因此13112比12131来的大,返回true。

如果在相同位数下无法判断,比如12 vs. 1213。记相同部分为s1,后续部分为s2,就变成比较s1s2s1和s1s1s2的大小关系,转化为比较s1与s2的大小关系。

由于12<13,因此12<1213,应为121312而不是121213,返回true。

排完序只要逆序加就可以了(大的在前面),注意全0时简写为一个0.

注意函数atoi的使用和c_str以及to_string的使用,这些都是我不太熟悉的地方。

class Solution {
public:
static bool compare(int a, int b)
{
string as = to_string(a);
string bs = to_string(b);
int sizea = as.size();
int sizeb = bs.size();
int i = ;
while(i<sizea && i<sizeb)
{
if(as[i] < bs[i])
return true;
else if(as[i] > bs[i])
return false;
i ++;
}
if(i==sizea && i==sizeb)
return false; //equal returns false
else if(i==sizea)
{//as finished
if(bs[i] == '')
return false;
else
return compare(atoi(as.c_str()), atoi(bs.substr(i).c_str()));
}
else
{//bs finished
if(as[i] == '')
return true;
else
return compare(atoi(as.substr(i).c_str()), atoi(bs.c_str()));
}
}
string largestNumber(vector<int> &num) {
sort(num.begin(), num.end(), compare);
int size = num.size();
string ret;
while(size--)
ret += to_string(num[size]);
if(ret[] != '')
return ret;
else
return "";
}
};

  

Largest Number——STL的深层理解的更多相关文章

  1. LeetCode-179. Largest Number

    179. Largest Number Given a list of non negative integers, arrange them such that they form the larg ...

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

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

  3. LeetCode——Largest Number

    Description: Given a list of non negative integers, arrange them such that they form the largest num ...

  4. HDOJ1016 Prime Ring Problem(DFS深层理解)

      Prime Ring Problem                                                                       时间限制: 200 ...

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

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

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

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

  7. 【leetcode】Largest Number

    题目简述: Given a list of non negative integers, arrange them such that they form the largest number. Fo ...

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

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

  9. 【leetcode】Largest Number ★

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

随机推荐

  1. [zhuan]tomcat环境配置

    http://jingyan.baidu.com/article/8065f87fcc0f182330249841.html 一.安装JDK和Tomcat 1,安装JDK:直接运行jdk-7-wind ...

  2. vector去除重复的元素

    vector<int> v; sort(v.begin(),v.end()); v.erase(unique(v.begin(), v.end()), v.end());

  3. 破解wingide编辑器

    先到官网下载最新版的wingide(我下载的是5.1.11-1),然后安装,打开,出现下面的界面时选第三个,然后输入“ENX27-HWM6G-XYVFA-165PG”,如下图所示: 接下来你软件会给你 ...

  4. 洛谷:P3809 【模板】后缀排序(后缀数组模板)

    P3809 [模板]后缀排序 题目链接:https://www.luogu.org/problemnew/show/P3809 题目背景 这是一道模板题. 题目描述 读入一个长度为 nn 的由大小写英 ...

  5. XOR and Favorite Number (莫对算法)

    E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  6. stout代码分析之四:Try类

    stout的在异常捕获上遵循于谷歌类似的原则,不适用try...catch...,而是从函数返回值判断异常.Try类正是实现了这样的一个功能. 同Option一样,Try是一个模板类,每个类对象都有两 ...

  7. 轻量级HTTP服务器Nginx(配置与调试Nginx)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://ixdba.blog.51cto.com/2895551/790611 Nginx ...

  8. sublime 常用插件 感觉比较全了 够用了

    插件介绍 Package Control 功能:安装包管理 简介:sublime插件控制台,提供添加.删除.禁用.查找插件等功能 使用:https://sublime.wbond.net/instal ...

  9. HDU 5876 Sparse Graph BFS+set删点

    Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices s ...

  10. 社会网络分析——Social Network Analysis

    什么是社会网络分析,英文social network analysis.现在这个分析越来越时髦,也越来越显现其在社会科学的研究价值.我在2000年的时候受祝建华老师的邀请到香港城市大学作研究,接触到 ...