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.
原题链接: https://oj.leetcode.com/problems/largest-number/
算法分析
case1(一般情况):
[3, 30, 34, 5, 9] -> (9 -> 5 -> 34 -> 3 -> 30) -> 9534330
直观想法按位从高到底排序
可以很容易得到9->5的顺序,然而接下来问题来了,位相等的情况怎么办?
考虑3,30,34(数字组1)
简单考虑[3, 30],显然3->30要比30->3的值更大,即3>30的个位0;
再考虑[3, 34],(34->3) > (3->34),即34的个位4>3;
最后[30, 34],34 > 30;
所以数字组1的排序为34->3->30;
最终结果为9->5->34->3->30
case2(不止一位相等,多位高位相等的情况):
[824, 8247] -> (824 -> 8247) -> 8248247
逐一从高位到低位比较,那么第二个数字的最低位7应该与第一个数字的哪位比较呢?决定这两数顺序的不外乎,824->8247,8247->824这两种情况,直观上7应与第一个数字的第一位8比较,由于7<8,所以824->8247
case3 (不止一位相等,多位高位相等的情况):
[824, 82483] -> (82483 -> 824) -> 82483824
case4(重复数字):
[33, 333] -> 33333
一般考虑假设待比较的数字为a1a2, b1b2b3,a1b1…均为位;在重复数字的情况下
如
a1 a2
|| ||
b1 b2 b3
且b3 == a1,b1 == a2,此时可以得到b1 == a1 == a2 == b2 == b3,即全等,因此最大的比较次数为数字1的位数加数字2的位数 - 1次,该例子的情况为4次。
题目陷阱
case1(有数字为0):
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
case2(数字均为0):
[0, 0]
算法设计
Integer类,将int按位存储,next取出下一位方法;
class Integer {
public:
Integer(int i); int getCount() { return count; } int next() {
if (!tmp_count) {
tmp_count = count;
}
return digits[--tmp_count];
} private:
int _i;
int count;
int tmp_count;
int digits[];
}; Integer::Integer(int i):count(),tmp_count() {
// there has a great trap when i == 0
if (i) {
while (i) {
digits[count++] = i % ;
i /= ;
}
} else {
++count;
digits[] = ;
}
tmp_count = count;
}
比较函数cmp,按位从高到低循环比较,等于最大比较次数后退出;
bool cmp(const int& a, const int& b) {
Integer ia(a);
Integer ib(b); int maxCmpCount = ia.getCount() + ib.getCount() - ;
int curCmpCount = ; while (curCmpCount < maxCmpCount) {
int bita = ia.next();
int bitb = ib.next(); if (bita > bitb) {
return true;
} if (bita < bitb) {
return false;
} ++curCmpCount;
} return false;
}
完整代码(Runtime:9ms)
#include <string>
#include <vector>
#include <cstdio> class Integer {
public:
Integer(int i); int getCount() { return count; } int next() {
if (!tmp_count) {
tmp_count = count;
}
return digits[--tmp_count];
} private:
int _i;
int count;
int tmp_count;
int digits[];
}; Integer::Integer(int i):count(),tmp_count() { // there has a great trap when i == 0
if (i) {
while (i) {
digits[count++] = i % ;
i /= ;
}
} else {
++count;
digits[] = ;
}
tmp_count = count;
} bool cmp(const int& a, const int& b) {
Integer ia(a);
Integer ib(b); int maxCmpCount = ia.getCount() + ib.getCount() - ;
int curCmpCount = ; while (curCmpCount < maxCmpCount) {
int bita = ia.next();
int bitb = ib.next(); if (bita > bitb) {
return true;
} if (bita < bitb) {
return false;
} ++curCmpCount;
} return false;
} class Solution {
public:
std::string largestNumber(std::vector<int> &num) {
// there is a trap when nums is all zero
bool allZero = true;
for (auto itr = num.begin(); allZero && itr != num.end(); ++itr) {
if (*itr != ) {
allZero = false;
}
} if (allZero) {
return std::string("");
} std::sort(num.begin(), num.end(), cmp);
std::string rel;
char tmp[];
for (auto itr = num.begin(); itr != num.end(); ++itr) {
sprintf(tmp, "%d", *itr);
rel += tmp;
}
return rel;
}
};
Leetcode:Largest Number详细题解的更多相关文章
- [LeetCode] Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Leetcode Largest Number c++ solution
Total Accepted: 16020 Total Submissions: 103330 Given a list of non negative integers, arrange t ...
- LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用
Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...
- LeetCode——Largest Number
Description: Given a list of non negative integers, arrange them such that they form the largest num ...
- [LeetCode] Largest Number 排序
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [LeetCode] Largest Number At Least Twice of Others 至少是其他数字两倍的最大数
In a given integer array nums, there is always exactly one largest element. Find whether the largest ...
- LeetCode() Largest Number
全排列,超时,知道超时,只是想验证一下. class Solution { public: string largestNumber(vector<int>& nums) { so ...
- LeetCode之“排序”:Largest Number
题目链接 题目要求: Given a list of non negative integers, arrange them such that they form the largest numbe ...
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...
随机推荐
- 使用jquery生成二维码
二维码已经渗透到生活中的方方面面,不管到哪,我们都可以用扫一扫解决大多数问题.二狗为了准备应对以后项目中会出现的二维码任务,就上网了解了如何使用jquery.qrcode生成二维码.方法简单粗暴,[] ...
- div+css 圆角加阴影
.test{ display: inline-block; padding: 5px 10px 6px; text-decoration: none; border-radius: 5px; -moz ...
- QT5: QApplication, no such file or directory
In QT5, when I use this: #include<QApplication>, QT tells :no such file or directory, even tho ...
- JBoss 系列九十六:JBoss MSC - 简介及一个简单演示样例
什么是 JBoss MSC JBoss MSC 即 JBoss Modular Service Container,是第三代 JBoss 产品 JBoss 7和WildFfly的内核,JBoss MS ...
- qDebug 学习小结
在qtcentre中看到有网友问这样一个问题: Why this doesn't work? qDebug() << "Test" << std::endl ...
- ActiveX控件打包成Cab置于网页中自动下载安装(转载)
原文出自http://www.iteye.com/topic/110834 [背景] 做过ActiveX控件的朋友都知道,要想把自己做的ActiveX控件功能放在自己的网页上使用,那么用户在客户端就必 ...
- mysql 变量is null 和 not exists区别
问题: 使用游标遍历时,发现使用 select var into tmp where var=? 然后判断if tmp is null时,不能走完所有的遍历.经debug发现, 当var为空时,则跳出 ...
- Android TagFlowLayout完全解析 一款针对Tag的布局(转)
一.概述 本文之前,先提一下关于上篇博文的100多万访问量请无视,博文被刷,我也很郁闷,本来想把那个文章放到草稿箱,结果放不进去,还把日期弄更新了,实属无奈. ok,开始今天的博文,今天要说的是Tag ...
- 在MacOs上配置Hadoop和Spark环境
在MacOs上配置hadoop和spark环境 Setting up Hadoop with Spark on MacOs Instructions 准备环境 如果没有brew,先google怎样安装 ...
- linux的常用命令及常用快捷键
常用指令 ls 显示文件或目录 -l 列出文件详细信息l(list) -a 列出当前目录下所有文件及目录,包括隐藏的a(all) mkdir ...