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 on()绑定动态元素出现的问题小结
jQuery on()方法是官方推荐的绑定事件的一个方法.使用 on() 方法可以给将来动态创建的动态元素绑定指定的事件,通过本文给大家介绍jQuery on()绑定动态元素出现的问题小结,需要的朋友 ...
- struts2校验器规范错误解决
今天struts2的校验器的配置文件文件头出现了错误,配置如下: <!DOCTYPE validators PUBLIC "-//OpenSymphony Group// ...
- C primer plus 读书笔记第二章
这章的标题是C语言概述,内容大概是介绍一些简单的示例程序,来了解和熟悉C语言的一些基本特征. 这是书里的第一段代码,敲敲找找感觉.推荐在linux环境下写代码. PS:倒腾sublime text一下 ...
- 了解SVG
页的节点类型,我们将说明怎样通过Illustrator高速的把SVG文档加入到网页中.我们还会讲讲D3.js,一个强大的.SVG控制的JavaScript库. "SVG并不仅仅用于像素处理. ...
- Robotium API -- 判断测试结果的方法assert、is、search
下面的这些方法都主要用来判断测试结果是否与预期结果相符,一般把is和search方法放在assert里面判断.assert最常用的还是assertThat方法,是Junit的判断,这里就不多说了. 断 ...
- [转] gdb中忽略信号处理
信号(Signals) 信号是一种软中断,是一种处理异步事件的方法.一般来说,操作系统都支持许多信号.尤其是UNIX,比较重要应用程序一般都会处理信号.UNIX定义了许 多信号,比如SIGINT表示中 ...
- Virtualbox安装增强工具失败
在安装Virtualbox增强工具安装时出现unable to find the sources of your current Linux kernel,安装失败,导致主机与虚拟机之间不能共享文件夹 ...
- phpmyadmin登陆提示#2002 无法登录 MySQL 服务器和设置自增
看看mysql启动没有,结果是mysql服务没有启动,找了半天,是这个原因,那就右键计算机->管理->服务->启动mysql服务 设置自增:在显示出来的一行字段定义中把浏览器的滚动条 ...
- Struts2 模型驱动及页面回显
* 要从页面中获取表单元素的值,需要在动作类中声明与页面元素同名的属性.导致动作类中既有javabean又有业务方法. * 将javabean和业务方法进行分离: * 将重 ...
- java事件处理2
Document事件 这个事件有点特别,需要用getDocument()返回到自己所维护的文档,然后就可以添加监视器 (textArea1.getDocument).addDocumentListen ...