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. PyNN standard model(转)

    PyNN standard model 转自http://blog.csdn.net/qq_34886403/article/details/76667477

  2. springboot 常用的异常处理方式

    springboot常用的异常处理推荐: 一.创建一个异常控制器,并实现ErrorController接口: package com.example.demo.controller; import o ...

  3. python 文件格式为 txt 转换成 csv 格式

    1  txt 文件的读取 open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=Tr ...

  4. 反射,hashlib模块,正则匹配,冒泡,选择,插入排序

    一.反射(自省) 首先通过一个例子来看一下本文中可能用到的对象和相关概念. import sys # 模块,sys指向这个模块对象import inspectdef foo(): pass # 函数, ...

  5. python调用php函数

    由于php不支持多线程,所以想借助python搞一个.1.import subprocessimport time#Simple caller, disguard outputmethod=" ...

  6. FarBox的使用经历

    新年伊始,一个崭新的开始,我的博客也有个新的起点.怎么会有这个想法呢?个人觉得这是程序员那颗不安分的心开始躁动了(其实就是开始作了~~哈哈,开个玩笑). 更佳界面.更流畅的操作.更方便的查看.更炫酷动 ...

  7. Cookie用法简介

    java操作Cookie---javax.servlet.http.Cookie 1.增加一个Cookie Cookie cookie = new Cookie("username" ...

  8. windows7下手工搭建Apache2.2 php5.3 Mysql5.5开发环境

    Apache2.2(apache_2.2.2-win32-x86-no_ssl)php5.3.5(php-5.3.5-Win32-VC6-x86,请注意选择VC6版本,否则无法加载php5apache ...

  9. 【转】linux驱动开发

    转自:http://www.cnblogs.com/heat-man/articles/4174899.html 首先理一理驱动/内核/应用程序的一些概念,以前总没有具体的去关注过! 我们的pc直观来 ...

  10. linux内核调试+qemu+eclipse【转】

    本文转载自:https://blog.csdn.net/WANG__RONGWEI/article/details/54922727 一.调试环境: 在ubuntu16.04下,在虚拟机里边运行的ub ...