GitHub Viewer
package basic;
/**
* Created by zxc on 2018/3/23.
*/
public class ArithmeticTwo {
public static void main(String[] args) {
int[] arrays = {5, 5, 6, 4, 4, 6, 3};
//singleNumber(arrays);
drawStar();
}
/**
* 画星星
*/
public static void drawStar() {
// 我要画5行的星星
int row = 5;
for (int i = 1; i 继续入栈
stack[++front] = arrays[i];
} else {
// 只要不一致,就出栈
front--;
}
}
// 只要该数字出现次数大于数组长度的2/1,那么留下来的数字肯定在栈顶中
System.out.println("关注公众号:Java3y--->" + stack[0]);
}
public static void findMajorityElement2(int[] arrays) {
// 装载栈的元素
int candidate = -1;
// 栈的大小(长度)
int count = 0;
// 遍历给出的数组
for (int i = 0; i < arrays.length; i++) {
// 判断该栈为空,那么直接将元素入栈
if (count == 0) {
candidate = arrays[i];
count++;
} else if (candidate == arrays[i]) { // 该元素是否与栈的元素一致-->入栈(栈多一个元素)
count++;
} else {
// 只要不一致-->出栈(栈少一个元素)
count--;
}
}
// 只要该数字出现次数大于数组长度的2/1,那么留下来的数字肯定在栈顶中
System.out.println("关注公众号:Java3y--->" + candidate);
}
}