Leetcode 179 Largest Number 贪心
此题主要是讲给你一组数,如何将数连在一起能得到最大值(最小值反之),注意局部最优,就是说如果 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 贪心的更多相关文章
- 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 ...
随机推荐
- Selenium2+python自动化22-发送各种类型附件邮件
前言 最近一些小伙伴,在搞邮箱的事情,小编于是去折腾了一下!总结了一些干货,与大家分享一下!速来,抱大腿,我要开车了! 基本思路就是,使用MIMEMultipart来标示这个邮件是多个部分组成的,然后 ...
- 在windows环境下基于sublime text3的node.js开发环境搭建
首先安装sublime text3,百度一堆,自己找吧.理论上sublime text2应该也可以.我只能说一句:这个软件实在是太强悍了. 跨平台,丰富的插件体系,加上插件基本上就是一个强悍的ide了 ...
- SQL保留关键字不能用作表名
com.microsoft.sqlserver.jdbc.SQLServerException: 关键字 'User' 附近有语法错误. 一看就是SQL语句错误,发现控制台console上打印出来的S ...
- 問題排查:F5啟動偵錯後所提示的錯誤 (2)
原始專案版本:Visual Studio 2005 開發環境:Visual Studio 2013 偵錯運行環境:IIS Express 啟動偵錯後,錯誤提示內容如下: HTTP 错误 403.14 ...
- option3
option = { tooltip : { trigger: 'item', formatter: "{a} <br/>{b} : {c} ({d}%)" }, le ...
- jsp-javabean练习1
package javaBean; public class JavaB2 { private String name="lhy"; private int xuehao=123; ...
- android text
"@you bang--- go on -------" 需要做分享内容,前面有段格式固定写死,同时颜色为灰色:后面的内容可以编辑,颜色为黑色,同时支持多行 有人用textview ...
- lab 7 函数超级多的类
#include<iostream>#include<string>#include<cmath>using namespace std; class Ration ...
- 文件与base64二进制转换
/// <summary> /// 文件转换为Base64二进制流 /// </summary> /// <param name="FilePath" ...
- View的事件处理流程
一直对view的事件处理流程迷迷糊糊,今天花了点时间写了个栗子把它弄明白了. 1.view的常用的事件分为:单击事件(onClick).长按事件(onLongClick).触摸事件(onTouch), ...