输入一个非负整数数组numbers,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。
例如输入数组[3,32,321],则打印出这三个数字能排成的最小数字为321323。
1.输出结果可能非常大,所以你需要返回一个字符串而不是整数
2.拼接起来的数字可能会有前导 0,最后结果不需要去掉前导 0
2.拼接起来的数字可能会有前导 0,最后结果不需要去掉前导 0
数据范围:
0<=len(numbers)<=100
[11,3]
"113"
[]
""
[3,32,321]
"321323"
function PrintMinNumber(numbers)
{
let arr = numbers.sort((a,b)=>{
a = a.toString().split('')
b = b.toString().split('')
let length = a.length>b.length?b.length:a.length;
for(let i = 0;i<length;i++){
let aa = a[i]
let bb = b[i]
if(aa !== bb) return bb - aa
}
})
let res = ''
for(let i = arr.length-1;i>=0;i--){
res = res + arr[i]
}
return res
} function PrintMinNumber(numbers)
{
return numbers.sort((a, b) => Number(a.toString() + b.toString()) - Number(b.toString() + a.toString())).join('');
} function PrintMinNumber(numbers)
{
return numbers.sort((a, b) => +(a + '' + b) - +(b + '' + a)).join('');
}
function PrintMinNumber(numbers)
{
var str='';
for(var i = 0; i<numbers.length;i++)
{
for(var j = i+1;j<numbers.length;j++)
{
var a = parseInt(numbers[i]+""+numbers[j])
var b= parseInt(numbers[j]+""+numbers[i])
if(a > b)
{
var t = numbers[j];
numbers[j] = numbers[i];
numbers[i] = t;
}
}
}
return numbers.join('')
} function PrintMinNumber(numbers)
{
// write code here
numbers.sort((a,b)=>{
let p = (a + '').split('');
let q = (b + '').split('');
for(let i=0; i<p.length || i<q.length; i++){
if(i === p.length){
while(q[0] === q[i] && i < q.length-1){
q.shift();
}
return q[0] - q[i];
}else if(i === q.length){
while(p[0] === p[i] && i < p.length-1){
p.shift();
}
return p[i] - p[0];
}else if(p[i] !== q[i]){
return p[i] - q[i];
}
}
return 0;
})
return numbers.join('');
} function PrintMinNumber(numbers)
{
if(!numbers.length) return ''//空数组返回‘’
//冒泡排序的思想
for(var i=0;i<numbers.length-1;i++){
for(var j=0;j<numbers.length-i-1;j++){
//将比较的值转换成字符串
var a=numbers[j].toString()
var b=numbers[j+1].toString()
while(a.length && b.length){
//比较最高位,如果前面项比后面项要大,则交换位置
if(a[0]>b[0]){
[numbers[j],numbers[j+1]]=[numbers[j+1],numbers[j]]
break;
}
//长度都为1的时候结束
if(a.length==1 && b.length==1) break
//去最高位后面的数,如果本身长度为1,则不变
a=a.substring(1) || a
b=b.substring(1) || b
}
}
}
return Number(numbers.join(''))
} 语言:Javascript
function PrintMinNumber(numbers)
{
var minNum = '';
for(var i = 0; i < numbers.length; i++) {
for(var j = i + 1; j < numbers.length; j++) {
var a = numbers[i].toString() + numbers[j].toString();
var b = numbers[j].toString() + numbers[i].toString();
if(a > b) {
var tmp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = tmp;
}
}
}
for(var k = 0; k < numbers.length; k++) {
minNum += numbers[k];
}
return minNum;
} function compare(a,b){
var ab = a+""+b;
var ba = b+""+a;
for(var i=0;i<ab.length;i++){
if(ab.charAt(i) > ba.charAt(i)){
return 1;
}
if(ab.charAt(i) < ba.charAt(i)){
return -1;
}
}
return 1;
}
function PrintMinNumber(numbers)
{
numbers = numbers.sort(compare);
var str="";
for(var i=0;i<numbers.length;i++){
str = str+numbers[i];
}
return str;
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Solution {
public String PrintMinNumber(int [] numbers) {
int n;
String s="";
ArrayList<Integer> list= new ArrayList<Integer>();
n=numbers.length;
for(int i=0;i<n;i++){
list.add(numbers[i]);
}
Collections.sort(list, new Comparator<Integer>(){
public int compare(Integer str1,Integer str2){
String s1=str1+""+str2;
String s2=str2+""+str1;
return s1.compareTo(s2);
}
});
for(int j:list){
s+=j;
}
return s;
}
}