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)的更多相关文章

  1. leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数

    这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...

  2. [LeetCode] 179. Largest Number 最大组合数

    Given a list of non negative integers, arrange them such that they form the largest number. Example ...

  3. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  4. leetcode 179. Largest Number 求最大组合数 ---------- java

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  5. Java for LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  6. 179. Largest Number -- 数字字符串比较大小

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  7. 179. Largest Number

    题目: Given a list of non negative integers, arrange them such that they form the largest number. For ...

  8. [LeetCode] 179. Largest Number 解题思路

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  9. [leed code 179] Largest Number

    1 题目 Given a list of non negative integers, arrange them such that they form the largest number. For ...

随机推荐

  1. Java IO流学习总结五:转换流-InputStreamReader、OutputStreamWriter

    类的继承关系 Reader |__ BufferedReader.StringReader.InputStreamReader |__ FileReader Writer |__ BufferedWr ...

  2. a stop job is running for Security Auditing Services

    内核是3.10.0-514.el7,启动时有如下报错: a stop job is running for Security Auditing Services(56s / 1min 30s) 系统启 ...

  3. ReactiveX 学习笔记(2)创建数据流

    操作符(Operators) Rx 的操作符能够操作(创建/转换/组合) Observable. Creating Observables 本文主题为创建/生成 Observable 的操作符. 这里 ...

  4. Python调用外部系统命令

    利用Python调用外部系统命令的方法可以提高编码效率.调用外部系统命令完成后可以通过获取命令执行返回结果码.执行的输出结果进行进一步的处理.本文主要描述Python常见的调用外部系统命令的方法,包括 ...

  5. 将IP地址字符串转为32位二进制

    def str2bin(s): temp = s.split('.') result = '' for i in range(len(temp)): temp[i] = str(bin(int(tem ...

  6. python 基础回顾 一

    Python 基础回顾 可变类型:list ,dict 不可变类型:string,tuple,numbers tuple是不可变的,但是它包含的list dict是可变的. set 集合内部是唯一的 ...

  7. ArcGIS案例学习笔记-批处理擦除挖空挖除相减

    ArcGIS案例学习笔记-批处理擦除挖空挖除相减 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:批处理擦除.挖空.挖除.相减 数据源:chp13/ex5/pa ...

  8. 本地Navicat连不上Linux虚拟机MySQL数据库问题

    LinuxAndMySQL 版权声明:本文为博主原创文章,未经博主允许不得转载. 在Linux登录到MySQL数据:mysql -uroot -p 输入密码 切换到mysql数据库 mysql> ...

  9. 使用yii\filters下的比如\PageCache需要在web.php里面的组件上配置'cache' => [ 'class' => 'yii\caching\FileCache', ],

    public function behaviors(){ /*需要在config文件下的web.php里面加上 'cache' => [ 'class' => 'yii\caching\F ...

  10. js正则表达式子校验

    //正则表达式校验new RegExp(/^[1-9]\d{4,8}$/,"g").test(1234);//执行一个字符串所表达的方法 eval(this['字符串']) 正则表 ...