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 ...
随机推荐
- ExtJs在页面上window再调用Window的事件处理
今天在开发Ext的过程中遇到了一个恶心的问题,就是在ext.window页面,点击再次弹出window时,gridpanel中的store数据加载异常,不能正常被加载,会出现缓存,出现该问题,是因为w ...
- jsp弹出新窗口代码
1.最基本的弹出窗口代码其实代码非常简单: <SCRIPT LANGUAGE="javascript"> <!-- window.open (page.html) ...
- ACE反应器(Reactor)模式(3)
转载于:http://www.cnblogs.com/TianFang/archive/2006/12/18/595938.html 在服务器端使用Reactor框架 使用Reactor框架的服务器端 ...
- Cows POJ - 2481 树状数组
Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can ...
- Qt中内存泄露和退出崩溃的问题 delete
Qt中帮程序员做了一些内存回收的事情,但正因为这些反而让对此不熟悉的人会屡屡犯错. 收录一篇不错的文章: 在C++中学习过程中,我们都知道: delete 和 new 必须 配对使用(一 一对应):d ...
- Selenium判断获取的元素是否可见(display:none)
在爬虫中需要自动登陆并判断是否登陆成功,如果登陆错误的话还需要知道错误提示信息,此时需要判断提示信息是否可见 if self.element_exist_xpath('//*[@id="bu ...
- dbms_output.put与put_line
BEGIN DBMS_OUTPUT.ENABLE (buffer_size => NULL);--with no limit on the output. dbms_output.put('a' ...
- SpringMVC+MyBatis开发中指定callSettersOnNulls,可解决返回字段不全的问题
Spring+MyBatis开发过程中,在xxMapper.xml配置文件进行select查询时resultType="map",如果要查询的字段是空值,在返回的map中会出现找不 ...
- UVA 1262 Password
https://vjudge.net/problem/UVA-1262 字典序第k小 注意两点: 1. k-- 2.去重 #include<cstring> #include<cst ...
- [Luogu 2073] 送花
很容易想到的平衡树,加个维护区间和. 只需要插入和删除操作即可. kth其实都不用的,最小和最大可以从根节点log n一直向左/一直向右跑到叶子节点而求得. 记得每插入完一个点一定要更新区间和!!更新 ...