水贴王之续,找出数组里出现频率最高的元素
2014-05-12 12:37
260 查看
找出数组里出现频率最高的元素
个人信息:就读于燕大本科软件工程专业 目前大三;
本人博客: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
test.java
博客之续
解决数组里只允许自然数的情况,现在我要解决的问题是可以处理负数。问题来了
这时候哈希表的下标准则要重新定义了
一开始,我们以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 ;
}
}
个人信息:就读于燕大本科软件工程专业 目前大三;
本人博客: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 ;
}
}
相关文章推荐
- 水贴王之续,找出数组里出现频率最高的元素
- 【谷歌面试题】找出二叉查找树中出现频率最高的元素
- 在一个数组或字符串中找到出现频率最高的那个元素的方法
- Javascript面试题-找出数组中频率最高元素的多种方法
- 给定一个字符串如何判断该字符串中某一字符出现的频率并找出出现频率最高的字符和频率
- JAVA代码—算法基础:找出整形数组中只出现过1次的两个元素
- 怎样从10亿查询词找出出现频率最高的10个
- java版的小明的筷子,主要是找出数组中只出现一次的元素
- java找出一个数组中出现次数最多的那个元素
- Majority Element 找出一个数组中出现次数最多的元素
- 怎样从10亿查询词找出出现频率最高的10个
- 按序输出整数数组中所有出现频率最高的数字
- 如何找出数组中出现奇数次的元素
- 数组长度为n,其中有k-1个元素出现的次数大于 n/K(k>1),请找出这个K-1元素?
- 由n个元素组成的数组,n-2个数出现了偶数次,两个数出现了奇数次,且这两个数不相等,如何用O(1)的空间复杂度,找出这两个数
- 算法 n大小的数组中找出出现3/n次数以上的元素
- 基础算法----找出集合中最大和值的子数组,插入排序,找出数组中出现最多的元素
- [原]找出数组中出现奇数次的元素<异或的应用>
- 一个数组中,除两个元素其余都出现了两次,找出这两个元素
- 如何找出数组中出现次数为奇数次的唯一元素