题目如下:

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,同时用字典dic记录A中每个元素出现的次数,如果A[i] 是偶数并且A[i]/2存在于dic中,那么把A[i]/2在dic中出现的次数减去1,如果出现次数降为0,从dic中删除该key值;否则继续判断A[i]*2是否存在于dic中;如果两个条件都不满足,把A[i]加入dic中。最后判断dic的长度是否为0即可,

代码如下:

class Solution(object):
def canReorderDoubled(self, A):
"""
:type A: List[int]
:rtype: bool
"""
A.sort()
dic = {}
for i in A:
if i % 2 == 0 and i/2 in dic:
i = i / 2
dic[i] -= 1
if dic[i] == 0:
del dic[i]
elif i * 2 in dic:
i = i * 2
dic[i] -= 1
if dic[i] == 0:
del dic[i]
else:
dic[i] = dic.setdefault(i, 0) + 1
continue
return len(dic) == 0

【leetcode】954. Array of Doubled Pairs的更多相关文章

  1. 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)

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

  2. 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 ...

  3. 【LeetCode】561. Array Partition I 解题报告(Java & Python)

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

  4. 【leetcode】561. Array Partition I

    原题: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...

  5. 【LeetCode】565. Array Nesting 解题报告(Python & C++)

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

  6. 【LeetCode】Rotate Array

    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...

  7. 【LeetCode】24. Swap Nodes in Pairs (3 solutions)

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  8. 114th LeetCode Weekly Contest Array of Doubled Pairs

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

  9. 【leetcode】1243. Array Transformation

    题目如下: Given an initial array arr, every day you produce a new array using the array of the previous ...

随机推荐

  1. 转载关于struts命名空间的一则报警

    今天花了点时间把struts2警告事件彻底的测试了一边,终于有点眉目了.希望能给其他人带来一点帮助.文章属于原创.并不需要转载的时候注明出处,而是希望转载的朋友一定要看明白本文内容再转载,因为我你都清 ...

  2. Qt + VS 【如何添加图片资源】

    熟悉qt creator,之后发现其debug能力不如vs强,随后转战 qt + vs. 发现图片资源添加不像qt那样直接添加,vs本身会生成一个qrc,我们可以直接去打开然后添加,不必在自己去添加, ...

  3. OC学习篇之---@class关键字的作用以及#include和#import的区别

    前一篇文章说到了OC中类的三大特性:http://blog.csdn.net/jiangwei0910410003/article/details/41707161今天我们来看一下在学习OC的过程中遇 ...

  4. postgreSQL执行计划

    " class="wiz-editor-body wiz-readonly" contenteditable="false"> explain命 ...

  5. 反向代理Reverse proxy

    https://www.zhihu.com/question/24723688/answer/160252724 反向代理在计算机世界里,由于单个服务器的处理客户端(用户)请求能力有一个极限,当用户的 ...

  6. HTML-参考手册: HTML 拾色器

    ylbtech-HTML-参考手册: HTML 拾色器 1.返回顶部 1. HTML 拾色器 选取颜色:     或输入颜色值: OK 或使用 HTML5: 选择的颜色: 黑色文本 阴影 白色文本 阴 ...

  7. PicoCTF 2013 Dark Star 分析

    0x00题目 题目可以从GitHub中找到:https://github.com/picoCTF/2013-Problems/blob/master/Dark%20Star/darkstar.img ...

  8. Notepad++ 连接 FTP 实现编辑 Linux文件

    下载并安装插件 github 下载 :https://github.com/ashkulz/NppFTP/releases/ 安装过程 将下载后解压的文件夹中的 NppFTP.dll 文件,拷贝到 n ...

  9. ffmpeg -视频旋转和高清转码示例

    手头有一个竖屏拍摄的视频(真诚建议不要这么做..),导入到电脑上以后势必要把它旋转90°,可是没想到就这样简单的一个功能,尝试了N个非编软件(openshot, pitivi,还有坑爹的lives)后 ...

  10. Kattis - barcode

    Kattis - barcode 题目原文: To prepare for ACM-ICPC 2017 in Saigon, the host univeristy – Ho Chi Minh cit ...