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. 如何将自己的windows设置为mysql服务器

    1.安装mysql 服务器 2.创建超级用户,即 用户管理 mysql>use mysql; 查看 mysql> select host,user,password from user ; ...

  2. 10天学会phpWeChat——第七天:创建一个自适应PC网站+H5移动端的模块

    本教程基于phpWeChat核心框架1.1.0+版本.下载地址:http://s.phpwechat.com/app_38026ed22fc1a91d92b5d2ef93540f20 通过前面六讲的系 ...

  3. SSL安全证书-概念解析

    一.关于证书 数字证书是一种认证机制.简单点说,它代表了一种由权威机构颁发授权的安全标志. 由来 在以前,传统网站采用HTTP协议进行数据传输,所有的数据几乎都用的明文,很容易发生隐私泄露.为了解决安 ...

  4. [转]Windows 下的进程间通讯及数据共享

    http://blog.codingnow.com/2005/10/interprocess_communications.html Windows 下有很多方法实现进程间通讯,比如用 socket, ...

  5. [系统开发] 一个基于Django和PureCSS的内容管理系统

    这是我刚开发的一套基于Django和PureCSS的内容管理系统,目标是优雅.简洁.实用,目前功能还在完善中. 系统参考了网上的教程,除了文章管理.搜索.RSS,还增加了类别管理.用户管理,以及评论管 ...

  6. PHP空数组转化为json对象的问题

    例子: $a = []; echo json_encode($a); echo json_encode($a, JSON_FORCE_OBJECT); 输出结果: [] {}

  7. mysql_fetch_row,mysql_fetch_array,mysql_fetch_object,mysql_fetch_assoc的区别!

    php从mysql中访问数据库并取得数据,取得结果的过程中用到好几个类似的方法,区别及用法值得区分一下,看下面的代码   代码如下: <?php $link=mysql_connect('loc ...

  8. 读取 java.nio.ByteBuffer 中的字符串(String) 写入方式flash.utils.ByteArray.writeUTF

    通过研究ByteArray的写入格式以及方法说明,可以发现writeUTF是先使用2位写入字符串的长度,然后在其后写入字符串编码. flash.utils.ByteArray.writeUTF(val ...

  9. 解决HttpWebRequest首次连接特别慢的问题

    针对这个问题,网上各种搜,然后看到的解决方案大致相同,改web.config,问题来了,按网上说的,没感觉快了多少 <?xml version="1.0"?> < ...

  10. 轨迹记录App是怎样对定位轨迹进行过滤、优化和平滑处理的

    https://www.zhihu.com/question/39983016 卡尔曼滤波原理 卡尔曼滤波学习笔记 卡尔曼滤波的原理说明 http://www.cs.unc.edu/~welch/ka ...