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. maven2 + tomcat6 + eclipse集成配置

    转载:http://wenku.baidu.com/view/d64147c676eeaeaad1f330d4.html?re=view /*maven2 + tomcat6 + eclipse集成配 ...

  2. GC垃圾回收之GC.KeepAlive方法

    http://msdn.microsoft.com/zh-cn/library/system.gc.keepalive.aspx http://www.cnblogs.com/ren700622/ar ...

  3. React事件处理函数的bind复用和name复用

    一.bind复用 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=&qu ...

  4. WP之Sql Server CE数据库

    如何在WP8中进行数据存储,你首先想到应该是独立存储,但是独立存储似乎存储文件更方便,如果我们希望像处理对象的形式,该怎么办呢,答案就是Sql Server CE. Sql Server CE并不是新 ...

  5. hibernate 联合主键生成机制(组合主键XML配置方式)

    hibernate 联合主键生成机制(组合主键XML配置方式)   如果数据库中用多个字段而不仅仅是一个字段作为主键,也就是联合主键,这个时候就可以使用hibernate提供的联合主键生成策略. 具体 ...

  6. 写个Python练练手吧

    在Python的交互式命令行写程序,好处是一下就能得到结果,坏处是没法保存,下次还想运行的时候,还得再敲一遍. 所以,实际开发的时候,我们总是使用一个文本编辑器来写代码,写完了,保存为一个.py文件, ...

  7. Docker实例教程[超详细](一)

    Docker Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互 ...

  8. android.content.Context 含义及使用

    Context字面意思上下文,位于framework package的android.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄,很多方法需要通过Con ...

  9. Android开发之ProgressDialog与ProgressBar

    ProgressDialog,继承AlertDialog.所以ProgressDialog就是一个在对话框中显示ProgressDialog,并显示进度的文本信息. 并且没有取消和确定按钮,只能通过b ...

  10. 面试题_17_to_30_数据类型和 Java 基础面试问题

    17)Java 中应该使用什么数据类型来代表价格?(答案)如果不是特别关心内存和性能的话,使用BigDecimal,否则使用预定义精度的 double 类型. 18)怎么将 byte 转换为 Stri ...