179. Largest Number(INT, String)
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.
思路:
要对数字进行排列,首位大的在前。
比较每个数字的首位,这在不知道位数的情况下,很难实现,所以将INT转换成string再比较。
由于string的连接操作非常方便,所以比较首位的大小,可以转化成比较连接后string的大小。
class Solution {
public:
static bool cmp(const string &s1, const string &s2){ //注意static,const,引用的使用
return (s1+s2) > (s2+s1); // >表示按降序排列
}
string largestNumber(vector<int>& nums) {
int size = nums.size();
stringstream ss;
vector<string> s_nums(size);
string ret = "";
for(int i = ; i < size; i++){
ss << nums[i];
ss >> s_nums[i];
ss.clear();
}
sort(s_nums.begin(), s_nums.end(),cmp);
if(s_nums[]=="") return s_nums[];
for(int i = ; i < size; i++){
ret += s_nums[i];
}
return ret;
}
};
179. Largest Number(INT, String)的更多相关文章
- leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数
这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...
- [LeetCode] 179. Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. Example ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- leetcode 179. Largest Number 求最大组合数 ---------- java
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Java for LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 179. Largest Number -- 数字字符串比较大小
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 179. Largest Number
题目: Given a list of non negative integers, arrange them such that they form the largest number. For ...
- [LeetCode] 179. Largest Number 解题思路
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [leed code 179] Largest Number
1 题目 Given a list of non negative integers, arrange them such that they form the largest number. For ...
随机推荐
- leetcode540
这道题目的要求,Note: Your solution should run in O(log n) time and O(1) space. 因此应该用二分查找的方式,代码如下: class Sol ...
- day14-函数
1.定义函数 一个函数就是封闭一个功能def 函数名(): 函数代码注意:函数名不要用默认的关键字.否则会将默认关键字函数覆盖掉. 命名规则与变量相同,使用字母.数字.下划线组成,不能以数字开关 2. ...
- UnicodeDecodeError: 'ascii' codec can't decode byte 0x9c in position 1: ordinal not in range(128)
待研究: compressed_data = zlib.compress(json.dumps(data), 9) file_data = MySQLdb.escape_string(compress ...
- 设计模式入门——Head First
设计模式是被前人发现.经过总结形成了一套某一类问题的一般性解决方案.使用模式最好的方式是:把模式装进脑子,然后在设计和已有的应用中,寻找何处可以使用它们.以往是代码复用,现在是经验复用. 从模拟鸭子游 ...
- Kotlin语言编程技巧集
空语句 Kotlin 语言中的空语句有 {} Unit when (x) { 1 -> ... 2 -> ... else -> {} // else -> Unit } Wh ...
- JDBC使用步骤分哪几步?
(1) 加载JDBC驱动程序: Cllass.forName(" 驱动程序" ); //你要连接的数据库对象 (2) 建立连接 Connection conn=DriverMa ...
- [jQ]使用jQuery将多条数据插入模态框的方法
---------------------------------------------------------------------------------------------------- ...
- [PHP]快速实现:将二维数组转为一维数组
如何将下面的二维数组转为一维数组. $msg = array( array( 'id'=>'45', 'name'=>'jack' ), array( 'id'=>'34', 'na ...
- Microsoft® SQL Server® 2012 功能包
Microsoft® SQL Server® 2012 功能包 http://www.microsoft.com/zh-cn/download/details.aspx?id=29065 Micros ...
- ESET官方下载地址
官方远程下载 ESET NOD32 Antivirus(32位) ESET NOD32 Antivirus(64位) ESET Smart Security(32位) ESET Smart Secur ...