26 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。

参考链接:https://yq.aliyun.com/articles/642782
一、题目
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。

二、解题思路
解法一:基于Partition 函数的O(n)算法
数组中有一个数字出现的次数超过了数组长度的一半。如果把这个数组排序,那么排序之后位于数组中间的数字一定就是那个出现次数超过数组长度一半的数字。也就是说,这个数字就是统计学上的中位数,即长度为n 的数组中第n/2 大的数字。
这种算法是受快速排序算法的启发。在随机快速排序算法中,我们先在数组中随机选择一个数字,然后调整数组中数字的顺序, 使得比选中的数字小数字都排在它的左边,比选中的数字大的数字都排在它的右边。如果这个选中的数字的下标刚好是n/2,那么这个数字就是数组的中位数。如果它的下标大于n/2 ,那么中位数应该位于它的左边,我们可以接着在它的左边部分的数组中查找。如果它的下标小于n/2,那么中位数应该位于它的右边,我们可以接着在它的右边部分的数组中查找。这是一个典型的递归过程。

解法二:借助hashmap存储数组中每个数出现的次数,最后看是否有数字出现次数超过数组长度的一半;

解法三:根据数组组特点找出O(n)的算法
数组中有一个数字出现的次数超过数组长度的一半,也就是说它出现的次数比其他所有数字出现次数的和还要多。因此我们可以考虑在遍历数组的时候保存两个值: 一个是数组中的一个数字, 一个是次数。当我们遍历到下一个数字的时候,如果下一个数字和我们之前保存的数字相同,则次数加1,如果下一个数字和我们之前保存的数字不同,则次数减1 。如果次数为零,我们需要保存下一个数字,并把次数设为1 。由于我们要找的数字出现的次数比其他所有数字出现的次数之和还要多,那么要找的数字肯定是最后一次把次数设为1 时对应的数字
解法一:先排序在找中位数
1、偷懒的做法:系统的sort排序

  public int MoreThanHalfNumSolution(int[] array) {
        Arrays.sort(array);
        int half = array.length / 2;
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == array[half])
                count++;
        }
        if (count > half)
            return array[half];
        else
            return 0;
    }

2、快排:
快排方式一 挖坑法

  public int MoreThanHalfNum_Solution3(int[] number) {
  		//int array04[] = {1, 2, 3, 2, 2, 2, 5, 4, 2};
        //{1,2,2,2,2,2,3,4,5}   9 
        if (number == null || number.length <= 0)
            return 0;
        int low = 0;
        int high = number.length - 1;
        int index = getMiddle(number, low, high);

        while (index != number.length >> 1) {
            if (index < number.length >> 1) {
                low = index + 1;
                index = getMiddle(number, low, high);//{1,2,2,2,2,2,3,4,5}
            } else {
                high = index - 1;
                index = getMiddle(number, low, high);
            }

        }
        //判断次数是否超过一半
        int num = number[index];
        int times = 0;
        for (int i = 0; i < number.length; i++) {
            if (number[i] == num) {
                times++;
            }
        }
          if (times * 2 > number.length) {
            return num;
        }
        return 0;


    }
  public static int getMiddle(int[] numbers, int low, int high) {
        int temp = numbers[low]; // 数组的第一个作为中轴
        while (low < high) {
            while (low < high && numbers[high] >=temp) {  //=不能忘记,否则就查找不出了
                high--;
            }
            numbers[low] = numbers[high];// 比中轴小的记录移到低端

            while (low < high && numbers[low] <=temp) {
                low++;
            }

            numbers[high] = numbers[low]; // 比中轴大的记录移到高端
        }
        numbers[low] = temp; // 中轴记录到尾

        return low; // 返回中轴的位置
    }

快排方式二

   public int MoreThanHalfNum_Solution(int[] array) {
        if (array == null || array.length <= 0)
            return 0;
        int low = 0;
        int high = array.length - 1;
        int index = partition(array, low, high);
 
        while (index != array.length >> 1) {
            if (index < array.length >> 1) {
                low = index + 1;
                index = partition(array, low, high);
            } else {
                high = index - 1;
                index = partition(array, low, high);
            }
       
        }
        //判断次数是否超过一半
        int num = array[index];
        int times = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == num) {
                times++;
            }
        }
   
        if (times * 2 > array.length) {
            return num;
        }
        return 0;
    }

    private int partition(int[] array, int low, int high) {
        int pivotKey = array[low];
        while (low < high) {
            while (low < high && array[high] >= pivotKey)
                high--;
            int temp = array[low];
            array[low] = array[high];
            array[high] = temp;
            while (low < high && array[low] <= pivotKey)
                low++;
            temp = array[low];
            array[low] = array[high];
            array[high] = temp;
        }
        return low;
    }

快排方式二

  public int MoreThanHalf(int[] nums) {
        if (nums.length == 0)
            return -1;
        int start = 0;
        int end = nums.length - 1;
        int index = Partition(nums, start, end);
        int mid = nums.length / 2;
        while (index != mid) {
            if (index > mid)
                //如果调整数组以后获得的index大于middle,则继续调整start到index-1区段的数组
                index = Partition(nums, start, index - 1);
            else {
                //否则调整index+1到end区段的数组
                index = Partition(nums, index + 1, end);
            }
        }
        return nums[index];
    }

    public int Partition(int[] nums, int start, int end) {
        int pivotkey = nums[start];
        int origin = start;
        while (start < end) {
            while (start < end && nums[end] >= pivotkey) end--;
            while (start < end && nums[start] < pivotkey) start++;
            swap(nums, start, end);
        }
        swap(nums, start, end);
        swap(nums, origin, end);
        return end;
    }

    public int[] swap(int[] ints, int x, int y) {
        int temp = ints[x];
        ints[x] = ints[y];
        ints[y] = temp;
        return ints;
    }

解法二:

import java.util.HashMap;
import java.util.Map;
public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        int length = array.length;
        for(int i=0; i<length; i++){
            if(!map.containsKey(array[i]))
                map.put(array[i], 1);
            else
                map.put(array[i], map.get(array[i])+1);
        }
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if(entry.getValue()*2>length)
                return entry.getKey();
        }
        return 0;
    }
}

解法三:

    public static int moreThanHalfNum(int[] number) {
        if (number == null || number.length < 1) {
            System.out.println("数组为空");
            return -1;

        }
        int result = number[0];
        int count = 1;
        for (int i = 1; i < number.length; i++) {
            if (count == 0) {// 重新记录一个数,假设它是出现次数大于数组一半的
                result = number[i];
                count = 1;
            } else if (result == number[i]) { // 如果记录的值与统计值相等,记数值增加
                count++;
            } else {  // 如果不相同就减少,相互抵消
                count--;
            }

        }
        // 最后的result可能是出现次数大于数组一半长度的值
        // 统计result的出现次数

        count = 0;
        for (int a : number) {
            if (result == a) {
                count++;
            }

        }
        // 如果出现次数大于数组的一半就返回对应的值
        if (count > number.length / 2) {

            return result;
        } else {
            System.out.println("数组输入异常");
            return -1;
        }


    }

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值