-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path把数组排列成最小数.js
56 lines (55 loc) · 1.36 KB
/
把数组排列成最小数.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
// 快排解法
function isLarge(n, m){
return (n+''+m) > (m+''+n);
}
function PrintMinNumber(numbers)
{
function quickSort(i, j){
if(i>=j) return;
let begin = i, end = j;
let temp = numbers[begin];
++i;
while (true) {
while (isLarge(temp, numbers[i])) i++;
while (isLarge(numbers[j], temp)) j--;
if(i >= j){
break;
}
let num = numbers[i];
numbers[i] = numbers[j];
numbers[j] = num;
}
numbers[begin] = numbers[j];
numbers[j] = temp;
quickSort(begin, j-1);
quickSort(j+1, end);
}
quickSort(0, numbers.length-1);
return numbers.join('');
}
//冒泡排序解法
function isLarge(n, m){
return (n+''+m) > (m+''+n);
}
function PrintMinNumber(numbers)
{
let res = '';
for(let i=0; i<numbers.length; i++){
for(let j=i+1; j<numbers.length; j++){
if(isLarge(numbers[i], numbers[j])){
let temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
res = res + numbers[i];
}
return res
}