leetcode_[python/C++] 179. Largest Number(数字组合成最大数)
2016-11-15 18:06
821 查看
题目链接
【题目】
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.
【分析】
刚开始看到这道题是不是要好几个循环呀,后来想了想这不就是到排序题么,虽然很像是组合题,但其实只要改写下排序函数,进而对nums进行排序就可以了
变成string进行排序
比如 12 13
则排序”12” “13”
“12”+”13” = “1213”
“13”+”12” = “1312”
我们要的是”1312”>”1213”
排序函数需要注意就是
return a>b
or return a-b
这两个是不一样的
比如在C++ 的qsort中用到的是return a-b的做法
在一些map,set什么的用到的是return a>b的做法
而且:
return a-b 是从小到大
return b-a 是从大到小
return a>b 是从大到小
return b>a 是从小到大
可以看下下面这个链接浅谈C/C++排序函数中cmp()比较函数的写法
首先看下python的写法:
上面注释的写法读者可以自行检验,都是可以AC的写法
接着读者可以试一下把上面的comp函数改写下面这几种写法,然后自行意会
所以其实可以写成一行
C++
在C++中同样可以像python那样的写法,在discuss上看到的
反正我是不会写这种
但其实这样的效率比我自己写的好像好差一点的
【题目】
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.
【分析】
刚开始看到这道题是不是要好几个循环呀,后来想了想这不就是到排序题么,虽然很像是组合题,但其实只要改写下排序函数,进而对nums进行排序就可以了
变成string进行排序
比如 12 13
则排序”12” “13”
“12”+”13” = “1213”
“13”+”12” = “1312”
我们要的是”1312”>”1213”
排序函数需要注意就是
return a>b
or return a-b
这两个是不一样的
比如在C++ 的qsort中用到的是return a-b的做法
在一些map,set什么的用到的是return a>b的做法
而且:
return a-b 是从小到大
return b-a 是从大到小
return a>b 是从大到小
return b>a 是从小到大
可以看下下面这个链接浅谈C/C++排序函数中cmp()比较函数的写法
首先看下python的写法:
class Solution: # @param {integer[]} nums # @return {string} def largestNumber(self, nums): comp = lambda a,b:1 if a+b>b+a else -1 num_to_str = map(str,nums) num_to_str.sort(cmp = comp,reverse = True) # num_to_str.sort(cmp = lambda a,b:-1 if a+b<b+a else 0,reverse = True) #慢 # num_to_str.sort(cmp = lambda a,b:cmp(b+a,a+b),reverse = True) #not cmp(a+b,b+a) #return str(int(''.join(num_to_str))) return '0' if num_to_str[0] == '0' else ''.join(num_to_str)
上面注释的写法读者可以自行检验,都是可以AC的写法
接着读者可以试一下把上面的comp函数改写下面这几种写法,然后自行意会
comp = lambda a,b:1 if a+b>b+a else -1 true comp = lambda a,b:-1 if a+b<b+a else 0 true comp = lambda a,b:0 if a+b>b+a else -1 true comp = lambda a,b:1 if a+b>b+a else 0 false
所以其实可以写成一行
return str(int("".join(sorted([str(x) for x in nums], cmp = lambda a, b: int(b + a) - int(a + b)))))
C++
在C++中同样可以像python那样的写法,在discuss上看到的
class Solution { public: string largestNumber(vector<int>& nums) { sort(nums.begin(), nums.end(), [](int a, int b){ return to_string(a) + to_string(b) > to_string(b) + to_string(a);}); string ans = accumulate(nums.begin(), nums.end(), string(), [](string &r, int a){return r + to_string(a);}); return ans[0] == '0' ? "0" : ans; } };
反正我是不会写这种
但其实这样的效率比我自己写的好像好差一点的
class Solution { public: static bool compare(string a, string b){ return a+b<b+a; } string largestNumber(vector<int>& nums) { if (nums.empty()) return ""; vector<string> num_str; for (int i=0;i<nums.size();i++){ num_str.push_back(to_string(nums[i])); } sort(num_str.begin(),num_str.end(),compare); string res=""; for (int i=nums.size()-1;i>=0;i--){ res+=num_str[i]; } if (compare("0",res)) return res; else return "0"; } };
相关文章推荐
- C/C++实现多个数的拼接(组合)成最大数字
- 【C++】LeetCode 179. Largest Number
- LeetCode 179. Largest Number(最大数)
- leetcode_[Python/C++]_3_Longest Substring Without Repeating Characters(不重复子串最大长度)
- LeetCode--Largest Number(最大数字)Python
- /*C++任意输入一个整数,再把它的各位数字重新组合一个新的整数,使其值最大*/
- leetcode_[python/C++] 17.Letter Combinations of a Phone Number(手机号码字符组合)
- [LeetCode] Largest Number At Least Twice of Others 至少是其他数字两倍的最大数
- leetcode 179. Largest Number 数组可以组成最大的数
- LeetCode--Find the Duplicate Number(查找重复数字)Python
- LeetCode--Excel Sheet Column Number(Excel表格的列名转数字)Python
- 179. Largest Number 排成最大的数字(重要!)
- [C++]LeetCode: 116 Excel Sheet Column Number (excel列坐标转数字)
- Leetcode_valid-number(updated c++ and python version)
- LeetCode题解汇总(C++ Java Python,含题目翻译)
- LeetCode 62 — Unique Paths(C++ Java Python)
- LeetCode 27 — Remove Element(C++ Java Python)
- LeetCode 53 — Maximum Subarray(C++ Java Python)
- LeetCode 58 — Length of Last Word(C++ Java Python)
- LeetCode 136 — Single Number(C++ Java Python)