[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 ...
随机推荐
- Codeforces Round #503 (by SIS, Div. 2) D. The hat -交互题,二分
cf1020D 题意: 交互题目,在有限的询问中找到一个x,使得数列中的第x位和第(x+n/2)位的值大小相同.数列保证相邻的两个差值为1或-1: 思路: 构造函数f(x) = a[x] - a[x ...
- Codeforces 468 B Two Sets
Two Sets 题意:就是将一对数放进setA, setB中, 如果放进setA的话要求满足 x与a-x都在这个集合里面, 如果放进setB中要求满足x与b-x都在这个集合中. 题解:我们将能放进B ...
- codeforces 454 E. Little Pony and Summer Sun Celebration(构造+思维)
题目链接:http://codeforces.com/contest/454/problem/E 题意:给出n个点和m条边,要求每一个点要走指定的奇数次或者是偶数次. 构造出一种走法. 题解:可能一开 ...
- 牛客网 Wannafly挑战赛 C 列一列 简单题 (题目有点坑)
链接:https://www.nowcoder.com/acm/contest/71/C来源:牛客网 题目描述 小W在计算一个数列{An},其中A1=1,A2=2,An+2=An+1+An.尽管他计算 ...
- Educational Codeforces Round 69 (Rated for Div. 2)
A. DIY ...
- mysql 查询参数尾部有空格时被忽略
最近再使用mysql时,无意见发现,当我查询参数尾部输入若干个空格时,依然可以查出和不加空格相同的结果,像这样 select * from wa where name='be ' 等同于 select ...
- Java基础语法(总结篇)
关键字&标识符 关键字的概念与特征 概念:Java关键字是事先定义好的对Java的编译器有特殊的意义,他们用来表示一种数据类型,或者表示程序的结构等,关键字不能用作变量名.方法名.类名.包名和 ...
- 【第九篇】uploadify上传文件
依然不多说,上代码 首先是给文件夹的位置 然后上代码 <div class="upload"> <div class="uploadswf"& ...
- 小程序·云开发的HTTP API调用丨实战
小程序云开发之httpApi调用. 小程序云开发之httpApi调用(返回"47001处理") 技术栈 采用 nodejs + express 搭建web服务器,采用 axios ...
- c语言运算优先级与结合方向的问题
c语言运算的结合方向一直困扰我,不过我终于get到了哈哈 先附上c语言运算优先级和结合方向图片 以上有c语言一共的15个优先级,从上往下递增 c语言的运算符号有很多,当我们打代码或者看代码的时候符号多 ...