Largest Number——LeetCode
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然后写comparator,首先比较第一位数,然后比较第二位……后来想了一下其实直接把两个数转为String,然后分别前后拼接一下就可以比较了。直接sort就可以得到结果,代码更清晰。或者定义一个PriorityQueue,自定义comparator,把给定的数组转为这个queue也是一样的。
public String largestNumber(int[] nums) {
if (nums == null || nums.length < 1) {
return null;
}
LinkedList<String> res = new LinkedList<>();
for (int num : nums) {
res.add(String.valueOf(num));
}
StringBuilder sb = new StringBuilder();
Collections.sort(res, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String s1 = o2 + o1;
String s2 = o1 + o2;
return s1.compareTo(s2);
}
});
for (String next : res) {
sb.append(next);
}
int cnt = 0;
while (cnt < sb.length() && sb.charAt(cnt) == '0') {
cnt++;
}
return sb.substring(cnt == sb.length() ? cnt - 1 : cnt);
}
Largest Number——LeetCode的更多相关文章
- Largest Number || LeetCode
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 1000 int cm ...
- [LeetCode] Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 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 ...
- LeetCode之“排序”:Largest Number
题目链接 题目要求: Given a list of non negative integers, arrange them such that they form the largest numbe ...
- 【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 、剑指offer33 把数组排成最小的数
这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...
- [LeetCode] 179. Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. Example ...
随机推荐
- [转] linux下的c/c++调试器gdb
PS:1. 断点C++类函数,用b 命名空间::类名::方法名 2. 编译参数一定要加-g,才可断点调试 http://www.cnblogs.com/xd502djj/archive/2012/08 ...
- [转] __thread关键字
http://blog.csdn.net/liuxuejiang158blog/article/details/14100897 __thread是GCC内置的线程局部存储设施,存取效率可以和全局变量 ...
- Ubuntu设置环境变量的几种方法
1.Linux的变量种类 按变量的生存周期来划分,Linux变量可分为两类: 1.1 永久的:需要修改配置文件,变量永久生效. 1.2 临时的:使用export命令声明即可,变量在关闭shell时失效 ...
- JDK5-静态导入
import static 1. 导入一个类内所有静态成员 import static java.lang.Math.*; public class StaticImport { public sta ...
- Could not fetch https://api.github.com/repos/RobinHerbots/jquery
使用 composer 安装YII2时, 如题所示提示, 原因是由于yii安装中, 需要有一些相关的认证[或许说是composer的认证], 如有如下提示 Could not fetch https: ...
- Android 连接 SQL Server (jtds方式)——上
本文将介绍开发Android程序,连接SQL Server,通过第三方包jtds的方式. 如果你有同样的需求,请跟着做一遍,博主将以最详细的方式,进行介绍. 首先说明,Java.Android连接SQ ...
- codevs 1378 选课 (树形DP)
#include<iostream> #include<cstdio> #include<cstring> using namespace std; ][],f[] ...
- SQL 生成可配置流水号
需求背景每执行一次方法,根据公式返回最新的流水号.第一次使用时需要先插入一条数据,BizSeqValue 为流水起始号:A2014030000,Formula 为公式:A[yyyy][mm][c4], ...
- Nginx反向代理配置配置实例
为了节省支出,公司需要将分布在不同机器的站点都迁移到一台机器,而目前不同机器运行的是不同的web服务,部分是nginx,部分是apache,由于牵涉较多rewrite规则,为了节省修改功夫,打算迁移后 ...
- DbProviderFactories.GetFactory Oracle.ManagedDataAccess.Client
因为最近项目,要使用微软的EF框架不安装Oracle客户端的情况下,访问Oracle数据库.调用如下代码的时候会报错. System.Data.Common.DbProviderFactories.G ...