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 ...
随机推荐
- Qt 学习之路 :Qt 模块简介
Qt 5 与 Qt 4 最大的一个区别之一是底层架构有了修改.Qt 5 引入了模块化的概念,将众多功能细分到几个模块之中.Qt 4 也有模块的概念,但是是一种很粗的划分,而 Qt 5 则更加细化.本节 ...
- ASP.NET-FineUI开发实践-12
1.网上找到了行合并的示例,extjs写的,我把它挪过来改了下,FineUI也能用,就是只能放着看,选择和编辑行扩展列没有测试,放出来大家看着用吧. <script> F.ready(fu ...
- .NET六大剑客:栈、堆、值类型、引用类型、装箱和拆箱
.NET六大剑客:栈.堆.值类型.引用类型.装箱和拆箱 一.“堆”,“栈”专区 这两个字我相信大家太熟悉了,甚至于米饭是什么?不知道...“堆”,“栈”是什么?哦,这个知道... 之前我也写过一篇堆栈 ...
- 关于uploadify无法起作用,界面没有效果出现
<link href="<%: Url.Content("~/Res/uploadify/uploadify.css") %>" rel=&q ...
- ExcelApplication 另存Excel的SaveAs函数
procedure SaveAs(const Filename: WideString; FileFormat: OleVariant; Password: OleVariant; WriteResP ...
- javascript学习(知识点整理)
有了这个代码,就可以在定义 中增加更多的控制了 后面会举例关于extjs定义的更多控制 此种方案可以解决定义时需要一些函数调用的情况 函数作用域和声明提前: 即由于js是解释性语言,在执行前会 ...
- bash shell学习-shell script基础 (笔记)
A chain no stronger than its weakest link. "一着不慎,满盘皆输" 参考资料:鸟哥的Linux私房菜 基础学习篇(第三版) Linux ...
- Java学习----反复做某件事情
for循环: public class TestFor{ public static void main(String[] args){ for(int x = 1; x < 3; x++) { ...
- 你真的了解console吗?
对于前端开发者来说,在开发过程中需要监控某些表达式或变量的值的时候,用 debugger 会显得过于笨重,取而代之则是会将值输出到控制台上方便调试.最常用的语句就是console.log(expres ...
- underscorejs-pluck学习
2.14 pluck 2.14.1 语法: _.pluck(list, key) 2.14.2 说明: pluck方法根据key对list数组中的每个对象进行检索,返回检索成功的属性值,否则返回und ...