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 ...
随机推荐
- Android:打包apk
右击项目->导出export next,完成相关信息填写将得到.apk文件,即可部署到手机上. 第一次: 然后打开目录就可以看到生成的apk,可以发布到各大市场上.
- Android 给listview设置分割线与边界的距离
ListView可通过配置 android:dividerline android:dividerHeight 来实现分割listview中item的效果,如图: 如果想跟上图一样实现这种有设计的分割 ...
- OpenCV源码阅读(3)---matx.h---学习心得
在.h文件里定义类,可以通过内联函数的方法完成类基础函数的实现,这样就不需要额外写.cpp文件来写类的内容. 对于操作符重载,可以使用返回应用的方式减小内存开销 _Tp& someclass: ...
- 浅谈PHP自动化代码审计技术
原文出处: exploit 欢迎分享原创到伯乐头条 0×00 由于博客实在没什么可以更新的了,我就把目前做的事情总结一下,当做一篇博客,主要是谈一谈项目中所运用的一些技术.目前市面上有不少PHP的 ...
- 简单说说PHP优化那些事
我们在编写程序时,总是想要使自己的程序占用资源最小,运行速度更快,代码量更少.往往我们在追求这些的同时却失去了很多东西.下面我想讲讲我对PHP优化的理解.优化的目的是花最少的代价换来最快的运行速度与最 ...
- web rest api tools
https://chrome.google.com/webstore/search/postman-REST%20Client
- 分批次获取git for windows的源代码
$ git initInitialized empty Git repository in d:/SourceCode/GitHub/Git For Windows/Git/.git/ $ git r ...
- oracle command - creata database dbca & create network netca Demo
#Creata database command: dbca [root@redhat4 ~]# su - oracle[oracle@redhat4 ~]$ dbca # ...
- windows2003 iis6.0站点打不开,找不到服务器或 DNS 错误。
最近服务器经常出现打不开网站的现象,有时出现在上午,有时出现在中午,几乎天天都会出现一次,出现问题时,无论是回收程序池还是重启IIS或者关闭其它一些可能有影响的服务,都不能解决问题.网站打不开时,有如 ...
- Nlog Layout
Nlog.config <targets> <target type="Console" name="trace" layout=&q ...