题目网址:https://leetcode.com/contest/2/problems/elimination-game/

题意: 给定一个从1到n的数列,第一次从最左边开始,每隔一个淘汰一个数字。然后从剩下的数字中,最右边的数字开始,每隔一个淘汰一个数字。重复上述步骤,求最后剩下的那个数字。

解析:是一个模拟题。只需要确定每趟淘汰赛开始时的数字即可(最后一个数字就是最后一趟淘汰赛的开始的数字)。

假设第i趟开始的数字为start,此时等差数列的差(distance)为2^i,数字个数为n/(2^i)  (t)

则最后一个删除的数字与第一个删除的数字的差为distance*(t-1),且最后一个删除的数字与下一趟第一个删除的数字的差为distance/2.

另外,需要注意的一点是,当n=2*k + 1时与 n-1 = 2*k,最后剩下的数字相同。代码如下:

    int lastRemaining(int n) {
int ans = 1;
int cnt = 0;
int distance = 1; while(n > 1){ n /= 2;
cnt ++;
distance *= 2;
if(cnt&1){
ans += distance*(n-1) + distance/2;
}
else{
ans -= distance*(n-1) + distance/2;
}
}
return ans;
}

  

Leetcode Elemination Game的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

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

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

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. jQuery on 绑定的事件触发多次

    jquery用on绑定事件,在代码执行过程中,可能会遇到多次执行的情况. 解决方案是在on的事件前面加上一个off,再on. $('#btnBind').off('click').on('click' ...

  2. jquery 调用函数时加()和不加()的执行顺序是不同的

    编写JQUERY(3.0,向下兼容3.0)代码当我们调用一个函数时可以不加括号,但加括号与不加括号是不同的.如下代码: $(function(){ a(b);//先打印a 再打印 b a(b());/ ...

  3. Opencv出现错误“0xc000007b”的解决办法

    装了一下午的opencv.之前用VS2010装过opencv,当时使用的是cmake编译源码的办法,这个方法好处就是不用每新建一个工程就重新链接opencv库文件.今天装了个VS2013,再装open ...

  4. js中array的filter用法

    function bouncer(arr) { // Don't show a false ID to this bouncer. arr = arr.filter(function(val) { i ...

  5. python3 中mlpy模块安装 出现 failed with error code 1的决绝办法(其他模块也可用本方法)

    在python3 中安装其它模块时经常出现 failed with error code 1等状况,使的安装无法进行.而解决这个问题又非常麻烦. 接下来以mlpy为例,介绍一种解决此类安装问题的办法. ...

  6. css3 text-overflow属性

    页面: <ul> <li>· 测试测试测试测试测试测试</li> <li>· 测试测试测试测试测试测试</li> <li>· 测 ...

  7. ubuntu 'Unable to correct problems, you have held broken packages' 错误

    在用apt 安装软件时,有时会用国内的源以加快下载速度. 但是在使用ubuntu 14.04的过程中,这一过程可能会导致错误“Unable to correct problems, you have ...

  8. VUE 入门基础(6)

    六,条件渲染 v-if 添加一个条件块 <h1 v-if="ok">Yes</h1> 也可以用v-else 添加else 块 <template> ...

  9. sql搜索like通配符的用法详解

    http://www.lmwlove.com/ac/ID878 有很多朋友写了几年的like搜索,可能对like后面通配符的用法都不了解,甚至于%的作用是什么都不清楚.在这篇文章中,我们就一起来学习一 ...

  10. VC++ GDI 总结 一一 CBitmap类

    class CBitmap : public CGdiObject { DECLARE_DYNAMIC(CBitmap) public: static CBitmap* PASCAL FromHand ...