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 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
题目很短,题意很明显,说一下思路吧
首先得分出正数和负数,0就不管了都是0,正数是小到大,负数则反过来,然后我们只找两个数字一组,为什么是两个数字呢
如果是2 4 4 8,把2,4,8找了,那还有个4怎么办,所以应该是2,4 4,8就可以了
操作就是拿出现的数字*2去看有没有这个数,最后是否还留下数字
class Solution {
public:
bool canReorderDoubled(vector<int>& A) {
sort(A.begin(),A.end());
int len = A.size();
map<int,int>Mp;
vector<int>Ve1;
vector<int>Ve2;
vector<int>Ve;
set<int>Se;
int num;
for(int i=;i<len;i++){
Mp[A[i]]++;
if(A[i]>){
Ve1.push_back(A[i]);
}
if(A[i]<){
Ve2.push_back(A[i]);
}
}
int len1 = Ve1.size();
for(int i=;i<len1;i++){
Ve.clear();
num = Ve1[i];
while(Mp[num]>){
Mp[num]--;
Ve.push_back(num);
num*=;
if(Ve.size()==){
break;
}
}
for(int j=;j<Ve.size();j++){
//cout<<Ve[j]<<" ";
}
//cout<<endl;
if(Ve.size()==){
Mp[Ve1[i]]++;
}
}
int len2 = Ve2.size();
for(int i=len2-;i>=;i--){
Ve.clear();
num = Ve2[i];
while(Mp[num]>){
Mp[num]--;
Ve.push_back(num);
num*=;
if(Ve.size()==){
break;
}
}
for(int j=;j<Ve.size();j++){
//cout<<Ve[j]<<" ";
}
//cout<<endl;
if(Ve.size()==){
Mp[Ve2[i]]++;
}
}
for(int i=;i<len;i++){
//cout<<Mp[A[i]]<<"A"<<endl;
if(Mp[A[i]]&&A[i]!=){
return false;
}
}
return true;
}
};
114th LeetCode Weekly Contest Array of Doubled Pairs的更多相关文章
- 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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 Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- 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 ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
随机推荐
- 367. Valid Perfect Square判断是不是完全平方数
[抄题]: Given a positive integer num, write a function which returns True if num is a perfect square e ...
- 面试题:MySQL性能调优——索引详解与索引的优化 没用
——索引优化,可以说是数据库相关优化.理解尤其是查询优化中最常用的优化手段之一.所以,只有深入索引的实现原理.存储方式.不同索引间区别,才能设计或使用最优的索引,最大幅度的提升查询效率! 一.BTre ...
- 在线创建MongoDB免费集群(MangoDB Atlas)
MongoDB Atlas是MongoDB的云服务,构建在亚马逊的AWS上,MongoDB允许用户在上面创建一个免费集群作为学习使用. 1. 注册MongoDB cloud账号: 访问www.mong ...
- [GO]随机生成切片元素并使用冒泡排序方式进行排序
package main import ( "math/rand" "time" "fmt" ) func ButtleData(s []i ...
- HDU 4118 Holiday's Accommodation (dfs)
题意:给n个点,每个点有一个人,有n-1条有权值的边,求所有人不在原来位置所移动的距离的和最大值. 析:对于每边条,我们可以这么考虑,它的左右两边的点数最少的就是要加的数目,因为最好的情况就是左边到右 ...
- C#序列化xml,开发常用
序列化操作对于开发人员来说最熟悉不过了. 序列化分为:序列化和反序列化. 序列化名词解释:序列化是将对象状态转换为可保持或传输的格式的过程. 与序列化相对的是反序列化,它将流转换为对象.这两个过程结合 ...
- Oracle 已连接到空闲例程或ORA-01034: ORACLE not available
因为是本地数据库没有重要资料,所以可以随便自己折腾. 出现问题原因:从生产数据库导入一个表到本地库测试,因数据量过大,在导入4-5个小时后,手动中断导入.是否异常关机不能确定. 之后再打开数据库出现一 ...
- Server Sql 多表查询、子查询和分页
一.多表查询:根据特定的连接条件从不同的表中获取所需的数据 多表查询语法: SELECT table1.column, table2.column FROM table1, table2 WHERE ...
- Ubuntu不能上网解决办法
一.设置IP.网关.DNS 新安装的Ubuntu系统ifconfig后发现没有ip,所以要设置IP.网关.DNS等,编辑 /etc/networking/interfases sudo vi /et ...
- 数据库连接工具HeidiSql介绍(支持MySQL,MariaDB,Microsoft SQL或PostgreSQL)
前言 Navicat作为比较老牌的数据库连接工具知名度比较广,功能也比较完善,但对入门的广大初学者来讲,怎么去找安装的资源包是一大难题,虽然经过一些“渠道”能找到可以正常使用的绿色安装包,但从长期来讲 ...