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.
public class Solution { public String largestNumber(int[] num) { String result=""; int length=num.length; for(int i=0;i<length;i++){ int max=num[i]; int pos=i; for(int j=i;j<length;j++){ if((max+""+num[j]).compareTo(num[j]+""+max)<0){ max=num[j]; pos=j; } } if(pos!=i){ int temp=num[pos]; num[pos]=num[i]; num[i]=temp; } } for(int item:num){ result+=item; } if(result.startsWith("0")) result="0"; return result; } }
基本思路:排序,序列为[a,b] 排序的标准是当ab<ba时则两个数字交换。序列变成[b,a]
最后一个判断是防止[0,0] 的序列得出的结果是00.
相关推荐
扩展矩阵leetcode Leetcode Leetcode Answer-Java 数组 11.乘最多水容器 maxArea 26.删除排序数组中的重复项 removeDuplicates 33.搜索旋转排序数组 ...34.在排序数组中查找元素的第一个和最后一个...largestNumber 324.摆
191 |[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [C++](./C++/number-of-1-bits.cpp) [Python](./Python/number-of-1-bits.py) | _O(1)_ | _O(1)_ | Easy ||| 201 | [Bitwise AND of ...
Largest Rectangle in Histogram:单调栈 Island Perimeter:简单对比(map+zip的使用) or 遍历查找 Max Area of Island:DFS(本来想用DP,发现出不来) Number of Islands:DFS My Calendar II:小空间匹配 My ...
leetcode寻找最近的 设计模式 ...随时随记 随记笔记 ...最大数(golang)](Alg/leetcode/largestNumber.go [190. 颠倒二进制位(golang)](Alg/leetcode/reverseBits.go Docker 面试题 GO面试题 Redis面试题
leetcode 答案 easy_Maximum-Subarray 提交链接 / Submit (You need register/login first before submit.) (在提交前你需要先注册或登录) 题目描述 / Description Given an integer array nums, find the ...
largest element from an unsorted array in linear time. (1) If the number of elements in an array is large .Divide the array into arrays each with 5 elements. n/5 arrays. (2) Compute Median of these 5 ...
Number Leetcode 75. Sort Colors Leetcode 215. Kth Largest Element Leetcode 4. Median of Two Sorted Arrays 注意:后两题是与快速排序非常相似的快速选择(Quick Select)算法,面试中很常考 链表类(Linked ...
1. Introduction ... Largest Rectangle in Histogram ix. Maximal Rectangle x. Palindrome Number xi. Search a 2D Matrix xii. Search for a Range xiii. Search Insert Position xiv. Find Peak Element
加油站 leetcode 力码 该文件夹不仅包含算法和数据库的leetcode问题,还包含其他公开...largest distance from the sum of any subarray to the average of subarray (sum(a[n])/(k+1)). Output: a number of the Im
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 ...
leetcode Java 246 題目及解答 (英文) Contents 1 Rotate Array in Java 15 2 Reverse Words in a String II 19 3 Evaluate Reverse Polish Notation 21 4 Isomorphic Strings 25 5 Word Ladder 27 6 Word Ladder ...