LintCode :最大数
来源:互联网 发布:和平网络电视频道地址 编辑:程序博客网 时间:2024/05/01 03:17
LintCode :最大数
题目
给出一组非负整数,重新排列他们的顺序把他们组成一个最大的整数。
样例
给出 [1, 20, 23, 4, 8],返回组合最大的整数应为8423201。
思路
拿到题目我们知道先要int to string然后给string排序,但是这个问题上的排序规则跟普通的不太一样,我们如何判断两个string谁大谁小呢?按照题目的意思
20,23。2320>2023,所以23>20。
765432,84。84765432>76543284,所以84>765432。
也就是说两个字符串的两种不同的拼接方式,哪种拼接方式得到的数字大那拼接的顺序就是大小的顺序。
代码
int cmp(const string& a, const string& b) { string af = a + b; string bf = b + a; if(af != bf) return af > bf ? 1 : -1; else return 0;}int patition(vector<string> &A, int p, int r) { string x = A[r]; int i = p - 1; for(int j = p; j < r; j++) { if(cmp(A[j], x) != 1) { i++; swap(A[i], A[j]); } } swap(A[i + 1], A[r]); return i + 1;}void quicksort(vector<string> &A, int p, int r) { if(p < r) { int q = patition(A, p, r); quicksort(A, p, q - 1); quicksort(A, q + 1, r); }}string largestNumber(vector<int> &num) { vector<string> A; for(int i = 0; i < num.size(); i++) A.push_back(to_string(num[i])); quicksort(A, 0, A.size() - 1); string result = ""; for(int i = A.size() - 1; i >= 0; i--) if(result == "" && A[i] == "0") continue; else result += A[i]; if(result == "") result = "0"; return result;}
0 0
- LintCode-最大数
- lintcode-最大数-184
- LintCode-最大数
- LintCode:最大数
- LintCode 最大数
- LintCode-最大数
- LintCode 创建最大数
- 最大数,lintcode
- LintCode 184-最大数
- LintCode :最大数
- lintcode(184)最大数
- LintCode:创建最大数
- lintcode -- 最大数
- 最大数-LintCode
- LintCode:M-最大数
- 创建最大数-LintCode
- *[Lintcode]Largest Number 最大数
- lintcode 最大数(184)
- VS2010-MFC:用OpenGL在对话框中的PictureControl(图片控件)中绘制三维模型,可旋转、平移、缩放,可用于三维模型的预览
- 学习OpenCV2——绘制基本图形及文字
- 将博客搬至CSDN
- #OSG+VS#第五周
- SSL 字符串
- LintCode :最大数
- tomcat7 内存配置修改方法
- 通过GBDT组合的特征作为LR的输入
- java基础学习之设计模式 十七
- 两种“两数互换的方式”
- java HotSpot虚拟机垃圾回收优化(一、Introduction)
- @TransactionConfiguration过时与替代写法
- computer-database 项目需求提测
- 使用asm.jar将Android手机屏幕投影到电脑