尚未解答計算數學 Problem Solving- Leetcode 294 Flip Game II
計算數學 Problem Solving- Leetcode 294 Flip Game II
(更新)發現是 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
--
0個答案
計算數學 Problem Solving- Leetcode 294 Flip Game II