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


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

    方法一 ---- 基于随机快排
    如果数组中存在一个数字的出现次数大于数组长度一半,那么将这个数组排序后位于该数组的中位数就是这个数字。
    在随机快速排序中,现在数组中随机选择一个数字,然后调整数组中的数字的顺序,使得比选中的数字小的的数字都在它的左边,比他大的数字都在其右边。如果这个选中的数字的下标刚好是n/2,那么这个数字就是数组的中位数;如果这个下标大于n/2,那么中位数应该在它的左边,我们可以接着在它左边部分的数组中查找;如果这个下标小于n/2,那么表示中位数在它的右边,在右边的数组中查找。是一个典型的递归过程。

    	/**
    	 * 利用Partition函数的时间复杂度为O(n)的算法
    	 * @param array
    	 * @return
    	 */
        public int MoreThanHalfNum_Solution(int [] array) {
            if(array == null || array.length == 0) {
            	return 0;
            }
            if(array.length == 1) {
            	return array[0];
            }
            
            int middle = array.length / 2;
            int start = 0;
            int end = array.length - 1;
            
            int index = Partition(array, start, end);
            while(index != middle) {
            	if(index > middle) {
            		end = index - 1;
            		index = Partition(array, start, end);
            	}
            	else {
            		start = index + 1;
            		index = Partition(array, start, end);
            	}
            }
            
            int result = array[middle];
            if(!CheckMoreThanHalf(array, result)) {
            	result = 0;
            }
            return result;
        }
        
        public boolean CheckMoreThanHalf(int[] elem, int result) {
        	int times = 0;
        	boolean isMoreThanHalf = true;
        	for (int i = 0; i < elem.length; i++) {
    			if(elem[i] == result) {
    				++times;
    			}
    		}
        	
        	if(times * 2 <= elem.length) {
        		isMoreThanHalf = false;
        	}
        	
        	return isMoreThanHalf;
        }
        
        public int Partition(int[] elem, int low, int high) {
        	int pivotkey = elem[low];
        	while(low < high) {
        		while((low < high) && elem[high] > pivotkey) {
        			high--;
        		}
        		swap(elem, low, high);
        		while((low < high) && elem[low] <= pivotkey) {
        			low ++;
        		}
        		swap(elem, low, high);
        		
        	}
        	return low;
        }
        
        
        
        public void swap(int[] elem, int i, int j) {
        	int temp = elem[i];
        	elem[i] = elem[j];
        	elem[j] = temp;
        }
    

    方法二 ---- 根据数组特点
    如果有一个数字在数组中出现次数大于数组的一半,那么也就是说他出现的次数比其他数字出现次数和还要多。我们可以设置两个变量,一个保存数组中的一个数字,另外一个保存次数,遍历数组,当遍历到下一个数字时,如果下一个数字和之前保存的数字相同,次数加1;如果不同次数减1;如果次数为0,那么我们需要保存下一个数字,并将次数还原为1。

        /**
         * 根据数组特点找出时间复杂度为O(n)的算法
         * @param args
         */
        
        public int MoreThanHalfNum_Solution_2(int [] array) {
        	if(array == null || array.length == 0) {
        		return 0;
        	}
        	
        	if(array.length == 1) {
        		return array[0];
        	}
        	
        	int tempNumber = array[0];
        	int times = 1;
        	
        	for (int i = 1; i < array.length; i++) {
        		if(tempNumber == array[i]) {
        			times++;
        		}
        		else {
        			
        			times--;
            		if(times == 0) {
            			tempNumber = array[i];
            			times = 1;
            		}
    
        		}
    		}
            if(!CheckMoreThanHalf(array, tempNumber)) {
            	tempNumber = 0;
            }
            return tempNumber;
        	
        }
    
  • 相关阅读:
    数据挖掘笔试面试(7)
    数据挖掘笔试面试(6)
    数据挖掘笔试面试(5)
    数据挖掘面试笔试(4)
    数据挖掘面试(3)
    数据挖掘面试题(2)
    学生-课程-成绩表设计
    树状结构表设计
    性能优化(1+N,list与iterator,缓存,事务)
    对象的三种状态
  • 原文地址:https://www.cnblogs.com/lishanlei/p/10707701.html
Copyright © 2020-2023  润新知