题解 | #明明的随机数#
明明的随机数
http://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0
注意: 1、要将输入数据从String转为Number类型; 2、Set和Array是不同的数据类型
let arrInput = [];
for(let i=0; i<parseInt(num); i++){
arrInput[i] = parseInt(readline());
}
// 数组去重
let arrSet = new Set([...arrInput]);
// 数组排序
let arr = Array.from(arrSet);
let arrSorted = quickSort(arr);
for(let i in arrSorted){
print(arrSorted[i]);
}
function quickSort(arr){
if(arr.length <= 1){
return arr;
}
let pivotIndex = Math.round(arr.length/2);
let pivot = arr[pivotIndex];
let left = [], right = [];
for(let i in arr){
if(arr[i] <= pivot && i != pivotIndex){
left.push(arr[i]);
}
if(arr[i] > pivot){
right.push(arr[i]);
}
}
return quickSort(left).concat(pivot,quickSort(right));
}