赞
踩
通过万岁!!!
java代码——写法一
class Solution { public int countStudents(int[] students, int[] sandwiches) { int len = students.length; int oneSum = Arrays.stream(students).sum(); int zeroSum = len - oneSum; int sanIdx = 0; int stuIdx = 0; boolean[] vis = new boolean[len]; while (true) { if (sandwiches[sanIdx] == 1 && oneSum == 0) { return zeroSum; } if (sandwiches[sanIdx] == 0 && zeroSum == 0) { return oneSum; } if (!vis[stuIdx % len] && sandwiches[sanIdx] == students[stuIdx % len]) { vis[stuIdx % len] = true; if (sandwiches[sanIdx] == 0) { zeroSum--; } else { oneSum--; } sanIdx++; } stuIdx++; if (zeroSum == 0 && oneSum == 0) { return 0; } } }}
java代码——写法2
class Solution { public int countStudents(int[] students, int[] sandwiches) { int oneSum = Arrays.stream(students).sum(); int zeroSum = students.length - oneSum; for (int i = 0; i < sandwiches.length; i++) { if (sandwiches[i] == 0 && zeroSum > 0) {// 有人吃 zeroSum--; } else if (sandwiches[i] == 1 && oneSum > 0) { oneSum--; } else {// 没人吃 break; } } return oneSum + zeroSum; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。