[LeetCode] 179. 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.

问题:给定一个数组,求将数组元素拼凑成一个值最大的整数。

将整型数组的转化为字符串数组,用 std::sort 对字符串数组排序,使得排序后元素直接连接成为最大值整数。

使用默认的比较函数排序,会得到不一定是最大值,例如 {360, 36} 排序后得到的是 36036 ,明显不是小于 36360。解决这个问题,就是重写比较函数,使得排序后的两个元素连接成为较大的整数即可,很简洁。

 1     int static comp(string s1, string s2){
 2         return (s1 + s2) > (s2 + s1);
 3     }
 4
 5     string largestNumber(vector<int>& nums) {
 6         vector<string> numsStr;
 7         for (int i = 0 ; i < nums.size(); i++) {
 8             numsStr.push_back(to_string(nums[i]));
 9         }
10
11         std::sort(numsStr.begin(), numsStr.end(), comp);
12
13         string res = "";
14         for (int i = 0 ; i < numsStr.size(); i++) {
15             res += numsStr[i];
16         }
17
18         int i = 0;
19         while (i < res.size()-1 && res[i] == ‘0‘) {
20             i++;
21         }
22         res = res.substr(i);
23
24         return res;
25     }
时间: 2024-10-12 20:19:49

[LeetCode] 179. Largest Number 解题思路的相关文章

8.15 [LeetCode] 179 Largest Number

[LeetCode 179] Largest Number | COMMENTS Question link 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 ver

leetCode 179. Largest Number 字符串排序 | Medium

179. 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 st

[Lintcode]184. Largest Number/[Leetcode]179. Largest Number

184. Largest Number/179. Largest Number 本题难度: Medium Topic: Greedy Description Largest Number 中文English Given a list of non negative integers, arrange them such that they form the largest number. Example Given [1, 20, 23, 4, 8], the largest formed nu

Java for LeetCode 179 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 i

Java 特定规则排序-LeetCode 179 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 i

【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

leetcode[179]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 i

[LeetCode] 179. Largest Number Java

题目: 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

LeetCode 179. Largest Number(排序)

题目 题意:给你一个数组,让你把数组里的元素组合起来,组合成的新数字最大 题解:把数字给排个序,但是排序的标准是啥呢?两个数字孰大孰小呢?判断标准就是两个数字分别前后组合,得出的数字哪个大,则前面的那个数字就大. 一开始我的思路错了,根据数字每个位上的数字判断,这样反而是变得更加复杂了! class Solution { public: string largestNumber(vector<int>& nums) { vector<string> strs; for(in