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的大小。

  1. class Solution {
  2. public:
  3. static bool cmp(const string &s1, const string &s2){ //注意static,const,引用的使用
  4. return (s1+s2) > (s2+s1); // >表示按降序排列
  5. }
  6. string largestNumber(vector<int>& nums) {
  7. int size = nums.size();
  8. stringstream ss;
  9. vector<string> s_nums(size);
  10. string ret = "";
  11.  
  12. for(int i = ; i < size; i++){
  13. ss << nums[i];
  14. ss >> s_nums[i];
  15. ss.clear();
  16. }
  17. sort(s_nums.begin(), s_nums.end(),cmp);
  18.  
  19. if(s_nums[]=="") return s_nums[];
  20. for(int i = ; i < size; i++){
  21. ret += s_nums[i];
  22. }
  23. return ret;
  24. }
  25. };

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. Word,excel开发指南

    New Document dfsdfds &dsfds &sdf; dsf dsf dsfds fsdfdsfdsf dsfs dsfds dsf dsfd sfds   sdf fd ...

  2. 讲讲亿级PV的负载均衡架构

    引言 本来没想写这个题材的,为了某某童鞋能够更好的茁壮成长,临时写一篇负载均衡的.负载均衡,大家可能听过什么3层负载均衡.4层负载均衡.7层负载均衡什么的?那这是怎么分的呢,ok,是根据osi七层网络 ...

  3. Ubuntu12.04 内核树建立

    先查看自己使用的内核版本 lin@lin-virtual-machine:~$ uname -r --generic 如果安装系统时,自动安装了源码.在 /usr/src 目录下有对应的使用的版本目录 ...

  4. linux 任务调度

    crontab 定时任务 -e  编辑 -l  查看 -r  删除 参数 * * * * * 分钟  小时  天数   月   星期几

  5. WPF 自定义鼠标光标

    在程序中使用自定义鼠标光标的三种方式: RadioButton senderButton = sender as RadioButton; 方式一:                       str ...

  6. 使用原生js实现前端分页功能

    背景: 从后台提取出来数据,在前端进行分页. 代码: user-manage.js window.onload = function(){ var result = { message : " ...

  7. 初识web.xml文件

    做了那么久的web项目都没有花心思了充分解下这个文件有什么用,看项目配制是否都差不多呢 ======================================================== ...

  8. linux查询硬件信息

    硬件信息查询 sudo dmidecode -t baseboard

  9. 建立一个php 基础类

    在些PHP文件的时候,一般首先都是要先写一下基础类: 主要包括以下几个方面: 1.服务器的链接:包括主机,用户名,密码 2.数据库的选择:要操作哪个数据库 3.字符集的设置:设置什么样的编码 4.查询 ...

  10. sqlalchemy学习-- 重要参数

    Base = declarative_base 基类: 1.存储表 2.建立class-Table 的映射关系 engine = create_engine('mysql://root:root@lo ...