iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++实现LeetCode(179.最大组合数)
  • 435
分享到

C++实现LeetCode(179.最大组合数)

2024-04-02 19:04:59 435人浏览 薄情痞子
摘要

[LeetCode] 179. Largest Number 最大组合数 Given a list of non negative integers, arrange them su

[LeetCode] 179. Largest Number 最大组合数

Given a list of non negative integers, arrange them such that they fORM the largest number.

Example 1:

Input: [10,2]
Output: "210"

Example 2:

Input: [3,30,34,5,9]
Output: "9534330"

Note: The result may be very large, so you need to return a string instead of an integer.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

这道题给了我们一个数组,让将其拼接成最大的数,那么根据题目中给的例子来看,主要就是要给数组进行排序,但是排序方法不是普通的升序或者降序,因为9要排在最前面,而9既不是数组中最大的也不是最小的,所以要自定义排序方法。如果不参考网友的解法,博主估计是无法想出来的。这种解法对于两个数字a和b来说,如果将其都转为字符串,如果 ab > ba,则a排在前面,比如9和34,由于 934>349,所以9排在前面,再比如说 30 和3,由于 303<330,所以3排在 30 的前面。按照这种规则对原数组进行排序后,将每个数字转化为字符串再连接起来就是最终结果。代码如下:


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

GitHub 同步地址:

https://github.com/grandyang/leetcode/issues/179

参考资料:

Https://leetcode.com/problems/largest-number/

https://leetcode.com/problems/largest-number/discuss/53158/My-Java-Solution-to-share

https://leetcode.com/problems/largest-number/discuss/53157/A-simple-C%2B%2B-solution

到此这篇关于c++实现LeetCode(179.最大组合数)的文章就介绍到这了,更多相关C++实现最大组合数内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C++实现LeetCode(179.最大组合数)

本文链接: https://www.lsjlt.com/news/131787.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C++实现LeetCode(179.最大组合数)
    [LeetCode] 179. Largest Number 最大组合数 Given a list of non negative integers, arrange them su...
    99+
    2024-04-02
  • C++实现LeetCode(53.最大子数组)
    [LeetCode] 53. Maximum Subarray 最大子数组 Given an integer array nums, find the contiguous...
    99+
    2024-04-02
  • C++实现LeetCode(152.求最大子数组乘积)
    [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积 Given an integer array nums, find th...
    99+
    2024-04-02
  • C++实现数组中元素组合出最大值
    目录数组中元素组合出最大值如题:这可以算是一个算法类数组或vector求最大值最小值1.求数组的最大值或最小值2.求数组最大值最小值对应的下标数组中元素组合出最大值 如题:这可以算...
    99+
    2024-04-02
  • C++实现LeetCode(85.最大矩形)
    [LeetCode] 85. Maximal Rectangle 最大矩形 Given a 2D binary matrix filled with 0's and 1's, fin...
    99+
    2024-04-02
  • C++怎么实现数组中元素组合出最大值
    本篇内容介绍了“C++怎么实现数组中元素组合出最大值”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!数组中元素组合出最大值如题:这可以算是一个...
    99+
    2023-06-30
  • C++实现LeetCode(39.组合之和)
    [LeetCode] 39. Combination Sum 组合之和 Given a set of candidate numbers (candidates)...
    99+
    2024-04-02
  • C++实现LeetCode(209.最短子数组之和)
    [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和 Given an array of n positive in...
    99+
    2024-04-02
  • C++实现LeetCode(77.Combinations 组合项)
    [LeetCode] 77.Combinations 组合项 Given two integers n and k, return all possib...
    99+
    2024-04-02
  • C++实现LeetCode(164.求最大间距)
    [LeetCode] 164. Maximum Gap 求最大间距 Given an unsorted array, find the maximum difference betw...
    99+
    2024-04-02
  • C++实现LeetCode(769.可排序的最大块数)
    [LeetCode] 769.Max Chunks To Make Sorted 可排序的最大块数 Given an array arr that is a pe...
    99+
    2024-04-02
  • C++怎么获得最大组合数
    这篇文章主要介绍“C++怎么获得最大组合数”,在日常操作中,相信很多人在C++怎么获得最大组合数问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++怎么获得最大组合数”的疑惑有所帮助!接下来,请跟着小编一起来...
    99+
    2023-06-20
  • C++实现LeetCode(88.混合插入有序数组)
    [LeetCode] 88. Merge Sorted Array 混合插入有序数组 Given two sorted integer arrays nums1 ...
    99+
    2024-04-02
  • c++求数组最大最小值函数的实现
    目录求数组元素最大最小值函数c++中min和max函数求数组元素最大最小值函数 #include<iostream> #include<algorithm> ...
    99+
    2024-04-02
  • C++实现LeetCode(40.组合之和之二)
    [LeetCode] 40. Combination Sum II 组合之和之二 Given a collection of candidate numbers (candidate...
    99+
    2024-04-02
  • C++实现LeetCode(768.可排序的最大块数之二)
    [LeetCode] 768.Max Chunks To Make Sorted II 可排序的最大块数之二 This question is the same as "Max Ch...
    99+
    2024-04-02
  • C++实现LeetCode(169.求大多数)
    [LeetCode] 169. Majority Element 求大多数 Given an array nums of size n, return&...
    99+
    2024-04-02
  • C++实现LeetCode(189.旋转数组)
    [LeetCode] 189. Rotate Array 旋转数组 Given an array, rotate the array to the right by k&#...
    99+
    2024-04-02
  • C++实现LeetCode数组练习题
    目录1、存在重复元素2、最大子序和3、两数之和4、合并两个有序数组5、两个数组的交集II6、买卖股票的最佳时机7、杨辉三角8、重塑矩阵9、有效的数独10、矩阵置零总结1、存在重复元素...
    99+
    2024-04-02
  • C++中怎么利用LeetCode求最大子数组乘积
    这期内容当中小编将会给大家带来有关C++中怎么利用LeetCode求最大子数组乘积,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。[LeetCode] 152. Maximum Product Subarr...
    99+
    2023-06-20
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作