题目:

1.给定一个整型数组,打印其中出现次数大于一半的数。如果没有出现这样的数,打印提示信息。

如:1,2,1输出1。    1,2,3输出no such number。

2.给定一个整型数组,再给一个整数K,打印所有出现次数大于N/K的数,如果没有这样的数,打印提示信息。

解答:

两道题都可以使用哈希表记录每个数出现的次数,额外的空间复杂度为O(N)。

其他的方法:

1.时间复杂度为O(N)。额外空间复杂度为O(1)。

public static void printHalfMajor(int[] arr) {
int cand = 0;
int times = 0;
for (int i = 0; i < arr.length; i++) {
if (times == 0) {
cand = arr[i];
times++;
} else if (arr[i] == cand) {
times++;
} else {
times--;
}
}
times = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == cand)
times++;
}
if (times > arr.length / 2) {
System.out.println(cand);
} else {
System.out.println("no such number.");
}
}

第一个for循环结束后,如果这样的数真的存在,那么cand的值就是所求的值。但是cand的值不一定就是所求的值。所以最后还要用for循环判断一下。

2.时间复杂度为O(N*K)。额外空间复杂度为O(K)。

public static void printKMajor(int[] arr, int K) {
if (K < 2) {
System.out.println("the value of K is invalid.");
return;
}
HashMap<Integer, Integer> cands = new HashMap<Integer, Integer>();
for (int i = 0; i != arr.length; i++) {
if (cands.containsKey(arr[i])) {
cands.put(arr[i], cands.get(arr[i]) + 1);
} else {
if (cands.size() == K - 1) {
allCandsMinusOne(cands);
} else {
cands.put(arr[i], 1);
}
}
}
HashMap<Integer, Integer> reals = getReals(arr, cands);
boolean hasPrint = false;
for (Entry<Integer, Integer> set : cands.entrySet()) {
Integer key = set.getKey();
if (reals.get(key) > arr.length / K) {
hasPrint = true;
System.out.print(key + " ");
}
}
System.out.println(hasPrint ? "" : "no such number.");
} public static void allCandsMinusOne(HashMap<Integer, Integer> map) {
List<Integer> removeList = new LinkedList<Integer>();
for (Entry<Integer, Integer> set : map.entrySet()) {
Integer key = set.getKey();
Integer value = set.getValue();
if (value == 1) {
removeList.add(key);
}
map.put(key, value - 1);
}
for (Integer removeKey : removeList) {
map.remove(removeKey);
}
} public static HashMap<Integer, Integer> getReals(int[] arr,
HashMap<Integer, Integer> cands) {
HashMap<Integer, Integer> reals = new HashMap<Integer, Integer>();
for (int i = 0; i != arr.length; i++) {
int curNum = arr[i];
if (cands.containsKey(curNum)) {
if (reals.containsKey(curNum)) {
reals.put(curNum, reals.get(curNum) + 1);
} else {
reals.put(curNum, 1);
}
}
}
return reals;
}

[算法]在数组中找到出现次数大于N/K的数的更多相关文章

  1. 《程序员代码面试指南》第八章 数组和矩阵问题 在数组中找到出现次数大于N/K 的数

    题目 在数组中找到出现次数大于N/K 的数 java代码 package com.lizhouwei.chapter8; import java.util.ArrayList; import java ...

  2. 在数组中寻找出现次数大于N/K的数

    给定一个int[]数组,给定一个整数k,打印所有出现次数大于N/k的数,没有的话,给出提示信息. === 核心思想:一次在数组中删除K个不同的数,不停的删除,直到剩下的数的种类不足K就停止删除,那么如 ...

  3. 算法总结之 在数组中找到出现次数 > N/K的数

    题目1 给定一个整型数组arr,  打印其中出现次数大于一半的数, 如果没有这样的数,打印提示信息 进阶 给定一个整型数组arr, 再给定一个整数K, 打印所有出现次数大于 N/K的数,如果没有这样的 ...

  4. 剑指Offer面试题:32.数字在排序数组中出现的次数

    一.题目:数字在排序数组中出现的次数 题目:统计一个数字在排序数组中出现的次数.例如输入排序数组{1,2,3,3,3,3,4,5}和数字3,由于3在这个数组中出现了4次,因此输出4. 二.解题思路 2 ...

  5. [LeetCode169]Majority Element求一个数组中出现次数大于n/2的数

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

  6. 编程算法 - 数字在排序数组中出现的次数 代码(C)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/36869869 数字在排序数组中出现的次数 代 ...

  7. [PHP] 算法-统计一个数字在排序数组中出现的次数的PHP实现

    统计一个数字在排序数组中出现的次数. 1.有序的数组查找,使用二分法 2.二分法查找第一次出现的位置,二分法查找最后一次出现的位置,end - start +1 left=getLeft(data,k ...

  8. 【剑指Offer】37、数字在排序数组中出现的次数

      题目描述:   统计一个数字在排序数组中出现的次数.例如,输入排序数组{1,2,3,3,3,3,4,5}和数字3,由于数字3在该数组中出现了4次,所以函数返回4.   解题思路:   既然输入的数 ...

  9. 剑指Offer(三十七):数字在排序数组中出现的次数

    剑指Offer(三十七):数字在排序数组中出现的次数 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.n ...

随机推荐

  1. JQuery小结(转)

    一.页面加载 JQ的页面加载比JS要快,当整个dom树结构生成完毕后就会加载 JQ页面加载不存在覆盖问题,加载的时候是顺序执行 JQ的页面加载最简写的方式为: $(function(){ alert( ...

  2. Atitit.pdf 预览 转换html attilax总结

    Atitit.pdf 预览 转换html attilax总结 1. Swf flash还是html1 2. pdf2htmlEX1 3. iText 5.5.0 发布,Java 的 PDF 操作类库1 ...

  3. 分享牛人就是的volatilekeyword

    volatile作用 一个定义为volatile的变量是说这变量可能会被意想不到地改变.这样,编译器就不会去如果这个变量的值了. 精确地说就是.优化器在用到这个变量时必须每次都小心地又一次读取这个变量 ...

  4. .net之Ajax获取接口数据并实现循环播放

    <script type="text/javascript"> var xhr; ; var res; window.onload = function () { xh ...

  5. TCP粘包处理通用框架--C代码

    说明:该文紧接上篇博文“ linux epoll机制对TCP 客户端和服务端的监听C代码通用框架实现 ”讲来 (1)TCP粘包处理数据结构设计 #define MAX_MSG_LEN 65535 ty ...

  6. centos7防火墙--firewall

    centos7的防火墙变成firewall了 systemctl start firewalld.service#启动firewallsystemctl stop firewalld.service# ...

  7. error items-9022:missing required icon file.the bundle does not contain an app icon for iPhone/iPad Touch of exactly '120x120' pixels,in.pen format for ios versions >= 7.0

    error items-9022:missing required icon file.the bundle does not contain an app icon for iPhone/iPad ...

  8. python高级-------python2.7教程学习【廖雪峰版】(四)

    2017年6月9日17:57:55 任务: 看完高级部分 笔记:1.掌握了Python的数据类型.语句和函数,基本上就可以编写出很多有用的程序了.2.在Python中,代码不是越多越好,而是越少越好. ...

  9. element开源框架

    vue-element-admin:https://gitee.com/accest/bod-element   https://gitee.com/liuyuantao/vue-element-ad ...

  10. VC++MFC对话框程序中给对话添加背景图片

    VC对话框怎么显示背景图片呢.在MFC中实现背景图片,不像C#应用程序那么简单.今天就和朋友们说说如何在VC界面中设置背景图片 ^_^   工具/原料 Visual C++ 2010 方法一:用Pic ...