class Solution { public: int LastRemaining_Solution(int n, int m) { if(m == 0 || n == 0)return -1; list<int> list1; for(int i = 0; i < n; i++){ list1.push_back(i); } list<int>::iterator it = list1.begin(); while(list1.size() > 1){ for(int i = 1 ;i < m; i++){ it++; if(it == list1.end()){ it = list1.begin(); } } it = list1.erase(it); if(it == list1.end()){ it = list1.begin(); } } return list1.front(); } };
这里调用了标准库中的函数。
原文地址:https://www.cnblogs.com/icehole/p/12129551.html
时间: 2024-10-07 02:18:00