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. leetcode1017

    这道题目很巧妙,似乎是有些过于巧妙了,属于我未知领域的类型,把原作者的解决方案放这里.(看大家都在给差评,我也顺手给个差评吧--) 参考:https://leetcode.com/problems/c ...

  2. Mysql 预查询处理 事务机制

    预处理 PDO支持sql预处理功能,可以有效的防止sql注入的问题 例如: 以下操作会导致数据表中所有数据删除 $host = 'localhost'; $port = 3306; $dbname = ...

  3. django 无法生成表

    1.删除该APP下migration下的文件,只留init文件即可 2.删除表django_migration的关于该app的所有记录 3.makemigrations,migrate

  4. leetcode题解 candy

    要求的条件是: 1.每个人最少一个糖果. 2.相邻的小朋友,要保证,评分高的比评分低的糖果多. 如果从一侧扫描的话,容易确定的就是递增序列,只要上升1个就够了. 容易出现问题的就是:遇到下降期,或者相 ...

  5. js 提示框的实现---组件开发之(二)

    接着第上一个,在js文件里再增加一个 popModal 模块,实现弹框效果 css 代码: .alert { padding: 15px; margin-bottom: 20px; border: 1 ...

  6. javascript:图片转base64

    第一种: <!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta ...

  7. while 循环居然可以用else

    while 循环居然可以用else python 3 while a<50: print a a=a+1 else: print"over."

  8. Haskell语言开发工具

    Stack How to Script with Stack Originate Guides - Haskell Tool Stack 配置 Intellij Idea IntelliJ plugi ...

  9. 漫谈C指针:参数传递的三道题目

    漫谈C指针:参数传递的三道题目 2009-07-02 开讲之前,我先请你做三道题目.(嘿嘿,得先把你的头脑搞昏才行……唉呀,谁扔我鸡蛋?) 考题一,程序代码如下: [c] view plaincopy ...

  10. 'Could not find first log file name in binary log index file'的解决办法

    数据库主从出错: Slave_IO_Running: No 一方面原因是因为网络通信的问题也有可能是日志读取错误的问题.以下是日志出错问题的解决方案: Last_IO_Error: Got fatal ...