Enter a positive integer array, the array of all the numbers arranged in a number spliced together, the splice can print out all numbers smallest one.
3,32,321 input array} {e.g., print the minimum number of three numbers can be arranged to 321,323.
The core ideas:
ab>ba 则 a>b
function PrintMinNumber(numbers)
{
numbers.sort(function(n1, n2){ //用于对数组的元素进行排序
if(n1 + '' + n2 > n2 + '' + n1){
return 1;
}else if(n1 + '' + n2 < n2 + '' + n1){
return -1;
}else{
return 0;
}
});
return numbers.join('');
}
function PrintMinNumber(numbers)
{
// write code here
numbers.sort(function(a,b){
var c1 = `${a}${b}`;
var c2 = `${b}${a}`;
return c1 > c2;
});
var min = '';
numbers.forEach((i) => min += i);
return min;
}