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. TCL语言笔记:TCL中的String命令

    一.介绍 字符串是 Tcl 中的基本数据类型,所以有大量的字符串操作命令.一个比较重要的问题就是模式匹配,通过模式匹配将字符串与指定的模式(格式)相匹配来进行字符串的比较.搜索等操作. 二.strin ...

  2. WCF入门(二)-----实战开发

    在这个实战中我们将使用DataContract,ServiceContract来构建WCF服务,并使用VS2008内置的“WCFSVCHost”运行我们创建的WCF服务,并使用“WCF测试客户端”来测 ...

  3. No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? 问题

    maven编译项目时出错,提示信息如下: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3 ...

  4. 223. Rectangle Area

    题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...

  5. muParser公式库使用简介( 转)

    muParser是一个跨平台的公式解析库,它可以自定义多参数函数,自定义常量.变量及一元前缀.后缀操作符,二元操作符等,它将公式编译成字节码,所以计算起来非常快. 当前版本V1.28,官方网址http ...

  6. objectC时间用法

    #define kDEFAULT_DATE_TIME_FORMAT (@"yyyy-MM-dd HH:mm:ss") //获取当前日期,时间+(NSDate *)getCurren ...

  7. javascript算法汇总(持续更新中)

    1. 线性查找 <!doctype html> <html lang="en"> <head> <meta charset="U ...

  8. 1888. Pilot Work Experience(dfs+bfs)

    1888 dfs找出连通块 块内构造数据 bfs找出最值 如果有多个连通块 那max就为49 可以起点不同 这样记得修改后面的数据 写的老长了.. #include <iostream> ...

  9. 安卓学习之--如何关闭所有的activity

    根据Activity的声明周期 方法1   我们知道Android的窗口类提供了历史栈,我们可以通过stack的原理来巧妙的实现,这里我们在A窗口打开B窗口时在Intent中直接加入标志 Intent ...

  10. fil_space_t

    typedef struct fil_space_struct fil_space_t; /** Tablespace or log data space: let us call them by a ...