Description

On a table are N cards, with a positive integer printed on the front and back of each card (possibly different).

We flip any number of cards, and after we choose one card.

If the number X on the back of the chosen card is not on the front of any card, then this number X is good.

What is the smallest number that is good? If no number is good, output 0.

Here, fronts[i] and backs[i] represent the number on the front and back of card i.

A flip swaps the front and back numbers, so the value on the front is now on the back and vice versa.

Example:

Input: fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
Output: 2
Explanation: If we flip the second card, the fronts are [1,3,4,4,7] and the backs are [1,2,4,1,3].
We choose the second card, which has number 2 on the back, and it isn't on the front of any card, so 2 is good.

Note:

  1. 1 <= fronts.length == backs.length <= 1000.
  2. 1 <= fronts[i] <= 2000.
  3. 1 <= backs[i] <= 2000.

Analyse

桌子上有N张牌,正反面都印有正整数

可以翻转任意张牌,然后选一张牌,如果这张牌背面的数字X在牌的正面没有,这个数字X就是要输出的答案

一张牌的正反面如果是同一个数字,那这个数字肯定不是答案,这个例子里的1,4就不可能是答案,在剩下的元素中输出最小的那个

fronts = [1,2,4,4,7]
backs = [1,3,4,1,3]

写出的第一个版本

bool isExist(int value, vector<int>& vec)
{
vector<int>::iterator it = find(vec.begin(), vec.end(), value);
if (it != vec.end())
{
return true;
} return false;
} int flipgame(vector<int>& fronts, vector<int>& backs) {
int min = 2001;
int tmp; vector<int> intersect = {}; for (int i = 0; i < fronts.size(); i++)
{
if (fronts[i] == backs[i])
{
intersect.push_back(fronts[i]);
continue;
}
} for (int i = 0; i < fronts.size(); i++)
{
if (!isExist(fronts[i], intersect))
{
tmp = fronts[i] <
if (isExist(backs[i], intersect))
{
continue;
}
else
{
tmp = backs[i];
}
}
else
{
if (isExist(backs[i], intersect))
{
tmp = fronts[i];
}
else
{
tmp = fronts[i] < backs[i] ? fronts[i] : backs[i];
}
} min = min < tmp ? min : tmp;
} return min == 2001 ? 0 : min;
}

把代码简化一下

bool isExist(int value, vector<int>& vec)
{
vector<int>::iterator it = find(vec.begin(), vec.end(), value);
if (it != vec.end())
{
return true;
} return false;
} int flipgame(vector<int>& fronts, vector<int>& backs) {
int min = 2001;
int tmp; vector<int> intersect = {}; for (int i = 0; i < fronts.size(); i++)
{
if (fronts[i] == backs[i])
{
intersect.push_back(fronts[i]);
continue;
}
} for (int i = 0; i < fronts.size(); i++)
{
if (!isExist(fronts[i], intersect))
{
tmp = fronts[i] <
if (isExist(backs[i], intersect))
{
continue;
}
else
{
tmp = backs[i];
}
}
else
{
if (isExist(backs[i], intersect))
{
tmp = fronts[i];
}
else
{
tmp = fronts[i] < backs[i] ? fronts[i] : backs[i];
}
} min = min < tmp ? min : tmp;
} return min == 2001 ? 0 : min;
}

继续优化,把vector换成unordered_set,在LeetCode上就会有巨大的提升

int flipgame(vector<int>& fronts, vector<int>& backs) {
int min = 2001;
int tmp; unordered_set<int> intersect = {}; for (int i = 0; i < fronts.size(); i++)
{
if (fronts[i] == backs[i])
{
intersect.insert(fronts[i]);
continue;
}
} for (int i = 0; i < fronts.size(); i++)
{
if (intersect.count(fronts[i]) == 0)
{
min = min < fronts[i] ? min : fronts[i];
} if (intersect.count(backs[i]) == 0)
{
min = min < backs[i] ? min : backs[i];
}
} return min == 2001 ? 0 : min;
}

看看LeetCode上评价最高的版本

思路是差不多的,改成了用数组存储,有点bitmap的思想,

int flipgame(vector<int>& fronts, vector<int>& backs) {
int res[2002] = {0}; // -1: bad. 1:exist.
for (int i = 0; i < fronts.size(); i++)
{
if (fronts[i] == backs[i])
{
res[fronts[i]] = -1;
}
else
{
if (res[fronts[i]] != -1)
res[fronts[i]] = 1;
if (res[backs[i]] != -1)
res[backs[i]] = 1;
}
} for (int i = 0; i < 2002; i++)
{
if (res[i] == 1)
return i;
}
return 0;
}

比如

fronts = [1,2,4,4,7]
backs = [1,3,4,1,3]

res[1] = -1;

res[2] = 1;

res[3] = 1;

res[4] = -1;

res[7] = 1;

for循环的时候从index小的开始,遇到的第一个值为1的就是要找的值

Reference

  1. set_intersection - C++ Reference

  2. find - C++ Reference

[LeetCode] 822. Card Flipping Game的更多相关文章

  1. 【LeetCode】822. Card Flipping Game 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/card-flip ...

  2. [LeetCode] Card Flipping Game 翻卡片游戏

    On a table are N cards, with a positive integer printed on the front and back of each card (possibly ...

  3. [Swift]LeetCode822. 翻转卡片游戏 | Card Flipping Game

    On a table are N cards, with a positive integer printed on the front and back of each card (possibly ...

  4. 【LeetCode】832. Flipping an Image 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 翻转 + 异或 直接计算 日期 题目地址:https ...

  5. [LeetCode] Score After Flipping Matrix 翻转矩阵后的分数

    We have a two dimensional matrix A where each value is 0 or 1. A move consists of choosing any row o ...

  6. LeetCode算法题-Flipping an Image(Java实现)

    这是悦乐书的第324次更新,第347篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第194题(顺位题号是832).给定二进制矩阵A,我们想要水平翻转图像,然后反转它,并返 ...

  7. Java实现 LeetCode 822 翻转卡片游戏(暴力)

    822. 翻转卡片游戏 在桌子上有 N 张卡片,每张卡片的正面和背面都写着一个正数(正面与背面上的数有可能不一样). 我们可以先翻转任意张卡片,然后选择其中一张卡片. 如果选中的那张卡片背面的数字 X ...

  8. LeetCode题解之Flipping an Image

    1.题目描述 2.题目分析 使用C++的迭代器 3.代码 vector<vector<int>> flipAndInvertImage(vector<vector< ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. Codeforces#398 &767C. Garland 树形求子节点的和

    传送门 题意:在一个树上,问能否切两刀,使得三块的节点值的和相同. 思路: 由于这个总的节点和是不变的,每块的节点值和sum固定,dfs搜索,和等于sum/3,切.若不能分成三块(不能被3整除,-1) ...

  2. adb logcat命令

    1.http://blog.csdn.net/tumuzhuanjia/article/details/39555445 2.http://blog.csdn.net/xyz_lmn/article/ ...

  3. Oracle数据库之七 多表查询

    七.多表查询 ​ 对于查询在之前已经学过了简单查询.限定查询.查询排序,这些都属于 SQL 的标准语句,而上一章的单行函数,主要功能是为了弥补查询的不足. ​ 而从多表查询开始就正式进入到了复杂查询部 ...

  4. CCPC-Wannafly Summer Camp #1(部分解题报告)

    A:Birthday 时间限制: 1 Sec  内存限制: 256 MB 题目描述 恬恬的生日临近了.宇扬给她准备了一个大蛋糕. 正如往常一样,宇扬在蛋糕上插了n支蜡烛,并把蛋糕分为m个区域.因为某种 ...

  5. 第11讲-Java泛型和文件操作

    1.知识点 1.1.课程回顾 1.2.本章重点 1.2.1.泛型 1.2.2.文件操作 2.具体内容 2.1.Java泛型 2.1.1.为什么需要泛型 我们发现在List中,底层是Object[ ]数 ...

  6. Java 多线程实现接口Runnable和继承Thread区别(转)

    Java 多线程实现接口Runnable和继承Thread区别 Java中有两种实现多线程的方式.一是直接继承Thread类,二是实现Runnable接口.那么这两种实现多线程的方式在应用上有什么区别 ...

  7. eclipse中离线安装activit插件

    离线安装activiti教程: 1.先下载压缩包和jar包 链接:https://pan.baidu.com/s/1hSToZt_4A262rUxc8KToCw 密码:j5r1 2.将下载好的jars ...

  8. 使用Python SMTP发送邮件

    import smtplibfrom email.mime.text import MIMEText # 服务器SMPTserver = "smtp.163.com"# 发送邮件的 ...

  9. ImageView的功能和使用

    ImageView继承自View类,它的功能用于显示图片, 或者显示Drawable对象 xml属性: src和background区别 参考:http://hi.baidu.com/sunboy_2 ...

  10. Net基础篇_学习笔记_第十天_方法_方法的调用问题

    在Main()函数中,调用Test()函数,我们管Main()函数称之为调用者,管Test()函数称之为被调用者.如果被调用者想要得到调用者的值:1).传递参数.2).使用静态字段来模拟全局变量.如果 ...