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 ...
随机推荐
- Vue3.0+TypeScript
序言 资料 https://www.cnblogs.com/chanwahfung/p/11968205.html
- java+批量下载大文件
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...
- CF1155D Beautiful Array 贪心,dp
CF115DBeautiful Array 题目大意:给一个有n个元素的a数组,可以选择其中一个区间的所有数都乘上x,也可以不选,求最大子序列和. 如果没有前面的操作,就是只求最大子序列和,我们都知道 ...
- js--BOM对象(2)
一.window对象是整个bom的核心 二.window对象的属性: history:(有关客户访问过的url信息) 方法: back() 加载 history 对象列表中的前一个URL forwar ...
- zookeeper系列(四)zookeeper的使用场景
作者:leesf 掌控之中,才会成功:掌控之外,注定失败. 出处:http://www.cnblogs.com/leesf456/p/6036548.html感谢原著公开这么好的博文供大家学习 ...
- Java官方操纵byte数组的方式
java官方提供了一种操作字节数组的方法——内存流(字节数组流)ByteArrayInputStream.ByteArrayOutputStream ByteArrayOutputStream——by ...
- Netty入门官方例子
参考链接:https://blog.csdn.net/wocjy/article/details/78661464 maven依赖: <!-- Netty开始 --> <!-- ht ...
- 去掉input type=file的默认样式
原样式: 解决: 加style="opacity: 0;"变成透明的 然后可以外面套个div,在div上自定义样式.
- centos7.2 安装nginx+php
Nginx的安装 安装快速HTTP服务器“的Nginx”并配置HTTP服务器# install from EPEL [root@linuxprobe~]# yum --enablerepo=epel ...
- 使用SlidingDrawer(滑动式抽屉)实现抽屉效果
SlidingDrawer隐藏屏外的内容,并允许用户通过handle以显示隐藏内容.它可以垂直或水平滑动,它有俩个View组成,其一是可以拖动的handle,其二是隐藏内容的View.它里面的控件必须 ...