LeetCode——Largest Number
Description:
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.
把所给的数字排列成最大的数,首先想到的思路是按位比较,但是那样比较麻烦而且容易出BUG,所以还有一种简单的思路就是在排序的时候比较两种情况前后排列取结果最大的那种顺序。代码少好理解。
public class Solution {
public String largestNumber(int[] nums) {
ArrayList<Number> list = new ArrayList<Number>();
for(int i : nums) {
list.add(new Number(i+""));
}
Collections.sort(list);
StringBuilder sb = new StringBuilder();
for(int i=list.size()-1; i>=0; i--) {
sb.append(list.get(i).val);
}
java.math.BigInteger b = new java.math.BigInteger(sb.toString());
return b.toString();
}
}
class Number implements Comparable<Number> {
String val;
public Number(String val) {
this.val = val;
}
public int compareTo(Number n) {
java.math.BigInteger a = new java.math.BigInteger(this.val + n.val);
java.math.BigInteger b = new java.math.BigInteger(n.val + this.val);
return a.compareTo(b);
}
}
LeetCode——Largest Number的更多相关文章
- [LeetCode] Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Leetcode Largest Number c++ solution
Total Accepted: 16020 Total Submissions: 103330 Given a list of non negative integers, arrange t ...
- LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用
Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...
- [LeetCode] Largest Number 排序
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [LeetCode] Largest Number At Least Twice of Others 至少是其他数字两倍的最大数
In a given integer array nums, there is always exactly one largest element. Find whether the largest ...
- LeetCode() Largest Number
全排列,超时,知道超时,只是想验证一下. class Solution { public: string largestNumber(vector<int>& nums) { so ...
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...
- Leetcode:Largest Number详细题解
题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...
- [LeetCode][Python]Largest Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...
随机推荐
- [小技巧]diff的文件夹忽略使用方式
当我们比较两个文件夹时经常需要忽略.svn或者.git,那么如下 diff -r -x ".git" -x "*.ko" -x "*.o" ...
- 【Hibernate步步为营】--核心对象+持久对象全析(二)
上篇文章讨论了Hibernate的核心对象,在开发过程中经经常使用到的有JTA.SessionFactory.Session.JDBC,当中SessionFactory可以看做数据库的镜像,使用它可以 ...
- expect模块的使用,主要没装包折腾一晚上
第一步首先下载expect 模块,yum list |grep expect ,安装下面的模块. expect.x86_64 5.44.1. ...
- 深入浅出Cache
章节 ① 什么是Cache? Cache的目标? ② Caching住哪些内容? ③ 我们想要的Cache产品 ④ Cache使用方式 ⑤ 对于总体系统的提高 ⑥ 关于Sharding ⑦ Cache ...
- bootstrap首页案例
<html><head> <meta http-equiv="Content-Type" content="text/html; chars ...
- SQL SERVER 2005快捷键
一.SQL SERVER 2005快捷键 快捷键 功能 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O打开项目 ...
- 关于Cocos2d-x中掉帧导致游戏一卡一卡的网上一些的解决方法
方法1 掉帧主要是setpostion引起的 因为每一帧每一个精灵都要set一次虽然不知道为什么会这样但是if(poX<1000&&pox>-100){ xx-& ...
- ubuntu开启SSH服务远程登录
http://blog.csdn.net/jackghq/article/details/54974141 ubuntu开启SSH服务远程登录
- 【转】【C#】ZIP、RAR 压缩与解压缩
压缩文件夹 源码如下 using System; using System.Data; using System.Configuration; using System.Web; using Syst ...
- C++中 char *s 和 char s[] 的区别
原因 刚好看到给main传递参数,书上(C++Primer)说“ int main(int argc, char *argv[])也可以写成 int main(int argc, char **arg ...