【LeetCode】954. Array of Doubled Pairs 解题报告(Python)
作者: 负雪明烛
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:
- 0 <= A.length <= 30000
- A.length is even
- -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)相关推荐
- leetcode 954. Array of Doubled Pairs | 954. 二倍数对数组(Java)
题目 https://leetcode.com/problems/array-of-doubled-pairs/ 题解 对于每一个数n来说,它要么和 n / 2 凑一对,要么和 n * 2 凑一对. ...
- 【LeetCode】436. Find Right Interval 解题报告(Python)
[LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- LeetCode第45场双周赛-解题报告
LeetCode第45场双周赛-解题报告 A. 唯一元素的和 原题链接 https://leetcode-cn.com/problems/sum-of-unique-elements/ 解题思路 因为 ...
- 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 ...
- leetcode 214. 最短回文串 解题报告
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串. 示例 1: 输入: "aacecaaa" 输出: "aaa ...
- 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 ...
- 【LeetCode】517. 超级洗衣机 解题报告 (python)
原题地址:https://leetcode-cn.com/problems/super-washing-machines/submissions/ 题目描述: 假设有 n 台超级洗衣机放在同一排上.开 ...
- 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 ...
- [leetcode] 273. Integer to English Words 解题报告
题目链接:https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its e ...
最新文章
- 用脚写字考上985!无臂硕士开学报到,宿舍设计太细节了……
- getHibernateTemplate()的用法 (转)
- java的并发框架_java并发框架有哪些
- hadoop MapReduce实例解析
- 成功解决No such file or directory: site-packages\\pyyaml-5.3-py3.6-win-amd64.egg\\EGG-INFO\\top_level.t
- python写入excel数据教程_python 将数据写入excel
- 领域研究热点的绝妙探索方法
- 使用JUnit 5进行更清洁的参数化测试
- Press ^C at any time to quit.
- android 4.4 屏幕方向,Android4.4 增加屏幕旋转功能
- oracle 中 case的用法
- 在Windows环境下搭建Nginx文件服务器(简单实用版)
- 美团构建实时数仓的痛点是什么?如何解决?
- Android Studio 随手记
- 股票的大底部形态,常见几种底部形态详解
- [小小明]Python正则表达式速查表与实操手册
- linux系统查看串口占用,Linux 系统串口信息查看
- 那些CTA策略的表现如何(一)
- Could not transfer artifact (https://repo.maven.apache.org/maven2): Received fatal alert: protocol_v
- 资本资产定价模型简介-多因子寻找Alpha统计套利