水贴王之续,找出数组里出现频率最高的元素

找出数组里出现频率最高的元素

个人信息:就读于燕大本科软件project专业 眼下大三;

本人博客:google搜索“cqs_2012”就可以;

个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;

博客内容:水贴王问题之续

博客时间:2014-5-12;

编程语言:Java ;

编程坏境:Windows 7 专业版 x64;

编程工具:jdk,eclipse x64;

制图工具:office 2007 powerpoint;

硬件信息:7G-3 笔记本;


真言:

痛苦的活着比死了更好。


题目: 

找出数组里出现频率最高的元素。


思路:

利用哈希表统计元素的数目。

哈希表数据结构例如以下

举例:


平均时间复杂度O(N)

空间复杂度O(N)

实验:


实验1

int[] data ={1,1,2,2,2};

凝视 元素2出现了三次。

实验2

int[] data ={1,1,2,2};

凝视 元素1出现了2次(当然2也出现了两次,没有求出全部的是不足之处。我暂且仅仅求了当中一个)

代码:

piar.java

package test;

public class pair {
	public int first;
	public int second;
	pair()
	{
		first = second = -1;
	}
}

test.java

package test;


public class test {
	private static final int size = 100 ;
	public static void main(String[] args) {
		int[] data ={1,1,2,2};
		System.out.println(_Find_most_elem(data).first+" "+_Find_most_elem(data).second);
		
		
	}
	
	static pair _Find_most_elem(int[] data)
	{
		int[][] hash = new int[data.length][2];
		int it_hash = 0;
		pair result = new pair();

		
		for(int i=0;i<data.length;i++)
		{
			// find firstly
			if( hash[ data[i] % data.length ][0] == data[i] )
			{
				hash[ data[i] % data.length ][1] ++ ;
			}
			// find secondly
			else
			{
				it_hash = data[i] % data.length;
				while(hash[ it_hash ][0] != data[i] && hash[it_hash ][1] != 0)
				{
					it_hash = (it_hash+1) % data.length ;
				}	
				if( hash[ it_hash ][1] == 0 )
					hash[ it_hash ][0] = data[i];	
				hash[ it_hash ][1] ++;			
			}

		}
		// find most elem
		it_hash = 0;
		for(int i=1;i<data.length;i++)
		{
			if( hash[i][1] > hash[it_hash][1] )
				it_hash = i ;
		}
		if(hash[it_hash][1] != 0)
		{
			result.first = hash[it_hash][0];
			result.second = hash[it_hash][1];
		}
		return result ;                                                                                                                                          
	}
}


博客之续

解决数组里仅仅同意自然数的情况。如今我要解决的问题是能够处理负数。问题来了

这时候哈希表的下标准则要又一次定义了

一開始,我们以0为标准,然后去相应哈希表的下表去填充

如今我们又一次选择參考对象。以int的最小值作为參考值,这时候我们又一次得到的数据值就会溢出,我们要用long类型去保存数据

然后依据模去相应哈希表下表填充数据

实验

int[] data ={1,1,2,2,-2,-2,-2,-2};

哈希表的长度为数组的长度 length =  8

java 最小值:Integer.MIN_VALUE=-2147483648

建表规则

针对data【0】。(1-(-2147583658)) %  length = 1 ;

这时检測表中没有data【0】;则插入;

那么 hash[1][0] = 1; hash[1][1] ++;

否则直接 数量加一。

对于data里的数据都这样处理以此类推。

见表例如以下。空白处为默认初始值。



代码

pair.java

package test;

public class pair {
	public int first;
	public int second;
	pair()
	{
		first = second = -1;
	}
}

test.java

package test;


import java.lang.Integer;

public class test {
	private static final int size = 100 ;
	public static void main(String[] args) {
		int[] data ={1,1,2,2,-2,-2,-2,-2};

		System.out.println(_Find_most_elem(data).first+" "+_Find_most_elem(data).second);
	}
	
	
	
	// data array including 负数
	static pair _Find_most_elem(int[] data) 
	{
		int[][] hash = new int[data.length][2];
		long it_hash = 0;
		pair result = new pair();
		
		for(int i=0;i<data.length;i++)
		{
			// find firstly
			it_hash = (long)data[i] - (long)(Integer.MIN_VALUE);
			if( hash[  (int) (it_hash % (long)(data.length)) ][0] == data[i] )
			{
				hash[ (int) (it_hash % (long)(data.length)) % data.length ][1] ++ ;
			}
			// find secondly
			else
			{
				it_hash = it_hash % (long)data.length;
				while(hash[ (int)it_hash ][0] != data[i] && hash[(int)it_hash ][1] != 0)
				{
					it_hash = (it_hash+1) % (long)data.length ;
				}	
				if( hash[ (int)it_hash ][1] == 0 )
					hash[ (int)it_hash ][0] = data[i];	
				hash[ (int)it_hash ][1] ++;			
			}
		}
		// find most elem
		it_hash = 0;
		for(int i=1;i<data.length;i++)
		{
			if( hash[i][1] > hash[(int)it_hash][1] )
				it_hash = i ;
		}
		if(hash[(int)it_hash][1] != 0)
		{
			result.first = hash[(int)it_hash][0];
			result.second = hash[(int)it_hash][1];
		}
		return result ;                                                                                                                                          
	}
}





posted @ 2017-04-27 19:57  yfceshi  阅读(346)  评论(0编辑  收藏  举报