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 ...
随机推荐
- windows phone控件
常用控件: 包括: Button控件.CheckBox控件.HyperlinkButton控件.Iamege控件.ListBox控件.PasswordBox控件.ProgressBar控件.Radio ...
- Android开发圆形ImageView实现
效果图如下 1.自定义属性,在value文件夹下新建attrs文件,声明如下属性 <declare-styleable name="CircleImageView"> ...
- JDBC的详细使用
1.首先说一下需要用到的工具: ①我这里用的数据库是MySql5.6 ,MySql6.0开始被Oracle收购需要付费了,6.0以下版本免费. ②去Maven仓库下载JDBC的jar包 Maven仓库 ...
- 时序分析:串匹配—Brute-Force算法
在使用KMP算法之前,使用了BF算法用于串匹配:原文链接已无法查找..... 设有主串s和子串t,子串t的定位就是要在主串s中找到一个与子串t相等的子串.通常把主串s称为目标串,把子串t ...
- [Intermediate Algorithm] - Finders Keepers
题目 写一个 function,它浏览数组(第一个参数)并返回数组中第一个通过某种方法(第二个参数)验证的元素. 提示 Array.filter() 测试用例 find([1, 3, 5, 8, 9, ...
- 偏函数应用(Partial Application)和函数柯里化(Currying)
偏函数应用指的是固化函数的一个或一些参数,从而产生一个新的函数.比如我们有一个记录日志的函数: 1: def log(level, message): 2: print level + ": ...
- jq遍历table 下的 td 添加类
<script> $('#btntb').click(function () { $('#tab tr').each(function (i) { // 遍历 tr $(this).chi ...
- Spark中Task,Partition,RDD、节点数、Executor数、core数目的关系和Application,Driver,Job,Task,Stage理解
梳理一下Spark中关于并发度涉及的几个概念File,Block,Split,Task,Partition,RDD以及节点数.Executor数.core数目的关系. 输入可能以多个文件的形式存储在H ...
- Python Shell 中敲击方向键显示「^[[C^[[D」,原因是什么?如何修复?
[root@hk45-node02-47 ahao.mah]# yum -y install readline-devel
- C#第八节课
for的穷举迭代.while.do while using System;using System.Collections.Generic;using System.Linq;using Syste ...