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 class_2022_08_1_week;
// 找到数组中的水王数
// 本题来自,大厂刷题班,23节
// 为了讲述下一个题,才重新讲述这个题
// 比较简单
public class Code02_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;