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. Ret2Libc 练习(2) -- VirtualProtect

    这几天做了NSCTF和GCTF,耽误了几天,今天继续. 这次绕过DEP的方法是利用VirtualProtect函数将shellcode所在的内存属性改成可执行状态就可以绕过DEP了. 首先看一下Vir ...

  2. OpenGL角轴

    概述 轴旋转 角轴 概述 OpenGL旋转矩阵 旋转角度直接影响OpenGL GL_MODELVIEW矩阵的前三列,准确地说是向左.向上与向前三轴元素.例如,如果一沿X轴的单位向量(1,0,0)与任一 ...

  3. 嵌入式系统添加无线wifi模块

    开发环境:fl2440开发板,linux3.0内核,交叉编译器路径/opt/buildroot-2011.11/arm920t/usr/bin/arm-linux-,无线网卡RT3070 平时开发板联 ...

  4. bash shell,调用ffmpeg定期截图

    #!/bin/bash #获取当前目录中所有m3u8文件,并 var=$(ls |grep '.m3u8'|cut -d '.' -f1) #死循环 = ] do #循环每个文件 for stream ...

  5. MyBatis学习(三)、动态SQL语句

    三.动态SQL语句 有些时候,sql语句where条件中,需要一些安全判断,例如按某一条件查询时如果传入的参数是空,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息.使用Orac ...

  6. java获取图片原始尺寸

    java获取图片原始尺寸 URL url = null; InputStream is = null; BufferedImage img = null; try { url = new URL(pi ...

  7. sysbench 安装遇到的问题

    sysbench 作为性能测试工具,提供了很多有用的参数,使用方法网络上一抓一把,这里记录下安装过程中遇到的问题已经解决办法 .tar.gz cd sysbench- ./autogen.sh ./c ...

  8. PICT安装与使用

    一.PICT简介 PICT工具是在微软公司推出的一款成对组合的命令行生成工具,下载地址http://download.microsoft.com/download/f/5/5/f55484df-849 ...

  9. 读写hdfs文件(工作笔记)

    import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.Map; ...

  10. html中空格转义字符

    记录一下,空格的转义字符分为如下几种: 平时一般用的是  1.  &160#;不断行的空白(1个字符宽度) 2.  &8194#;半个空白(1个字符宽度) 3.  &8195# ...