LeetCode 1823. 找出游戏的获胜者
思路
循环数字
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| class Solution { public: int findTheWinner(int n, int k) { vector<int> players(n+1); int total=n; int p=0; while(total!=1){ for(int i=0;i<k;i++){ p=p%n+1; if(players[p]==1){ i--; } } players[p]=1; total--; } for(int i=0;i<k;i++){ p=p%n+1; if(players[p]==1){ i--; } } return p; } };
|