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的更多相关文章

  1. [LeetCode] Largest Number 最大组合数

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  2. LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用

    Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...

  3. LeetCode——Largest Number

    Description: Given a list of non negative integers, arrange them such that they form the largest num ...

  4. [LeetCode] Largest Number 排序

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  5. [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 ...

  6. LeetCode() Largest Number

    全排列,超时,知道超时,只是想验证一下. class Solution { public: string largestNumber(vector<int>& nums) { so ...

  7. Leetcode:Largest Number详细题解

    题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...

  8. [LeetCode][Python]Largest Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...

  9. LeetCode之“排序”:Largest Number

    题目链接 题目要求: Given a list of non negative integers, arrange them such that they form the largest numbe ...

随机推荐

  1. java nio管道

    管道(Pipe) (本部分原文链接,作者:Jakob Jenkov,译者:黄忠,校对:丁一) Java NIO 管道是2个线程之间的单向数据连接.Pipe有一个source通道和一个sink通道.数据 ...

  2. Swift入门(十一)——类型转换与is、as操作

    三种操作:is.as?和as! Swift是强类型语言,但也允许开发者通过is.as?和as!这三种操作来对类型进行判断和强制转换.其中is用作类型判断,而as?和as!则分别是类型转换的可选形式和强 ...

  3. QT5删除隐藏目录+隐藏文件(使用Process::start函数调用系统命令,且等待到结束)

    1.功能需求 删除一个目录(包括目录本身),同时删除该目录中所有文件及目录(含隐藏的) 2.遇到的问题 qt5中已经有了递归删除目录的函数--->bool QDir::removeRecursi ...

  4. 喵星人教你记 HTTP 状态码

    记忆HTTP状态码是有一些困难的,因为状态码很多且很难记忆.GirlieMac,也就是Tomomi Imura利用她巧妙的构思,PS了一系列的HTTP状态信息.在你看过这些图片之后,你绝对可以记住一些 ...

  5. Django admin site(二)ModelAdmin methods

    ModelAdmin methods save_model(request, obj, form, change) 此方法为admin界面用户保存model实例时的行为.request为HttpReq ...

  6. win7右下角声音图标不见的解决方法

    1.启动任务管理器 2.在进程选项卡里找到"explorer.exe",然后结束该进程 3.接着点击应用程序选项卡,点击新任务 4.输入explorer.exe,然后点击确定 5. ...

  7. git中手动删除的文件如何在git中删除

    在日常开发中,我们可能或手动删除(delete键删除的)一些文件,然而我们本来应该是用git rm fileName命令删除的,但是现在我们手动删除了,那么要如何在git里面讲那些手动删除的文件删除呢 ...

  8. System.exit(0)

    表示程序正常退出 System.exit(status) 当status非0时,表示程序为非正常退出. status=0, 关闭当前正在运行的虚拟机. 求质因数分解的程序如下: 两种算法: 一种是用S ...

  9. asp.net 使用JQuery 调用Ashx 后面直接写方法名,通过反射找到对应的方法

    using System.Reflection; public class Industry_Manager : IHttpHandler { HttpRequest gRequest = null; ...

  10. CORS 跨域 实现思路及相关解决方案

    本篇包括以下内容: CORS 定义 CORS 对比 JSONP CORS,BROWSER支持情况 主要用途 Ajax请求跨域资源的异常 CORS 实现思路 安全说明 CORS 几种解决方案 自定义CO ...