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. Python入门 —— 2048实战(字符界面和图形界面)

    2048 game (共4种实现方法) 目录: .. 图形界面 ... pygame 和 numpy .. 字符界面 ... 第一种 ... curses ... wxpython ... 第二种 . ...

  2. header()函数用处

    header() 函数向客户端发送原始的 HTTP 报头. 认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 PHP 4 以及更高的版本中,您可以使用输出缓存来解决此 ...

  3. 大数据学习--day09(this、static)

    this.static this 关键字 类不可以定义 this 属性 ,  但是每个类都有一个 隐藏起来的 this 属性  . 每个对象被创建了 , 都会给其属性分配空间  , 也会给 this  ...

  4. 大数据学习--day05(嵌套循环、方法、递归)

    嵌套循环.方法.递归 图形打印 public static void main(String[]arg) { /** * * * * * * */ // 3 2 1 0 // 1 3 5 for(in ...

  5. BugkuWeb本地包含

    知识点:$_REQUEST不是一个函数,它是一个超全局变量,里面包括有$_GET $_POST $_COOKIE的值,$_REPUEST 是接收了 $_GET $_POST $_COOKIE 三个的集 ...

  6. 转载:git和github新手安装使用教程(三步入门)

    转载防止以后电脑重装,找不到记录. 教程地址:https://www.cnblogs.com/ttjsndx/p/7943444.html

  7. Codeforces Round #490 (Div. 3) :F. Cards and Joy(组合背包)

    题目连接:http://codeforces.com/contest/999/problem/F 解题心得: 题意说的很复杂,就是n个人玩游戏,每个人可以得到k张卡片,每个卡片上有一个数字,每个人有一 ...

  8. 常用 Maven 配置

    打包为带依赖的 JAR <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEnc ...

  9. DevExpress通过girdcontrol实现分页

    简介:DevExpress中如何实现GridControl的分页功能, 主要是利用DataNavigator和GridControl组合,自定义事件实现分页功能 接下来,我们就去实现分页功能,先看下效 ...

  10. LSD_SLAM(一)运行数据集

    虽然按照作者的推荐使用的是OpenCV 2.4.8,但编译依然通不过,一直有错误: 因为lsd_slam是2014年出的,用的ros编译系统不是catkin而是rosbuild,有一些不理解的地方. ...