Largest Number
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
小技巧为排序中的比较函数,另外记住不能有=号
class Solution {
public:
static bool compareFunc(string &s1, string& s2) {
return s1+s2 > s2+s1;
}
string largestNumber(vector<int>& nums) {
string res;
int n = nums.size();
vector<string> vecStr;
for (int i = ; i < n; i++) {
vecStr.push_back(to_string(nums[i]));
}
sort(vecStr.begin(), vecStr.end(), compareFunc);
if(vecStr[] == "") return "";
for (int i = ; i < n; i++) {
res += vecStr[i];
}
return res;
}
};
Largest Number的更多相关文章
- 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-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 ★
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 ...
随机推荐
- R之data.table -melt/dcast(数据合并和拆分)
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 30.0px "Helvetica Neue"; color: #323333 } p. ...
- SQL触发器实例
SQL触发器实例讲解(本文是来自百度文库) 备注:本人建了一个站特价汇,我想记录每个商品的点击量,然后按照点击量来牌名商品,想要提高效率,所以必须得用触发器,下面是本人在百度文库中的找到的学习资料,分 ...
- C#:类和结构的区别
第一.引用类型和值类型 类属于引用类型,而结构属于值类型. 结构在赋值时进行复制. 将结构赋值给新变量时,将复制所有数据,并且对新副本所做的任何修改不会更改原始副本的数据. 第二.继承性 类可以继承类 ...
- HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP
题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...
- js、jquery对于html内容的转义
-------2016-7-27 14:23:34-- source:[1]js转义html
- hdu3065病毒侵袭持续中
链接 上一篇的姊妹篇 没啥好说的 套模板 #include <iostream> #include<cstdio> #include<cstring> #inclu ...
- Android 之计算控件颜色透明度
Android 之计算控件颜色透明度 1.UI会给一个数值,例如:#EFE000,透明度30% 2.用255乘以30%等于76.5,然后四舍五入等于77 3.用计算器将十进制的77转成十六进制的数据为 ...
- NGUI UIToggle
NGUI UIToggle 1.add a UI Toggle(Script) and UI Toggle Objects(Script) to a Tab Button(Which has a UI ...
- Java第一个程序
public class HelloWorld{ public static void main(String[] args){ System.out.println("七期项目二部&quo ...
- 常用js,css文件统一加载方法,并在加载之后调用回调函数
原创内容,转载请注明出处! 为了方便资源管理和提升工作效率,常用的js和css文件的加载应该放在一个统一文件里面完成,也方便后续的资源维护.所以我用js写了以下方法,存放在“sourceControl ...