作者: 负雪明烛
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: continueelif A[i] < 0:if A[i] % 2 == 1 or count[A[i] / 2] == 0:return Falseelse: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 Falseelse: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. leetcode 954. Array of Doubled Pairs | 954. 二倍数对数组(Java)

    题目 https://leetcode.com/problems/array-of-doubled-pairs/ 题解 对于每一个数n来说,它要么和 n / 2 凑一对,要么和 n * 2 凑一对. ...

  2. 【LeetCode】436. Find Right Interval 解题报告(Python)

    [LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  3. LeetCode第45场双周赛-解题报告

    LeetCode第45场双周赛-解题报告 A. 唯一元素的和 原题链接 https://leetcode-cn.com/problems/sum-of-unique-elements/ 解题思路 因为 ...

  4. LeetCode 922 Sort Array By Parity II 解题报告

    题目要求 Given an array A of non-negative integers, half of the integers in A are odd, and half of the i ...

  5. leetcode 214. 最短回文串 解题报告

    给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: "aaa ...

  6. LeetCode: Median of Two Sorted Arrays 解题报告

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  7. 【LeetCode】517. 超级洗衣机 解题报告 (python)

    原题地址:https://leetcode-cn.com/problems/super-washing-machines/submissions/ 题目描述: 假设有 n 台超级洗衣机放在同一排上.开 ...

  8. Leetcode 1190. Reverse Substrings Between Each Pair of Parentheses解题报告(python)

    1190. Reverse Substrings Between Each Pair of Parentheses Reverse Substrings Between Each Pair of Pa ...

  9. [leetcode] 273. Integer to English Words 解题报告

    题目链接:https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its e ...

最新文章

  1. 用脚写字考上985!无臂硕士开学报到,宿舍设计太细节了……
  2. getHibernateTemplate()的用法 (转)
  3. java的并发框架_java并发框架有哪些
  4. hadoop MapReduce实例解析
  5. 成功解决No such file or directory: site-packages\\pyyaml-5.3-py3.6-win-amd64.egg\\EGG-INFO\\top_level.t
  6. python写入excel数据教程_python 将数据写入excel
  7. 领域研究热点的绝妙探索方法
  8. 使用JUnit 5进行更清洁的参数化测试
  9. Press ^C at any time to quit.
  10. android 4.4 屏幕方向,Android4.4 增加屏幕旋转功能
  11. oracle 中 case的用法
  12. 在Windows环境下搭建Nginx文件服务器(简单实用版)
  13. 美团构建实时数仓的痛点是什么?如何解决?
  14. Android Studio 随手记
  15. 股票的大底部形态,常见几种底部形态详解
  16. [小小明]Python正则表达式速查表与实操手册
  17. linux系统查看串口占用,Linux 系统串口信息查看
  18. 那些CTA策略的表现如何(一)
  19. Could not transfer artifact (https://repo.maven.apache.org/maven2): Received fatal alert: protocol_v
  20. 资本资产定价模型简介-多因子寻找Alpha统计套利

热门文章

  1. Dijskra迪杰斯特拉算法
  2. 达梦企业管理器DEM的安装部署
  3. 计算机技能大赛 英语,计算机科学与技术学院英语技能大赛圆满结束
  4. 微命令、微指令、微操作
  5. LiveMe x TiDB丨单表数据量 39 亿条,简化架构新体验
  6. ACM--过沼泽--模拟--HDOJ 5477--A Sweet Journey
  7. Windos 前后端项目的部署
  8. 少儿机器人教育在国内的情况
  9. js实现完美身份证号有效性验证+身份证与姓名匹配
  10. 前一个标签自动增加,后面的标签自动减小,如微信的群发功能