LintCode-最大数
2015-10-20 21:01
615 查看
分享代码~
bool cmp(string a,string b)
{
if (a+b>b+a)
return true;
else
return false;
}
class Solution {
public:
/**
*@param num: A list of non negative integers
*@return: A string
*/
string toString(int x)
{
if (x==0)
{
return "0";
}
string s="";
while (x>0)
{
char ch='0'+x%10;
s=ch+s;
x/=10;
}
return s;
}
string largestNumber(vector<int> &num) {
// write your code here
vector<string> nums;
for (int i=0;i<num.size();++i)
{
nums.push_back(toString(num[i]));
}
sort(nums.begin(),nums.end(),cmp);
string r="";
for (int i=0;i<nums.size();++i)
{
r+=nums[i];
}
if (r[0]=='0')
return "0";
return r;
}
};
bool cmp(string a,string b)
{
if (a+b>b+a)
return true;
else
return false;
}
class Solution {
public:
/**
*@param num: A list of non negative integers
*@return: A string
*/
string toString(int x)
{
if (x==0)
{
return "0";
}
string s="";
while (x>0)
{
char ch='0'+x%10;
s=ch+s;
x/=10;
}
return s;
}
string largestNumber(vector<int> &num) {
// write your code here
vector<string> nums;
for (int i=0;i<num.size();++i)
{
nums.push_back(toString(num[i]));
}
sort(nums.begin(),nums.end(),cmp);
string r="";
for (int i=0;i<nums.size();++i)
{
r+=nums[i];
}
if (r[0]=='0')
return "0";
return r;
}
};
相关文章推荐
- Subtree
- LintCode --number-of-airplanes-in-the-sky(数飞机)
- LintCode --invert-binary-tree(翻转二叉树)
- LintCode --find-the-missing-number(寻找缺失的数)
- LintCode--best-time-to-buy-and-sell-stock(买卖股票的最佳时机)
- LintCode--best-time-to-buy-and-sell-stock-ii(买卖股票的最佳时机 II)
- LintCode 翻转二叉树
- LintCode 寻找缺失的数
- Lintcode 二叉树的后序遍历
- Lintcode 二叉树中序遍历
- Lintcode 二叉树前序遍历
- Lintcode 尾部零的个数
- Lintcode 比较字符串
- Lintcode 主元素
- Lintcode解题笔记 - 岛屿的个数
- LintCode -合并两个排序链表
- 20150708 lintcode 总结 Binary Tree Maximum Path Sum Show result *****
- 20150708 lintcode 总结 Minimum Path Sum
- 20150708 lintcode 总结 Binary Search Tree Iterator
- 20150707 lintcode 总结 Search Range in Binary Search Tree