solution.md 3.4 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
# 最大数

<p>给定一组非负整数 <code>nums</code>,重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数。</p>

<p><strong>注意:</strong>输出结果可能非常大,所以你需要返回一个字符串而不是整数。</p>

<p> </p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入<code></code></strong><code>nums = [10,2]</code>
<strong>输出:</strong><code>"210"</code></pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入<code></code></strong><code>nums = [3,30,34,5,9]</code>
<strong>输出:</strong><code>"9534330"</code>
</pre>

<p><strong>示例 3:</strong></p>

<pre>
<strong>输入<code></code></strong>nums = [1]
<strong>输出:</strong>"1"
</pre>

<p><strong>示例 4:</strong></p>

<pre>
<strong>输入<code></code></strong>nums = [10]
<strong>输出:</strong>"10"
</pre>

<p> </p>

<p><strong>提示:</strong></p>

<ul>
	<li><code>1 <= nums.length <= 100</code></li>
	<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>

<p>以下错误的选项是?</p>

## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
    Solution sol;
    vector<int> nums = {3, 30, 34, 5, 9};
    string res = sol.largestNumber(nums);
    cout << res;

    return 0;
}
```

## 答案
```cpp
class Solution
{
public:
    string largestNumber(vector<int> &nums)
    {
        string ans;
        for (int i = 0; i < nums.size(); i++)
        {
            for (int j = nums.size() - 1; j > i; j--)
            {
                if (to_string(nums[j - 1]) + to_string(nums[j]) > to_string(nums[j]) + to_string(nums[j - 1]))
                {
                    int tmp = nums[j - 1];
                    nums[j - 1] = nums[j];
                    nums[j] = tmp;
                }
            }
            ans += to_string(nums[i]);
        }
        if (ans + "0" == "0" + ans)
            return "0";
        return ans;
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    string largestNumber(vector<int> &nums)
    {
        string ans;
        sort(nums.begin(), nums.end(), [](int a, int b)
             { return to_string(a) + to_string(b) > to_string(b) + to_string(a); });
        for (auto it : nums)
        {
            ans += to_string(it);
        }
        return ans[0] == '0' ? "0" : ans;
    }
};
```

### B
```cpp
class Solution
{
public:
    string largestNumber(vector<int> &nums)
    {
        if (*max_element(nums.begin(), nums.end()) == 0)
            return to_string(0);
        vector<string> vec(nums.size());
        for (int i = 0; i < nums.size(); ++i)
            vec[i] = to_string(nums[i]);
        sort(vec.begin(), vec.end(), [](const string &s1, const string &s2)
             { return s1 + s2 > s2 + s1; });
        return accumulate(vec.begin(), vec.end(), string());
    }
};
```

### C
```cpp
class Solution
{
public:
    string largestNumber(vector<int> &nums)
    {
        string res = "";
        int n = nums.size();
        vector<string> tmp;
        for (int i = 0; i < n; i++)
        {
            tmp.push_back(to_string(nums[i]));
        }
        sort(tmp.begin(), tmp.end(), compare);
        for (int i = 0; i < tmp.size(); i++)
        {
            res += tmp[i];
        }
        if ('0' == res[0])
        {
            return "0";
        }
        else
        {
            return res;
        }
    }
};
```