【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 ...
随机推荐
- 【Java学习笔记】百度面试问题回顾(一)
今天回顾了部分百度面试时被问到的问题: 1.常见的包装类有哪些,他们与基本类型的区别,有哪些方法? Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的,这在实际使用时存在很 ...
- 模板方法模式TemplateMethod
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11407071.html 1. 定义定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子 ...
- mui-popover显示、隐藏弹出菜单的方法
一.mui-popover要显示.隐藏弹出菜单,可使用锚点方式. <div id="popover" class="box mui-popover mui-popp ...
- LA 3971 Assemble(二分)
题目: 给你b元钱,让你组装一台电脑,有n个配件,属性有 种类 名字 价格 品质,每种类型选至少一个,并且最小品质最大.输出这个最大的最小品质. 白书上说了,最小值最大的问题一般是二分来求解答案.在这 ...
- POJ 2528(线段树+离散化+特殊离散化)网上博客很少有人真正写对!!! 是POJ数据太水...
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- 【dart学习】-- Dart之JSON
概述 现在很难想象移动应用程序不需要与后台交互或者存储结构化数据.现在开发,数据传输方式基本都是用JSON,在Flutter中是没有GSON/Jackson/Moshi这些库,因为这些库需要运行时反射 ...
- Windows Xp Sp3官方简体中文版(原版) 纯净安装版 百度网盘下载
百度网盘下载: 1.链接:https://pan.baidu.com/s/1o-HcKddSG6IAz_0COKhq8Q 提取码:hkhr 2.扫码下载:
- dedecms SESSION变量覆盖导致SQL注入漏洞修补方案
dedecms的/plus/advancedsearch.php中,直接从$_SESSION[$sqlhash]获取值作为$query带入SQL查询,这个漏洞的利用前提是session.auto_st ...
- 【Tomcat】1.Tomcat在Windows系统的安装和使用
1.下载与安装 安装Tomcat的[前提条件]是安装好JDK或者JRE(本文略过)Tomcat在Windows系统中可以通过[压缩包]或[安装包]来安装建议使用[安装包]来简化安装步骤登录官网http ...
- jQuery函数API,各版本新特性汇总
jQuery API 速查表 选择器 基本 #id element .class * selector1,selector2,selectorN 层级 ancestor descendant pare ...