题解
根据学生是循环队列,所以结果与学生的排列方式是无关的,只与0和1在学生中的数量有关。
接下来模拟三明治出栈就可以了,有两种结束方式,一种是所有三明治都没了,一种是三明治对应的学生没了。
代码
class Solution(object):
def countStudents(self, students, sandwiches):
"""
:type students: List[int]
:type sandwiches: List[int]
:rtype: int
"""
count = [students.count(0), students.count(1)]
while(True):
if len(sandwiches) == 0:
return 0
if count[sandwiches[0]] != 0:
count[sandwiches[0]] -= 1
del sandwiches[0]
else:
return sum(count)
emmm
妙妙妙,python异或运算实现:link