Array of Doubled Pairs
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
class Solution {
public boolean canReorderDoubled(int[] A) {
Map<Integer, Integer> map = new HashMap<>();
for (int item : A) {
if (item == ) {
continue;
}
map.put(item, map.getOrDefault(item, ) + );
}
Arrays.sort(A);
for (int i = ; i < A.length; i++) {
if (A[i] != && map.get(A[i]) < ) {
return false;
}
if (A[i] != && map.get(A[i]) > ) {
int target = A[i] * ;
if (A[i] < ) {
if (A[i] % != ) {
return false;
} else {
target = A[i] / ;
}
}
if (!map.containsKey(target)) {
return false;
}
map.put(target, map.get(target) - map.get(A[i]));
map.put(A[i], );
}
}
return true;
}
}
Array of Doubled Pairs的更多相关文章
- 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 ...
- [Swift]LeetCode954. 二倍数对数组 | Array of Doubled Pairs
Given an array of integers A with even length, return true if and only if it is possible to reorder ...
- Array of Doubled Pairs LT954
Given an array of integers A with even length, return true if and only if it is possible to reorder ...
- 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】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】954. Array of Doubled Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 算法与数据结构基础 - 数组(Array)
数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- Weekly Contest 114
955. Delete Columns to Make Sorted II We are given an array A of N lowercase letter strings, all of ...
随机推荐
- jquery bind()方法 语法
jquery bind()方法 语法 作用:bind() 方法为被选元素添加一个或多个事件处理程序,并规定事件发生时运行的函数. 说明:规定向被选元素添加的一个或多个事件处理程序,以及当事件发生时运行 ...
- luogu 1593 因子和 约数+线性筛
等比数列那里忘判项数等于 $1$ 的情况了. Code: #include <cstdio> #include <vector> #include <algorithm& ...
- Codevs 4019 想越狱的小明
4019 想越狱的小明 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 钻石 Diamond 题目描述 Description 这次小明来到了经典美剧<越狱>的场景里-- 它 ...
- java中jsp的EL的定义以及使用
1.定义: EL(Expression Language) 是为了使JSP写起来更加简单.表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 JSP 中简化表达式的方 ...
- codeforces555B
Case of Fugitive CodeForces - 555B Andrewid the Android is a galaxy-famous detective. He is now chas ...
- phpcms9 从注入点入手和 从前台getshell
弄了3天了 这个点 总结一下这三天的坑吧 0X01 注入点入手 /index.php?m=wap&c=index&a=init&siteid=1 获取cookie 传给 us ...
- 化学结构SDF文件
参考博客 第一行:一般作为分子名字,如 Levetiracetam 第二行:注释,ChemDraw06111413562D 第三行:一般是空行 第四行:是原子个数 键的个数等的起始行. M END所在 ...
- elasticsearch与kibana安装过程(linux)
elasticsearch与kibana安装 下载 Elasticsearch 官网:https://www.elastic.co/,elastic search应用本质就是一个jvm进程,所以需要J ...
- (十九)C语言之指针
- TCP之Nagle算法与TCP_NODELAY
1. Nagle 算法 在一个 Rlogin 连接上客户一般每次发送一个字节到服务器,这就产生了一些 41 字节长的分组:20 字节的 IP 首部.20 字节的 TCP 首部和 1 个字节的数据.在局 ...