题目

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详细题解的更多相关文章

  1. [LeetCode] Largest Number 最大组合数

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  2. Leetcode Largest Number c++ solution

    Total Accepted: 16020 Total Submissions: 103330     Given a list of non negative integers, arrange t ...

  3. LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用

    Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...

  4. LeetCode——Largest Number

    Description: Given a list of non negative integers, arrange them such that they form the largest num ...

  5. [LeetCode] Largest Number 排序

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  6. [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 ...

  7. LeetCode() Largest Number

    全排列,超时,知道超时,只是想验证一下. class Solution { public: string largestNumber(vector<int>& nums) { so ...

  8. LeetCode之“排序”:Largest Number

    题目链接 题目要求: Given a list of non negative integers, arrange them such that they form the largest numbe ...

  9. JavaScript中sort方法的一个坑(leetcode 179. Largest Number)

    在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...

随机推荐

  1. 完整版的OpenLDAP搭建全过程

    总结:          先写总结,再写正文,嘿嘿嘿.这还是第一次认真的写个文档,写个总结,哈哈.大概在一个月前,第一次听说这个东西,完全没有概念,刚开始的时候看理论的知识,看了几次之后就没看了,看不 ...

  2. python爬虫__第一个爬虫程序

    前言 机缘巧合,最近在学习机器学习实战, 本来要用python来做实验和开发环境 得到一个需求,要爬取大众点评中的一些商户信息, 于是开启了我的第一个爬虫的编写,里面有好多心酸,主要是第一次. 我的文 ...

  3. javascript 判断是否是数组

    function isArray(object){ return object && typeof object==='object' && typeof object ...

  4. JVM性能调优监控工具

    命令:jps.jstat.jmap.jhat.jstack 简介:(1) jmap -dump:format=b,file=eclipse.bin 10481  生成堆转储快照eclipse.bin ...

  5. Robotium--takeScreenshot(截图)

    在Robotium中,截图的方法时调用takeScreenshot(). 但有使用你会发现明明代码里调用了solo.takeScreenshot(),但却没有截图成功,那是因为被测试的应用没有SD卡的 ...

  6. 使用nice命令调整进程优先级

    Adjusting Process Priority with nice   When Linux processes are started, they are started with a spe ...

  7. eclipse中svn版本不兼容问题

    eclipse中导入本地svn管理的Android项目 使用svn时弹出以下提示: org.apache.subversion.javahl.ClientException: Unsupported ...

  8. Code First研究学习1_Reverse Enginner Code First

    最近因为公司需要,自己开始研究Code First,之前还是听说过这个,也知道是代码优先的意思!至于具体怎么的代码优先,我的理解如下! 在听说code  first的时候,心里也就觉得怪了,是怎么将M ...

  9. 武汉科技大学ACM:1010: 零起点学算法27——判断是否直角三角形

    Problem Description 输入三个整数,分别代表三角形的三条边长度,判断能否构成直角三角形 Input 输入3个整数a,b,c(多组数据,-5000000<a,b,c<500 ...

  10. Helloworld和程序员人生

    转:Helloworld和程序员人生 高中时期 10 PRINT "HELLO WORLD" 20 END 大学新生 program Hello(input, output) be ...