数组中出现次数超过一半的数字

该博客探讨了如何在数组中找到一个出现次数超过数组长度一半的数字。提供了两种方法:一种是通过排序后查找中间数字,另一种是遍历数组计数。并提及这是剑指offer编程题中的一个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

方法1:根据位置判断
将数组按照顺序排序后,超过数组长度一半的数字一定是排序后中间的那个数字
首先找到位于中间的数字,然后判断它的个数判断是否合法

public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        if(array.length==0)
            return 0;
        int start=0,end=array.length-1;
        int mid=array.length/2;
        int index=partition(array,start,end);
        while(index!=mid){                //找到中间的位置的数据,array[index]
            if(index>mid){
                end=index-1;
                index=partition(array,start,end);
            }else{
                start=index+1;
                index=partition(array,start,end);
            }
        }
        int index_count=count(array,index);
        return index_count>array.length/2?array[index]:0;
    }
    
    public int partition(int[] array,int low,int high){   //快排,partition
        int temp=array[low];
        while(low<high){
            while(low<high && array[high]>=temp)
                high--;
            array[low]=array[high];
            while(low<high && array[low]<=temp)
                low++;
            array[high]=array[low];
        }
        array[low]=temp;
        return low;
    }
    public int count(int[] array,int index){   //统计index位置个数 
        int index_count=0;
        for(int i=0;i<array.length;i++){
            if(array[i]==array[index])
                index_count++;
        }
        return index_count;
    }
}

或者

import java.util.Arrays;
public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        if(array.length==0)
            return 0;
        Arrays.sort(array);
        return count(array,array[array.length/2])>array.length/2?array[array.length/2]:0;
    }
     public int count(int[] array,int result){   //统计index位置个数 
        int index_count=0;
        for(int i=0;i<array.length;i++){
            if(array[i]==result)
                index_count++;
        }
        return index_count;
    }
}

方法2:根据个数判断
因为数字在数组中出现次数的超过一半,遍历数组,保存两个值:一个是数组中的一个数字,另一个是次数
当遍历到下一个数字的时候,如果下一个数字和之前保存的数字相同,则次数加一;如果下一个数字和我们之前保存的数字不同,则次数减一。如果次数为0,那么我们需要保存下一个数字,并把次数设为一
最终,要找的数字肯定是最后一次次数设为1对应的数字

public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        if(array.length==0)
            return 0;
        int result=array[0];
        int times=1;
        for(int i=1;i<array.length;i++){
            if(times==0){
                result=array[i];
                times=1;
            }else if(result==array[i]){
                times++;
            }else{
                times--;
            }
        }
        return count(array,result)>array.length/2?result:0;
    }
     public int count(int[] array,int result){   //统计index位置个数 
        int index_count=0;
        for(int i=0;i<array.length;i++){
            if(array[i]==result)
                index_count++;
        }
        return index_count;
    }
}

剑指offer编程题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值