I encounter strange errors in my code.


  • 0
    C

    My answer for this problem is as follows:

    class Solution {
    public:
        map<int,int> m;
        void help(vector<vector<int>> &res,vector<int> &temp,vector<int>&nums){
            if(temp.size()==nums.size()){
                res.push_back(temp);
                return;
            }
            int index;
            int last;
            cout<<"b"<<endl;
            for(index=0;index<nums.size();){
                int cur = nums[index];
                if(m[cur]!=0){
                    cout<<"index: "<<index<<endl;
                    m[cur] -= 1;
                    temp.push_back(cur);
                    help(res,temp,nums);
                    temp.pop_back();
                    m[cur] += 1;
                    last = cur;
                }
                index++;
                while(index<nums.size()&&nums[index]==last) index++;
            }
        }
        vector<vector<int>> permuteUnique(vector<int>& nums) {
            for(auto item:nums){
                auto c = m.find(item);
                if(c == m.end()) m[item] = 1;
                else c->second += 1;
            }
            vector<vector<int>> res;
            vector<int> temp;
            sort(nums.begin(),nums.end());
            help(res,temp,nums);
            cout<<res.size()<<endl;
            return res;
        }
    };
    

    It can be accepted. However, when I delete the two cout<<... lines, My code get "WA", do anyone know how can this happen?


Log in to reply
 

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