Largest Number

简介: 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.

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.

 

C++代码实现:

#include<iostream>
#include<string>
#include<vector>
using namespace std;

class Solution {
public:
    string largestNumber(vector<int> &num) {
        if(num.empty())
            return "";
        string ret;
        int i,j;
        while(!num.empty())
        {
            i=0;
            for(j=1;j<(int)num.size();j++)
            {
                if((to_string(num[i])+to_string(num[j]))<(to_string(num[j])+to_string(num[i])))
                    i=j;
            }
            ret+=to_string(num[i]);
            num.erase(num.begin()+i);
        }
        while(ret.size()>1&&ret[0]=='0')
            ret.erase(ret.begin());
        return ret;
    }
};

int main()
{
    Solution s;
    vector<int> vec={3,30,34,5,9};
    cout<<s.largestNumber(vec)<<endl;
}

 

直接调用STL算法进行排序:

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
//不能声明non-static成员函数
bool Mycmp(int n1,int n2)
{
    string s1=to_string(n1);
    string s2=to_string(n2);
    return s1+s2>s2+s1;
}
class Solution
{
public:
    string largestNumber(vector<int> &num)
    {
        if(num.empty())
            return "";
        string ret="";
        sort(num.begin(),num.end(),Mycmp);
//sort(num.begin(),num.end(),[](int n1,int n2){ return to_string(n1)+to_string(n2)>to_string(n2)+to_string(n1);});
        if(num[0]==0)
            return "0";
        for(auto a:num)
            ret+=to_string(a);
        return ret;
    }
//声明为static成员是可以的
static bool Mycmp(int n1,int n2)
{
    string s1=to_string(n1);
    string s2=to_string(n2);
    return s1+s2>s2+s1;
}
}; 
int main()
{
  Solution s;
  vector<int> num= {3,32,321};
  cout<<s.largestNumber(num)<<endl;
}

  

相关文章
[LeetCode] Largest Number
Well, this problem is designed for radix sort. For more information about radix sort, Introduction to Algorithms, 3rd edition has some nice examples.
734 0
[LeetCode] Largest Number
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
1039 0
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
259 1
|
存储 SQL 算法
LeetCode 题目 65:有效数字(Valid Number)【python】
LeetCode 题目 65:有效数字(Valid Number)【python】
|
存储 算法
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
219 0
|
存储
Leetcode Single Number II (面试题推荐)
给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。
140 0
|
算法
LeetCode 414. Third Maximum Number
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
287 0
LeetCode 414. Third Maximum Number
|
存储
LeetCode 313. Super Ugly Number
编写一段程序来查找第 n 个超级丑数。 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数。
195 0
LeetCode 313. Super Ugly Number