题目如下:

解题思路:对于这种数字类型的题目,数字一般都会有内在的规律。不管怎么操作了多少次,本题的数组一直是一个等差数列。从[1 2 3 4 5 6 7 8 9] -> [2 4 6 8] -> [2 6] -> [6]这个序列中,我们可以得到公差分别是1,2,4。如果我们把n扩大一点,打印出其中每一步剩余的数组序列,我们很容易发现公差是pow(2,n)次方,发现了这个规律后,一切就水到渠成了。接下来,我们只要记录每一次操作后剩下序列的low,high以及序列的长度,直到最后序列只有一个元素即可。

代码如下:

class Solution(object):
def lastRemaining(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1:
return 1
times = 1
low = high = None
length = n
multiple = None
while True:
if times == 1:
length = length / 2
low = 2
if n % 2 == 0:
high = n
else:
high = n -1
multiple = pow(2, times)
elif times % 2 == 0:
length = length / 2
high -= multiple
multiple = pow(2, times)
low = high - multiple*(length-1)
else:
length = length / 2
low += multiple
multiple = pow(2, times)
high = low + multiple * (length - 1)
times += 1
if low >= high:
return high



【leetcode】390. Elimination Game的更多相关文章

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

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

  2. 【LeetCode】390. 消除游戏

    题目 给定一个从1 到 n 排序的整数列表. 首先,从左到右,从第一个数字开始,每隔一个数字进行删除,直到列表的末尾. 第二步,在剩下的数字中,从右到左,从倒数第一个数字开始,每隔一个数字进行删除,直 ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. linux 进程间共享内存示例

    写入端: #include <iostream> #include <unistd.h> #include <stdlib.h> #include <stdi ...

  2. CSS3 基础

    style 标签 <style type="text/css"> h1 { font-size:12px; color:#F00; } </style> 行 ...

  3. CSS——插入形式 基本格式 常见css代码

    常见css代码 无下划线链接 字体颜色   +   左边距 背景颜色 字体.字体颜色.大小 文本对齐方式[取代了<center>]

  4. unlink- ctf-stkof

    stkof 程序下载:https://pan.baidu.com/s/1_dcm8OFjhKbKYWa3WBtAiQ 提取码:pkyb unlink 基础操作 # define unlink #def ...

  5. 14.使用Crunch创建字典----Armitage扫描和利用----设置虚拟渗透测试实验室----proxychains最大匿名

    使用Crunch创建字典 kali自带的字典 usr/share/wordlists cd Desktop mkdir wordlists cd wordlists/ crunch --help cr ...

  6. 只需要2个工具,百度云盘大文件就能用迅雷和IDM下载

    不会代码,不懂脚本,没关系 ,能找到一座通往它们的桥梁,照样能到达彼岸. 这里以360极速浏览器为例. 在浏览器地址框输入以下地址直接到达浏览器安装扩展插件的地方(偷个懒,复制网址吧),https:/ ...

  7. docker--docker compose 编排工具

    11 docker compose 编排工具 11.1 docker compose 介绍 根据前面所学的知识可知,想要使用Docker部署应用,就要先在应用中编写Dockerfile 文件来构建镜像 ...

  8. [转帖]CentOS 7 使用kubeadm 部署 Kubernetes

    CentOS 7 使用kubeadm 部署 Kubernetes   关闭swap 执行swapoff临时关闭swap. 重启后会失效,若要永久关闭,可以编辑/etc/fstab文件,将其中swap分 ...

  9. 关于XML的一些事

    XML:可扩展标记语言! 01.很像HTML 02.着重点是数据的保存 03.无需预编译 04.符合W3C标准 可扩展:我们可以自定义,完全按照自己的规划来! 标记:计算机所能认识的信息符号! 比如: ...

  10. TApplication,TForm,TControl,TComponent,TWinControl研究(博客索引)good

    TApplication,TForm,TControl,TComponent,TWinControl研究 http://blog.csdn.net/suiyunonghen/article/detai ...