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. 安装MYSQL时出现libaio.so.1 is needed by MySQL-server-5.5,49.1等等

    安装MYSQL是出现: warning: MySQL-server--.linux2..i386.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: ...

  2. GCE 创建一个Linux VM

    sudo yum install wget 安装Java sudo wget --no-check-certificate --no-cookies --header "Cookie: or ...

  3. Google Cloud Platfrom中运行基础的Apache Web服务

    Links: https://cloud.google.com/compute/docs/tutorials/basic-webserver-apache 步骤: 1.安装Apache 2.重写Apa ...

  4. R中的参数传递函数:commandArgs(),getopt().

    1.commandArgs(),是R自带的参数传递函数,属于位置参数. ##test.R args=commandArgs(T) print (args[1])##第一个外部参数 print (arg ...

  5. 各种排序算法-用Python实现

    冒泡排序 # 冒泡排序 def bubble_sort(l): length = len(l) # 外层循环 length遍,内层循环少一遍 while length: for j in range( ...

  6. 多校hdu5738 寻找

    这道题前面给了两个公式,其实仔细分析一下,就会发现其实是给了你一堆点的坐标,然后让你求这些点有多少种组合可以形成共线的情况当两个点在一个坐标上时这两个点可以看做是不同的两个点,也就是说如果两个点在一个 ...

  7. spring配置hibernate映射文件-------通配符

    <!-- 这里一定要注意是使用spring的mappingLocations属性进行通配的 -->      <property name="mappingLocation ...

  8. jar 冲突解决方案

    val urlOfClass = classOf[Nothing].getClassLoader.getResource("org/slf4j/spi/LocationAwareLogger ...

  9. DelphiXE_画图

    1.基本 DelphiXE FireMonkey 如何画图 http://www.delphitop.com/html/FireMonkey/2647.html 2. 3.

  10. Liberty glance 新功能 healthcheck

    oslo.middleware‘s healthcheck http://specs.openstack.org/openstack/oslo-specs/specs/kilo/oslo-middle ...