This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
package 第02期.mca_01;
// 找到数组中的水王数
// 本题来自,大厂刷题班,23节
public class Problem01_WaterKing {
public static int waterKing(int[] arr) {
int cand = 0;
int hp = 0;
for (int i = 0; i < arr.length; i++) {
if (hp == 0) {
cand = arr[i];
hp = 1;
} else if (arr[i] == cand) {
hp++;
} else {
hp--;
}
return -1;
hp = 0;
if (arr[i] == cand) {
return hp > arr.length / 2 ? cand : -1;