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 ...
随机推荐
- Activity生命周期(二)
------siwuxie95 在项目 ActivityLifeCircle 的 MainActivity.java 中添加方法: onCreate() onStart() onResume() ...
- Python全栈--7.1--字符串的格式化
Python字符串格式化:(百分号/format) 1.百分号的方式: %[(name)][flags][width].[precision]typecode (name) 可选,用于选择指 ...
- 附10 kibana创建新的index patterns
elk整体架构图: 一.logstash indexer 配置文件: input { stdin{} } filter { } output { elasticsearch { hosts => ...
- URLEncoder编码
客户端在进行网页请求的时候,网址中可能会包含非ASCII码形式的内容,比如中文. 而直接把中文放到网址中请求是不允许的,所以需要用URLEncoder编码地址, 将网址中的非ASCII码内容转换成可以 ...
- Winform中DockPanel(引用WeifenLuo.WinFormsUI.Docking.dll组件)的使用
WeiFenLuo.WinFormsUI.Docking.dll是开源项目DockPanel Suite的一个类库,可实现像Visual Studio的窗口停靠.拖拽等功能.WeifenLuo.Win ...
- POJ 3686 The Windy's 最小费用最大流
每个工厂拆成N个工厂,费用分别为1~N倍原费用. //#pragma comment(linker, "/STACK:1024000000,1024000000") #includ ...
- AT91-PWM应用
步骤1: make menuconfig配置内核, 开启PWM输出功能. Device Drivers ---> Misc devices ---> <*>Atmel AT3 ...
- hp小机定位网卡位置
rad已经被olrad取代 HPUX下定位网卡位置 一台HP小型机,可能配了多块网卡,在系统中以la ...
- Ext开场布局设计Viewport
//加载dwr dwr.engine.setAsync(false); //***************************************框架定义部分***************** ...
- Apache配置默认首页面
conf -> httpd.conf下设置成 <IfModule dir_module> DirectoryIndex index.php index.html index.htm ...