# 腾讯笔试题 微信红包

[1,2,3,2,2],5

/**
* 超过一半,如果存在,中位数就是.
* Created by ustc-lezg on 16/4/7.
*/
public class Solution {

public int getValue(int[] gifts, int n) {
return quickSort(gifts, 0, n - 1);
}

public int quickSort(int[] arr, int low, int high) {
int mid = (high + 1) >> 1;
int pivot = partition(arr, low, high);
while (pivot != mid) {//利用快排找到中位数
if (pivot < mid) {
pivot = partition(arr, pivot + 1, high);
} else {
pivot = partition(arr, low, pivot - 1);
}
}
int res = arr[pivot];
if (!check(arr, high + 1, res)) {
return 0;
}
return res;
}

public int partition(int arr[], int low, int high) {
int left = low;
int right = high;
int temp = arr[low];
while (left < right) {
while (left < right && arr[right] >= temp) {
--right;
}
if (left < right) {
arr[left] = arr[right];
}
while (left < right && arr[left] <= temp) {
++left;
}
if (left < right) {
arr[right] = arr[left];
}
}
arr[left] = temp;
return left;
}

public boolean check(int[] arr, int n, int res) {
int count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == res) {
++count;
}
}
if (count * 2 <= n) {
return false;
} else {
return true;
}
}

}

• 本文已收录于以下专栏：

举报原因： 您举报文章：腾讯笔试题 微信红包 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)