解法:用栈暴力循环遍历,如果不匹配计算循环的次数,然后终止循环
class Solution {
public int countStudents(int[] students, int[] sandwiches) {
LinkedList<Integer> stu = new LinkedList<>();
for(int i = students.length - 1;i >= 0;i -- ){
stu.offer(students[i]);
}
int index = 0;
int flag = 0; // 计算循环次数
while (!stu.isEmpty()){
int peek = stu.poll();
if(peek == sandwiches[index]){
index++;
flag = 0;
}else{
stu.offer(peek);
flag++;
if(flag == sandwiches.length - index)return stu.size();
}
}
return 0;
}
}