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. 409. Longest Palindrome 最长对称串

    [抄题]: Given a string which consists of lowercase or uppercase letters, find the length of the longes ...

  2. 643. Maximum Average Subarray I 最大子数组的平均值

    [抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...

  3. 532. K-diff Pairs in an Array绝对值差为k的数组对

    [抄题]: Given an array of integers and an integer k, you need to find the number of unique k-diff pair ...

  4. ROS naviagtion analysis: costmap_2d--ObstacleLayer

    博客转载自:https://blog.csdn.net/u013158492/article/details/50493676 构造函数 ObstacleLayer() { costmap_ = NU ...

  5. wamp安装两个,数据库丢了,怎么办

    wampserver3.*下载了好几天一直没有安装,今天发现必须安装,已升级自己的php版本,不过也饿可以自己手动配置PHP版本,既然有安装包就算了吧,当安装完后,发现忘记备份自己的数据库了,幸好之前 ...

  6. 丢弃昂贵的Detours Professional 3.0,使用免费强大的EasyHook

    我们要先看看微软官方的著名HOOK库: Detours Professional 3.0 售价:US$9,999.95 功能列表: Detours 3.0 includes the following ...

  7. windows phone开发必备工具翔

    1.图标设计 http://www.flaticon.com/ http://www.iconfont.cn/ 2.界面设计: 2.1.behance.com    2.2.dribbble.com  ...

  8. .Net Core 项目引用本地类库方式(二)

    上篇文章有详细的介绍.Net Core 项目中引用本地类库通过打包,然后Nugety引用方式,这里再介绍一种引用包的方式

  9. (二)RabbitMQ使用笔记

    1.RabbitMQ简介 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQP(高级消息队列协议)的标准实现. 官网:http://www.rabbitmq.c ...

  10. web静态资源的访问(笔记)

    1.在web-inf外的静态资源访问: 三种方式: 2.访问web-inf内的静态资源: 使用请求转发的方式访问web-inf静态资源(重定向相当于在浏览器输入,无法访问) 参考:https://bl ...