标签:
A natural recursion thought.. Please note we can cache intermediate results.
class Solution { unordered_map<string, bool> hs; public: bool canWin(string s) { if(hs.count(s)) { return hs[s]; } size_t n = s.length(); if (n < 2) return false; bool ret = false; int i = 0; while(i < n - 1) { if(s[i] == s[i + 1] && s[i] == ‘+‘) { string ns = s; ns[i] = ns[i + 1] = ‘-‘; if(!canWin(ns)) { ret = true; break; } } i ++; } hs[s] = ret; return ret; } };
标签:
原文地址:http://www.cnblogs.com/tonix/p/4884786.html