赞
踩
题目描述:
1700. 无法吃午餐的学生数量 - 力扣(LeetCode) (leetcode-cn.com)
自测用例:
- [1,1,0,0]
- [0,1,0,1]
- [1,1,1,0,0,1]
- [1,0,0,0,1,1]
- [1]
- [0]
- [1]
- [1]
- [1,1]
- [0,0]
- [1,1,1]
- [1,1,1]
- [1,1,1,1,1,1,1]
- [1,0,1,0,1,0,1]
Java代码:
- class Solution {
- public int countStudents(int[] stu, int[] fd) {
- int fdHd=0;
- for(int i=0,cnt=0;fdHd!=fd.length;i=(i+1)%stu.length){
- if(stu[i]==-1)continue;
- if(stu[i]==fd[fdHd]){
- fdHd++;
- stu[i]=-1;
- cnt=0;
- }else if(++cnt==fd.length-fdHd)break;
- }
- return fd.length-fdHd;
- }
- }
Java代码二:
- class Solution {
- public int countStudents(int[] stu, int[] fd) {
- int[] cnt=new int[2];
- for(int e:stu)cnt[e]++;
- for(int e:fd)if(cnt[e]--==0)break;
- return Math.max(0,cnt[0])+Math.max(0,cnt[1]);
- }
- }
- class Solution {
- public int countStudents(int[] stu, int[] fd) {
- int[] cnt=new int[2];
- for(int e:stu)cnt[e]++;
- for(int e:fd){
- if(cnt[e]==0)break;
- cnt[e]--;
- }
- return cnt[0]+cnt[1];
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。