8.6 第一次做,无法 AC

题目描述


原题链接:https://leetcode-cn.com/problems/yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof/

解题思路


K 神题解:https://leetcode-cn.com/problems/yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof/solution/

8.6 心得

  • 额,这题看题解有点难理解,现阶段先死记叭!
  • x = (x + m) % i
  • i 从 2 开始,因为 1 的结果就是 0,不要从 1 开始了!
    1. class Solution {
    2. public int lastRemaining(int n, int m) {
    3. int x = 0;
    4. for(int i = 2; i <= n; i++) {
    5. x = (x + m) % i;
    6. }
    7. return x;
    8. }
    9. }