lintcode-184-最大数
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-最大数的更多相关文章
- [LintCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- lintcode 滑动窗口的最大值(双端队列)
题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为 ...
- lintcode算法周竞赛
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- LintCode笔记 - 145.大小写转换 - 极简之道 - 最短代码
这道题目一眼就能看出是送分题,当然在这里也不谈高难度的实现逻辑,肯定有同学会想直接用自带函数实现不就可以了吗? 对的,就是这么简单,然而今天的重点是如何把代码简写到最短. 本文章将带你把代码长度从 一 ...
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8748 Solved: 3835[Submi ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
随机推荐
- python matplotlibmat 包mplot3d工具 三维视图透视取消
https://stackoverflow.com/questions/23840756/how-to-disable-perspective-in-mplot3d 简单的解决方法是 ax = fig ...
- P2257 YY的GCD
P2257 YY的GCD 题目描述 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 k ...
- STL---llist
#include<iostream> #include<algorithm> #include<list> using namespace std; struct ...
- P2P通讯
转载: http://www.cnblogs.com/pannengzhi/p/4800526.html http://blog.csdn.net/lee353086/article/details/ ...
- 浅谈fail-fast机制
fail-fast机制即为快速失败机制,个人认为是一种防护措施,在集合结构发生改变的时候,使尽全力抛出ConcurrentModificationException,所以该机制大部分用途都是用来检测B ...
- Scala学习笔记(三)—— 方法和函数
1. 方法 方法使用 def 定义: def 方法名(参数名:参数列表,…) :返回值类型 = { 方法结构体 } scala> def add(x : Int ,y : Int):Int = ...
- HDOJ:6333-Problem B. Harvest of Apples(组合数学+莫队算法+逆元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 解题心得: 这个题可以说是十分精彩了,首先推组合数学的公式,其中一个很重要的公式是Cnm = C ...
- xmind打开文件报错
可以尝试使用导入 文件——导入 选择此方式打开即可.
- 【BZOJ4753】最佳团体(分数规划,动态规划)
[BZOJ4753]最佳团体(分数规划,动态规划) 题面 BZOJ Description JSOI信息学代表队一共有N名候选人,这些候选人从1到N编号.方便起见,JYY的编号是0号.每个候选人都由一 ...
- 【POJ2482】Stars in Your Window
[POJ2482]Stars in Your Window 题面 vjudge 题解 第一眼还真没发现这题居然™是个扫描线 令点的坐标为\((x,y)\)权值为\(c\),则 若这个点能对结果有\(c ...