[leetcode]179. Largest Number最大数
Given a list of non negative integers, arrange them such that they form the largest number.
Input: [10,2]
Output: "210"
Input: [3,30,34,5,9]
Output: "9534330"
题意:
给定一堆数字,将它们排列顺序,使其连一块儿能形成最大的数。
思路:
先转化成一个字符串数组
然后把这个数组按特定规则排个序(熟练使用字符串的compareTo方法)
最后建一个StringBuilder
将排好序的字符串按顺序加到StringBuilder后面(注意字符串开头可能为0的极端情况)
最后将StringBuilder转成String输出
代码:
class Solution {
public String largestNumber(int[] a) {
// int[] --> String[]
String[] array = new String[a.length];
for(int i = 0; i< a.length; i++){
array[i] = a[i] +"";
}
// sort string
Arrays.sort(array, (sA, sB)->(sB + sA).compareTo(sA + sB));
// string[] -> result
StringBuilder sb = new StringBuilder();
for (String s : array) {
sb.append(s);
}
return sb.charAt(0) == '0' ? "0" : sb.toString();
}
}
[leetcode]179. Largest Number最大数的更多相关文章
- 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 ...
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...
- Java 特定规则排序-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 求最大组合数 ---------- java
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Java for 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 解题思路
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- LeetCode 179 Largest Number 把数组排成最大的数
Given a list of non negative integers, arrange them such that they form the largest number.For examp ...
- Leetcode 179 Largest Number 贪心
此题主要是讲给你一组数,如何将数连在一起能得到最大值(最小值反之),注意局部最优,就是说如果 123 234两个连在一起怎么样最大,显然是234123比123234大,对于3个数我们可以找到类似的性质 ...
随机推荐
- Oracle中分页查询和联表查询
1.使用ROWNUM伪列查询 1.1.查询十条数据(rownum<=n) SELECT ROWNUM,A.* FROM v_sjjx_unit_info A WHERE ROWNUM<=1 ...
- Unreal Engine 4 笔记 2
转自:http://blog.csdn.net/st_dark/article/details/48005947 2.Actor继承自aactor,可以看成是一个容器,用来装"组件" ...
- 听听八年阿里架构师怎样讲述Dubbo和Spring Cloud微服务架构
转自:https://baijiahao.baidu.com/s?id=1600174787011483381&wfr=spider&for=pc 微服务架构是互联网很热门的话题,是互 ...
- Data Provider 中没有.net framework Data provider for Mysql 的解决方法
近来做的一个项目中,数据库用的是 MySql, 而在项目使用 Entity Data Model 来做数据服务层,可是在项目中添加 Data Entty Model 时,一般我们都会选择从数据库中直接 ...
- Python——连接操作数据库
1.安装MySQL驱动程序.下载自动安装包,双击安装即可,非常简单. 2.连接MySQL,下面是Python示例代码. import MySQLdbconn=MySQLdb.connect(host= ...
- nginx, supervisor
Nginx(单进程): 反向代理, 负载均衡.图解 将配置文件 nginx.conf 的 user xx 配置好 xx用户 检查语法 $ sudo service nginx configtest 重 ...
- MVC,MVP 和 MVVM 模式如何选择?
转摘:http://www.linuxidc.com/Linux/2015-10/124622.htm 前言 做客户端开发.前端开发对MVC.MVP.MVVM这些名词不了解也应该大致听过,都是为了解决 ...
- 0_Simple__simpleTexture + 0_Simple__simpleTextureDrv
使用纹理引用来旋转图片,并在使用了静态编译和运行时编译两种环境. ▶ 源代码:静态编译 #include <stdio.h> #include <windows.h> #inc ...
- linux装机首先需要关闭的服务
关闭selinux和iptables:setenforce 0 iptables -Fiptables -t nat -Fsystemctl stop firewalldsystemctl disab ...
- form表单提交参数封装
function getFormValues(element,options) { var data = {}; if(element == null || element == undefined) ...