作者wheels ()
看板Prob_Solve
标题[问题] Leetcode 294 Flip Game II
时间Sun Dec 8 16:21:48 2019
(更新)发现是 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://webptt.com/cn.aspx?n=bbs/Prob_Solve/M.1575793310.A.E66.html
※ wheels:转录至看板 Soft_Job 12/08 16:23
※ 编辑: wheels (60.245.65.132 台湾), 12/08/2019 19:06:07