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 ...
随机推荐
- innodb存储引擎之内存
1.innoDB存储引擎体系架构 如上图所示,innoDB存储是基于磁盘存储的,并且其中的记录以页的方式进行管理,但为什么要引入一个内存池呢? 其目的就是为了协调CUP速度与磁盘速度的鸿沟,基于磁盘的 ...
- 平时工作常用linux命令总结
mkdir 创建目录 make dir cp 拷贝文件 copy mv 移动文件 move rm 删除文件 remove # 创建连级目录 mkdir -p a/b/c # 拷贝文件夹a到文 ...
- DHCP显示
两种PXE启动芯片 开机显示:Inter® Boot Agent GE V1.2.45或者Intel UNDI PXE2.0 (Build 082):其中UNDI是Universal Network ...
- Bridge的数据在内核处理流程
转:http://blog.sina.com.cn/s/blog_67cc0c8f0101oh33.html 转载一篇Bridge的数据在内核处理流程,文章写的不错啊! (2013-07-05 16: ...
- JavaScript【对象的学习】
JavaScript对象的了解 1.js的String对象创建String对象:var str = "abc";方法和属性(参照W3C文档详细学习)属性 length:字符串的长度 ...
- Java 通过Math.random() 生成6位随机数
public static void main(String[] args) { String sjs=""; for (int i = 0; i < 6; i++) { i ...
- POJ1722 算法竞赛进阶指南 SUBSTRACT减操作
原题连接 题目描述 给定一个整数数组\(a_1,a_2,-,a_n\). 定义数组第 i 位上的减操作:把\(a_i\)和\(a_{i+1}\)换成\(a_i - a_{i+1}\). 用con(a, ...
- IDEA 使用LiveEdit插件
第一步: 第二步: 第三步: 第四步: 等待下载完成 第五步: 第六步: 第七步: 配置tomcat时注意选择chrome浏览器,并勾选右边的多选框 完成之后,就可以启动项目了,然后可以改变html代 ...
- 寻找hive视图
如何hive视图 1.mysql数据库 [centos@s201 ~]$ mysql -uroot -proot mysql> show databases; +---------------- ...
- PHP类知识----静态属性和方法
<?php class mycoach { public $name="陈培昌"; CONST hisage =; ; private $favorite = "喜 ...