设为首页 加入收藏

TOP

Leetcode: Largest Number
2015-07-20 17:20:54 来源: 作者: 【 】 浏览:3
Tags: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.

Solution:

class Solution {
public:
static bool cmp(string a, string b) {
	return a + b > b + a;
}
string largestNumber(vector
  
    &num) {
	vector
   
     snum; for (int i = 0; i < num.size(); i++) snum.push_back(to_string(num[i])); sort(snum.begin(), snum.end(), cmp); string res = ""; for (int i = 0; i < snum.size(); i++) res += snum[i]; if (res[0] == '0') return "0"; return res; } };
   
  






】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇PAT03-1. 二分法求多项式单根(20) 下一篇[LeetCode]Valid Parentheses

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)
·使用华为开发者空间 (2025-12-27 04:19:24)
·Getting Started wit (2025-12-27 03:49:24)
·Ubuntu 上最好用的中 (2025-12-27 03:49:20)