[LeetCode] 822. Card Flipping Game
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 <= fronts.length == backs.length <= 1000.
- 1 <= fronts[i] <= 2000.
- 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
[LeetCode] 822. Card Flipping Game的更多相关文章
- 【LeetCode】822. Card Flipping Game 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/card-flip ...
- [LeetCode] Card Flipping Game 翻卡片游戏
On a table are N cards, with a positive integer printed on the front and back of each card (possibly ...
- [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 ...
- 【LeetCode】832. Flipping an Image 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 翻转 + 异或 直接计算 日期 题目地址:https ...
- [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 ...
- LeetCode算法题-Flipping an Image(Java实现)
这是悦乐书的第324次更新,第347篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第194题(顺位题号是832).给定二进制矩阵A,我们想要水平翻转图像,然后反转它,并返 ...
- Java实现 LeetCode 822 翻转卡片游戏(暴力)
822. 翻转卡片游戏 在桌子上有 N 张卡片,每张卡片的正面和背面都写着一个正数(正面与背面上的数有可能不一样). 我们可以先翻转任意张卡片,然后选择其中一张卡片. 如果选中的那张卡片背面的数字 X ...
- LeetCode题解之Flipping an Image
1.题目描述 2.题目分析 使用C++的迭代器 3.代码 vector<vector<int>> flipAndInvertImage(vector<vector< ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- cogs 1377. [NOI2011] NOI嘉年华 (dp
题意:给你n个活动的起止时间,要你从中选一些活动在2个会场安排(不能有两个活动在两个会场同时进行),使活动较少的会场活动数最大,以及在某个活动必须选择的前提下,求该答案. 思路:由于n很小,时间很大, ...
- CF985B Switches and Lamps 思维 第十九
Switches and Lamps time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- LeetCode探索初级算法 - 动态规划
LeetCode探索初级算法 - 动态规划 今天在LeetCode上做了几个简单的动态规划的题目,也算是对动态规划有个基本的了解了.现在对动态规划这个算法做一个简单的总结. 什么是动态规划 动态规划英 ...
- MySQL基础/数据库和表的设计
MySQL基础 一:安装MySQL(按步骤操作,如果下载后使用不了,试着用360安全卫士卸载MySQL,清除残留的,方便在下载造成不必要的麻烦:如果这样也不行,那就需要重做系统在进行下载) 二:创建数 ...
- SpringCloud学习笔记(5):Hystrix Dashboard可视化监控数据
简介 上篇文章中讲了使用Hystrix实现容错,除此之外,Hystrix还提供了近乎实时的监控.本文将介绍如何进行服务监控以及使用Hystrix Dashboard来让监控数据图形化. 项目介绍 sc ...
- Hive入门--2.分区表 外部分区表 关联查询
1.查看mysql中metastore数据存储结构 Metastore中只保存了表的描述信息(名字,列,类型,对应目录) 使用SQLYog连接itcast05 的mysql数据库 查看hive数据库 ...
- jsp学习:jsp学习阶段性总结2019.9.21
Jsp学习 jsp语法格式: 脚本程序:<% 代码片段 %> jsp声明:<%! declaration; [ declaration; ]+ ... %> 表达式:<% ...
- 50 (OC)* URL Scheme 网页地址协议
在Xcode 9 下,新建的工程,在plist文件中注册URL Schemes,从safari无法打开问题 1:URL Scheme是什么 2:URL Scheme有什么作用 3:URL Scheme ...
- ActiveMQ高级特性
一.常用配置属性 以下配置文件目录均为:${activemq_home}/conf/activemq.xml 1.定期扫描清理 ActiveMQ中有一项功能:Delete Inactive Desti ...
- C++ 变量判定的螺旋法则
C++ 中一个标识符配合着各种修饰界定符,使得标识符的本意不那么直观一眼就能看出,甚至需要仔细分析,才能知道该标识符的具体你含义. 比如: void (*signal(int, void (*fp)( ...