Question about a C++ Solution


  • 0

    In the DFS function, I have a state argument to record the current state. I think it should be pass by reference. However passing by value gives the right answer, but passing by reference doesn't... Does anyone have an idea? Thanks.

        bool helper(string& state, unordered_map<string, bool>& dict, int maxChoosableInteger, int desiredTotal) {
            string curr = state;
            cout << state << endl;
            if( dict.find(curr) != dict.end() ) return dict[curr];
            else{
                for(int i = 0; i < maxChoosableInteger; ++i) {
                    if(state[i] == '1') continue;
                    
                    state[i] = '1';
                    if(i + 1 >= desiredTotal || !helper(state, dict, maxChoosableInteger, desiredTotal - i - 1)) {
                        dict[curr] = true;
                        return true;
                    }
                    state[i] = '0';
                }
                dict[curr] = false;
                return false;
            }
        }
    

Log in to reply
 

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