【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 ...
随机推荐
- UVA1411 Ants
想出的一道题竟然是原题QAQ 非常有趣的一个题 根据三角形两边之和大于第三边 所以相交的线段一定是比不相交的线段要长的 所以直接二分图构图 最小费用最大流即可 (我不管我不管我要把这个出到NOIP膜你 ...
- 【leetcode】991. Broken Calculator
题目如下: On a broken calculator that has a number showing on its display, we can perform two operations ...
- mui-popover显示、隐藏弹出菜单的方法
一.mui-popover要显示.隐藏弹出菜单,可使用锚点方式. <div id="popover" class="box mui-popover mui-popp ...
- 初识localstorage用法
最近在做一个类似填报信息的页面,一共有8页,当点击切换到下一页的时候要求把上一页的数据存到本地,以便下次切换到这个页面的时候自动把值填上去,并且在最后一页提交数据的时候直接用localstorage里 ...
- 关于自动化测试学习 selenium
selenium学习路线 配置你的测试环境,真对你所学习语言,来配置你相应的selenium 测试环境.selenium 好比定义的语义---“问好”,假如你使用的是中文,为了表术问好,你的写法是“你 ...
- php 字符串 定界符 json_last_error()
字符串的3种赋值 1:单引号 $str = '111111111111 '; 2:双引号 $str =" 11111111111 "; 3:定界符 $str = <<& ...
- 优化问题及KKT条件
整理自其他优秀博文及自己理解. 目录 无约束优化 等式约束 不等式约束(KKT条件) 1.无约束优化 无约束优化问题即高数下册中的 “多元函数的极值" 部分. 驻点:所有偏导数皆为0的点: ...
- Hive 窗口函数
举例: row_number() over(partition by clue_id order by state_updated desc) 业务举例: select distinct a.clue ...
- VTemplate模板引擎的使用--进阶篇
1.<vt:template>与<vt:include>标签的不同 <vt:template>和<vt:include> 标签都包含file属性,如果这 ...
- 21. Blog接口开发
一般的系统由登录.增删改查所组成.我们的Blog同样如此.我们会开发登录.创建博客.删除博客.修改博客.查询博客等功能.话不多说,我们直接展开实践吧. 思路分析 创建项目.既然我们要创建一个blog, ...