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(最大数字)的更多相关文章

  1. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  2. Leetcode:Largest Number详细题解

    题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...

  3. JavaScript中sort方法的一个坑(leetcode 179. Largest Number)

    在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...

  4. [LeetCode][Python]Largest Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...

  5. [LeetCode] 179. Largest Number 最大组合数

    Given a list of non negative integers, arrange them such that they form the largest number. Example ...

  6. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  7. leetcode 179. Largest Number 求最大组合数 ---------- java

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  8. 【leetcode】Largest Number ★

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  9. Java for LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  10. [leetcode]179. Largest Number最大数

    Given a list of non negative integers, arrange them such that they form the largest number. Input: [ ...

随机推荐

  1. 006-MySQL中使用SHOW PROFILE命令分析性能

    一.概述 1.版本支持 Show profiles是5.0.37之后添加的,要想使用此功能,要确保版本在5.0.37之后. 查看数据库版本: Select version(); 2.查看开启关闭和默认 ...

  2. Oracle学习笔记—数据字典和常用命令(转载)

    转载自: oracle常用数据字典和SQL语句总结 Oracle常用命令大全(很有用,做笔记) 一.Oracle数据字典 数据字典是Oracle存放有关数据库信息的地方,其用途是用来描述数据的.比如一 ...

  3. hadoop linux 杂记

    切换到root        su    修改sudo sudo + 命令 --> root权限 + 命令        su root        vim /etc/sudoers      ...

  4. facebook开源了他们的分布式大数据DB

    https://github.com/facebook/presto facebook 3天前开源了他们的 分布式大数据DB Distributed SQL query engine for big ...

  5. 【LeetCode】【矩阵旋转】Rotate Image

    描述 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...

  6. PHP 数字转大写

    <?php header("content-type:text/html;charset=utf-8"); function numToRmb($num){ $rmbNum ...

  7. 嵌入式boa服务器移植

    开发板:EDUKIT-III实验箱,S3C2410+LINUX2.4,实验箱随箱光盘提供的Zimage,nor flash启动. 主机:ubnutn10.4LTS,arm-linux-gcc 2.95 ...

  8. INSPIRED启示录 读书笔记 - 第17章 产品人物角色

    理解目标用户 人物角色又称为用户特征记录(user profile),是指通过与用户沟通交流,确定典型的目标用户类型,在理解各类目标用户的特征的基础上建立的人物原型 为了发掘潜在的人物角色,产品经理必 ...

  9. Linux mysql主从同步配置

    一.在两台Ubuntu机器上安装mysql1.检查系统中是否安装了mysql 这个是已经安装了的 没有安装的话执行上条命令===============================MySQL的一些 ...

  10. 【bzoj1925】地精部落[SDOI2010](dp)

    题目传送门:1925: [Sdoi2010]地精部落 这道题,,,首先可以一眼看出他是要我们求由1~n的排列组成,并且抖来抖去的序列的方案数.然后再看一眼数据范围,,,似乎是O(n^2)的dp?然后各 ...