Largest Number

mac2022-06-30  40

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 instead of an integer.

Solution:

class Solution { public: string largestNumber(vector<int>& nums) { vector<string> strs; for (auto& n : nums) strs.push_back(to_string(n)); sort(begin(strs), end(strs), [](string& a, string& b) { return a+b>b+a; }); string rc; for (auto& s : strs) rc += s; int i = 0; for (; i < (int)rc.length(); ++i) if (rc[i] != '0') break; return i == rc.length() ? "0" : rc.substr(i); } };

转载于:https://www.cnblogs.com/deofly/p/largest-number.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)