【刷题-LeetCode】179 Largest Number
- 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.
解 使用排序,自定义比较函数:对于数字a, b
- 如果ab > ba,则a > b
- 如果ab < ba,则a < b
然后调用sort函数
class Solution {
public:
string largestNumber(vector<int>& nums) {
sort(nums.begin(), nums.end(), cmp);
string ans;
for(int x : nums)ans += to_string(x);
while(ans.size() > 1 && ans[0] == '0')ans.erase(0, 1);
return ans;
}
static bool cmp(int n1, int n2){
int len1 = 0, len2 = 0;
int tmp1 = n1, tmp2 = n2;
do{
len1++;
n1 /= 10;
}while(n1 != 0);
do{
len2++;
n2 /= 10;
}while(n2 != 0);
return tmp1 * pow(10, len2) + tmp2 > tmp1 + tmp2 * pow(10, len1);
}
};
【刷题-LeetCode】179 Largest Number的更多相关文章
- leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数
这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...
- [LeetCode] 179. Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. Example ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...
- leetcode 179. Largest Number 求最大组合数 ---------- java
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Java for LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [LeetCode] 179. Largest Number 解题思路
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [leetcode]179. Largest Number最大数
Given a list of non negative integers, arrange them such that they form the largest number. Input: [ ...
- LeetCode 179 Largest Number 把数组排成最大的数
Given a list of non negative integers, arrange them such that they form the largest number.For examp ...
- Leetcode 179 Largest Number 贪心
此题主要是讲给你一组数,如何将数连在一起能得到最大值(最小值反之),注意局部最优,就是说如果 123 234两个连在一起怎么样最大,显然是234123比123234大,对于3个数我们可以找到类似的性质 ...
随机推荐
- ViewModel的创建
ViewModel的创建 ViewModel本身只是ViewModel这个类的子类: class MainViewModel: ViewModel() { } 在屏幕旋转UI重建的时候, 它是如何拥有 ...
- org.apache.taglibs.standard.tlv.JstlBaseTLV.validate
exception org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP org.apache. ...
- 【LeetCode】1405. 最长快乐字符串 Longest Happy String
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcode ...
- Java中PO、DO、DTO、 VO、 BO、POJO 、DAO、TO的概念
1. PO(persistant object) 持久对象 在 O/R 映射的时候出现的概念,如果没有 O/R 映射,没有这个概念存在了. 通常对应数据模型 ( 数据库 ), 本身还有部分业务逻辑的 ...
- Essentially No Barriers in Neural Network Energy Landscape
目录 梗概 主要内容 path的定义 path的逼近 Mechanical Model Nudged Elastic Band 局部最优 Draxler F, Veschgini K, Salmhof ...
- SpringBoot集成log4j,解决log4j.properties不生效问题
Spring Boot集成log4j其实比较简单,maven的话,在xml中增加log4j依赖就行 <dependency> <groupId>org.springframew ...
- linux的用户主目录(~)指向问题
最近在Ubuntu上安装rabbitmq之后,发现~目录指向出问题了,原本~指向当前shell登录的用户主目录,如下图 现在变成了这样: 登录之后进入的不是~目录,而是直接显示的主目录,而~指向的目录 ...
- LCA/在线(倍增)离线(Tarjan)
概念 祖先 公共祖先 最近公共祖先 方法1:暴力爬山法 方法2:倍增 求公共祖先 求俩点的距离 Tarjan 概念 祖先 有根树中,一个节点到根的路径上的所有节点被视为这个点的祖先,包括根和它本身 公 ...
- python 安装包时提示“unsupport command install”
为什么提示找不到? 电脑安装了LoadRunnder,LoadRunner也有pip.exe,导致找不到python的exe 解决方法: 切换到python pip的路径进行安装,进到这个路径下,进行 ...
- pymysql防止SQL注入的方法
import pymysql class Db(object): def __init__(self): self.conn = pymysql.connect(host="192.168. ...