LeetCode OJ: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 {
public:
string largestNumber(vector<int>& nums) {
stringstream ss;
vector<string> numStr;
numStr.clear();
ss.str("");
int sz = nums.size();
for(int i = ; i < sz; ++i){
ss << nums[i];
numStr.push_back(ss.str());
ss.str(""); //清空ss
}
sort(numStr.begin(), numStr.end(), Compare);
string ret = "";
for(int i = ; i < sz; ++i){
ret += numStr[i];
}
if(ret[] == '')
ret = "";
return ret;
}
static bool Compare(string s1, string s2)
{
string res1 = s1 + s2;
string res2 = s2 + s1;
return res1 > res2;
}
};
java版本的如下所示,由于String的处理比较方便,可以直接的将Int型转换成一个String,Comparator对象使用起来也较为容易,代码如下所示:
public class Solution {
public String largestNumber(int[] nums) {
int sz = nums.length;
String [] numStr = new String[sz];
for(int i = ; i < sz; ++i){
numStr[i] = String.valueOf(nums[i]);
}
Arrays.sort(numStr, new Comparator<String>(){ //这里就不用单独的去创造一个函数对象了,直接new一个使用就可以了
public int compare(String s1, String s2){
String tmp1 = s1+s2;
String tmp2 = s2+s1;
return tmp2.compareTo(tmp1);
}
});
String ret = new String("");
for(int i = ; i < sz; ++i){
ret += numStr[i];
}
if(ret.charAt() == '')
ret = "";
return ret;
}
}
LeetCode OJ:Largest Number(最大数字)的更多相关文章
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- Leetcode:Largest Number详细题解
题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...
- [LeetCode][Python]Largest Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...
- [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 ...
- 【leetcode】Largest Number ★
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. Input: [ ...
随机推荐
- 字符串之strchr
功能:查找字符在字符串中第一次出现的位置. #include <iostream> #include <assert.h> using namespace std; char ...
- Log4j详细配置解释
原文地址:https://www.cnblogs.com/godtrue/p/6444158.html log4j(七)——log4j.xml简单配置样例说明 一:测试环境与log4j(一)——为什么 ...
- 剑指offer 面试29题
面试29题: 题目:顺时针打印矩阵(同LeetCode 螺旋矩阵打印) 题:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 ...
- LeetCode:整数转罗马数字【12】
LeetCode:整数转罗马数字[12] 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 10 ...
- CentOS 6.4下OpenSSH升级到6.7操作
一.升级前准备 1.下载openssh-6.7p1.tar.gz: cd /usr/local/src/wget http://ftp.openbsd.org/pub/OpenBSD/OpenSSH/ ...
- Loadrunder之脚本篇——事务时间简介
事务概念 事务是指用户在客户端做一种或多种业务所需要的操作集(actions),通过事务开始和结束函数可以标记完成该业务所需要的操作内容(脚本section).定义事务来衡量服务器的性能,例如,你可以 ...
- Python学习进程(1)Python简介
Python是一种结合了"解释性"."编译性"."互动性"和"面向对象"的脚本语言. (1)官方介绍: Pyth ...
- 微信小程序相关资料整理
微信小程序官方介绍https://mp.weixin.qq.com/debug/wxadoc/introduction/index.html?t=201818 微信小程序开发资源https://jue ...
- libhdfs的配置和使用
测试环境:centos6.10,hadoop2.7.3,jdk1.8 测试代码:HDFSCSample.c #include "hdfs.h" #include <strin ...
- P4755 Beautiful Pair
题目 洛谷 做法 \(i≤x≤j,a[i]<\frac{a[x]}{a[j]}\) 考虑\(a[x]\)的贡献,单调栈预处理\(L,R\)能作为最大值的区间 枚举一端点,仅需另一端点满足条件即可 ...