剑指offer 面试题29 数组中出现次数超过一半的数字
来源:互联网 发布:手机间谍软件官方网站 编辑:程序博客网 时间:2024/05/21 10:03
剑指offer 面试题29 数组中出现次数超过一半的数字
题目:
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。
例如输入一个长度为 9 的数组 {1, 2, 3, 2, 2, 2, 5, 4, 2}。
由于数字 2 在数组中出现了 5 次,超过数组长度的一半,因此输出 2。
package algorithm.foroffer.top30;/** * Created by liyazhou on 2017/5/29. * 面试题29:数组中出现次数超过一半的数字 * * 题目: * 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。 * 例如输入一个长度为 9 的数组 {1, 2, 3, 2, 2, 2, 5, 4, 2}。 * 由于数字 2 在数组中出现了 5 次,超过数组长度的一半,因此输出 2。 * * 问题: * 1. 对如此题目晕菜,第一感觉就是统计每一个数字出现的次数 * * 思路: * 1. 遍历数组时,保存两个值,一个是数组中的一个数字,一个是次数。 * 当遍历到下一个数字时候,如果其和之前保存的数字相同,则次数加 1,否则次数减 1。 * 如果次数为零,则保存下一个数字,并把次数设为 1。 * 最后一次把次数设置为 1 时,保存的数字即为出现次数超过数组长度一半的数字。 */public class Test29 { public static int findNum(int[] array){ if (array == null) throw new RuntimeException("Invalid input, array is null."); int num = array[0]; int times = 1; for (int i = 1; i < array.length; i ++){ if (times == 0){ // // 如果次数等于0,则保存当前数组,并将次数设置为1 num = array[i]; times ++; }else{ // 如果次数不等于0,若当前值跟保存值相等则次数加1,否则减1 if (array[i] == num) times ++; else times --; } } return num; } public static void main(String[] args){ int[][] numbers = { {1, 2, 3, 2, 2, 2, 5, 4, 2}, {1, 2, 3, 2, 2, 2, 5, 4, 2, 1, 1, 1, 1, 1, 1, 1, 1}, }; for (int i = 0; i < numbers.length; i++) System.out.println(findNum(numbers[i])); }}
另外,牛客上的额外要求是,如果不存在过半的数字则返回0,代码如下:
public static int findNum2(int[] array){ if (array == null) throw new RuntimeException("Invalid input, array is null."); int num = array[0]; int times = 1; for (int i = 1; i < array.length; i ++){ if (times == 0){ // // 如果次数等于0,则保存当前数组,并将次数设置为1 num = array[i]; times = 1; }else{ // 如果次数不等于0,若当前值跟保存值相等则次数加1,否则减1 if (array[i] == num) times ++; else times --; } } // 验证是否过半 int counter = 0; for (int i = 0; i < array.length; i ++){ if (array[i] == num) counter ++; } // 如果不过半,则返回0 if (counter <= array.length/2) num = 0; return num; }
阅读全文
1 0
- [剑指offer][面试题29]数组中出现次数超过一半的数字
- 剑指Offer:面试题29 数组中出现次数超过一半的数字
- 剑指offer 面试题29—数组中出现次数超过一半的数字
- 《剑指Offer》学习笔记--面试题29:数组中出现次数超过一半的数字
- 【剑指Offer学习】【面试题29 :数组中出现次数超过一半的数字】
- 剑指Offer面试题29(java版):数组中出现次数超过一半的数字
- 剑指offer面试题29-数组中出现次数超过一半的数字
- 剑指offer-面试题29:数组中出现次数超过一半的数字
- 剑指offer之面试题29:数组中出现次数超过一半的数字
- 剑指offer之面试题29数组中出现次数超过一半的数字
- 剑指Offer----面试题29:数组中出现次数超过一半的数字
- 剑指offer-----面试题29(数组中出现次数超过一半的数字)
- 剑指Offer面试题29:数组中出现次数超过一半的数字 Java实现
- 剑指offer面试题29:数组中出现次数超过一半的数字
- 剑指offer--面试题29:数组中出现次数超过一半的数字
- 剑指Offer面试题29(java版):数组中出现次数超过一半的数字
- 剑指offer——面试题29:数组中出现次数超过一半的数字
- 剑指Offer之面试题29:数组中出现次数超过一半的数字
- java中多线程编程
- UVa 201 Squares
- 【简记】大规模Web开发技术(第十二章)
- uestc L
- python3学习--4 字典
- 剑指offer 面试题29 数组中出现次数超过一半的数字
- linux tomcat具体安装
- 我是一个线程
- 我和程序结下了一生的缘分
- 什么是单例模式?单例模式有哪些?单例模式怎么写?
- DrawerLayout侧滑高斯模糊实现
- 明年也许不会在写代码了
- 常见算法时间复杂度比较
- [hackinglab] 逗比验证码第二期