题目链接

  题目要求:

  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. 物料REVISION控制

    --新增 INV_ITEM_REVISION_PUB.Create_Item_Revision ( p_api_version IN NUMBER , p_init_msg_list IN VARCH ...

  2. linux下的环境变量

    环境变量有时候要查找,但是经常忘记有哪些文件,现在做一个总结: /etc/profile                 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/e ...

  3. Dynamics CRM2016 New features in Microsoft Dynamics CRM Online 2015 Update 1 are now available

    很多人看过Dynamics CRM Online 2015 Update 1后,被它新的一个界面风格所吸引,还有它的很多新增功能,虽然官网放出了些补丁,但最重要的Server补丁一直没出,包括我在内很 ...

  4. 6、Android Content Provider测试

    如果你的应用中使用了Content Provider来与其他应用进行数据交互,你需要对Content Provider进行测试来确保正常工作. 创建Content Provider整合测试 在Andr ...

  5. 对LCS算法及其变种的初步研究

    LCS的全称为Longest Common Subsequence,用于查找两个字符串中的最大公共子序列,这里需要注意区分子序列与子串,所谓子序列,指的是从前到后,可以跳跃元素筛选,而字串则必须连续筛 ...

  6. cocos2dx 3.3 C++工程添加lua支持

    准备工作: 1. 拷贝cocos2d-x-3.3rc0\external\lua整个文件夹到项目中(如myProject\cocos2d\external\lua) 2. 拷贝cocos2d-x-3. ...

  7. iOS中 如何将自己的框架更新到cocopods上 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 为了更方便的集成第三方框架有了cocopods 的, 当我们有了相对比较好的框架的时候如何更新到cocopods ...

  8. Android进阶(十三)网络爬虫&json应用

    刚开始接触网络爬虫,怎一个"菜"字了得!经过几次的折磨,对其原理以及其中用到的json技术有了大致的了解,故作一总结,供有同样迷惑的朋友参考学习. 自己爬取的网站内容为12306的 ...

  9. CSS House

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. java中Error与Exception有什么区别

    Error类和Exception类都继承自Throwable类. Error的继承关系: java.lang.Object  java.lang.Throwable      java.lang.Er ...