题目链接

  题目要求:

  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.

  Credits:
  Special thanks to @ts for adding this problem and creating all test cases.

  在博文Leetcode:Largest Number详细题解有相对详细的分析,但本文没有参考其做法,而是参考了LeetCode论坛上的一个解决方法。具体程序如下:

 class Solution {
public:
static bool com(const int & m, const int & n)
{
return to_string(m) + to_string(n) > to_string(n) + to_string(m);
} string largestNumber(vector<int>& nums) {
string ret;
sort(nums.begin(), nums.end(), com); int sz = nums.size();
if(sz != && nums[] == )
return ""; for(int i = ; i < sz; i++)
ret += to_string(nums[i]); return ret;
}
};

  这里边值得一提的就是字符串大小比较的原理

  字符串的大小比较是逐个相应字符进行比较(比较他们的ASCII码),直到有两个字符不相等为止,ASCII码大的字母所在字符串就大,与字符串长度无关。对两个相等长度的字符串,若每个字符都比较完毕后仍相等,则两字符串相等;对不等长的字符串,若当短的字符串比较完毕时所有字符仍相等,则长度较长的字符串大!

LeetCode之“排序”:Largest Number的更多相关文章

  1. 【Leetcode】179. Largest Number

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

  2. [leetcode sort]179. Largest Number

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

  3. 【LeetCode #179】Largest Number 解题报告

    原题链接:Largest Number 题目描述: Given a list of non negative integers, arrange them such that they form th ...

  4. LeetCode 179. 最大数(Largest Number) 21

    179. 最大数 179. Largest Number 题目描述 给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 每日一算法2019/5/24Day 21LeetCode179. La ...

  5. 【刷题-LeetCode】179 Largest Number

    Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...

  6. LeetCode OJ:Largest Number(最大数字)

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

  7. 【LeetCode】747. Largest Number At Least Twice of Others 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 寻找两次最大值 排序 大顶堆 日期 题目地址:htt ...

  8. LeetCode算法题-Largest Number At Least Twice of Others(Java实现)

    这是悦乐书的第308次更新,第328篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第177题(顺位题号是747).在给定的整数数组中,总有一个最大的元素.查找数组中的最大 ...

  9. LeetCode题解之Largest Number

    1.题目描述 2. 将整数值转换为string  ,然后排序. 3.代码 string largestNumber(vector<int>& nums) { vector<s ...

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

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

随机推荐

  1. SpriteKit给游戏弹跳角色添加一个高度标示器

    这是一个类似于跳跃涂鸦的小游戏,主角不断吃能量球得到跳跃能量向更高的地方跳跃,如果图中碰到黑洞就挂了- 在游戏调试过程中如果能实时知道主角的高度就好了,这将有助于程序猿动态的判断游戏胜败逻辑. 你可以 ...

  2. 下载Android源代码编译错误总结

    错误1: prebuilts/sdk/api/18.txt:22055: error 9: Removed public method android.telephony.gsm.SmsMessage ...

  3. javascript命名规则

    javascript对大小写敏感(关键字.函数名.变量名等),标识符的首字符必须是字母.下划线或者$符,其后的字符可以含数字 如果之声明了变量,并未对其赋值,默认为undefined javascri ...

  4. shell脚本实现冒泡排序

    手动输入一行字符串,并对其排序. 脚本如下: #!/bin/bash #a test about sort echo "please input a number list" re ...

  5. DB2数据库代码页和实例代码页的区别(解决DB2乱码问题)

    DB2CODEPAGE:     DB2 实例级别的代码页设置,它会影响DB2相关应用程序对代码页转换时做出代码页判定.     可以通过 db2set DB2CODEPAEG= 命令将 DB2COD ...

  6. Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(三)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 下面看一下CatSprite中最复杂的moveToward方法, ...

  7. APACHE,NGINX 详细分析

    Apache是目前最流行的Web应用服务器,占据了互联网应用服务器70%以上的份额.Apache能取得如此成功并不足为奇:它免费.稳定且性能卓越:但Apache能取得如此佳绩的另一个原因是,当时互联网 ...

  8. Chapter 2 User Authentication, Authorization, and Security(3):保护服务器避免暴力攻击

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/38756693,专题目录:http://blog.csdn.net/dba_huangzj ...

  9. 利用编辑距离(Edit Distance)计算两个字符串的相似度

    利用编辑距离(Edit Distance)计算两个字符串的相似度 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可 ...

  10. Windows下配置nginx+FastCgi + Spawn-fcgi

    前提: 下载nginx, FastCgi, Spawn-fcgi Spawn-fcgi有个Windows的版本,但不能在VS中编译,这里有一个编译好的版本:http://download.csdn.n ...