【leetcode】954. Array of Doubled Pairs
题目如下:
Given an array of integers
Awith even length, returntrueif and only if it is possible to reorder it such thatA[2 * i + 1] = 2 * A[2 * i]for every0 <= i < len(A) / 2.Example 1:
Input: [3,1,3,6]
Output: falseExample 2:
Input: [2,1,2,6]
Output: falseExample 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: falseNote:
0 <= A.length <= 30000A.lengthis even-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的更多相关文章
- 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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】561. Array Partition I 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【leetcode】561. Array Partition I
原题: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...
- 【LeetCode】565. Array Nesting 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- 【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 ...
- 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 ...
- 【leetcode】1243. Array Transformation
题目如下: Given an initial array arr, every day you produce a new array using the array of the previous ...
随机推荐
- 15.stop引发的数据不一致
/** * 数据不一致问题 * stop */ public class StopDemo { public static Student student = new Student(); publi ...
- mysql 根据字母排序
select CONVERT(u.user_name USING gbk) AS user_name from table where 1=1 order by user_name
- range类型(Python)
range 不是 iterator >>> R = range(3) >>> next(R) Traceback (most recent call last): ...
- cf round599 CDE
C:结论题:设n=k*p1+r=a*p2+b,只要n有两个及以上质因子,那么必然可以用第一个质因子表示出第二个质因子,所以答案是1 反之显然是其最小质因子 /* 1 2 3 4 1 3 2 4 n的所 ...
- <自动化测试>之<unittest框架使用1>
要说单元测试和UI自动化之间的是什么样的一个关系,说说我个人的一些心得体会吧,我并没有太多的这方面经验,由于工作本身就用的少,还有就是功能测试点点对于我这种比较懒惰的人来说,比单元测试复杂...思考单 ...
- 微信小程序学习笔记(四)--框架-视图层
WXML(WeiXin Markup Language)是框架设计的一套标签语言,结合基础组件.事件系统,可以构建出页面的结构. 数据绑定 使用{{}}绑定数据. 简单绑定 <view clas ...
- DBA-io
- qrcode.js生成二维
使用到qrcode.js生成二维码 pako.js压缩字符串:https://github.com/nodeca/pako 参照代码如下: <!DOCTYPE HTML PUBLIC " ...
- c/c++ int 范围的原因
在C语言中, signed char 类型的范围为-128~127,每本教科书上也这么写,但是没有哪一本书上(包括老师)也不会给你为什么是-128~127,这个问题貌似看起来也很简单容易, 以至于不用 ...
- ubuntu php多版本共存切换
做开发时,由于本机开发的php版本跟线上发布的php版本不一致,很容易在上线后,发现因版本的影响导致一些bug,但又不想重新去换本机的php版本,那么多版本共存就很方便了!有必要时,切换到指定版本测试 ...