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 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:
0 <= A.length <= 30000A.lengthis even-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的更多相关文章
- 【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 ...
- [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 ...
- Array of Doubled Pairs
Given an array of integers A with even length, return true if and only if it is possible to reorder ...
- [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 ...
- 算法与数据结构基础 - 数组(Array)
数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺 ...
- Weekly Contest 114
955. Delete Columns to Make Sorted II We are given an array A of N lowercase letter strings, all of ...
随机推荐
- Nginx 无法重启
报错如下 Starting nginx... nginx (pid)already running. 重启nginx时,说多个进程已存在,,, 执行 ps -ef | grep nginx 发现 有多 ...
- 开源Android 恶意软件Radio Balouch
安全研究机构 ESET 首次发现了开源 Android 间谍软件在 Google Play 上的恶意信息窃取行为,并且在被删除后仍在Google Play 重复出现.据悉,第一个间谍软件是基于开源间 ...
- mysql数据库:mysql初识
1.什么是数据库 ***** 存放数据的仓库 已学习的文件操作的缺陷 1.IO操作 效率问题 2.多用户竞争数据 3.网络访问 ...
- 【C】文件操作
文件操作 文件的打开 FILE * fopen(const char filename,const char * mode); 文件的打开操作 fopen 打开一个文件 (几种操作文件的组合) 文件的 ...
- java-消息队列相关-activeMQ
,1,如何防止activeMQ崩溃导致消息丢失呢? 第一点,首先消息需要使用持久化消息,服务挂掉,重启服务后消息依然可以消费,不会丢失: 第二点,ActiveMQ采用主从模式搭建集群,比如搭建3台主从 ...
- HelloWorld编写过程中注意事项
一.package关键字 * package表示当前代码所属的包(package),是一种组织结构.其他package通过包名调用这个包下内容* package是必须的,每个文件的package必须存 ...
- 同一电脑如何安装多个jdk
1.安装对应的jdk 本机测试只安装jdk1.7和1.8 2.切换jdk 以我的环境为例,一开始装的是jdk1.7,要切换到jdk1.8时,需修改以下内容 环境变量,该为对应jdk的bin路径 修改注 ...
- 一键生成 dao service serverImpl controller 层
package com.nf147.policy_publishing_platform.util.auto; import java.io.File; import java.io.FileWrit ...
- Hadoop-No.16之Kafka
Apache Kafka 是一種发布-订阅消息的分布式系统.能够将消息归类为不同主题.应用程序能在Kafka上发布信息,或订阅主题进而接受特定主题下发布的消息.Producer发布消息,而Consum ...
- django 之csrf、auth模块及settings源码、插拔式设计
目录 基于django中间件拷贝思想 跨站请求伪造简介 跨站请求伪造解决思路 方式1:form表单发post请求解决方法 方式2:ajax发post请求解决方法 csrf相关的两个装饰器 csrf装饰 ...