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. OpenCV 2.4.13 installed in Ubuntu 14 and CMakeLists Demo

    1. 配置编译器环境 [compiler] sudo apt-get install build-essential 2. 安装OpenCV的依赖包 [required] -dev pkg-confi ...

  2. 498B Name That Tune

    传送门 题目大意 n首音乐,第i首被听出来的概率为pi,刚开始听第一首,1s后如果听出来了则放第下一首,否则接着听这一首,第i首在连续听了ti s之后一定会被听出来,问Ts后听出来的歌的期望数量. 分 ...

  3. php手机号正则

    preg_match("/^1[34578]{1}\d{9}$/", $phone)

  4. dynamic和匿名类和var的混合使用 没提示照样点

    using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...

  5. SQL语句性能分析常用命令

    DBCC freeproccache DBCC dropcleanbuffers 1.set statistics IO {ON| OFF} /*Transact-SQL 语句生成的磁盘活动量的信息* ...

  6. Entity Framework Tutorial Basics(41):Multiple Diagrams

    Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...

  7. 关于Tomcat中封装请求-响应的结构的分析

    在编写Servlet时,往往只重写了doGet和doPost方法,使用Tomcat通过(HttpServletRequest 和 HttpServletResponse)接口传递来的request和r ...

  8. Mybatis——Spring整合

    一.引入依赖 Spring的相关jar包 mybatis-3.4.1.jar mybatis-spring-1.3.0.jar mysql-connector-java-5.1.37-bin.jar ...

  9. Controlling Session Behavior in Asp.Net MVC4

    Posted By : Shailendra Chauhan, 06 Jan 2013 Updated On : 11 Jun 2014 By default, Asp.Net MVC support ...

  10. Struts2 配置及运行时遇到问题

    1.java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilt ...