Can anyone help me?


  • 0
    W

    I know this may be a stupid question but it confuses me whole morning.

    class Solution {
    public:
        bool canWin(string s) {
        return win(s);
        }
        
        bool win(string s)
        {
            for(int i=0;i<s.size()-1;i++)
            {
                if(s[i]=='+'&&s[i+1]=='+')
                    {
                        s[i]='-';
                        s[i+1]='-';
                        bool p2win=win(s);
                        if(!p2win) return true;
                        s[i]='+';
                        s[i+1]='+';
                    }
            }
            return false;
        }
    };
    

    why my above code doesn't work but the following works well when s=""?

    class Solution {
    public:
        bool canWin(string s) {
        return win(s);
        }
        
        bool win(string s)
        {
            int a=s.size()-1;
            for(int i=0;i<a;i++)
            {
                if(s[i]=='+'&&s[i+1]=='+')
                    {
                        s[i]='-';
                        s[i+1]='-';
                        bool p2win=win(s);
                        if(!p2win) return true;
                        s[i]='+';
                        s[i+1]='+';
                    }
            }
            return false;
        }
    };
    

    Thanks


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.