LeetCode OJ 之 Largest Number (最大的数字)
2015-05-18 15:26
169 查看
题目:
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.
给定一个非负整型数组,重新组合它们使得结果最大。
注意:使用字符串存储最大的数字,防止溢出。
思路:
使用sort函数,自定义比较函数。题目:
class MyGreat { public: bool operator()(const int x ,const int y) //自定义比较函数 { string s1 = to_string(x) + to_string(y); string s2 = to_string(y) + to_string(x); return (s1.compare(s2) > 0); } }; class Solution { public: string largestNumber(vector<int>& nums) { sort(nums.begin() , nums.end() , MyGreat()); string result; if(nums.empty()) return result; for(int i = 0 ; i < nums.size() ; i++) { result += to_string(nums[i]); } if(result[0] == '0') //如果首位为0,说明所有数字都是0,则返回0即可。 return "0"; else return result; } };
相关文章推荐
- LeetCode OJ:Largest Number(最大数字)
- [每日编程]求 largest Number - 给出一组非负整数,求这些非负整数可以拼接出的最大数字
- Leetcode 179 Largest Number 构造最大数字
- LeetCode--Largest Number(最大数字)Python
- Largest Number(最大数字)
- java算法程序题,输入三个数求哪个是最大数,先判断这三个数是否整数,不是就打印提示:请输入数字,若是就打印出那个最大数
- poj 3181 把n 划分成 不大于m的若干数字 最大的划分方法 (dp +大数)
- 彩色图像边缘检测--求RGB的最大变化率方向(数字图像处理(Digital Image Processing Second Edition)冈萨雷斯)
- 找出一串数字中,n个连续的数之和最大c++代码实例及运行结果
- input 输入数字而且最大为10
- 关于任意数字类型的数组求最大值解决办法
- Java找出一组数字的最大值
- 数字的最大组合
- 今天大家做的一个比赛题:有6个文件,每个文件里大约200w整数,每行一个找出所有文件里最大的一个数字
- 输入数字n,打印1到最大的n位数
- 排列数组中数字得出最大数算法
- 客户端JavaScript实现监视用户键盘输入(只能输入数字,最大输入8个)
- LeetCode OJ 之 Single Number (唯一的数字)
- 最大数字子串和
- 给定一个数组,当中有正负数,求当中的一段“子数组”(即任意长度,连续的数字), 使得这个“子数组”的和是所有“子数组”和中最大的