184-最大数

给出一组非负整数,重新排列他们的顺序把他们组成一个最大的整数。

注意事项

最后的结果可能很大,所以我们返回一个字符串来代替这个整数。

样例

给出 [1, 20, 23, 4, 8],返回组合最大的整数应为8423201。

挑战

在 O(nlogn) 的时间复杂度内完成。

标签

排序

思路

主体操作就是对 num 排序,要求在 O(nlogn) 的时间复杂度内完成,所以使用快排,即 sort() 函数,这里需要自定义比较函数 cmp

在比较 2 个 int 型数据时,将其转换成 string 更加方便,比较思路为:

  • 若 a 与 b 中,a[i] > b[i],则 a > b (如 2345 < 245)

  • 若 a 与 b 中,a.size() < b.size() 且 a[i] = b[i],则只需比较 b[a.size()] 与 b[0]

    - 若 b[a.size()] > b[0],则 a < b (如 2221 < 222)
    - 否则,则 a > b (如 2224 > 222)

code

class Solution {
public:
/**
*@param num: A list of non negative integers
*@return: A string
*/ string largestNumber(vector<int> &num) {
// write your code here
int size = num.size();
if (size <= 0) {
return string("");
} sort(num.begin(), num.end(), cmp);
string result;
if (num[0] == 0) {
result = "0";
return result;
}
for (int i = 0; i < num.size(); i++) {
char temp[50];
sprintf(temp, "%d", num[i]);
result += temp;
}
return result;
} static bool cmp(int a, int b) {
char temp[50];
sprintf(temp, "%d", a);
string stra = temp;
sprintf(temp, "%d", b);
string strb = temp; for (int i = 0; i < stra.size() && i < strb.size(); i++) {
if (stra[i] > strb[i]) {
return true;
}
else if (stra[i] < strb[i]) {
return false;
}
}
if (stra.size() < strb.size()) {
return strb[stra.size()] > strb[0] ? false : true;
}
if (stra.size() > strb.size()) {
return stra[strb.size()] > stra[0] ? true : false;
}
return false;
}
};

lintcode-184-最大数的更多相关文章

  1. [LintCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  2. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  3. lintcode 滑动窗口的最大值(双端队列)

    题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为  ...

  4. lintcode算法周竞赛

    ------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...

  5. LintCode笔记 - 145.大小写转换 - 极简之道 - 最短代码

    这道题目一眼就能看出是送分题,当然在这里也不谈高难度的实现逻辑,肯定有同学会想直接用自带函数实现不就可以了吗? 对的,就是这么简单,然而今天的重点是如何把代码简写到最短. 本文章将带你把代码长度从 一 ...

  6. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  7. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  8. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  9. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  10. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

随机推荐

  1. tp js结合时间戳

    $(document).ready(function(){ $.extend({ show:function(){ } }); setInterval("show()",1000) ...

  2. hive 入门

    hive-site.xml 配置 <configuration> <property> <name>javax.jdo.option.ConnectionURL&l ...

  3. 大数据技术原理与应用——分布式文件系统HDFS

    分布式文件系统概述 相对于传统的本地文件系统而言,分布式文件系统(Distribute File System)是一种通过网络实现文件在多台主机上进行分布式存储的文件系统.分布式文件系统的设计一般采用 ...

  4. DXP常用的设置及快捷键

    原文地址:http://www.cnblogs.com/NickQ/p/8799240.html 测试环境:Altium Designer Summer 16 一.快捷键 1.原理图和PCB通用快捷键 ...

  5. python应用:日期时间

    计算时间差时,注意天数差引发的问题,获取天数差为 (date2-date1).days 此处,需谨记date2>date1,以保证结果的正确性 具体应用如下: # -*-coding:utf8- ...

  6. Artistic Style 3.1

    Artistic Style 3.1 Tab 选项 下面的示例显示空白字符.一个空格(space)用一个 . 表示,一个制表符(tab)用 > (大于号) 表示. ** 默认缩进 ** 如果没有 ...

  7. Maximum sum

    描述 Given a set of n integers: A={a1, a2,-, an}, we define a function d(A) as below: t1 t2 d(A) = max ...

  8. [AGC011F] Train Service Planning [线段树优化dp+思维]

    思路 模意义 这题真tm有意思 我上下楼梯了半天做出来的qwq 首先,考虑到每K分钟有一辆车,那么可以把所有的操作都放到模$K$意义下进行 这时,我们只需要考虑两边的两辆车就好了. 定义一些称呼: 上 ...

  9. 北京Uber优步司机奖励政策(2月20日~2月21日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. 【LG1975】[国家集训队]排队

    [LG1975][国家集训队]排队 题面 洛谷 题解 又是一个偏序问题 显然\(CDQ\) 交换操作不好弄怎么办? 可以看成两次删除两次插入 排序问题要注意一下 代码 #include <ios ...