179. Largest Number(INT, String)
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.
思路:
要对数字进行排列,首位大的在前。
比较每个数字的首位,这在不知道位数的情况下,很难实现,所以将INT转换成string再比较。
由于string的连接操作非常方便,所以比较首位的大小,可以转化成比较连接后string的大小。
class Solution {
public:
static bool cmp(const string &s1, const string &s2){ //注意static,const,引用的使用
return (s1+s2) > (s2+s1); // >表示按降序排列
}
string largestNumber(vector<int>& nums) {
int size = nums.size();
stringstream ss;
vector<string> s_nums(size);
string ret = "";
for(int i = ; i < size; i++){
ss << nums[i];
ss >> s_nums[i];
ss.clear();
}
sort(s_nums.begin(), s_nums.end(),cmp);
if(s_nums[]=="") return s_nums[];
for(int i = ; i < size; i++){
ret += s_nums[i];
}
return ret;
}
};
179. Largest Number(INT, String)的更多相关文章
- 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 ...
- 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 ...
- 179. Largest Number -- 数字字符串比较大小
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 179. Largest Number
题目: Given a list of non negative integers, arrange them such that they form the largest number. For ...
- [LeetCode] 179. Largest Number 解题思路
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 ...
随机推荐
- ps记录
图层 ctrl+j:通过拷贝的图层 颜色填充 alt+delete:前景色填充(或alt+backspace) ctrl+delete:背景色填充(或ctrl+backspace) 多图层合并一 分组 ...
- ANg-基础概念
分类 机器学习可以分为两类:监督学习(Supervised Learning)和无监督学习(Unsupervised Learning) 监督学习 Supervised Learning 监督学习是从 ...
- ArcGIS案例学习笔记2_1_学校选址适宜性分析
ArcGIS案例学习笔记2_1_学校选址适宜性分析 计划时间:第二天上午 目的:学校选址,适宜性分析 内容:栅格数据分析 教程:pdf page=323 数据:chapter8/ex1/教育,生活,土 ...
- C++ 关于MFC多线程编程中的一些注意事项 及自定义消息的处理
在多线程编程中,最简单的方法,无非就是利用 AfxBeginThread 来创建一个工作线程,看一下这个函数的说明: CWinThread* AFXAPI AfxBeginThread( AFX_T ...
- C# windows服务:创建Windows服务(Windows Services)的一般步骤
C#创建Windows服务(Windows Services) Windows服务在Visual Studio 以前的版本中叫NT服务,在VS.net启用了新的名称.用Visual C# 创建Wind ...
- web.xml中的load-on-startup
1)load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法). 2)它的值必须是一个整数,表示servlet应该被载入的顺序 2)当值为0或 ...
- 基于WebQQ3.0协议写一个QQ机器人
最近公司需要做个qq机器人获取qq好友列表,并且能够自动向选定的qq好友定时发送消息.没有头绪,硬着头皮上 甘甜的心情瞬间变得苦涩了 哇 多捞吆 1.WEBQQ3.0登陆协议 进入WEBQQ, htt ...
- yii Nav:widget 配置参数encodeLabels
echo Nav::widget([ 'options' => ['class' => 'navbar-nav navbar-right'], 'encodeLabels' => f ...
- Appium appium 安装不了
npm --registry http://registry.cnpmjs.org install -g appium使用npm的国内镜像可以安装,速度很不错.以后不想输入ip的话可以输入以下命令:n ...
- linux安装mysql5.1
一.卸载mysql 1.检测系统是否已经安装过mysql或其依赖,若已装过要先将其删除 # yum list installed | grep mysql mysql-libs.i686 ...