题解 | #牛牛出入圈#
牛牛出入圈
https://www.nowcoder.com/practice/94b5c710f30c490f89be4f08b477edb4
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param enter int整型一维数组 * @param leave int整型一维数组 * @return bool布尔型 */ public bool validateCowCircle (List<int> enter, List<int> leave) { // write code here Stack<int> stack = new Stack<int>(); int i = 0; int j = 0; while (i < enter.Count) { if (stack.Count != 0 && stack.Peek() == leave[j]) { stack.Pop(); j++; continue; } if (enter[i] != null) { stack.Push(enter[i]); i++; } } while (j < leave.Count) { if (stack.Count != 0 && stack.Peek() == leave[j]) { stack.Pop(); j++; } else { return false; } } return true; } }