详见:https://leetcode.com/problems/elimination-game/description/

C++:

方法一:

class Solution {
public:
int lastRemaining(int n) {
return help(n, true);
}
int help(int n, bool left2right)
{
if (n == 1)
{
return 1;
}
if (left2right)
{
return 2 * help(n / 2, false);
}
else
{
return 2 * help(n / 2, true) - 1 + n % 2;
}
}
};

方法二:

class Solution {
public:
int lastRemaining(int n) {
return n==1?1:2*(1+n/2-lastRemaining(n/2));
}
};

方法三:

class Solution {
public:
int lastRemaining(int n) {
int base = 1, res = 1;
while (base * 2 <= n)
{
res += base;
base *= 2;
if (base * 2 > n)
{
break;
}
if ((n / base) % 2 == 1)
{
res += base;
}
base *= 2;
}
return res;
}
};

详见:https://www.cnblogs.com/grandyang/p/5860706.html

390 Elimination Game 淘汰游戏的更多相关文章

  1. [LeetCode] 390. Elimination Game 淘汰游戏

    There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number ...

  2. [LeetCode] Elimination Game 淘汰游戏

    There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number ...

  3. 【LeetCode】390. Elimination Game 解题报告(Python)

    [LeetCode]390. Elimination Game 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/elimina ...

  4. 390. Elimination Game

    正规解法直接跳到代码上面一点的部分就可以了.但我想记录下自己的思考和尝试过程,希望二刷能看到问题所在. 找规律的时候写了好多,虽然规律很简单. 只要随便写3以上的例子,就应该发现,相邻的2个最后结果是 ...

  5. [leetcode] 390 Elimination Game

    很开心,自己想出来的一道题 There is a list of sorted integers from 1 to n. Starting from left to right, remove th ...

  6. 【leetcode】390. Elimination Game

    题目如下: 解题思路:对于这种数字类型的题目,数字一般都会有内在的规律.不管怎么操作了多少次,本题的数组一直是一个等差数列.从[1 2 3 4 5 6 7 8 9] -> [2 4 6 8] - ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. Python----多元线性回归

    多元线性回归 1.多元线性回归方程和简单线性回归方程类似,不同的是由于因变量个数的增加,求取参数的个数也相应增加,推导和求取过程也不一样.. y=β0+β1x1+β2x2+ ... +βpxp+ε 对 ...

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. Ubuntu12.04之SSH

    Ubuntu 12.04 关于SSH的知识 (1)安装完ubuntu系统12.04. (2)查看网络配置,输入命令ip addr后,显示有IP地址. (3)使用SSH终端工具Xshell连接系统,发现 ...

  2. POJ 3169_Layout

    大早上水一发=.= 题意: n头牛按编号顺序站成一列,给定n头牛之间的位置关系,求出第n头牛和第一头牛之间的最大距离. 分析: 差分约束系统,这题不等式关系还是挺好找的.注意因为按照顺序排列,所以有d ...

  3. codevs——1031 质数环

    1031 质数环  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 一个大小为N(N<=17 ...

  4. Eclipse-Java代码规范和质量检查插件-SonarLint

    SonarQube(Sonar)之前的提供的本地工具是需要依赖SonarQube服务器的,这样导致其运行速度缓慢. 新出的SonarLint的扫描引擎直接安装在本地,速度超快,实时探测代码技术债务,给 ...

  5. Hive之执行计划分析(explain)

    Hive是通过把sql转换成对应mapreduce程序,然后提交到Hadoop上执行,查看具体的执行计划可以通过执行explain sql知晓 一条sql会被转化成由多个阶段组成的步骤,每个步骤有执行 ...

  6. laravel有用的方法

    1.tinker 造假数据 factory('App\User',3)->create(); DB::table 返回collection,可以用collection中的很多方法 比如-> ...

  7. Zabbix ---proxy 代理

    Zabbix zabbix 官网 : https://www.zabbix.com/ 环境准备: 三台服务器: server 端: 192.168.206.6 proxy 端 :  192.168.2 ...

  8. WebLog Expert

    Weblog expert是一个快速和强大的访问日志分析器.这会让你了解你的网站的访客:活动统计,访问的文件的路径,通过该网站,信息指页面,搜索引擎,浏览器,操作系统,和更多.该计划所产生的易于阅读的 ...

  9. 一个有趣的问题:ls -l显示的内容中total究竟是什么?

    当我们在使用ls -l的命令时,我们会看到例如以下类似的信息. 非常多人可能对于第一行的total 12的数值并非非常在意,可是你是否想过,它到底是什么意思? man中的说明,我们能够看出total的 ...

  10. 使用adb在电脑和手机间传文件

    首先须要root手机. 然后,"Win + R",打开cmd窗体.以下以copy d:\1.txt到/system/文件夹为例说明. adb push source(localpa ...