Leetcode Largest Number c++ solution
Total Accepted: 16020 Total Submissions: 103330
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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
会超过内存限制;把string改成char *就过了,但很难看!
bool comp(const string& a, const string& b) {
string x = a, y = b;
int diff = x.length() - y.length();
if (diff > 0) {
char c = y[0];
y.insert(y.end(), diff, c);
} else if (diff < 0) {
char c = y[0]; //lastChar(x);
x.insert(x.end(), -diff, c);
}
for (int i = 0; i < x.length(); i++) {
if (x[i] < y[i]) return false;
if (x[i] > y[i]) return true;
}
return comp(a+b, b+a);
}
class Solution {
public:
string largestNumber(vector<int> &num) {
int len = num.size();
string* snum = new string[len];
for (int i = 0; i < len; i++) {
snum[i] = to_string(num[i]);
}
stable_sort(snum, snum + len, comp);
string ret;
int i = 0;
for (; i < len && snum[i] == "0"; i++);
for (; i < len; i++)
ret += snum[i];
return ret.empty() ? "0" : ret;
}
};
把string改成char*
#define MAX_BUF 10
bool comp(char* a, char* b) {
char* x = a, *y = b;
char *short_, c[2];
int diff = strlen(x) - strlen(y);
short_ = diff > 0 ? y : x;
sprintf(c, "%c", short_[0]);
for (int i = 0; i < abs(diff); i++)
strcat(short_, c);
int len = strlen(x);
auto restore = [&]() {
for (int i = 0; i < abs(diff); i++)
short_[len - 1 - i] = '\0';
};
for (int i = 0; i < len; i++) {
if (x[i] < y[i]) {
restore();
return false;
}
if (x[i] > y[i]) {
restore();
return true;
}
}
restore();
if (strlen(x) == strlen(y)) return false;
char temp1[MAX_BUF*2], temp2[MAX_BUF*2];
sprintf(temp1, "%s%s", a, b);
sprintf(temp2, "%s%s", b, a);
return comp(temp1, temp2);
}
class Solution {
public:
string largestNumber(vector<int> &num) {
int len = num.size(); char** snum = new char*[len];
for (int i = 0; i < len; i++) {
snum[i] = new char[MAX_BUF];
sprintf(snum[i], "%d", num[i]);
}
sort(snum, snum + len, comp);
string ret;
int i = 0;
for (; i < len && strcmp(snum[i],"0") == 0; i++);
for (; i < len; i++)
ret += snum[i];
return ret.empty() ? "0" : ret;
}
};
重点来了,用python只需要几行
class Solution:
# @param num, a list of integers
# @return a string
def largestNumber(self, num):
num = sorted([str(x) for x in num], cmp = self.compare)
ans = ''.join(num).lstrip('0')
return ans or '0' def compare(self, a, b):
return [1, -1][a + b > b + a]
Leetcode Largest Number c++ solution的更多相关文章
- [LeetCode] Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用
Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...
- LeetCode——Largest Number
Description: Given a list of non negative integers, arrange them such that they form the largest num ...
- [LeetCode] Largest Number 排序
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [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 ...
- LeetCode() Largest Number
全排列,超时,知道超时,只是想验证一下. class Solution { public: string largestNumber(vector<int>& nums) { so ...
- Leetcode:Largest Number详细题解
题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...
- [LeetCode][Python]Largest Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...
- LeetCode之“排序”:Largest Number
题目链接 题目要求: Given a list of non negative integers, arrange them such that they form the largest numbe ...
随机推荐
- MFC、WTL、WPF、wxWidgets、Qt、GTK、Cocoa、VCL 各有什么特点?
WTL都算不上什么Framework,就是利用泛型特性对Win API做了层封装,设计思路也没摆脱MFC的影响,实际上用泛型做UI Framework也只能算是一次行为艺术,这个思路下继续发展就会变得 ...
- WCF揭秘(一)——简单的WCF开发实例
一.WCF是什么 WCF是微软为了实现各个开发平台之间的无疑缝连接而开发一种崭新工具,它是为分布式处理而开发.WCF将DCOM.Remoting.Web Service.WSE.MSMQ.AJAX服务 ...
- 编写自己的TRACE函数
TRACE函数是MFC里面的一个宏,是对OutputDebugString的封装. OutputDebugString的作用是输出调试信息,不要以为这个函数只有在Debug版本才会打日志,即使是Rel ...
- BZOJ 1923 外星千足虫(高斯消元)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1923 题意:有n个数字,m次测试.每个数字为0或者1.每次测试选出一些数字出来把他们加起 ...
- [POJ1330]Nearest Common Ancestors(LCA, 离线tarjan)
题目链接:http://poj.org/problem?id=1330 题意就是求一组最近公共祖先,昨晚学了离线tarjan,今天来实现一下. 个人感觉tarjan算法是利用了dfs序和节点深度的关系 ...
- POJ 1753 Flip Game (高斯消元 枚举自由变元求最小步数)
题目链接 题意:4*4的黑白棋,求把棋全变白或者全变黑的最小步数. 分析:以前用状态压缩做过. 和上题差不多,唯一的不同是这个终态是黑棋或者白棋, 但是只需要把给的初态做不同的两次处理就行了. 感觉现 ...
- Android+Eclipse+Java:在“正在启动 CrazySnake”期间发生了内部错误, java.lang.NullPointerException
删除工作空间下的.metadata 文件夹 重启 Eclipse 清理工作空间
- POJ 3041 Asteroids (最小点覆盖集)
题意 给出一个N*N的矩阵,有些格子上有障碍,要求每次消除一行或者一列的障碍,最少消除多少次可以全部清除障碍. 思路 把关键点取出来:一个障碍至少需要被它的行或者列中的一个消除. 也许是最近在做二分图 ...
- HDU 5303 Delicious Apples 美味苹果 (DP)
题意: 给一个长为L的环,起点在12点钟位置,其他位置上有一些苹果,每次带着一个能装k个苹果的篮子从起点出发去摘苹果,要将全部苹果运到起点需要走多少米? 思路: 无论哪处地方,只要苹果数超过k个,那么 ...
- git - 搭建git仓库
1. 更新git版本: http://codelife.me/blog/2013/06/25/upgrade-git-on-centos-6-4/ 2. 建立git仓库: git init --bar ...