【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: 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)的更多相关文章
- 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 ...
- 【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 ...
- 【LeetCode】677. Map Sum Pairs 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 前缀树 日期 题目地址:https://lee ...
- 【LeetCode】Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
随机推荐
- ggplot 画堆叠柱状图
1. 关注下方公众号可获得更多精彩
- matplotlib 画饼图
有个瑕疵,某一块儿比例过小时,文字会重叠. 1 def pizza(data,labs,title): 2 import matplotlib 3 import matplotlib.pyplot a ...
- Linux之crond定时任务
1. 使用crontab工具配置的定时任务 2. 配置定时任务建议规范 3. 定时任务配置问题导致系统出现故障实例 1. 使用crontab工具配置的定时任务 名称 crontab - 维护单个用户的 ...
- mac系统升级导致VirtualBox报Kernel driver not installed (rc=-1908)
一.背景 最近将我的Mac升级成了Monterey版本,结果发现之前的安装的VirtualBox虚拟机无法启动,报了如下错误. Kernel driver not installed (rc=-190 ...
- 04 Windows安装python运行环境
安装python运行环境 使用微信扫码关注微信公众号,并回复:"Python工具包",免费获取下载链接! 1.卸载程序(电脑未装此程序,跳过此过程) 卸载这两个程序 出现下图所示, ...
- 重新整理 .net core 实践篇——— endpoint[四十七]
前言 简单整理一些endpoint的一些东西,主要是介绍一个这个endpoint是什么. 正文 endpoint 从表面意思是端点的意思,也就是说比如客户端的某一个action 是一个点,那么服务端的 ...
- ICCV2021 | TOOD:任务对齐的单阶段目标检测
前言 单阶段目标检测通常通过优化目标分类和定位两个子任务来实现,使用具有两个平行分支的头部,这可能会导致两个任务之间的预测出现一定程度的空间错位.本文提出了一种任务对齐的一阶段目标检测(TOOD) ...
- JAVA中的六种日期类型使用
基本的6种日期类 /** * 六种时间类型的类 * 数据库格式的时间三种格式 */ java.util.Date date = new java.util.Date();//年与日时分秒 //数据库的 ...
- 【Elasticsearch-Java】Java客户端搭建
Elasticsearch Java高级客户端 1. 概述 Java REST Client 有两种风格: Java Low Level REST Client :用于Elasticsearch ...
- 访问者模式(Visitor Pattern)——操作复杂对象结构
模式概述 在软件开发中,可能会遇到操作复杂对象结构的场景,在该对象结构中存储了多个不同类型的对象信息,而且对同一对象结构中的元素的操作方式并不唯一,可能需要提供多种不同的处理方式,还有可能增加新的处理 ...