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 ...
随机推荐
- js中json字符串与对象的转换及是否为空
1.json对象(数组)转字符串 var b=[ { "CategoryName" : "Beverages", "ProductName" ...
- Image Base64 Datasnap Image delphi与c#互相兼容识别
delphi用,不能与java.c#互相识别. procedure TServerMethods.UpdateDoc(ItemID : integer; doc : TStream); delphi用 ...
- JS中,如何判断一个被转换的数是否是NaN
var x="abc"; //isNaN()函数判断是否是NaN if (isNaN(parseInt(x))) { alert("非数字"); } else{ ...
- es6 初级之let
1.在es6 中,定义变量使用 let. 1.1 var定义变量: <!DOCTYPE html> <html lang="en"> <head> ...
- Java IO流学习总结六:ByteArrayInputStream、ByteArrayOutputStream
类的继承关系 InputStream |__ ByteArrayInputStream OutputStream |__ ByteArrayOutputStream ByteArrayInputStr ...
- Visual Studio for Mac 使用笔记
无法打开控制台运行程序 Project -> Project Options -> Run -> Configurations -> Default 勾选Run on exte ...
- 强制停止ORACLE数据库
操作环境 SuSE+Oracle11gR2 适用场景 shutdown immediate停止数据库失败 操作命令 1.kill掉oracle实例相关进程 2.清除oracle占用的共享内存段 ipc ...
- 两台计算机有相同的IP地址会发生什么情况?两台计算机有相同的MAC地址会发生什么情况?
1 相同IP a) 同一网段内 会发生IP地址冲突.两台主机在特定情况下是可以同时使用同一个IP地址的.但是如果这两台主机在同一个网络内,大多数情况下,二者或者其中之一的连通性将会被破坏.比方 ...
- 16.Set、List、Queue集合;Map.md
目录 1.Set 1.2HashSet TreeSet 2.List 2.1ArrayList 2.1.1ArrayList和Vector的区别 2.2LinkedList 3.Queue 4.各种线 ...
- HTML框架、列表、表格
本章内容一.列表1.有序列表ol <ol> <li></li> </ol>type的值有3个 默认为1(阿拉伯数字), 还有A/a(大小写字母),I/i ...