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

思路:

忧伤的发现,人的思路真的是很容易定型。这道题我做过的,但是过了这么久,我还是只记得我自己的老思路。

直接上正确答案, 关键是排序时比较的策略, 用s1+s2 和 s2+s1 比较即可,就是把两种方式排列的情况都试一下,看哪个大!!

class Compare{
public:
    bool operator()(string s1, string s2)
    {
        return s1 + s2 < s2 + s1;
    }
};

class Solution {
public:
    string largestNumber(vector<int> &num) {
        int i;
        for (i = 0; i < num.size(); i++)
            if (num[i] != 0)
                break;
        if (i == num.size()) // All numbers are 0
            return "0";
        vector<string> v;
        for (i = 0; i < num.size(); i++)
            v.push_back(to_string(num[i]));
        sort(v.begin(), v.end(), Compare());
        string result;
        for (i = v.size() - 1; i >= 0; i--)
            result += v[i];
        return result;
    }
};

我自己的思路:

两个数比较 如 724 和7247 那么比较 724|724 与 7247|7247 即相同时就不断复制自己,直到遇到不同的数字得到大小。 如果两个数字同时达到末尾,但是数字一直相同,则这两个数字相同。

代码很长,但是也AC了,慢一些。主要是二分归并排序是我自己写的。

class Solution {
public:
    string largestNumber(vector<int> &num) {

        MergeSort(num, 0, num.size() - 1);
        string ans;
        for(int i = 0; i < num.size(); i++)
        {
            stringstream ss;
            ss << num[i];
            string s = ss.str();
            ans.append(s);
        }
        if(!ans.empty() && ans[0] == ‘0‘)
        {
            ans = "0";
        }
        return ans;
    }

    bool islarge(int aa, int bb)
    {
        stringstream ss;
        ss << aa;
        string sa = ss.str();
        ss << bb;
        string sb = ss.str();

        int i = 0, j = 0;
        while(sa[i % sa.length()] == sb[j % sb.length()]
               && !((i + 1) % sa.length() == 0 && (j + 1) % sb.length() == 0)) //两边同时结束时跳出,防止相同数字无限循环
        {
            i++; j++;
        }

        return sa[i % sa.length()] > sb[j % sb.length()];
    }

    void MergeSort(vector<int> &in, int l, int r)
    {
        if(l < r)
        {
            int mid = (l + r) / 2;
            MergeSort(in, l, mid);
            MergeSort(in, mid + 1, r);
            Merge(in, l, mid, r);
        }
    }
    void Merge(vector<int> &in, int l, int mid, int r)
    {
        vector<int> left(in.begin() + l, in.begin() + mid + 1), right(in.begin() + mid + 1, in.begin() + r + 1);
        int i = 0, j = 0, k = 0;
        while(i < left.size() && j < right.size())
        {
            if(islarge(left[i], right[j]))
            {
                in[l + k] = left[i++];
            }
            else
            {
                in[l + k] = right[j++];
            }
            k++;
        }
        while(i < left.size())
        {
            in[l + k] = left[i++];
            k++;
        }
        while(j < right.size())
        {
            in[l + k] = right[j++];
            k++;
        }
    }
};
时间: 2024-09-29 10:47:18

【leetcode】Largest Number ★的相关文章

【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

【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】Single Number (2 solutions)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

【LeetCode】Single Number

原文: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解答: 常规解法:先对数组进行排序,然后通过按顺序判断每相邻两个数是否相同即可

【LeetCode】Single Number I &amp; II

Single Number I : Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Solution: 解法不少,贴一种: 1 cla

【LeetCode】Valid Number 解题报告

[题目] Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to be ambig

【Leetcode】Guess Number Higher or Lower II

题目链接: 题目: We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number I picked is higher or lower. However, when you guess a

【Leetcode】Happy Number

题目链接:https://leetcode.com/problems/happy-number/ 题目: Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the sq

【Leetcode】Largest Divisible Subset

题目链接:https://leetcode.com/problems/largest-divisible-subset/ 题目: Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0. If there are multiple