C++实现LeetCode(179.最大组合数)
[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++实现最大组合数内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
相关推荐
-
C++实现LeetCode(166.分数转循环小数)
[LeetCode] 166.Fraction to Recurring Decimal 分数转循环小数 Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. Fo
-
C++实现LeetCode(173.二叉搜索树迭代器)
[LeetCode] 173.Binary Search Tree Iterator 二叉搜索树迭代器 Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and
-
C++实现LeetCode(169.求大多数)
[LeetCode] 169. Majority Element 求大多数 Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. Example 1
-
C++实现LeetCode(168.求Excel表列名称)
[LeetCode] 168.Excel Sheet Column Title 求Excel表列名称 Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ... Example 1: Input: 1 O
-
C++实现LeetCode(170.两数之和之三 - 数据结构设计)
[LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计 Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure. find - Find if there exists any pai
-
C++实现LeetCode(167.两数之和之二 - 输入数组有序)
[LeetCode] 167.Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序 Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of t
-
C++实现LeetCode(172.求阶乘末尾零的个数)
[LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数 Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explanation: 3! = 6, no trailing zero. Example 2: Input: 5 Output: 1 Explanation: 5! = 120, one trailing
-
C++实现LeetCode(171.求Excel表列序号)
[LeetCode] 171.Excel Sheet Column Number 求Excel表列序号 Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -&g
-
C++实现LeetCode(179.最大组合数)
[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
-
基于Python数据结构之递归与回溯搜索
目录 1. 递归函数与回溯深搜的基础知识 2. 求子集 (LeetCode 78) 3. 求子集2 (LeetCode 90) 4. 组合数之和(LeetCode 39,40) 5. 生成括号(LeetCode 22) 6. N皇后(LeetCode 51,52) 7. 火柴棍摆正方形(LeetCode 473) 1. 递归函数与回溯深搜的基础知识 递归是指在函数内部调用自身本身的方法.能采用递归描述的算法通常有这样的特征:为求解规模为N的问题,设法将它分解成规模较小的问题,然后从这些小问题的解
-
C++实现LeetCode(Combinations 组合项)
[LeetCode] Combinations 组合项 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 这道题让求1到n共n个数字里k个数的组合数的所有情况,还是要用深度优先搜索D
-
C++实现LeetCode(62.不同的路径)
[LeetCode] 62. Unique Paths 不同的路径 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner
-
C++实现LeetCode(119.杨辉三角之二)
[LeetCode] 119. Pascal's Triangle II 杨辉三角之二 Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note that the row index starts from 0. In Pascal's triangle, each number is the sum of the two numbers directly
-
LeetCode -- Path Sum III分析及实现方法
LeetCode -- Path Sum III分析及实现方法 题目描述: You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must go downwards
-
基于Java实现杨辉三角 LeetCode Pascal's Triangle
Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 这道题比较简单, 杨辉三角, 可以用这一列的元素等于它头顶两元素的和来求. 数学扎实的人会看出, 其实每一列都是数学里的排列组合, 第4行, 可以用 C30 =
-
asp.net求3位不同数字的组合数
简单的: 复制代码 代码如下: public partial class _Default : System.Web.UI.Page { string m1 = ""; protected void Page_Load(object sender, EventArgs e) { string n = "123"; string m = ""; zuhe(n);
-
C语言回溯法 实现组合数 从N个数中选择M个数
前言 在平时的算法的题目中,时常会遇到组合数相关的问题,暴力枚举.在N个数中挑选M个数出来.利用for循环也可以处理,但是可拓展性不强,于是写这个模板供以后参考. 两个函数和全局变量可以直接用. 代码: #include<iostream> #include<cstdio> #define N 10 //被选择的数目 #define M 5 //要选出来的数目 using namespace std; int vis[N+1]; //标志, int ans=0;
-
Java面试之动态规划与组合数
最近在刷力扣上的题目,刷到了65不同路径,当初上大学的时候,曾在hihocoder上刷到过这道题目,但是现在已经几乎全忘光了,大概的知识点是动态规划,如今就让我们一起来回顾一下. 从题目说起 题目原文是: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为"Finish"). 问总共有多少条不同的路径? 例如,上图是一个7 x 3 的网格.有多少可能
随机推荐
- 通过数据库对Django进行删除字段和删除模型的操作
- SQLite教程(五):索引和数据分析/清理
- 详解mysql索引总结----mysql索引类型以及创建
- 仿CSDN 右下角悬挂的浮动层效果
- Android HorizontalScrollView左右滑动效果
- python 实现堆排序算法代码
- DropDownList设置客户端事件思路
- ASP.NET数据绑定之Repeater控件
- C#中Invoke 和 BeginInvoke 的真正涵义
- 如何在标题栏显示框架内页面的标题
- mysql实现本地keyvalue数据库缓存示例
- 用JS实现图片轮播效果代码(一)
- Ajax 无刷新在注册用户名时判断是否为空是否被使用
- jQuery中:input选择器用法实例
- jquery判断复选框是否选中进行答题提示特效
- python获得图片base64编码示例
- js 事件小结 表格区别
- Java禁止使用finalize方法
- javascript input输入框模糊提示功能的实现
- 删除MySQL数据库的简单教程