179. Largest Number

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.

由题意可以知道,结果最大数字符串是由这些数字转成字符串后按其降序排列而来。理解这一点这道题就简单了。

另外需要考虑一些特殊情况:如全是0的情况、为空的情况等等。

class Solution {
public:
string largestNumber(vector<int>& nums) {
string result;
vector<string> arr;
for (const auto n:nums)
{
arr.push_back(to_string(n));
} sort(arr.begin(), arr.end(), [](const string& s1, const string& s2){return s1 + s2 > s2 + s1; });
for (const auto& e:arr)
{
result += e;
} if (result.at(0) == '0')
{
return "0";
} return result;
}
};

上面代码是先将int窗口转成相应string的容器,然后对string容器中string进行降序排序,最后排序后的string拼接成一个字符串,再排除特殊情况即可。

下面给出的方法从代码角度来看更简洁,但性能方面会差一点,因在排序过程,每两个int转成字符串比较过程中,每次to_string都会构造一个string, 然后两个string再进行+运算符, 最后两个string再进行>运算符,额外开销有点大。

class Solution
{
public:
string largestNumber(vector<int>& nums)
{
string result; if (nums.empty())
{
return "0";
} sort(nums.begin(), nums.end(),
[](const int n1, const int n2){return to_string(n1) + to_string(n2) > to_string(n2) + to_string(n1); }); for (const auto e:nums)
{
result += to_string(e);
} if (result.at(0) == '0')
{
return "0";
} return result;
}
};

上面两种方法在leetcode上模拟运行,方法1运行大概8ms, 方法2大概28ms, 主要性能消耗还是在排序过程中对int数据按字符串比较排序,不断地string隐式构造及string运算符+及>的重载。其实这里我们可以将及单独实现,优化掉这块的消耗。

class Solution
{
static bool intAsstrCompare(int a, int b)
{
char aBuf[64];
char bBuf[64]; memset(aBuf, 0, sizeof(aBuf));
memset(bBuf, 0, sizeof(bBuf)); sprintf(aBuf, "%d%d", a, b);
sprintf(bBuf, "%d%d", b, a); return strcmp(aBuf, bBuf) > 0;
} public:
string largestNumber(vector<int>& nums)
{
string result; if (nums.empty())
{
return "0";
} sort(nums.begin(), nums.end(), intAsstrCompare); for (const auto e:nums)
{
result += to_string(e);
} if (result.at(0) == '0')
{
return "0";
} return result;
}
};

上面代码运行后为12ms, 竟然还没有方法1快!!!有兴趣的可以接着去研究一下。

LeetCode-179. Largest Number的更多相关文章

  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. JavaScript中sort方法的一个坑(leetcode 179. Largest Number)

    在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...

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

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

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

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

  6. Java for LeetCode 179 Largest Number

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

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

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

  8. [leetcode]179. Largest Number最大数

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

  9. LeetCode 179 Largest Number 把数组排成最大的数

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

  10. Leetcode 179 Largest Number 贪心

    此题主要是讲给你一组数,如何将数连在一起能得到最大值(最小值反之),注意局部最优,就是说如果 123 234两个连在一起怎么样最大,显然是234123比123234大,对于3个数我们可以找到类似的性质 ...

随机推荐

  1. input中加入搜索图标

    刚吃了一份宫保鸡丁刀削面,幸福感满满,写篇博客消耗一下热量. 今天工作遇到的一个问题是在input输入框中加入图标,当输入内容后,图标和提示语一起隐藏,类似于placeholder的功能.如淘宝页面, ...

  2. java JSONObject/JSONArray详解

    应用架包:json-lib-2.4-jdk15.jar.及相关依赖架包. 一.JSONObject和JSONArray对象 -------------------------------------- ...

  3. SQL 语言 - 数据库系统原理

    SQL 发展历程 从 1970 年美国 IBM 研究中心的 E.F.Codd 发表论文到 1974 年 Boyce 和 Chamberlin 把 SQUARE 语言改为 SEQUEL 语言,到现在的 ...

  4. 基于.NET平台常用的框架整理【转】

    转:http://www.cnblogs.com/hgmyz/p/5313983.html 自从学习.NET以来,优雅的编程风格,极度简单的可扩展性,足够强大开发工具,极小的学习曲线,让我对这个平台产 ...

  5. js获取ModelAndView值的问题

    Springmvc中使用ModelAndView传值 return new ModelAndView(url).addObject(CommonConstant.PAGE_KEY, page) .ad ...

  6. 安装zabbix,make的时候报错

    CDPATH= && /bin/bash /install/Mesa-/bin/missing aclocal-1.14 -I m4 /install/Mesa-/bin/missin ...

  7. Squid代理服务器

    缓存代理概述:做为应用层的代理服务软件,squid主要提供缓存加速,应用层过滤控制的功能. 1.代理的工作机制 当客户机通过代理来请求web页面时,指定的代理服务器会先检查自己的缓存,如果缓存中已经有 ...

  8. Ubuntu 14.04 (Trusty Tahr) LTS发布,附下载地址,各种镜像【bubuko.com】

    Ubuntu 14.04 有很多的改进和新功能: 同时还发布几个不同版本:Ubuntu GNOME.Kubuntu.Xubuntu.Lubuntu.Edubuntu.Ubuntu Kylin.Ubun ...

  9. jq插件的传值

    因插件方式写的少,先慢慢记录. 默认的参数值 jQuery.fn.shadow =function(options){ var defaults = { slices : 5, opacity : 0 ...

  10. IIS7.5 伪静态 脚本映射 配置方法

    首先,是IIS7.0的配置,由于Windows Server 2008操作系统默认的IIS版本为7.0,我们知道,IIS7.0与IIS6.0 核心注意的地方:先要将应用池设置为集成模式,修改OK后,再 ...