P163、面试题29:数组中出现次数超过一半的数字
题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。
package com.yyq;
/**
* Created by gao on 15-10-23.
*/
public class MoreThanHalfNumber {
public static boolean g_bInputInvalid = false;
public static boolean checkInvalidArray(int[] numbers, int length) {
g_bInputInvalid = false;
if (numbers == null || length <= 0) {
System.out.printf("Numbers is null! Invalid Parameters.");
g_bInputInvalid = true;
}
return g_bInputInvalid;
}
public static boolean checkMoreThanHalf(int[] numbers, int length, int number) {
int times = 0;
for (int i = 0; i < length; i++) {
if (number == numbers[i])
times++;
}
boolean isMoreThanHalf = true;
if (times * 2 <= length) {
System.out.println("the number you find is not more than half.");
isMoreThanHalf = false;
g_bInputInvalid = true;
}
return isMoreThanHalf;
}
public static int Partition(int[] numbers, int length, int start, int end) {
if (numbers == null || length <= 0 || start < 0 || end >= length) {
System.out.printf("Invalid Parameters!");
return -1;
}
int index = numbers[start];
while (start < end) {
while (start < end && numbers[end] >= index) end--;
numbers[start] = numbers[end];
while (start < end && numbers[start] <= index) start++;
numbers[end] = numbers[start];
}
numbers[start] = index;
return start;
}
//==================方法一:partition========================
public static int moreThanHalfNum_solution1(int[] numbers, int length) {
if (checkInvalidArray(numbers, length)) {
return 0;
}
int middle = length >> 1;
int start = 0;
int end = length - 1;
int index = Partition(numbers, length, start, end);
while (index != middle) {
if (index > middle) {
end = index - 1;
index = Partition(numbers, length, start, end);
} else {
start = index + 1;
index = Partition(numbers, length, start, end);
}
}
int result = numbers[middle];
if (!checkMoreThanHalf(numbers, length, result)) {
result = 0;
}
return result;
}
public static int moreThanHalfNum_solution2(int[] numbers, int length) {
if (checkInvalidArray(numbers, length)) {
return 0;
}
int result = numbers[0];
int times = 1;
for(int i = 1; i < length; i++){
if (times == 0){
result = numbers[i];
times = 1;
}
else if(numbers[i] == result){
times ++;
}else{
times--;
}
}
if (!checkMoreThanHalf(numbers, length, result)) {
result = 0;
}
return result;
}
// ====================测试代码====================
public static void Test(String testName, int[] numbers, int length, int expectedValue, boolean expectedFlag)
{
if(testName != null)
System.out.println("\n"+testName+" begins: ");
int[] copy = new int[length];
for(int i = 0; i < length; ++i)
copy[i] = numbers[i];
System.out.printf("Test for solution1: ");
int result = moreThanHalfNum_solution1(numbers, length);
if(result == expectedValue && g_bInputInvalid == expectedFlag)
System.out.println("Passed.");
else
System.out.println("Failed.");
System.out.printf("Test for solution2: ");
result = moreThanHalfNum_solution2(copy, length);
if(result == expectedValue && g_bInputInvalid == expectedFlag)
System.out.println("Passed.");
else
System.out.println("Failed.");
copy = null;
}
// 存在出现次数超过数组长度一半的数字
public static void Test1()
{
int numbers[] = {1, 2, 3, 2, 2, 2, 5, 4, 2};
Test("Test1", numbers, numbers.length, 2, false);
}
// 不存在出现次数超过数组长度一半的数字
public static void Test2()
{
int numbers[] = {1, 2, 3, 2, 4, 2, 5, 2, 3};
Test("Test2", numbers, numbers.length, 0, true);
}
// 出现次数超过数组长度一半的数字都出现在数组的前半部分
public static void Test3()
{
int numbers[] = {2, 2, 2, 2, 2, 1, 3, 4, 5};
Test("Test3", numbers, numbers.length, 2, false);
}
// 出现次数超过数组长度一半的数字都出现在数组的后半部分
public static void Test4()
{
int numbers[] = {1, 3, 4, 5, 2, 2, 2, 2, 2};
Test("Test4", numbers, numbers.length, 2, false);
}
// 数组为1个元素
public static void Test5()
{
int numbers[] = {1};
Test("Test5", numbers, 1, 1, false);
}
// 输入空指针
public static void Test6()
{
Test("Test6", null, 0, 0, true);
}
public static void main(String[] args)
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
}
}
Test1 begins:Test for solution1: Passed.Test for solution2: Passed. Test2 begins:Test for solution1: the number you find is not more than half.Passed.Test for solution2: the number you find is not more than half.Passed. Test3 begins:Test for solution1: Passed.Test for solution2: Passed. Test4 begins:Test for solution1: Passed.Test for solution2: Passed. Test5 begins:Test for solution1: Passed.Test for solution2: Passed. Test6 begins:Test for solution1: Numbers is null! Invalid Parameters.Passed.Test for solution2: Numbers is null! Invalid Parameters.Passed. Process finished with exit code 0
P163、面试题29:数组中出现次数超过一半的数字的更多相关文章
- 剑指Offer:面试题29——数组中出现次数超过一半的数字(java实现)
PS:在前几天的面试中,被问到了这个题.然而当时只能用最低效的方法来解. 问题描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2, ...
- 《剑指offer》面试题39. 数组中出现次数超过一半的数字
问题描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 你可以假设数组是非空的,并且给定的数组总是存在多数元素. 示例 1: 输入: [1, 2, 3, 2, 2, 2, 5, 4, ...
- 面试题五 数组中出现次数超过一半的数字 时间为O(n)
也就是说 该数字出现的次数比其他所有数字出现次数的和还要多. 因此可以保存两个值,一个数字,一个次数. 遍历时 1.如果数字相同,count++ 2.如果count == 0 count = 1 nu ...
- Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)
剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...
- 九度OJ 1370 数组中出现次数超过一半的数字
题目地址:http://ac.jobdu.com/problem.php?pid=1370 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2 ...
- 《剑指offer》第三十九题(数组中出现次数超过一半的数字)
// 面试题39:数组中出现次数超过一半的数字 // 题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例 // 如输入一个长度为9的数组{1, 2, 3, 2, 2, 2, 5, ...
- 数组中出现次数超过一半的数字 -java
数组中出现次数超过一半的数字 -java 方法一: 数组排序,然后中间值肯定是要查找的值. 排序最小的时间复杂度(快速排序)O(NlogN),加上遍历. 方法二: 使用散列表的方式,也就是统计每个数组 ...
- 【C语言】统计数组中出现次数超过一半的数字
//统计数组中出现次数超过一半的数字 #include <stdio.h> int Find(int *arr, int len) { int num = 0; //当前数字 int ti ...
- 《剑指offer》— JavaScript(28)数组中出现次数超过一半的数字
数组中出现次数超过一半的数字 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超 ...
- 剑指Offer - 九度1370 - 数组中出现次数超过一半的数字
剑指Offer - 九度1370 - 数组中出现次数超过一半的数字2013-11-23 03:55 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组 ...
随机推荐
- Windows内存管理[转]
本文主要内容:1.基本概念:物理内存.虚拟内存:物理地址.虚拟地址.逻辑地址:页目录,页表2.Windows内存管理3.CPU段式内存管理4.CPU页式内存管理 一.基本概念1. 两个内存概念物理内存 ...
- log4j配置只打印指定jar或包的DEBUG信息
有的时候查问题的时候需要打印第三方jar里面的debug信息,假如全部jar都打印的话日志文件会很大,这个时候可以配置log4j只打印指定jar的debug信息或者包,同时输出到了一个新的文件中. 比 ...
- shadow服务端、客户端配置流程
服务端 系统环境 CentOS 7 64位,由于系统自带python,shadowsocks服务端我们选择python版,过程如下 yum install python-setuptools & ...
- [Accessibility] Missing contentDescription attribute on image [可取行]失踪contentDescription属性图像
问题描述: [Accessibility] Missing contentDescription attribute on image [可取行]失踪contentDescription属性图像 原因 ...
- Jquery实现搜索框提示功能
博客的前某一篇文章中http://www.cnblogs.com/hEnius/p/2013-07-01.html写过一个用Ajax来实现一个文本框输入的提示功能.最近在一个管理项目的项目中,使用后发 ...
- 使用OpenXml操作Excel,以下方法用于在添加列时修改Cell的CellReference属性。
以下方法实现了递增Excel中单元格的CellReference的功能,只支持两位字母. public static string CellReferenceIncrement(string cell ...
- Microsoft Expression Blend 4制作简单的按钮
在博客园混了这么久了,第一次写博客.本人标准的理工男,文笔不敢说一般,只能用还学过语文.勉强达意而已.见笑!! 由于本人能有有限,错误之处在所难免,望大牛们批评指正,共同进步.^_^!!!!!!!!! ...
- if...else..的错误用法
1.最近在写js代码完成一个前段DOM操作的函数时,自己错误的使用了if..else..控制体.为什么是错误的呢?看看我的 代码你就明白了: document.getElementsByClassNa ...
- 解决maven官方库中没有oracle jdbc驱动的问题:Missing artifact com.oracle:ojdbc14:jar:10.2.0.1.0
最近在整合SSHE项目时,想要添加Oracle驱动包时,Maven的pom.xml总是报Missing artifact com.oracle:ojdbc14:jar:10.2.0.1.0错, 下面我 ...
- 10个优秀的Objective-C和iOS开发在线视频教程
如果你自己开发iOS应用,你肯定会发现网上有很多资源.学习编程的一个最好的方法就是自己写代码,而开始写代码的最快的方式就是看其他人怎么写.我们从海量视频和学习网站中整理出了我 如果你自己开发iOS应用 ...