LeetCode-179. Largest Number
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的更多相关文章
- leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数
这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...
- [LeetCode] 179. Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. Example ...
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- leetcode 179. Largest Number 求最大组合数 ---------- java
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Java for LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [LeetCode] 179. Largest Number 解题思路
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [leetcode]179. Largest Number最大数
Given a list of non negative integers, arrange them such that they form the largest number. Input: [ ...
- LeetCode 179 Largest Number 把数组排成最大的数
Given a list of non negative integers, arrange them such that they form the largest number.For examp ...
- Leetcode 179 Largest Number 贪心
此题主要是讲给你一组数,如何将数连在一起能得到最大值(最小值反之),注意局部最优,就是说如果 123 234两个连在一起怎么样最大,显然是234123比123234大,对于3个数我们可以找到类似的性质 ...
随机推荐
- EF6 连接Oracle 迁移数据错误解决方法
环境:vs2015 + EF6 +ODP 数据库Oracle 11G add-migratioin 正常,但在update-database 时报如下错误: System.Runtime.Serial ...
- dvwa第一次接触
DVWA (Damn Vulnerable Web Application)DVWA是用PHP+Mysql编写的一套用于常规WEB漏洞教学和检测的WEB脆弱性测试程序.包含了SQL注入.XSS.盲注等 ...
- web三种跨域请求数据方法
以下测试代码使用php,浏览器测试使用IE9,chrome,firefox,safari <!DOCTYPE HTML> <html> <head> < ...
- [综] Sparse Representation 稀疏表示 压缩感知
稀疏表示 分为 2个过程:1. 获得字典(训练优化字典:直接给出字典),其中字典学习又分为2个步骤:Sparse Coding和Dictionary Update:2. 用得到超完备字典后,对测试数据 ...
- Best Practices for Performance_1、2 memory、Tips 性能和小的优化点、 onTrimMemory
http://developer.android.com/training/articles/memory.htmlhttp://developer.android.com/tools/debuggi ...
- springmvc--json--返回json的日期格式问题
(一)输出json数据 springmvc中使用jackson-mapper-asl即可进行json输出,在配置上有几点: 1.使用mvc:annotation-driven 2.在依赖管理中添加ja ...
- Netty的TCP粘包/拆包(源码二)
假设客户端分别发送了两个数据包D1和D2给服务器,由于服务器端一次读取到的字节数是不确定的,所以可能发生四种情况: 1.服务端分两次读取到了两个独立的数据包,分别是D1和D2,没有粘包和拆包. 2.服 ...
- Jackson的使用
Jackson框架是基于Java平台的一套数据处理工具,被称为"最好的JavaJson解析器". Jackson框架包含了3个核心库:streaming,databind,anno ...
- AMap编辑折线、多边形、圆
<!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...
- Python创建cvs文件,包含标签和图片数据
在深度学习或者机器学习的时候,常常需要对数据进行整理和分类,最常见的是通过对数据路径和标签写入 到一个整合的txt或者csv文件中,训练进行读取. #coding=utf-8 #!/usr/bin/ ...