leetcode_num179_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
.
两两比較 能够利用sort函数来排序,自己定义compare函数。即比較规则
可用vector来取代数组。easy定位
bool compare(int a,int b){
string t1=to_string(a)+to_string(b);
string t2=to_string(b)+to_string(a);
return t1>t2;
}
//return to_string(a)+to_string(b)>to_string(b)+to_string(a); class Solution {
public:
string largestNumber(vector<int> &num) {//use vector to present array
if(num.size()<=0)
return "";
sort(num.begin(),num.end(),compare);
string res;
for(int i=0;i<num.size();i++)
res+=to_string(num[i]);
return res[0]=='0'?"0":res;//only elements valued 0
}
};
leetcode_num179_Largest Number的更多相关文章
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Eclipse "Unable to install breakpoint due to missing line number attributes..."
Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...
- 移除HTML5 input在type="number"时的上下小箭头
/*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...
- iOS---The maximum number of apps for free development profiles has been reached.
真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...
- 有理数的稠密性(The rational points are dense on the number axis.)
每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] Number of Boomerangs 回旋镖的数量
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
随机推荐
- thinkphp方便分页的page方法
page方法也是模型的连贯操作方法之一,是完全为分页查询而诞生的一个人性化操作方法. 用法 我们在前面已经了解了关于limit方法用于分页查询的情况,而page方法则是更人性化的进行分页查询的方法,例 ...
- golang 字符串替换截取
package main import "fmt" func main() { str := "XBodyContentX" content := str[1 ...
- 网络流模板(模板题:POJ1273)
模板题:POJ1273 EK: #include <queue> #include <cstdio> #include <cstring> #include < ...
- POJ 1523 Tarjan求割点
SPF Description Consider the two networks shown below. Assuming that data moves around these network ...
- B - String Task
Problem description Petya started to attend programming lessons. On the first lesson his task was to ...
- 关于EasyUI datagrid editor combogrid搜索框的实现
首先需要datagrid editor对combogrid的扩展,这个是别人实现的: $.extend($.fn.datagrid.defaults.editors, { combogrid: { i ...
- SEO之如何做301转向
1.如果网站使用的是(Linux+Apache+MySQL+PHP)主机,可以使用.htaccess文件做301转向 比如把/index.html 301转向到http://www.xinlvtian ...
- Microsoft SQL Server数据库学习(一)
数据库的分类: 1.关系型数据库: 数据库名称 类型 公司 平台 Access 小型数据库 微软 Windows Mysql 小型数据库 AB--sun--甲骨文 Windows/linux/mac ...
- End to End Sequence Labeling via Bi-directional LSTM CNNs CRF
来看看今日头条首席科学家的论文: End-to-end Sequence Labeling via Bi-directional LSTM-CNNs-CRF 使用LSTM方法进行序列标注,完成大规模标 ...
- (转)Redis研究(一)—简介
http://blog.csdn.net/wtyvhreal/article/details/41855327 Redis是一个开源的高性能键值对数据库.它通过提供多种键值数据类型来适应不同场景下的存 ...