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. JSTL标签之core标签的使用

    参考:http://blog.csdn.net/qq_25827845/article/details/53311722 核心标签库的导入 <%@ taglib prefix="c&q ...

  2. 数据库MySQL 之 库操作

    数据库MySQL 之 库操作 浏览目录 系统数据库简介 数据库操作 命名规范 用户权限 修改密码 忘记密码 中文乱码问题 一.系统数据库简介 information_schema :虚拟库,不占用磁盘 ...

  3. python解释器的下载和安装

    1.python解释器的下载 python这样的语言,需要一个解释器.而且解释器还有多种语言的实现,我们介绍的是最常用的C语言的实现,称之为Cpython.Python通过在各种操作系统上都有各自的解 ...

  4. 如何获取.properties配置文件

    如何获取.properties配置文件 分析思路: 先使用流和文件关联,即读取文件 再读取文件内容,一行一行读取 字符分割“=”  键值对 然后把键值对放到集合中去 但是Properties类里面有方 ...

  5. Robot Framework - 基础关键字 BuiltIn 库(一)

    今天给大家分享的是Robot Framework 机器人框架中 BuiltIn 基础库的使用...BuiltIn 库里面提供了很多基础方法助力于我们在自动化测试领域中做的更好!——本系列教程是教会大家 ...

  6. 第01章-Spring之旅

    一.简化Java开发 1. Spring的主要特性 依赖注入DI和面向切面编程AOP. 2. 关键策略 轻量级和最小侵入性编程:POJO 松耦合:DI和AOP 声明式编程:切面和惯例 减少样板式代码: ...

  7. 编写高质量代码改善C#程序的157个建议——建议32:总是优先考虑泛型

    建议32:总是优先考虑泛型 泛型的优点是多方面的,无论泛型类还是泛型方法都同时具备可重用性.类型安全性和高效率等特性,这是非泛型和非泛型方法无法具备的. 以可重用性为例: class MyList { ...

  8. Diameter协议摘要

    ---------选择同学整理文档 1.   协议概述 Diameter协议主要为应用程序提供认证.鉴权.计费框架,即AAA,并支持本地AAA和漫游场景下的AAA. 1.1.  特点介绍 以前的AAA ...

  9. arcgis for android常见问题回答

    Q:arcgis for android最新版本是多少?(2014-7-18) Arcgis for android 10.2.3 sdk 百度盘下载地址:http://pan.baidu.com/s ...

  10. C# 中请使用Contains判断字符串是否包含另一段字符串

    ∵ :使用Contains 比 IndexOf 的性能要高很多. 因为 Contains 是判断某个字符串是否在该字符串里面,而IndexOf是返回对应下标值 但是在使用contains的时候,注意转 ...