- 浏览: 173354 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
给定一个数组,里面存放的都是非负整数,要求我们用里面的元素组成一个最大的数。这里我们首先将数组转换成一个一个字符串数组,这样我们就可以自定义Arrays.sort方法了,我们用到了Comparator接口,通过重写compare方法,来得到一个最大的数。关键就是compare的实现,我们知道compare有两个参数p1, p2, 如果p1 < p2时返回-1, p1 = p2返回0,p1 > p2返回1。在这道题目中,我们将两个参数先相加s1 = p1 + p2, s2 = p2 + p1,这样得到两个新的参数。为了更清楚的解释,我们假设此时p1 = "34", p2 = "56", 所以s1 = "3456" , s2 = "5634"。按照题目的要求我们要得到一个最大的数,这时我们希望p2和p1交换位置,因为s2 > s1, 所以我们应该返回1,因此我们在compare中返回s2.compareTo(s1)即可。代码如下:
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.
给定一个数组,里面存放的都是非负整数,要求我们用里面的元素组成一个最大的数。这里我们首先将数组转换成一个一个字符串数组,这样我们就可以自定义Arrays.sort方法了,我们用到了Comparator接口,通过重写compare方法,来得到一个最大的数。关键就是compare的实现,我们知道compare有两个参数p1, p2, 如果p1 < p2时返回-1, p1 = p2返回0,p1 > p2返回1。在这道题目中,我们将两个参数先相加s1 = p1 + p2, s2 = p2 + p1,这样得到两个新的参数。为了更清楚的解释,我们假设此时p1 = "34", p2 = "56", 所以s1 = "3456" , s2 = "5634"。按照题目的要求我们要得到一个最大的数,这时我们希望p2和p1交换位置,因为s2 > s1, 所以我们应该返回1,因此我们在compare中返回s2.compareTo(s1)即可。代码如下:
public class Solution { public String largestNumber(int[] nums) { StringBuilder sb = new StringBuilder(); if(nums == null || nums.length == 0) return null; String[] str = new String[nums.length]; for(int i = 0; i < nums.length; i++) str[i] = String.valueOf(nums[i]); Arrays.sort(str, new Comparator<String>() { public int compare(String i, String j) { String s1 = i + j; String s2 = j + i; return s2.compareTo(s1); } }); for(String s : str) sb.append(s); if(str[0].equals("0")) return "0"; return sb.toString(); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 227Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 226You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 342Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 333Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 451Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 511Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 430Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 619Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 428The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 387Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 517Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 531Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 371All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 861Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 883Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 555Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 601Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 764Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 735You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 629For a undirected graph with tre ...
相关推荐
代码
习题答案,过程详细,仅供参考。 摘录文档片段如下: 1-6. #include int main() {int a,b,c,max; printf("please input a,b,c:\n");... printf("The largest number is %d\n",max); return 0; }
在JavaScript中可以通过内置的 Math.max() 的最大值,但是要从多重数组中取出最大值,还是有一定的难度。 问题描述 假设你有一个数组,而且这个数组中...var largestNumber = 0; // 创建第二个变量,存储最大的数 //
MES2 DB 0AH,0DH, The largest number is:$ NUMB DB 0D9H,07H,8BH,0C5H,0EBH,04H,9DH,0F9H DDATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DDATA START: MOV AX,DDATA MOV DS,AX MOV SI,OFFSET NUMB MOV CX,...
#include <stdio.h> int main ( ) { printf ("**************************\n\n"); printf(" Very Good!\n\n"); ... printf ("**************... printf("The largest number is %d\n",max); return 0; }
Largest Number 小结 查找 Search for a Range Search Insert Position Search in Rotated Sorted Array Search in Rotated Sorted Array II Search a 2D Matrix Search a 2D Matrix II Find Minimum in Rotated ...
Description Now, there are a list of N numbers. Your job is to find the Mth largest number X. (there are M-1 numbers >= X)
println(“ The largest number is : “ + largestNumber(number1, number2)) 创建一个largestNumber方法,该方法将返回传入的2中最大的数字匹配密码检查器给定一个主要功能: Scanner scanInput = new Scanner ( ...
The ECMAScript 5 specification forms the basis of TypeScript and supplies the largest number of features in the language. Subsequent versions of the ECMAScript specification are rolled into ...
Use the largest number of mail system in enterprise. mail system based on Postfix/Sendmail is in need of a strong technical force to realize, but performance can reach very high, but very good ...
Use the largest number of mail system in enterprise. mail system based on Postfix/Sendmail is in need of a strong technical force to realize, but performance can reach very high, but very good ...
The number of questions is increasing recently. Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) ...
if (n1 >= n2 && n1 >= n3) printf("%.2f is the largest number.", n1); if (n2 >= n1 && n2 >= n3) printf("%.2f is the largest number.", n2); if (n3 >= n1 && n3 >= n2) printf("%.2f is the largest number....
对于Steam用户, /steamapps/common/World of Warships/bin/[largest number]/res_mods/ content复制到/steamapps/common/World of Warships/bin/[largest number]/res_mods/ 使用方法 WG游戏中心用户请把content放...
[仅限美联社] Fate Excalibur / EX咖喱棒 AP外壳模型修改安装对于WGC用户,将content复制到[WoWS folder]/bin/[largest number]/res_mods/对于Steam用户, /steamapps/common/World of Warships/bin/[largest number...
This approach has the lowest hardware and backup costs, but it has the highest sharing, because it allows you to serve the largest number of users per database server. Some proxy industry like ...
You should try this. this is very clear answer. The question is about printing the kth largest number in the array
由SEA集团主办安装对于WGC用户,将gui PnFMods和PnFModsLoader.py复制到[WoWS folder]/bin/[largest number]/res_mods/对于Steam用户, PnFMods gui PnFMods和PnFModsLoader.py复制到/steamapps/common/World of ...
对于Steam用户, PnFMods和PnFModsLoader.py复制到/steamapps/common/World of Warships/bin/[largest number]/res_mods/ 使用方法 WG游戏中心用户请把PnFMods和PnFModsLoader.py放到[游戏安装目录]/bin/[数字最大...