[問題] Leetcode 294 Flip Game II

看板Prob_Solve作者時間4年前 (2019/12/08 16:21), 4年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
(更新)發現是 Leetcode 的 test case 不夠完整造成的誤會,感謝收看。 因為是鎖起來的題目所以直接貼: You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive "++" into "--". The game ends when a person can no longer make a move and therefore the other person will be the winner. Write a function to determine if the starting player can guarantee a win. Example: Input: s = "++++" Output: true Explanation: The starting player can guarantee a win by flipping the middle "++" to become "+--+". Follow up: Derive your algorithm's runtime complexity. 這題我有用 recursion + memo 解出來, 但看到一個 finding pattern 的方法可以 AC 且 100%, 不過卻無法參透它,想請問有沒有人可以幫忙說明下,感激不盡。 # Python3 class Solution: def canWin(self, s: str) -> bool: S, length = set(), 0 for c in s + '-': if c == '-': if length and length % 4 != 1: length |= 1 S ^= {length} length = 0 else: length += 1 return S -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.245.65.132 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Prob_Solve/M.1575793310.A.E66.htmlwheels:轉錄至看板 Soft_Job 12/08 16:23 ※ 編輯: wheels (60.245.65.132 臺灣), 12/08/2019 19:06:07
文章代碼(AID): #1TxBAUvc (Prob_Solve)