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

题目很短,题意很明显,说一下思路吧

首先得分出正数和负数,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的更多相关文章

  1. 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【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 ...

  3. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  4. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  5. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  6. 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 ...

  7. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  8. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  9. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

随机推荐

  1. 基于Nginx简单实现动静分离

    1.首先安装Nginx 2.在Nginx.conf文件中添加如下配置: server{ listen 80; server_name www.lf.com; location ~ (.jpg|.png ...

  2. Linux设置串口波特率等参数

    转自 http://blog.csdn.net/zoomdy/article/details/50921336 mingdu.zheng at gmail dot com stty查看串口参数 stt ...

  3. 基于django rest framework做认证组件

    先导入要用到的类 from rest_framework.authentication import BaseAuthentication from rest_framework.exceptions ...

  4. this关键字剖析

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. hibernate的几个重要的类和接口

    Configuration类 该类的对象会自动加载hibernate.cfg.xml文件,同时也可以定义自己的配置文件 sessionFactory接口 由于SessionFactory是重量级的,也 ...

  6. UltraEdit 回车符替换空格

    查找和替换    输入 ^r^n   替换为:(空格)

  7. MySQL联合索引运用-最左匹配原则

    前言 之前看了很多关于MySQL索引的文章也看了<高性能MySQL>这本书,自以为熟悉了MySQL索引使用原理,入职面试时和面试官交流,发现对复合索引的使用有些理解偏颇,发现自己的不足整理 ...

  8. 以太坊系列之十三: evm指令集

    evm指令集手册 Opcodes 结果列为"-"表示没有运算结果(不会在栈上产生值),为"*"是特殊情况,其他都表示运算产生唯一值,并放在栈顶. mem[a.. ...

  9. LOJ#10172. 「一本通 5.4 练习 1」涂抹果酱

    题目链接:https://loj.ac/problem/10172 题目描述 Tyvj 两周年庆典要到了,Sam 想为 Tyvj 做一个大蛋糕.蛋糕俯视图是一个 N×MN×MN×M 的矩形,它被划分成 ...

  10. pdo + 事务处理 处理线性事务

    /* * 事物处理线性操作. * 以转账为例 */ header('Content-type:text/html;charset=utf-8'); $opt = array(PDO::ATTR_PER ...