题目直译法

无法吃午餐的学生数量

https://www.nowcoder.com/practice/2dac3d7567f741a88ec551caf907934d

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param students int整型vector 
     * @param sandwiches int整型vector 
     * @return int整型
     */
    int countStudents(vector<int>& students, vector<int>& sandwiches) {
        // write code here
        queue<int> stuq(deque<int>(students.rbegin(), students.rend()));
        stack<int, vector<int> > sands(vector<int>(sandwiches.rbegin(), sandwiches.rend()));

        int qsize, counts = 0;
        while(!sands.empty()) {
            qsize = stuq.size();
            int stu = stuq.front();
            if(stu == sands.top()) {
                counts = 0;
                stuq.pop();
                sands.pop();
            }
            else {
                if(counts == qsize) break;
                counts++;
                stuq.pop();
                stuq.push(stu);
            }
        }
        return stuq.size();
    }
};

直接按照题目意思去做即可。(第一次自己AC难题,开心)

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务