GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — zc/newcoder/codinginterviews/MoreThanHalfNum.java at master · codeaike/zc

Expand file tree

/

MoreThanHalfNum.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
138 lines (115 loc) · 4.04 KB
/

MoreThanHalfNum.java

Copy path

File metadata and controls

138 lines (115 loc) · 4.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.my.test.codinginterviews.array;
/**
* 题目:
* 数组中出现次数超过一半的数字 -- newcoder 剑指Offer 28
*
* 题目描述:
* 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。
*
* 例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。
* 由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。
* 如果不存在则输出0。
*/
public class MoreThanHalfNum {
/**
* 思路:
* 1、快排思想,获取排序后数组中间的数字(利用快排思想,不需要完成整个快排)
* 2、超过一半的数组必然位于排序后的数组中间
* 3、再确认该数字的出现次数是否超过一半
*/
public static int MoreThanHalfNum_Solution(int[] array) {
if (array == null || array.length <= 0) {
return 0;
}
int len = array.length;
int mid = getMiddle(array, 0, len-1);
while (mid != len/2) {
if (mid < len/2) {
mid = getMiddle(array, mid + 1, len-1);
} else {
mid = getMiddle(array, 0, mid - 1);
}
}
// 获取数组的中位数
int num = array[mid];
// 判断数据的出现次数 是否真正超过一半
int numCount = 0;
for (int i = 0; i< len; i++) {
if (array[i] == num) {
numCount++;
}
}
// 无出现次数大于一半的数
if (numCount * 2 <= len) {
return 0;
}
return num;
}
private static int getMiddle(int arr[], int low, int high) {
int base = arr[low];
while (low < high) {
while ( low < high && base <= arr[high]) {
high--;
}
arr[low] = arr[high];
while ( low < high && base >= arr[low]) {
low++;
}
arr[high] = arr[low];
}
// 基准值左侧小于基准值,右侧大于基准值
arr[low] = base;
return low;
}
/**
* 思路:
* 1、根据数字特征,因为数组中该数字出现次数超过一半
* 2、若遍历数组,记录该元素,遇到相同的就次数加1,不同的减一,为负数时,更换元素,
* 3、则最后一个必然是出现次数超过一半的数字
*/
public static int MoreThanHalfNum_SolutionII(int[] array) {
if (array == null || array.length <= 0) {
return 0;
}
int len = array.length;
int curNum = array[0];
int count = 1;
for (int i=1; i<len; i++) {
// 相同则增加count
if (array[i] == curNum) {
count++;
} else {
// 不同则减少
if (count > 0) {
count --;
// 出现负值情况则重置数据
} else {
curNum = array[i];
count = 1;
}
}
}
// 判断数据的出现次数 是否真正超过一半
int numCount = 0;
for (int i = 0; i< len; i++) {
if (array[i] == curNum) {
numCount++;
}
}
// 无出现次数大于一半的数
if (numCount * 2 <= len) {
return 0;
}
return curNum;
}
public static void main(String[] args) {
int[] arr1 = new int[]{1,4,3,4,4,4,5,6,4,4,4,1};
System.out.println(MoreThanHalfNum_Solution(arr1));
int[] arr2 = new int[]{1,4,3,1,5,6,4,4};
System.out.println(MoreThanHalfNum_Solution(arr2));
int[] arr3 = new int[]{1,4,3,4,4,4,5,6,4,4,4,1};
System.out.println(MoreThanHalfNum_SolutionII(arr3));
int[] arr4 = new int[]{1,4,3,1,5,6,4,4};
System.out.println(MoreThanHalfNum_SolutionII(arr4));
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.