作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/array-of-doubled-pairs/description/

题目描述

Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.

Example 1:

Input: [3,1,3,6]
Output: false

Example 2:

Input: [2,1,2,6]
Output: false

Example 3:

Input: [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].

Example 4:

Input: [1,2,4,16,8,4]
Output: false

Note:

  1. 0 <= A.length <= 30000
  2. A.length is even
  3. -100000 <= A[i] <= 100000

题目大意

问能不能重新对数组进行某种排列,使得A[2 * i + 1] = 2 * A[2 * i]对于0 <= i < len(A) / 2恒成立。

解题方法

题目的意思是奇数位置的数字能不能安排成它左边的那个位置的二倍。

使用的方法是统计次数、然后遍历查找的方式,和Two Sum之类的很类似。需要注意的是,我们对数组进行了排序,这样保证下面的遍历是从小到大开始的。另外,由于排序之后,对于负数而言,小数字是大数字的2倍,所以,需要做一个正负的判断。

对于负数来说,我们找出它的1/2是不是在数组中;对于正数来说,我们找出它的2倍是不是在数组中。如果找到了要找的数字之后,把它的次数减去当前的数字的次数,以方便后面的统计。所以,如果所有的数字都满足条件的话,那么就一波一波的都消除掉了。

class Solution(object):
def canReorderDoubled(self, A):
"""
:type A: List[int]
:rtype: bool
"""
A.sort()
N = len(A)
count = collections.Counter(A)
for i in range(N):
if A[i] == 0 or A[i] not in count: continue
elif A[i] < 0:
if A[i] % 2 == 1 or count[A[i] / 2] == 0:
return False
else:
count[A[i] / 2] -= count[A[i]]
if count[A[i] / 2] == 0:
del count[A[i] / 2]
del count[A[i]]
else:
if count[A[i] * 2] == 0:
return False
else:
count[A[i] * 2] -= count[A[i]]
if count[A[i] * 2] == 0:
del count[A[i] * 2]
del count[A[i]]
return True

日期

2018 年 12 月 9 日 —— 周赛懵逼了

【LeetCode】954. Array of Doubled Pairs 解题报告(Python)的更多相关文章

  1. LC 954. Array of Doubled Pairs

    Given an array of integers A with even length, return true if and only if it is possible to reorder ...

  2. 【leetcode】954. Array of Doubled Pairs

    题目如下: Given an array of integers A with even length, return true if and only if it is possible to re ...

  3. 【LeetCode】677. Map Sum Pairs 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 前缀树 日期 题目地址:https://lee ...

  4. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

  5. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  6. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  7. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...

  8. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  9. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

随机推荐

  1. kubernetes部署 flannel网络组件

    创建 flannel 证书和私钥flannel 从 etcd 集群存取网段分配信息,而 etcd 集群启用了双向 x509 证书认证,所以需要为 flanneld 生成证书和私钥. cat > ...

  2. 生产调优2 HDFS-集群压测

    目录 2 HDFS-集群压测 2.1 测试HDFS写性能 测试1 限制网络 1 向HDFS集群写10个128M的文件 测试结果分析 测试2 不限制网络 1 向HDFS集群写10个128M的文件 2 测 ...

  3. a这个词根

    a是个词根,有三种意思:1. 以某种状态或方式,如: ablaze, afire, aflame, alight, aloud, alive, afloat等2. at, in, on, to sth ...

  4. accomplish, accord

    accomplish =achieve; accomplishment=achievement. accomplished: well educated/trained, skilled. skill ...

  5. MapReduce的类型与格式

    MapReduce的类型 默认的MR作业 默认的mapper是Mapper类,它将输入的键和值原封不动地写到输出中 默认的partitioner是HashPartitioner,它对每条记录的键进行哈 ...

  6. 图形学3D渲染管线学习

    图形学3D渲染管线 DX和OpenGL左右手坐标系不同,会有一些差距,得出的矩阵会不一样; OpenGL的投影平面不是视景体的近截面: 顶点(vertexs) 顶点坐标,颜色,法线,纹理坐标(UV), ...

  7. 使用Mock测试

    一.前言 在前面的章节我们介绍过 Junit 的使用,也了解过 spring-test,今天我们来了解一个新玩意 -- mock 测试.这里仅仅做一个入门,对返回视图和返回 Json 数据的方法进行测 ...

  8. mybatis-plus分页记坑

    mapper接口方法返回IPage,如果不传page会报npe,底层assert page!=null有啥用?

  9. 【Go】【Basic】MacOS上搭建GO开发环境

    1. GO下载 1.1. 下载地址:https://www.golangtc.com/download (需要科学上网) 1.1.1. PKG安装: 下载这个包:go1.9.2.darwin-amd6 ...

  10. PHP之CURL实现含有验证码的模拟登录

    博主最近在为学校社团写一个模拟登录教务系统来进行成绩查询的功能,语言当然是使用PHP啦,原理是通过php数据传输神器---curl扩展,向学校教务系统发送请求,通过模拟登录,获取指定url下的内容. ...