Skip to content

Commit

Permalink
Create 179.Largest-Number.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Sep 25, 2020
1 parent 0fc22ff commit 0b8b114
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Sort/179.Largest-Number/179.Largest-Number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
static bool cmp(string a, string b)
{
string c=a+b;
string d=b+a;
return stoll(c)>stoll(d);
}
public:
string largestNumber(vector<int>& nums)
{
vector<string>Nums;
for (int i=0; i<nums.size(); i++)
Nums.push_back(to_string(nums[i]));

sort(Nums.begin(),Nums.end(),cmp);

string result;
for (int i=0; i<Nums.size(); i++)
result+=Nums[i];

int i=0;
while (result[i]=='0') i++;
if (i==result.size())
return "0";
else
return result.substr(i);

return result;
}
};

0 comments on commit 0b8b114

Please sign in to comment.