PS:在前几天的面试中,被问到了这个题。然而当时只能用最低效的方法来解。

问题描述:

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

思路1:

低效的做法:直接用哈稀表来存储<key, value>,并找出出现次数value大于等于一半的那个key。

代码:

import java.util.Map;
import java.util.HashMap;
public class Solution {
boolean InvalidInput = false;
public int MoreThanHalfNum_Solution(int [] array) { if(array == null || array.length == 0){
InvalidInput = true;
return 0;
}
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); int max = -1;
int ind = -1;
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(max < map.get(array[i])){
max = map.get(array[i]);
ind = array[i];
}
} if(max > array.length/2){
return ind;
}else{
return 0;
}
}
}

思路2:

—由于出现的次数超过了数组的一半,那么如果我们将数组排序,中间的那个数一定是要求的数字(前提是它一定存在)

—这里排序时采用快排的基本思想,随机选择一个数,调整数组使得它左边的数都小于它,右边的数都大于它。(参考Partition函数)(下一篇博客的代码中给出了关于Partition函数详细的注释)

import java.util.Map;
import java.util.HashMap;
public class Solution {
static boolean InvalidInput = false;
public int MoreThanHalfNum_Solution(int [] array) { if(array == null || array.length == 0){
InvalidInput = true;
return 0;
} int middle = array.length >> 1;//设定中位数位置
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 static boolean CheckMoreThanHalf(int[] array, int number){
int times = 0;
int length = array.length; for(int i = 0; i < length; i++){
if(array[i] == number){
times++;
}
} boolean isMoreThanHalf = true; if(times * 2 <= length){
InvalidInput = true;
isMoreThanHalf = false;
} return isMoreThanHalf;
} public static int Partition(int[] array, int start, int end){
if(array == null || array.length == 0 || start < 0 || end < 0){
InvalidInput = true;
return -1;
} int index = RandomInRange(start ,end);
int tmp = array[index];
array[index] = array[end];
array[end] = tmp; int small = start - 1; for(index = start; index < end; index++){
if(array[index] < array[end]){
small++; if(small != index){
int temp = array[index];
array[index] = array[small];
array[small] = temp;
}
}
} small++;
tmp = array[small];
array[small] = array[end];
array[end] = tmp; return small; }
public static int RandomInRange(int s, int e){
int Min = s;
int Max = e;
int result = Min + (int)(Math.random() * ((Max - Min) + 1));
return result; }
}

思路3:

根据数组特点找出O(n)的算法

1.数组中有一个数字出现的次数超过数组长度的一半,也就是说它出现的次数比其他所有的数字出现的次数的和还要多。

2.因此我们可以考虑在遍历数组的时候保存两个值:一个是数组中的一个数字,一个是次数。

3.当我们遍历到下一个数字的时候,如果下一个数字和我们之前保存的数字相同,则次数加1,否则减1.

代码:

import java.util.Map;
import java.util.HashMap;
public class Solution {
static boolean InvalidInput = false;
public int MoreThanHalfNum_Solution(int [] array) { if(array == null || array.length == 0){
InvalidInput = true;
return 0;
} int result = array[0];
int times = 1;
for(int i = 1; i < array.length; i++){
if(times == 0){
result = array[i];
}else if(array[i] == result){
times++;
}else{
times--;
} } if(!CheckMoreThanHalf(array, result)){
InvalidInput = true;
result = 0;
}
return result;
}
public static boolean CheckMoreThanHalf(int[] array, int number){
int times = 0;
int length = array.length; for(int i = 0; i < length; i++){
if(array[i] == number){
times++;
}
} boolean isMoreThanHalf = true; if(times * 2 <= length){
InvalidInput = true;
isMoreThanHalf = false;
} return isMoreThanHalf;
}
}

PS:检查出现频率最高的是否超过一半,也还是需要遍历一次数组的。

剑指Offer:面试题29——数组中出现次数超过一半的数字(java实现)的更多相关文章

  1. 剑指Offer - 九度1370 - 数组中出现次数超过一半的数字

    剑指Offer - 九度1370 - 数组中出现次数超过一半的数字2013-11-23 03:55 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组 ...

  2. 【剑指Offer】28、数组中出现次数超过一半的数字

      题目描述:   数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.   例如:输入如下所示的一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过 ...

  3. 《剑指offer》39题—数组中出现次数超过一半的数字

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

  4. 剑指Offer:找出数组中出现次数超过一半的元素

    题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...

  5. 剑指offer(28)数组中出现次数超过一半的数

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

  6. 数组中出现次数超过一半的数字 -java

    数组中出现次数超过一半的数字 -java 方法一: 数组排序,然后中间值肯定是要查找的值. 排序最小的时间复杂度(快速排序)O(NlogN),加上遍历. 方法二: 使用散列表的方式,也就是统计每个数组 ...

  7. 剑指Offer:面试题15——链表中倒数第k个结点(java实现)

    问题描述 输入一个链表,输出该链表中倒数第k个结点.(尾结点是倒数第一个) 结点定义如下: public class ListNode { int val; ListNode next = null; ...

  8. 【Offer】[39] 【数组中出现次数超过一半的数字】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如,输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数 ...

  9. 《剑指offer》面试题39. 数组中出现次数超过一半的数字

    问题描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 你可以假设数组是非空的,并且给定的数组总是存在多数元素. 示例 1: 输入: [1, 2, 3, 2, 2, 2, 5, 4, ...

随机推荐

  1. ”未在本地计算机上注册“microsoft.et.OLEDB.4.0”提供程序。“解决方案大集合

    本人在做一个连接Access数据库的时候,程序扔给我一个如此Bug——“未在本地计算机上注册“microsoft.et.OLEDB.4.0”, 请教度娘,告诉我可能是如下因素: 一.“设置应用程序池默 ...

  2. SPARK SQL 中registerTempTable与saveAsTable的区别

    使用registerTempTable注册表是一个临时表,生命周期只在所定义的sqlContext或hiveContext实例之中.换而言之,在一个sqlontext(或hiveContext)中re ...

  3. int类型究竟占几个字节

    我最近也在看深入理解计算机系统这本书,上面提到了在32位机器和64机器中int类型都占用4个字节.后来,别人查了The C Programming language这本书,里面有一句话是这样的: Ea ...

  4. cf 732c

    /* cf732c 错过的最少次数 _________________________________________________________________________________ ...

  5. 逻辑操作符“&&”的三层理解

    第一层:操作符“&&”可以对两个布尔值进行逻辑与运算,返回一个布尔值. 第二层:操作符“&&”可以对两个真假值进行逻辑与运算,并且返回一个真假值. 第三层:操作符“&a ...

  6. jd-gui报错INTERNAL ERROR 解决办法

    问题:我用dex2jar工具反编译了apk文件,但当我用jd-gui反编译前面操作获得的jar文件的时,能很完美地看到大部分类反编译后的代码,但有一部分类不能显示出来--constants类,仅仅显示 ...

  7. mybatis——使用mapper代理开发方式

    ---------------------------------------------------------------generatorConfig.xml------------------ ...

  8. 卡尔曼滤波器 Kalman Filter (转载)

    在学习卡尔曼滤波器之前,首先看看为什么叫“卡尔曼”.跟其他著名的理论(例如傅立叶变换,泰勒级数等等)一样,卡尔曼也是一个人的名字,而跟他们不同的是,他是个现代人! 卡 尔曼全名Rudolf Emil ...

  9. Unity 相关经典博客资源总结(持续更新)

    就作为一个记录吧,把平时看过的Unity相关的一些好的Blog记录并分享. 好的论坛: Unity官方脚本  点评:这个不用说了,最核心的内容,理解整个Unity引擎的方方面面,梳理结构. Unity ...

  10. nginx入门篇----功能特性

    1.nginx功能特性 可以作为http服务器或者反向代理服务器 能够快速响应静态页面(html)的请求 支持FastCGI.SSL.Virtual Host.URL Rewrite.HTTP.Gzi ...