数组中出现次数超过一半的数字(数组 时间效率)

题目描述:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。

思路一:

数组排序后,如果符合条件的数存在,则一定是数组中间那个数。

例如:1,2,2,2,3;或2,2,2,3,4;或2,3,4,4,4等等。

这种方法虽然容易理解,但由于涉及到快排sort,其时间复杂度为O(NlogN)并非最优。

import java.util.Arrays;

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

思路二:

利用HashMap存储数组元素—出现次数

import java.util.HashMap;

public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        if (array.length == 0) return 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < array.length; i++)
        {
            if (!map.containsKey(array[i]))
                map.put(array[i], 1);
            else
                map.put(array[i], map.get(array[i]) + 1);
            if (map.get(array[i]) > array.length / 2)
                return array[i];
        }
        return 0;
    }
}


思路三:

利用快排对数组进行排序

public class Solution {
     public int MoreThanHalfNum_Solution(int [] array) {
        if (array.length == 0) return 0;
        if (array.length == 1) return array[0];
        QuickSort(array, 0, array.length - 1);

        int mid = array.length >>1;
        int result = array[mid];
        int count = 0;
        for (int i = 0; i < array.length; i++)
        {
            if (array[i] == result)
                count++;
        }
        if (count > array.length /2)
            return result;
        else return 0;
    }

    private void QuickSort(int[] arr, int low, int high)
    {
        int index;
        while (low < high)
        {
            index = partition(arr, low, high);
            QuickSort(arr, low, index - 1);
            low = index + 1; //优化1:避免了尾递归
        }
    }

    private int partition(int[] arr, int low, int high)
    {
        int flag = arr[low];
        while (low < high)
        {
            while (low < high && arr[high] >= flag)
                high--;
            arr[low] = arr[high]; //优化2:直接赋值,不使用交换
            while (low < high && arr[low] <= flag)
                low++;
            arr[high] = arr[low];
        }
        arr[low] = flag;
        return low;
    }
}


思路四:

采用用户“分形叶”思路

目标数超过数组长度的一半,对数组同时去掉两个不同的数字,到最后剩下的一个数就是该数字。如果剩下两个,那么这两个也是一样的,就是结果

在其基础上把最后剩下的一个数字或者两个回到原来数组中,将数组遍历一遍统计一下数字出现次数进行最终判断。

public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        if (array.length == 0) return 0;
        if (array.length == 1) return array[0];
        int[] tempArray = new int[array.length];
        for (int i = 0; i < array.length; i++)
        {
            tempArray[i] = array[i];
        }
        
        for (int i = 0; i < tempArray.length - 1; i++)
        {
            if (tempArray[i] == 0) continue;
            for (int j = i + 1; j < tempArray.length; j++)
            {
                if (tempArray[j] != 0 && tempArray[i] != tempArray[j])
                {
                    tempArray[i] = tempArray[j] = 0;
                    break;
                }
            }
        }
        int res = 0;
        for (int i = 0; i < tempArray.length; i++)
        {
            if (tempArray[i] != 0)
            {
                res = tempArray[i];
                break;
            }
        }
        int count = 0;
        for (int i = 0; i < array.length; i++)
        {
            if (array[i] == res)
                count++;
        }
        return count > array.length / 2 ? res : 0;
    }
}

思路五:与思路四相同

public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
            if (array.length == 0) return 0;
            if (array.length == 1) return array[0];
            int res = array[0];
            int count = 1;

            for (int i = 1; i < array.length; i++)
            {
                if (count == 0)
                {
                    res = array[i];
                    count = 1;
                }
                else if (array[i] == res)
                    count++;
                else
                    count--;
            }

            count = 0;
            for (int i = 0; i < array.length; i++)
            {
                if (array[i] == res)
                    count++;
            }
            return count > array.length / 2 ? res : 0;
        }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值