题目如下:

解题思路:对于这种数字类型的题目,数字一般都会有内在的规律。不管怎么操作了多少次,本题的数组一直是一个等差数列。从[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. 那些年我们经历的BT面试题

    初入职场面试的我到处碰壁,以下是我个人对几道面试题的小总结: 1.一列数字的规则如下:1,1,2,3,5,8,13,21, 34........ 求第30位数字是多少,用递规和非递归两种方法算法实现. ...

  2. ES6中数组和对象的扩展运算符拷贝问题以及常用的深浅拷贝方法

    在ES6中新增了扩展运算符可以对数组和对象进行操作.有时候会遇到数组和对象的拷贝,可能会用到扩展运算符.那么这个扩展运算符到底是深拷贝还是浅拷贝呢? 一..使用扩展运算符拷贝 首先是下面的代码. le ...

  3. Cygwin 技巧

    apt-cyg mirror 'https://mirrors.aliyun.com/cygwin/' #设置镜像 windows下安装工具包 inetutils;其中包含telnet等命令; Pyt ...

  4. is_displayed()检查元素是否可见

    返回的结果是bool类型,以百度首页为案例,来验证"©2019 Baidu 使用百度前必读意见反馈京ICP证030173号 "是否可见,见实现的代码: from selenium ...

  5. LeetCode算法题-Goat Latin Easy(Java实现)

    这是悦乐书的第322次更新,第344篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第192题(顺位题号是824).给出句子S,由空格分隔的单词组成.每个单词仅由小写和大写 ...

  6. Linux setup

    Install centos 7: Config Network a config example in /etc/sysconfig/network-scripts/ifcfg-ens160 TYP ...

  7. Go语言入门篇-Golang之文本编码处理

    Golang之文本编码处理

  8. Java基础/Socket.io双向通信

    Socket.io基础知识(一) (一).socket.io提供了基于事件的实时双向通讯 Web端与服务端实时数据传输方式: 1.Ajax轮询方式(最早应用)   原理:设置定时器,定时通过Ajax同 ...

  9. adb常用命令和抓取log的方法

    一 adb常用的几个命令1. 查看设备adb devices这个命令是查看当前连接的设备, 连接到计算机的android设备或者模拟器将会列出显示 C:\Documents and Settings\ ...

  10. object in javascript

    枚举对象属性 for....in 列举obj的可枚举属性,包括自身和原型链上的 object.keys() 只列举对象本身的可枚举属性 创建对象的几种方式 对象字面量 const pre='test' ...