【leetcode】390. Elimination Game
题目如下:
解题思路:对于这种数字类型的题目,数字一般都会有内在的规律。不管怎么操作了多少次,本题的数组一直是一个等差数列。从[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 1times = 1low = high = Nonelength = nmultiple = Nonewhile True:if times == 1:length = length / 2low = 2if n % 2 == 0:high = nelse:high = n -1multiple = pow(2, times)elif times % 2 == 0:length = length / 2high -= multiplemultiple = pow(2, times)low = high - multiple*(length-1)else:length = length / 2low += multiplemultiple = pow(2, times)high = low + multiple * (length - 1)times += 1if low >= high:return high
转载于:https://www.cnblogs.com/seyjs/p/8934453.html
【leetcode】390. Elimination Game相关推荐
- 【LeetCode】390.消除游戏
一.描述 给定一个从1 到 n 排序的整数列表. 首先,从左到右,从第一个数字开始,每隔一个数字进行删除,直到列表的末尾. 第二步,在剩下的数字中,从右到左,从倒数第一个数字开始,每隔一个数字进行删除 ...
- 【LeetCode】390. 消除游戏
题目 给定一个从1 到 n 排序的整数列表. 首先,从左到右,从第一个数字开始,每隔一个数字进行删除,直到列表的末尾. 第二步,在剩下的数字中,从右到左,从倒数第一个数字开始,每隔一个数字进行删除,直 ...
- 【Leetcode】100. 相同的树
题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...
- 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵
1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...
- 【leetcode】486. Predict the Winner
题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...
- 【leetcode】132. Palindrome Partitioning II
题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...
- 【leetcode】86. Partition List
题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...
- 【Leetcode】103. 二叉树的锯齿形层次遍历
题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...
- 【Leetcode】79.单词搜索
题目 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格 ...
最新文章
- 如何解决Bluetooth系统设计的棘手问题
- ATL服务器:用 Visual C++创建的高性能的Web应用程序和XML Web 服务
- SAP 电商云 Spartacus UI 的 CORS 设置
- python中等高线填充颜色_Python matplotlib等高线图对数颜色
- HDU 3861 The King’s Problem (强连通缩点+DAG最小路径覆盖)
- 可用性高达五个9!支付系统高可用架构设计实战
- java filledrectangle_算法(第4版)-1.1.9 输入输出
- MySQL创建新连接时,不能成功连接的问题
- 计算机硬盘应该什么格式化,磁盘格式化是什么意思?快速格式化和一般格式化有什么区别...
- 高中数学:抛物线专题讲解利用韦达定理求解技巧
- NodeJS 服务 Docker 镜像极致优化指北
- 联想拯救者wif开不了_联想拯救者 + ubuntu16.04 + WIFI设置
- 宝宝出生前妈妈准备物品清单
- Unpacking objects
- Eclipse中文语言包下载后怎么使用
- matlab butter()函数解析
- IT人员转行写小白文可以么?
- Linux VI文本编辑器
- RF无线射频电路设计基础
- 在 isilon 的 Samba 中接入 LDAP 认证并严格控制权限
热门文章
- vue index.php,如何解决vue $index报错问题
- Android学习笔记View的工作原理
- [Java] 获取本月周次和日期时间段信息
- bzoj1180: [CROATIAN2009]OTOCI
- 服务器端 OR 客户端
- [转]闲话操作系统1
- 表单下拉框、表单修饰符(.lazy、.number、.trim)
- mysql最小费用最大流问题_图论-网络流之最小费用最大流问题
- 利用VS2012自带功能,将xml文档反序列化为对象
- C++文件操作(打开、关闭、文件读取数据存入数组)