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 ...
随机推荐
- bzoj3694: 最短路(树链剖分/并查集)
bzoj1576的帮我们跑好最短路版本23333(双倍经验!嘿嘿嘿 这题可以用树链剖分或并查集写.树链剖分非常显然,并查集的写法比较妙,涨了个姿势,原来并查集的路径压缩还能这么用... 首先对于不在最 ...
- 20181022 考试记录&高级数据结构
题目 W神爷的题解 高级数据结构 T1: 其实是一道easy题,$O(n^3log n)$ 也是能卡过去的,本着要的70分的心态,最后尽然A了. 如果是正解则是$O(n^3)$,当确定你要选择的列时, ...
- PHP汉字转拼音
<?php/** *+------------------------------------------------------ * PHP 汉字转拼音 *+----------------- ...
- [存一下]iptables模块
介绍地址: http://www.tummy.com/blogs/2005/07/17/some-iptables-modules-you-probably-dont-know-about/ [1] ...
- 爬虫服务集群处理nginx返回504
最近在对爬虫服务做分布式服务的时候总是遇到服务器返回504,搞了两天才发现原来是nginx中有对超时的设置参数,自己都是用默认的,然而客户端的等待时间超过了nginx默认的超时设置 修改 keepal ...
- 9.python爬虫--pyspider
pyspider简介 PySpider:一个国人编写的强大的网络爬虫系统并带有强大的WebUI.采用Python语言编写,分布式架构,支持多种数据库后端,强大的WebUI支持脚本编辑器,任务监视器,项 ...
- 选择排序Selection sort
顾名思意,就是直接从待排序数组里选择一个最小(或最大)的数字,每次都拿一个最小数字出来, 顺序放入新数组,直到全部拿完 再简单点,对着一群数组说,你们谁最小出列,站到最后边 然后继续对剩余的无序数组说 ...
- Python代码解决RenderView窗口not found问题
源 起 Error:setParent: Object 'renderView' not found 这是一个在工作中很常见的问题,以前做特效的时候有10%的概率会碰到,多发生在打开其他组交接来的Ma ...
- java修饰符——transient
一.背景 上星期去CRM上开发一个功能,该系统里面有自动分页,需要在实体类里加入一个分页变量 // 分页 private PageInfo pageInfo = new PageInfo(); 这个本 ...
- 移动端页面使用rem布局
阿里团队的高清布局方案代码 所谓高清方案就是根据设备屏幕的DPR(设备像素比,又称DPPX,比如dpr=2时,表示1个CSS像素由4个物理像素点组成) 动态设置 html 的font-size, 同时 ...