题目:
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1, 2, 3, 2, 2, 2, 5, 4, 2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。
第一思路:
给这个数组排好序,然后统计数组中每个数字在数组中出现的次数,然后看出现次数最多的这个元素的个数有没有大于数组长度的一半,有,则返回,没有返回0。
优化思路:基于partition( )方法的O(n)算法
数组中有一个数字出现的次数超过了数组长度的一般,如果把这个数组排序,那么排序之后位于数组中间的数字一定就是那个出现次数超过数组长度一半的数字。即,这个数字就是统计学上的中位数,即长度为n的数组中第n/2大的数字。
受快速排序的启发,在随机快速排序算法中,先在数组中随机选择一个数字,然后调整数组中数字的顺序,使得比选中的数字小的数字都排在它的左边,比选中大的数字都排在它的右边。如果选中的数字下标刚好是n/2,那么这个数字就是数组的中位数。如果它的下标大于n/2,那么中位数应该位于它的左边,我们可以接着在它的左边部分的数组中查找。如果它的下标小于n/2,那么中位数应该位于它的右边,我们接着在它的右边部分的数组中查找。这是一个典型的递归。
代码实现:
public int moreThanHalfNum(int nums[]){
if(nums == null || nums.length == 0){ //如果数组为空或者数组长度为0都返回0
return 0;
}
int len = nums.length;
int start = 0;
int middle = len >> 1;
int end = len - 1;
int index = partition(nums, start, end); //调用快速排序
while(index!=middle){
if(index > middle){ //如果它的下标大于n/2,那么中位数应该位于它的左边,接着在它的左边部分的数组中查找
end = index - 1;
index = partition(nums, start, end);
}else{ //如果它的下标小于n/2,那么中位数应该位于它的右边,接着在它的右边部分的数组中查找
start = index + 1;
index = partition(nums, start, end);
}
}
int result = nums[middle];
if(checkMoreThanHalf(nums, result)){ //判断一下result出现的次数是否超过result,超过返回结果否则返回0
return result;
}else{
return 0;
}
}
private int partition(int[] nums, int start, int end) { //快速排序
int privotKey = nums[start];
while(start < end){
while(start < end && privotKey <= nums[end]){
--end;
}
swap(nums, start, end);//交换位置,使大于privotkey的值位于数组右边
while(start < end && privotKey >= nums[start]){
++start;
}
swap(nums, start, end);//交换位置,使小于privotkey的值位于数组左边
}
return start;
}
private void swap(int[] nums, int start, int end) {//数组元素交换位置
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
}
private boolean checkMoreThanHalf(int nums[], int result){ //判断次数是否超过数组一半
int times = 0;
int lens = nums.length;
for (int i = 0; i < lens; i++) {
if(result == nums[i]){
++times;
}
}
if(times * 2 > lens){
return true;
}
return false;
}
public static void main(String[] args) {//测试
Main m = new Main();
int nums[] = {};
int num = m.moreThanHalfNum(nums);
System.out.println(num);
}
优化思路:根据数组特点找出O(n)的算法
数组中有一个数字出现的次数操作数组长度的一半,即它的出现次数比其他所有数字出现的次数的总和还要多。因此,我们可以考虑在遍历数组的时候保存两个值:一个是数组中的一个数字,一个是次数。当我们遍历到下一个数字的时候,如果下一个数字和我们之前保存的数字相同,则次数加1;如果下一个数字和我们之前保存的数字不同,则次数减1.如果次数为0,我们需要保存下一个数字,并把次数设置为1.由于我们要找的数字出现的次数比其他所有数字出现的次数之和还要多,那么要找的数字肯定是最后一次把次数设置为1时对应的数字。
代码实现:
public int moreThanHalfNum(int nums[]){
int lens = nums.length;
if(nums==null || lens == 0){
return 0;
}
int result = nums[0];
int times = 1;
for (int i = 1; i < lens; ++i) {
if(times==0){
result = nums[i];
times = 1;
}else if(nums[i] == result){
++times;
}else{
--times;
}
}
if(checkMoreThanHalf(nums, result)){ //判断一下result出现的次数是否超过result,超过返回结果否则返回0
return result;
}else{
return 0;
}
}
private boolean checkMoreThanHalf(int nums[], int result){
int times = 0;
int lens = nums.length;
for (int i = 0; i < lens; i++) {
if(result == nums[i]){
++times;
}
}
if(times * 2 > lens){
return true;
}
return false;
}
public static void main(String[] args) { //测试
Main1 m1 = new Main1();
int nums[] = {1, 2, 3, 2, 2, 3, 2, 4, 2};
int num = m1.moreThanHalfNum(nums);
System.out.println(num);
}
小结:
这道题考查我们对时间复杂度的理解。我们每次想出一种解法都要知道其时间复杂度。我们要寻找时间复杂度最优的解使我们的目标。