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 <= 30000
A.length
is 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 ...
随机推荐
- layDate面板出现红色花纹图案
要使用layDate,有两种方法: 1. 要么在引用layui.js和layui.css,然后通过layui.use('laydate', callback) 加载模块后,调用方法使用. 2. 去la ...
- PHP删除字符串中的空格和换行符 将字符串中的连续多个空格转换为一个空格
//删除空格和回车 function trimall($str){ $qian=array(" "," ","\t","\n&qu ...
- MySQL 高级 视图 事物 触发器 函数 索引优化
视图 1.什么是视图 视图就是通过查询得到一张虚拟表,然后保存下来,下次直接使用即可 2.为什么要用视图 如果要频繁使用一张虚拟表,可以不用重复查询 3.如何用视图 create view t ...
- VM 下增加磁盘空间
随着Linux虚拟机的不断使用,在VMware中经常遇到 预先装好的 linux 虚拟机的硬盘空间过小 的问题,造成很多软件不能安装, 而重新装一个,又挺麻烦.于是,上网搜了下关于 vmware 硬盘 ...
- 访问kubernetes api
kubernetes api介绍 作用: 将各种资源对象的数据都通过该api接口被提交到后端的持久化存储etcd中; 一个api的顶层元素由kind丶apiVersion丶metadata丶spec和 ...
- Toast的基本用法 吐司打印
//Toast.makeText(上下文,内容,显示时间);Toast toast =Toast.makeText(this,"位置="+position+"内容=&qu ...
- springboot的一些开源项目
原文标题:精选SpringBoot八大开源项目:支付.秒杀.全文搜索等 支付项目: 项目地址:https://gitee.com/52itstyle/spring-boot-pay 秒杀案例: 项目地 ...
- 【每日一包0011】pad
[github地址:https://github.com/ABCDdouyae...] pad 给字符串的左右加padding,也可以用于删减字符串两端 用法:pad(str, length, opt ...
- 自签名ssl
openssl req -nodes -newkey rsa:1024 -out myreq.pem -keyout privatekey.pem openssl req -in myreq.pem ...
- hdu 5533 正n边形判断 精度处理
Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...