Skip to content
New issue

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

剑指 Offer 62. 圆圈中最后剩下的数字 #89

Open
Tcdian opened this issue Mar 30, 2020 · 1 comment
Open

剑指 Offer 62. 圆圈中最后剩下的数字 #89

Tcdian opened this issue Mar 30, 2020 · 1 comment

Comments

@Tcdian
Copy link
Owner

Tcdian commented Mar 30, 2020

剑指 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

输入: n = 5, m = 3
输出: 3

Example 2

输入: n = 10, m = 17
输出: 2

Note

  • 1 <= n <= 10^5
  • 1 <= m <= 10^6
@Tcdian
Copy link
Owner Author

Tcdian commented Mar 30, 2020

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;
};

@Tcdian Tcdian changed the title 《剑指 Offer(第 2 版)》62. 圆圈中最后剩下的数字 剑指 Offer 62. 圆圈中最后剩下的数字 Jun 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant