题解 | #牛牛出入圈#
牛牛出入圈
https://www.nowcoder.com/practice/94b5c710f30c490f89be4f08b477edb4
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param enter int整型一维数组 * @param leave int整型一维数组 * @return bool布尔型 */ public boolean validateCowCircle (int[] enter, int[] leave) { // write code here Stack<Integer> cowHost = new Stack<>(); int i = 0; int j = 0; while (i <= enter.length){ while(!cowHost.isEmpty() && cowHost.peek() == leave[j]){ System.out.println(cowHost.peek()); cowHost.pop(); j++; } if (i == enter.length) break; cowHost.push(enter[i]); i++; } boolean ifValid = j >= leave.length ? true : false; return ifValid; } }