We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
0,1,,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字。求出这个圆圈里剩下的最后一个数字。
例如,0、1、2、3、4这5个数字组成一个圆圈,从数字0开始每次删除第3个数字,则删除的前4个数字依次是2、0、4、1,因此最后剩下的数字是3。
输入: n = 5, m = 3 输出: 3
输入: n = 10, m = 17 输出: 2
1 <= n <= 10^5
1 <= m <= 10^6
The text was updated successfully, but these errors were encountered:
官方题解
JavaScript Solution
/** * @param {number} n * @param {number} m * @return {number} */ var lastRemaining = function(n, m) { let d = 0; for (let i = 2; i <= n; i++) { d = (m % i + d) % i; } return d; };
Sorry, something went wrong.
No branches or pull requests
剑指 Offer 62. 圆圈中最后剩下的数字
0,1,,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字。求出这个圆圈里剩下的最后一个数字。
例如,0、1、2、3、4这5个数字组成一个圆圈,从数字0开始每次删除第3个数字,则删除的前4个数字依次是2、0、4、1,因此最后剩下的数字是3。
Example 1
Example 2
Note
1 <= n <= 10^5
1 <= m <= 10^6
The text was updated successfully, but these errors were encountered: