#include <queue> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param tickets int整型vector * @param k int整型 * @return int整型 */ int timeRequiredToBuy(vector<int>& tickets, int k) { queue<int> t; int tim=0; for (int i=0; i<tickets.size(); ++i) { t.push(i); } bool flag=true; int index=0; while (flag) { tim++; if (tickets[t.front()]>1) { tickets[t.front()]=tickets[t.front()]-1; index = t.front(); cout<<t.front(); t.pop(); t.push(index); } else { if (t.front()==k) { flag=false; } cout<<t.front(); t.pop(); } } cout<<t.front(); return tim; } };
对于自己由一个经常犯的错误是,总是把判断大小写在了下标里面,“tickets[t.front()>1]”这样是错的,应该是“tickets[t.front()]>1”。>1应该在中括号外面。