elimination-game
Posted 笨鸟居士的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elimination-game相关的知识,希望对你有一定的参考价值。
https://leetcode.com/problems/elimination-game/ // 一行代码就可以,不过原理有些复杂 // https://discuss.leetcode.com/topic/58042/c-1-line-solution-with-explanation // return n == 1 ? 1 : 2 * (1 + n / 2 - lastRemaining(n / 2)); // https://discuss.leetcode.com/topic/59293/easiest-solution-o-logn-with-explanation public class Solution { public int lastRemaining(int n) { boolean isLeft = true; int left = n; int step = 1; int head = 1; while (left != 1) { if (isLeft || left % 2 == 1) { head += step; } step *= 2; left /= 2; isLeft = !isLeft; } return head; } }
以上是关于elimination-game的主要内容,如果未能解决你的问题,请参考以下文章