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

Hide Tags

Sort


这题主要的需要考虑排序中的对比判断,对于给定的两个数lft,rgt,误区是直接 lft rgt 判断,根据题目的思路,介意将rgt 接到 lft后面, 与lft 接到 rgt 后面来对比,这样会简单很多,不过这样需要考虑到是否会溢出的问题,题目的验证好像没有对这一问题的严格控制,可以将相接的数值使用 unsigned long int 来存储,这样避免一定情况的溢出。

#include <vector>
#include <iostream>
#include <algorithm>
#include <sstream>
using namespace std;

class Solution {
public:
    string largestNumber(vector<int> &num) {
        struct MyCompared
        {
            bool operator ()(int lft,int rgt)
            {
                long unsigned int lidx=1,ridx=1;
                while(lidx!=1000000000)
                    if(lft>=10*lidx)  lidx*=10;
                    else    break;
                while(ridx!=1000000000)
                    if(rgt>=10*ridx)  ridx*=10;
                    else    break;
                lidx*=10;
                ridx*=10;
                return lft*ridx+rgt > rgt*lidx+lft;
            }
        }myCompared;
        if(num.size()==0)   return "";
        sort(num.begin(),num.end(),myCompared);
        ostringstream os;
        if(num[0]==0)   return "0";
        for(int i =0;i<num.size();i++)
            os<<num[i];
        return os.str();
    }
};

int main()
{
    vector<int > num{10,2};
    Solution sol;
    cout<<sol.largestNumber(num)<<endl;
    return 0;
}
时间: 2024-10-11 23:04:26

[LeetCode] Largest Number 排序的相关文章

LeetCode——Largest Number

Description: 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 in

leetcode Largest Number python

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 be very large, so you need to return a string

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 be very large, so you need to return a string instead of an i

LeetCode: Largest Number 解题报告

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 be very large, so you need to return a string

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 be very large, so you need to return a string instead of an i

[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 be very large, so you need to return a string instead of an i

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

LeetCode:Largest Number(Greedy)

problem: 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 instea

LeetCode 179. Largest Number(排序)

题目 题意:给你一个数组,让你把数组里的元素组合起来,组合成的新数字最大 题解:把数字给排个序,但是排序的标准是啥呢?两个数字孰大孰小呢?判断标准就是两个数字分别前后组合,得出的数字哪个大,则前面的那个数字就大. 一开始我的思路错了,根据数字每个位上的数字判断,这样反而是变得更加复杂了! class Solution { public: string largestNumber(vector<int>& nums) { vector<string> strs; for(in