【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 ...
随机推荐
- iSCSI配置流程
Windows群集两个节点:分别在SQL01和SQL02设置连接共享磁盘: 此前已经在存储服务器通过StarWind创建了三个虚拟磁盘:Quemon+data+backup:starwind安装请参考 ...
- Google Protocol Buffer 简单介绍
以下内容主要整理自官方文档. 为什么使用 Protocol Buffers .proto文件 Protocol Buffers 语法 编译.proto文件 Protocol Buffers API 枚 ...
- Linux命令行修改IP、网关、DNS、主机名 的方法
修改主机名:[改里面的 HOSTNAME 即可] vim /etc/sysconfig/network 网卡eth0 IP修改为 102.168.0.1 ifconfig eth0 102.16 ...
- mysql的隐式转化
MySQL隐式转化整理 前几天在微博上看到一篇文章:价值百万的 MySQL 的隐式类型转换感觉写的很不错,再加上自己之前也对MySQL的隐式转化这边并不是很清楚,所以就顺势整理了一下.希望对大家有所帮 ...
- ServiceManager: Permmission failure: android.permission.RECORD_AUDIO
今天在Android6.0系统的手机上测试一款APP,出现如题错误: ServiceManager: Permmission failure: android.permission.RECORD_AU ...
- CSS3必须要知道的10个顶级命令
1.边框圆角(Border Radiuas) 这个是我们在平常很常用的吧,以前我在用div圆角的时候,特别特别的痛苦,不管是用CSS来画圆角,还是用图片来画圆角都不那么容易,但是现在好了,在CSS3中 ...
- webpack 教程 那些事儿04-webpack项目实战分析
这节主要讲解真正项目用用到的 webpack配置问题,项目实战篇 就像我们不会完全做一个项目,不用别人的轮子一样.这个配置我们借用 vue-cli 搭建的配置来研究,因为它已经足够优秀. 有了前面的基 ...
- iOS开发——多线程篇——GCD
一.基本概念 1.简介什么是GCD全称是Grand Central Dispatch,可译为“牛逼的中枢调度器”纯C语言,提供了非常多强大的函数 GCD的优势GCD是苹果公司为多核的并行运算提出的解决 ...
- CSS技巧-rgba函数的妙用
先简单介绍一下: rgba()函数是平时开发中经常遇到的,这篇文章也做了一个比较详细的解读以及一系列的应用. 对它的工作原理做一番分析:就是具有一定透明度的盒子: 还比较了rgba()函数和不透明度属 ...
- word20161130
1. even 偶数 [ǒu shù][词典] even; [数] even number;[例句]在本例中,slot A中有奇数个阵列,slot B中有偶数个阵列.In this example, ...