【leetcode】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.
思路:
忧伤的发现,人的思路真的是很容易定型。这道题我做过的,但是过了这么久,我还是只记得我自己的老思路。
直接上正确答案, 关键是排序时比较的策略, 用s1+s2 和 s2+s1 比较即可,就是把两种方式排列的情况都试一下,看哪个大!!
class Compare{
public:
bool operator()(string s1, string s2)
{
return s1 + s2 < s2 + s1;
}
};
class Solution {
public:
string largestNumber(vector<int> &num) {
int i;
for (i = ; i < num.size(); i++)
if (num[i] != )
break;
if (i == num.size()) // All numbers are 0
return "";
vector<string> v;
for (i = ; i < num.size(); i++)
v.push_back(to_string(num[i]));
sort(v.begin(), v.end(), Compare());
string result;
for (i = v.size() - ; i >= ; i--)
result += v[i];
return result;
}
};
我自己的思路:
两个数比较 如 724 和7247 那么比较 724|724 与 7247|7247 即相同时就不断复制自己,直到遇到不同的数字得到大小。 如果两个数字同时达到末尾,但是数字一直相同,则这两个数字相同。
代码很长,但是也AC了,慢一些。主要是二分归并排序是我自己写的。
class Solution {
public:
string largestNumber(vector<int> &num) {
MergeSort(num, , num.size() - );
string ans;
for(int i = ; i < num.size(); i++)
{
stringstream ss;
ss << num[i];
string s = ss.str();
ans.append(s);
}
if(!ans.empty() && ans[] == '')
{
ans = "";
}
return ans;
}
bool islarge(int aa, int bb)
{
stringstream ss;
ss << aa;
string sa = ss.str();
ss << bb;
string sb = ss.str();
int i = , j = ;
while(sa[i % sa.length()] == sb[j % sb.length()]
&& !((i + ) % sa.length() == && (j + ) % sb.length() == )) //两边同时结束时跳出,防止相同数字无限循环
{
i++; j++;
}
return sa[i % sa.length()] > sb[j % sb.length()];
}
void MergeSort(vector<int> &in, int l, int r)
{
if(l < r)
{
int mid = (l + r) / ;
MergeSort(in, l, mid);
MergeSort(in, mid + , r);
Merge(in, l, mid, r);
}
}
void Merge(vector<int> &in, int l, int mid, int r)
{
vector<int> left(in.begin() + l, in.begin() + mid + ), right(in.begin() + mid + , in.begin() + r + );
int i = , j = , k = ;
while(i < left.size() && j < right.size())
{
if(islarge(left[i], right[j]))
{
in[l + k] = left[i++];
}
else
{
in[l + k] = right[j++];
}
k++;
}
while(i < left.size())
{
in[l + k] = left[i++];
k++;
}
while(j < right.size())
{
in[l + k] = right[j++];
k++;
}
}
};
【leetcode】Largest Number ★的更多相关文章
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【leetcode】Largest Number
题目简述: Given a list of non negative integers, arrange them such that they form the largest number. Fo ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
- 【leetcode】Largest Plus Sign
题目如下: In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the giv ...
- 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...
- 【LeetCode】1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 代码 复杂度分析 日期 题目地址:http ...
随机推荐
- [设计模式] Javascript 之 观察者模式
观察者模式:定议 定义对象间的一种一对多的关系,当一个对象状态改变时 (一般称为被观察者),依赖于该对象的对象被通知,并更新; 观察者模式:说明 1. 观察者模式是行为模式,也被称为:发布-订阅模式. ...
- java框架
Dash Reports 1.0发布 Java报表解决方案 http://developer.51cto.com/art/201205/337189.htm http://www.oschina.ne ...
- 用CSS画个三角形
<!DOCTYPE html> <html> <head> <style type="text/css"> #trangle { d ...
- nginx反向代理、动静分离
环境:根据http://www.cnblogs.com/zzzhfo/p/6032095.html配置 方法一:根据目录实现动静分离 在web01创建image并上传一张图片作为静态页面 [root@ ...
- 部署Apache网站访问统计-AWStats分析系统
环境根据:http://www.cnblogs.com/zzzhfo/p/5925786.html 1.安装AWStats软件包 将软件包解压到httpd服务器中的/usr/lcoal/目录下 [ro ...
- QQ空间个人中心的广告
http://qzonestyle.gtimg.cn/qzone/space_item/boss_pic/*.jpghttp://img*.paipaiimg.com/*.jpghttp://cn.q ...
- Codeforces 271 Div 2 C. Captain Marmot
题目链接:http://codeforces.com/contest/474/problem/C 解题报告:给一个n,然后输入4*n个平面坐标系上的点,每四个点是一组,每个点有一个中心,这四个点可以分 ...
- iOS开发——UI进阶篇(八)pickerView简单使用,通过storyboard加载控制器,注册界面,通过xib创建控制器,控制器的view创建,导航控制器的基本使用
一.pickerView简单使用 1.UIPickerViewDataSource 这两个方法必须实现 // 返回有多少列 - (NSInteger)numberOfComponentsInPicke ...
- BZOJ1146——[CTSC2008]网络管理Network
1.题目大意:就是在动态的树上路径权值第k大. 2.分析:这个就是树链剖分+树套树 #include <cstdio> #include <cstdlib> #include ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...