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码位点)来排,如果要根据大小排序,需要传入一个 ...
随机推荐
- Jenkins配置Java项目1(Java+Maven+Tomcat)
先收集几个网址,后续再自己动手过一遍 http://www.cnblogs.com/sunzhenchao/archive/2013/01/30/2883289.html https://my.osc ...
- 提交表单时的等待(loading)效果
$(document).ready(function () { $("body").prepend('<div id="overlay" class=&q ...
- 沧海一声笑,移动应用的CRASH原因我找到! --记最新款数字化測试“星云測试“的使用攻略
沧海一声笑,移动应用的CRASH原因我找到! --记最新款数字化測试"星云測试"的使用攻略 世界进步那么快,非常多新奇的点子层出不穷,于是我们创业.我们做最酷的手机应用,做最轰炸的 ...
- Android 布局 ViewGroup
布局 res/layout 命名规则(全部小写) activity_ fragment_ item_ 基础组件 com.android.widget包下 父类View view:屏幕上一块矩阵区域 能 ...
- C primer plus 读书笔记第九章
本章的标题是函数.C的设计原则是把函数作为程序的构成模块. 1.函数概述 函数的定义:函数是用于完成特定任务的程序代码的自包含单元. 使用函数的原因:1.函数的使用可以省去重复代码的编写.2.使得程序 ...
- [置顶] 我的Android进阶之旅------>介绍一款集录制与剪辑为一体的屏幕GIF 动画制作工具 GifCam
由于上一篇文章:我的Android进阶之旅------>Android之动画之Frame Animation实例 中展示的是Frame动画效果,但是之前我是将图片截取下来,不好说明确切的动画过程 ...
- Android集成Mina NIO Socket
Mina简介 Apache MINA(Multipurpose Infrastructure 多功能框架 for Network Applications) 是 Apache 组织一个较新的项目,它为 ...
- 编程小计——消除Graphics图像边缘颜色不纯(抗锯齿)
在很多时候,我们都要绘制纯色的图片,而用Graphics生成的往往是不纯的,尤其是绘制文字时.比如说绘制纯红色文字,往往R达不到255. C#中默认抗锯齿,给人看起来柔和:但是我们现实中往往用到锯齿. ...
- asp.net读取Access数据库。
注:数据库(表名 job id 工作id ,job工作字段) 数据库放在app_data文件中.名称为database.mdb 如果用codesmith生成,选择的数据库连接类型如下图: 项目结构图 ...
- WCF 接收我服务的 HTTP 响应时发生错误
错误内容: System.ServiceModel.CommunicationException: 接收对 https://xx.com/xx.svc的 HTTP 响应时发生错误.这可能是由于服务终结 ...