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

Note:

  1. 0 <= A.length <= 30000
  2. A.length is even
  3. -100000 <= A[i] <= 100000

Runtime: 84 ms, faster than 81.90% of C++ online submissions for Array of Doubled Pairs.

挺简单的一道median,但是要注意一些细节,

1. 0 ,正数,负数,分开考虑。

2. 去重前需要排序。

#define ALL(x) (x).begin(),(x).end()
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std; class Solution {
public:
bool canReorderDoubled(vector<int>& A) {
vector<int> positive;
vector<int> negative;
int zerocnt = ;
for(auto x : A) {
if(x == ) zerocnt++;
}
if(zerocnt& == ) return false;
for(auto x : A) {
if(x > ) positive.push_back(x);
else negative.push_back(-x);
}
return helper(positive) && helper(negative);
}
bool helper(vector<int>& A) {
unordered_map<int,int> map;
for(auto x : A) map[x]++;
vector<int> idA;
sort(ALL(A));
for(int i=; i<A.size(); i++){
if(i == || idA.back() != A[i]) idA.push_back(A[i]);
}
//reverse(ALL(idA));
//for(auto x : idA) cout << x << endl;
for(auto x : idA) {
//cout << x << endl;
if(map[x] == ) continue;
if(!map.count(x<<) || map[x<<] < map[x]) return false; map[x<<] -= map[x];
//map[x] = 0;
}
return true;
}
};

LC 954. Array of Doubled Pairs的更多相关文章

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

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

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

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

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

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

  6. Array of Doubled Pairs

    Given an array of integers A with even length, return true if and only if it is possible to reorder ...

  7. [LC] 24. Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

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

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

  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. remote mounting from windows to linux

    8 Ways To Mount SMBfs (SAMBA FILE SYSTEM) In Linux. Sep 8, 2009 How to Mount smbfs (SAMBA file syste ...

  2. Oracle笔记(二) SQLPlus命令

    对于Oracle数据库操作主要使用的是命令行方式,而所有的命令都使用sqlplus完成,对于sqlplus有两种形式. 一种是dos风格的sqlplus:sqlplus.exe; 另一种是window ...

  3. 6.JVM技术_java内存模型

    1.java内存模型图解 Java虚拟机在执行Java程序的过程中,会把它所管理的内存划分为若干个不同的数据区.这些区域有各自的用途,以及创建和销毁的 时间,有的区域随着虚拟机进程的启动而存在,有的区 ...

  4. @font-face字图标问题

    链接:http://www.exp99.com/htmlcss/htmlcss_229.html

  5. KNN分类算法

    K邻近算法.K最近邻算法.KNN算法(k-Nearest Neighbour algorithm):是数据挖掘分类技术中最简单的方法之一 KNN的工作原理 所谓K最近邻,就是k个最近的邻居的意思,说的 ...

  6. Spring——AOP

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

  7. Centos7网卡配置命令nmcli

    在配置Centos6时,大家第一想到的就是把networkManager这个服务关掉,让它消失,这个服务台太鸡肋了,不该起作用的时候经常起作用,给管理带来了不便,但是在Centos7当中network ...

  8. poj3691 DNA repair[DP+AC自动机]

    $给定 n 个模式串,和一个长度为 m 的原串 s,求至少修改原串中的几个字符可以使得原串中不包含任一个模式串.模式串总长度 ≤ 1000,m ≤ 1000.$ 先建出模式串的AC自动机,然后考虑怎么 ...

  9. nginx之"/"结尾

    一.配置127.0.0.1:8081的tomcat下的文件: 1.ROOT/a.html : this is ROOT page 2.ROOT/testa.html : nihao 3.ROOT/in ...

  10. 快速取出选中checkbox的值

    原料:jquery 直接上代码 html代码 <html> <head> <title>$Title$</title> </head> &l ...