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. 简单的文件ftp上传

    目录 简单的文件ftp上传 简单的文件ftp上传 server import socket import struct service=socket.socket() service.bind(('1 ...

  2. Oracle12cCDB和PDB数据库的启动与关闭说明

    在Oracle 12c中,分CDB 和PDB,他们的启动和关闭操作整理如下. 1 Container Database (CDB) 对于CDB,启动和关闭与之前传统的方式一样,具体语法如下: STAR ...

  3. Gitlab创建ssh key并添加配置

    1 生成ssh key  zj改成你自己的邮箱或者名字之类的 ssh-keygen -t rsa -C "zj" 2 找到你生成的ssh key copy 公钥 添加到gitlab ...

  4. 1.Nginx安装

    1.Nginx安装配置 Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发的高性能 Web和 反向代理服务器,也是一个 IMAP/POP3/ ...

  5. jdk提供的线程协调API suspend/resume wait/notify park/unpark

    线程通信(如 线程执行先后顺序,获取某个线程执行的结果等)有多种方式: 文件共享 线程1 --写入--> 文件 < --读取-- 线程2 网络共享 变量共享 线程1 --写入--> ...

  6. goaccess安装和使用

    安装依赖 $ sudo apt-get install libncursesw5-dev $ wget https://github.com/maxmind/geoip-api-c/releases/ ...

  7. PAT Basic 1092 最好吃的月饼 (20 分)

    月饼是久负盛名的中国传统糕点之一,自唐朝以来,已经发展出几百品种. 若想评比出一种“最好吃”的月饼,那势必在吃货界引发一场腥风血雨…… 在这里我们用数字说话,给出全国各地各种月饼的销量,要求你从中找出 ...

  8. 生产者消费者问题--lock

    # 代码: public class App { public static void main(String[] args) { Depot depot = new Depot(100); Prod ...

  9. Android异常与性能优化相关面试问题-冷启动优化面试问题详解

    什么是冷启动: 冷启动的定义:冷启动就是在启动应用前,系统中没有该应用的任何进程信息.实际也就是要执行Application.onCreate()方法的那次启动. 冷启动 / 热启动的区别:热启动:用 ...

  10. Python爬虫进阶之Scrapy框架安装配置

    Python爬虫进阶之Scrapy框架安装配置 初级的爬虫我们利用urllib和urllib2库以及正则表达式就可以完成了,不过还有更加强大的工具,爬虫框架Scrapy,这安装过程也是煞费苦心哪,在此 ...