此题主要是讲给你一组数,如何将数连在一起能得到最大值(最小值反之),注意局部最优,就是说如果 123 234两个连在一起怎么样最大,显然是234123比123234大,对于3个数我们可以找到类似的性质,4个数一样。。。因此我们得到这个局部最优的排序是全局最优的。因此这个实现最核心的代码就是函数cmp,其他的就是要注意全是0的情况和前导0的情况,这题就轻松解决。

 inline bool cmp(const std::string &a, const std::string &b)
{
return a + b > b + a;
}
class Solution
{
public:
Solution() = default;
~Solution() = default;
inline std::string inttostr_(int num)
{
char s[] = { };
sprintf_s(s,"%d",num);
return std::string(s);
} std::string largestNumber(std::vector<int>& nums)
{
std::vector<std::string> vstr;
for (std::vector<int>::size_type i = ; i < nums.size(); ++i){
vstr.push_back(inttostr_(nums[i]));
}
std::sort(vstr.begin(),vstr.end(),cmp);
std::vector<std::string>::size_type i = ;
for (; i < nums.size(); ++i){ //删除前导0
if (vstr[i] != "") break;
} if (i == nums.size()) return "";//全是0
else{ //前导0和其他情况
std::string ans("");
for (; i<nums.size(); ++i){
ans += vstr[i];
}
return ans;
}
}
};

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 ...

随机推荐

  1. C#版SQLHelper.cs类

    using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...

  2. java利用jxl操作Excel

    /** * 把从数据库查询到的数据,写入电子表格 * * @throws Exception */ public void createXls() throws Exception { Dao dao ...

  3. ORA-01439: column to be modified must be empty to change datatype

    修改数据库字段类型,但是由于数据表已经存在数据,无法修改: 显示错误:  写道 ORA-01439: column to be modified must be empty to change dat ...

  4. LVS三种工作方式八种算法

    一.集群简介 什么是集群 计算机集群简称集群是一种计算机系统,它通过一组松散集成的计算机软件和/或硬件连接起来高度紧密地协作完成计算工作.在某种意义上,他们可以被看作是一台计算机.集群系统中的单个计算 ...

  5. Unity(二)生命周期LifetimeManager

    描述:Unity的生命周期是注册的类型对象的生命周期,而Unity默认情况下会自动帮我们维护好这些对象的生命周期,我们也可以显示配置对象的生命周期,Unity将按照配置自动管理. //创建一个Unit ...

  6. 转 SVN 在vs中的使用

    给大家介绍一些SVN的入门知识!希望对大家的学习起到作用!      关于SVN与CVS的相关知识,大家可以自己去google一下.      一.准备         SVN是一个开源的版本控制系统 ...

  7. DNS主从TSIG加密传输

    BIND服务程序为了能够安全的提供解析服务而支持了TSIG加密机制,TSIG主要是利用密码编码方式保护区域信息的传送(Zone Transfer),也就是说保证了DNS服务器之间传送区域信息的安全. ...

  8. [Chapter 3 Process]Practice 3.3 Discuss three major complications that concurrent processing adds to an operating system.

    3.3  Original version of Apple's mobile iOS operating system provied no means of concurrent processi ...

  9. Qt 5.7 版本+2013VS环境配置

    原本是使用MSVC5.5.1的版本,碍于习惯,之前一直使用creator,后面才转向VS,因为它的调试实在是太棒了,小程序还是creator调试(比如抽出大程序中的小模块之类的) 不知道是版本问题还是 ...

  10. python基础语法(4)

    十.Python标准库 Python标准库是随Pthon附带安装的,包含了大量极其有用的模块. 1. sys模块 sys模块包含系统对应的功能 sys.argv ---包含命令行参数,第一个参数是py ...