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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. 【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 ...

  6. 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 算法与数据结构基础 - 数组(Array)

    数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺 ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. Weekly Contest 114

    955. Delete Columns to Make Sorted II We are given an array A of N lowercase letter strings, all of ...

随机推荐

  1. 【Python之路】特别篇--Python文件操作

    文件操作 open函数,该函数用于文件处理 操作文件时,一般需要经历如下步骤: (1)打开文件 (2)操作文件 一.打开文件 文件句柄 = open('文件路径', '模式','编码') 打开文件时, ...

  2. cx_freeze multiprocessing 打包后反复重启

    写了给flask程序,此外还需要用multiprocessing 启动一个守护进程. 不打包一切正常,用cx_freeze打包后,发现flask反复重启.任务管理器里这个GUI窗口的进程数不断增加. ...

  3. vue-cli3中axios如何跨域请求以及axios封装

    1. vue.config.js中配置如下 module.exports = { // 选项... // devtool: 'eval-source-map',//开发调试 devServer: { ...

  4. django CBV模式源码执行过程

    在说CBV模式之前,先看下FBV的url配置方式: urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^xxx/', login), ur ...

  5. Javascript引擎的单线程机制和setTimeout执行原理阐述

    工作中使用setTimeout解决了一个问题,于是对setTimeout的相关资料整理了下,以及对js引擎执行的原理一并整理了下,希望能给码农们一些帮助.若发现有错的地方大家及时指出,共同学习进步. ...

  6. svn 同步hook

    hook 目录下 cp pre-revprop-change.tmpl pre-revprop-change chmod a+x pre-revprop-change 同步代码初始化 sudo svn ...

  7. IDEA项目里Maven 的Plugins出现红线的解决方法

    1.删除项目里的libraries(快捷键ctrl+shift+alt+s):Project Settings->Libraries,全选删除 2.删除之前项目产生的target 3.然后再in ...

  8. git commit 合并到指定分支

    1. 将指定的commit合并到当前分支 git cherry-pick  commit_id 2. 合并多个连续 commit 到指定分支 假设需要合并 devlop 上从 fb407a3f 到 9 ...

  9. Nginx事件管理之核心模块ngx_events_module

    1. ngx_events_module核心模块的功能介绍 ngx_events_module 模式是一个核心模块,它的功能如下: 定义新的事件类型 定义每个事件模块都需要实现的ngx_event_m ...

  10. koa 基础(十)原生node.js 在 koa 中获取表单提交的数据

    1.app.js // 引入模块 const Koa = require('koa'); const router = require('koa-router')(); /*引入是实例化路由 推荐*/ ...