[Swift]LeetCode822. 翻轉卡片遊戲 | Card Flipping Game

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-yuljfmsg-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

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

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.this

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

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

Example:htm

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

Note:blog

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

在桌子上有 N 張卡片,每張卡片的正面和背面都寫着一個正數(正面與背面上的數有可能不同)。遊戲

咱們能夠先翻轉任意張卡片,而後選擇其中一張卡片。

若是選中的那張卡片背面的數字 X 與任意一張卡片的正面的數字都不一樣,那麼這個數字是咱們想要的數字。

哪一個數是這些想要的數字中最小的數(找到這些數中的最小值)呢?若是沒有一個數字符合要求的,輸出 0。

其中, fronts[i] 和 backs[i] 分別表明第 i 張卡片的正面和背面的數字。

若是咱們經過翻轉卡片來交換正面與背面上的數,那麼當初在正面的數就變成背面的數,背面的數就變成正面的數。

示例:

輸入:fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
輸出:
解釋:假設咱們翻轉第二張卡片,那麼在正面的數變成了  , 背面的數變成了 
接着咱們選擇第二張卡片,由於如今該卡片的背面的數是 2,2 與任意卡片上正面的數都不一樣,因此 2 就是咱們想要的數字。2[1,3,4,4,7][1,2,4,1,3]。

提示:

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

Runtime: 92 ms
Memory Usage: 19.6 MB
 1 class Solution {
 2     func flipgame(_ fronts: [Int], _ backs: [Int]) -> Int {
 3         var res:Int = Int.max
 4         var n:Int = fronts.count
 5         var same:Set<Int> = Set<Int>()
 6         for i in 0..<n
 7         {
 8             if fronts[i] == backs[i]
 9             {
10                 same.insert(fronts[i])
11             }
12         }
13         for front in fronts
14         {
15             if !same.contains(front)
16             {
17                 res = min(res, front)
18             }
19         }
20         for back in backs
21         {
22             if !same.contains(back)
23             {
24                 res = min(res, back)
25             }
26         }
27         return res == Int.max ? 0 : res      
28     }
29 }

92ms

 1 class Solution {
 2     func flipgame(_ fronts: [Int], _ backs: [Int]) -> Int {
 3         var sameSets = Set<Int>()
 4         for i in fronts.indices {
 5             if fronts[i] == backs[i] {
 6                 sameSets.insert(fronts[i])
 7             }
 8         }
 9 
10         var ans = Int.max
11         let arr = fronts+backs
12         for x in arr {
13             if !sameSets.contains(x) {
14                 ans = min(ans, x)
15             }
16         }
17         return ans == Int.max ? 0 : ans
18     }
19 }
相關文章
相關標籤/搜索