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 ...
随机推荐
- css命名冲突解决方法
css的命名冲突目前有几种解决方法: 1.命名约定 人为的制定一下命名规则以避免冲突,例如前缀,嵌套等 2.CSS in JS 在JavaScript中写CSS,使用工具编译为css,最常见的是sty ...
- 手动实现一个简易版SpringMvc
版权声明:本篇博客大部分代码引用于公众号:java团长,我只是在作者基础上稍微修改一些内容,内容仅供学习与参考 前言:目前mvc框架经过大浪淘沙,由最初的struts1到struts2,到目前的主流框 ...
- layui中对表格操作按钮集的判断
可用如下语法: {{# if(d.IsAudit==false){ }} <a class='layui-btn layui-btn-xs layui-btn-normal' lay-event ...
- Mybatis批量更新详解
转:http://www.cnblogs.com/winkey4986/p/3915151.html Mybatis批量更新 批量操作就不进行赘述了.减少服务器与数据库之间的交互.网上有很多关于批量插 ...
- notify()与notifyAll()
notify() :随机唤醒一个线程. notifyAll():唤醒等待某个锁的所有任务. 在技术上,可能会有多个任务在所创建的任务上处于wait()状态,调用notifyAll()比只调用notif ...
- [实战篇入门]01-POI读Excel
这一章的内容就是告诉各位同学如何入门POI的简单使用,再之后我们还会学习如何封装模版,由于个人时间问题,不定期更新!如果有需要,请再QQ中联系我,好了,开始工作! 新建一个Java项目,首先需要一些列 ...
- Java并发多线程 - 并发工具类JUC
安全共享对象策略 1.线程限制 : 一个被线程限制的对象,由线程独占,并且只能被占有它的线程修改 2.共享只读 : 一个共享只读的对象,在没有额外同步的情况下,可以被多个线程并发访问, 但是任何线程都 ...
- 【转】 GRASP(通用职责分配软件模式)模式
转自:http://www.cnblogs.com/sevenyuan/archive/2010/03/05/1678730.html 及:http://blog.csdn.net/lovelion ...
- Lua中调用C++方法
目前项目,使用了Lua脚本,至于使用Lua的好处不再赘述了.于是对Tolua做了一些小小的学习,总结一下吧. 主要说一下如何在Lua中调用C++方法. Lua调用C++的桥梁,是tolua.tolua ...
- Codeforces Round #483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!]
题目链接:http://codeforces.com/contest/984 A. Game time limit per test:2 seconds memory limit per test:5 ...