标签:ati -- subject ini size class 必须 back return
迭代器只有++ ,--,==,!=四种运算方法,不能将iter = iteration+ 1,因为迭代器是指针类型,1是整数类型,不能直接相加赋值给一个指针。
//超时牛客不能通过 class Solution { public: int LastRemaining_Solution(int n, int m){ if(n == 1){ return 0; } if(n == 0){ return -1; } list<int> num; for(int i = 0;i < n;++i){//存储节点 num.push_back(i); } list<int>::iterator iter = num.begin(); while(num.size() > 1){ for(int i = 1;i < m;++i){//找到指定节点 ++iter; if(iter == num.end()){ iter = num.begin(); } } ++iter; list<int>::iterator next = iter;//保存下一节点 if(next == num.end()){ next = num.begin(); } num.erase(--iter);//删除找到的节点 iter = next; } return *iter; } };
解法2:
class Solution { public: int LastRemaining_Solution(int n, int m){ if(n < 1 ||m < 1){ return -1; } int last = 0; for(int i = 1;i <= n;++i){ last = (last + m) % i; } return last; } };
标签:ati -- subject ini size class 必须 back return
原文地址:http://www.cnblogs.com/dingxiaoqiang/p/7498196.html