Largest Number——STL的深层理解
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的深层理解的更多相关文章
- LeetCode-179. Largest Number
		179. Largest Number Given a list of non negative integers, arrange them such that they form the larg ... 
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
		在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ... 
- LeetCode——Largest Number
		Description: Given a list of non negative integers, arrange them such that they form the largest num ... 
- HDOJ1016 Prime Ring Problem(DFS深层理解)
		Prime Ring Problem 时间限制: 200 ... 
- Java 特定规则排序-LeetCode 179 Largest Number
		Given a list of non negative integers, arrange them such that they form the largest number. For exam ... 
- [LeetCode] Largest Number 最大组合数
		Given a list of non negative integers, arrange them such that they form the largest number. For exam ... 
- 【leetcode】Largest Number
		题目简述: Given a list of non negative integers, arrange them such that they form the largest number. Fo ... 
- leetcode   179. Largest Number     求最大组合数    ----------  java
		Given a list of non negative integers, arrange them such that they form the largest number. For exam ... 
- 【leetcode】Largest Number ★
		Given a list of non negative integers, arrange them such that they form the largest number. For exam ... 
随机推荐
- ContestHunter暑假欢乐赛 SRM 09(TJM大傻逼选手再创佳绩)
			T1 f[i]为前i页最少被撕几页,用二分转移就行了,答案为ans=min(f[i]+(n-i)); 不知道为什么写挂了嗯 二分的l初始应该是0 T2 数位DP f[i][1/0][1/0][1/0] ... 
- phalcon安装
			参考网站:https://docs.phalconphp.com/zh/latest/reference/tools.html (中文版)cento6.5环境安装:cd ~mkdir phalconc ... 
- acm1217教训
			能用容器去做的用容器做,尽量少用数组,即使自己明确其数量的上届: #include<iostream> #include<cstring> #include<map> ... 
- Leetcode 144.二叉树的前序遍历
			1.题目描述 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 2.解法 ... 
- JAVA、android中常用的一些jar包的作用
			正文: 这里主要介绍的是hibernate使用到的.jar Hibernate一共包括了23个jar包,令人眼花缭乱.本文将详细讲解Hibernate每个jar包的作用,便于你在应用中根据自己的需要进 ... 
- Stick footers布局总结
			一.Sticky footers解释 在网页设计中,Sticky footers设计是最古老和最常见的效果之一,大多数人都曾经经历过.它可以概括如下:如果页面内容不够长的时候,页脚块粘贴在视窗底部:如 ... 
- 图论&数学:矩阵树定理
			运用矩阵树定理进行生成树计数 给定一个n个点m条边的无向图,问生成树有多少种可能 直接套用矩阵树定理计算即可 矩阵树定理的描述如下: 首先读入无向图的邻接矩阵,u-v G[u][v]++ G[v][u ... 
- Achain 钱包部署
			官网 GIT: [ Achain_linux ] 基础环境 OS: CentOS, Ubuntu Achain: 官网 [ release 最新版本 ] 安装 Achain 钱包 下载 CentOS ... 
- CSS animation怎么使用?(山东数漫江湖)
			animation可以为很多CSS属性添加动画,比如: color, background-color, height和width.animation的动画需要使用@keyframes来定义,随后被a ... 
- Zen Cart、Joy-Cart、Magento、ShopEX、ECshop电子商务系统比较
			1.Zen Cart 优点:历史较久,系统经过长时间充分的测试,比较成熟:免费开源便于功能二次开发:基础功能强大:安装插件简单,修改文件很少,甚至不用修改文件:应用非常广泛,插件.模块更新快,其中多为 ... 
