我正在执行912。使用quicksort对数组进行排序,并使用arr [high]作为枢轴。那么如何运行此代码?
错误是:
编译错误 第7行:错误:类型不兼容:void无法转换为int [] 返回sortHelper(nums,0,nums.length-1);
class Solution {
public int[] sortArray(int[] nums) {
if(nums == null || nums.length == 0) {
return null;
}
return sortHelper(nums, 0, nums.length - 1);
}
public void sortHelper(int arr[], int low, int high) {
if(low < high) {
int pi = partition(arr, low, high);
sortHelper(arr, low, pi - 1);
sortHelper(arr, pi + 1, high);
}
}
public int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for(int j = low; j < high; j++) {
if(arr[j] <= pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
}
答案 0 :(得分:0)
由于sortHelper为空(无返回值),因此请更改以下行:
return sortHelper(nums, 0, nums.length - 1);
收件人:
sortHelper(nums, 0, nums.length - 1);