java实现找出数组中出现次数超过一半的数字
2017-06-05 15:33
846 查看
package Nowcode; /** * 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。 * * @author Administrator 如果把这个数组排序那么排序之后数组中间的那个数一定就是那个出现次数超过数组长度一半的数字 * */ public class MoreThanHalfNum { // 解法一o(n):基于快速排序的分割思想:在随机快速排序算法中,我们先在数组中随机选择一个数字,然后调整数组中数字的顺序,使得比选中的数字小的数字排在它的左边,比选中 // 的数字大的排在它的右边,如果选中的数字的下标刚好是n/2,那么这个数字就是数组的中位数 public int MoreThanHalfNum_Solution(int[] array) { if (array == null) return 0; int length = array.length; int start = 0; int end = length - 1; int middle = length >> 2; int index = Partition(array, length, start, end); // 如果选中的数字的下标刚好是n/2,那么这个数字就是数组的中位数 while (index != middle) { // 如果选中的数字的下标刚好是n/2右边,调整左边数字 if (index > middle) { end = index - 1; index = Partition(array, length, start, end); } else { // 如果选中的数字的下标刚好是n/2左边,调整右边数字 start = index + 1; index = Partition(array, length, start, end); } } // 最后验证这个数字是不是真的超过数组的一半 int count = 0; for (int i = 0; i < array.length; i++) { if (array[i] == array[index]) count++; } if (count * 2 > array.length) return array[index]; else return 0; } // 在数组中选择一个数,比选择的数字小的数字移到数组的左边,比选择数字大的移动到数组的右边 int Partition(int[] data, int length, int start, int end) { if (data.length == 0 || length <= 0 || start < 0 || end >= length) { return -1; } // 选择从最后一个数为基准元素开始一次排序 int index = start - 1; for (int i = start; i < end; ++i) { if (data[i] < data[end]) // 以最后一个元素为基准点进行划分 { ++index; if (index != i) { // 交换 int temp = data[i]; data[i] = data[index]; data[index] = temp; } } } ++index; // 交换 int temp = data[index]; data[index] = data[end]; data[end] = temp; // System.out.print("index=" + index + " "); return index; // 返回一次排序后开始选择的数此刻所在最终位置索引 } public static void main(String[] args) { int[] array = { 4, 2, 4, 4, 2, 4 }; moreHalfNum m = new moreHalfNum(); System.out.println(m.MoreThanHalfNum_Solution(array)); } }package Nowcode;/*** 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0** @author Administrator**/public class moreHalfNum {// 方法二O(n):采用阵地攻守的思想public int MoreThanHalfNum_Solution(int[] array) {if (array == null)return 0;// 第一个数字作为第一个士兵,守阵地;count = 1;int result = array[0];int count = 1;for (int i = 1; i < array.length; i++) {// 当遇到count为0的情况,又以新的i值作为守阵地的士兵,继续下去if (count == 0) {result = array[i];count = 1;// 遇到相同元素,count++;} else if (array[i] == result)count++;// 遇到不相同元素,即为敌人,同归于尽,count--;elsecount--;}// 到最后还留在阵地上的士兵,有可能是主元素count = 0;for (int i = 0; i < array.length; i++) {if (array[i] == result)count++;}// 验证最后活下来的数字个数是不是超过数组的一半if (count * 2 > array.length)return result;elsereturn 0;}public static void main(String[] args) {// int [] array={1,2,3,2,2,2,5,4,2};// int [] array={1,2,3,2,4,2,5,2,3};int[] array = { 4, 2, 1, 4, 2, 4 };moreHalfNum m = new moreHalfNum();System.out.println(m.MoreThanHalfNum_Solution(array));}}
相关文章推荐
- 数组中有一个数字出现的次数超过了数组长度的一半,请找出这个数。java实现
- 数组中出现次数超过一半的数字(Java实现)
- 剑指Offer:面试题29——数组中出现次数超过一半的数字(java实现)
- java实现:数组中出现次数超过一半的数字
- 基于Java代码实现数字在数组中出现次数超过一半
- 数据结构与算法分析笔记与总结(java实现)--数组4:数组中出现次数超过一半的数字
- PHP实现找出数组中出现次数超过数组长度一半的数字算法示例
- 剑指Offer面试题29:数组中出现次数超过一半的数字 Java实现
- 找出数组中出现次数大于一半的数字 Java实现 剑指offer
- 《剑指offer》编程题java实现(十):数组中出现次数超过一半的数字
- Java实现 找出数组中出现次数超过数组长度一半的元素
- 剑指offer第二十八题【数组中出现次数超过一半的数字】c++实现
- 找出数组中出现次数超过一半的数字
- 数组中有一个数字出现的次数超过了数组长度的一半,找出这个数
- 【c语言】数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字
- 【剑指offer-Java版】29数组中出现次数超过一半的数字
- 剑指Offer面试题29(java版):数组中出现次数超过一半的数字
- java之数组中出现次数超过一半的数字
- 数组中有一个数字出现的次数超过了数组长度的一半,找出这个数字。
- C++数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。(牛客剑指offer)