码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Flip Game II"

时间:2015-10-16 13:21:08      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

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;
    }
};

LeetCode "Flip Game II"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4884786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!