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.
class Solution {
static bool cmp(string s1, string s2) //字符串比较 GOOD!
{
return s1+s2 > s2+s1;
}
public:
string largestNumber(vector<int>& nums) {
vector<string> vs;
for(int i = ; i < nums.size(); i++)
vs.push_back(to_string(nums[i])); //int转string
sort(vs.begin(), vs.end(), cmp);
string s = "";
for(int i = ; i < vs.size(); i++)
s += vs[i];
if(s[] == '')
return "";
return s;
}
};
179. Largest Number -- 数字字符串比较大小的更多相关文章
- leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数
这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...
- 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 sort]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. Example ...
- leetcode 179. Largest Number 求最大组合数 ---------- java
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 179. Largest Number(INT, String)
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [leed code 179] Largest Number
1 题目 Given a list of non negative integers, arrange them such that they form the largest number. For ...
随机推荐
- COM/ATL 资料收集
COM/ATL COM基础知识 COM技术分类
- [转]or cad drc 错误
本文转自 恋上姐的博客 http://blog.sina.com.cn/u/1750715103 用“取缔”一词,是源自<嘻哈四重奏>里面卢导的口头禅,哈哈借用一下!大多数DRC warn ...
- 多命令顺序执行、管道符 ; && || |
多命令顺序执行:
- Java——Image 图片合并
1.合并图片 package com.tb.image; import java.awt.Image; import java.awt.image.BufferedImage; import java ...
- .net 浏览器请求过程(图)
大致: 细节: (信息来源于传智播客教学视频)
- linux 查看是否安装perl模块
这里介绍两种linux中查看perl模块是否安装的方法,一种是对于单体的模块,一种是对于群体的. 单体验证: [root@root ~]# perl -MShell -e "print\&q ...
- HTML5探索之datalist研究
最近一个项目需要用到类似淘宝,百度搜索时的自动检索方案.自然想到了使用datalist标签.但是遇到一个bug,经过两天研究.小有结果给大家分享下~~ 首先看bug吧!~ 因为项目最开始测试就是用36 ...
- hibernate配置文件中的catalog属性
在hibernate表的映射文件中 <hibernate-mapping> <class name="com.sooyie.hibernate.orm.Link&qu ...
- ffmpeg,X264编码结果I帧QP比P帧还大
enc_ctx->profile =FF_PROFILE_H264_MAIN ; enc_ctx->time_base.den = 24; enc_ctx->time_base.nu ...
- Mysql 组合查询 UNION 与 UNION ALL