[leetcode-only] [test-case] Don't we need to consider the inputs that have same numbers in the collection?


  • 11
    W

    It seems the test cases of leetcode does not consider the inputs that have same numbers in the collection?
    For example, [1,3,3].


  • 17
    L

    That Permutations 2!

    Maybe in Permutations, "collection" should be replaced by "set".


  • 0
    W

    I did not notice that there is a "permutations II"


  • 0
    Y

    I have also considered this issue.


  • 1
    M

    Here is my solution which takes care of duplicate numbers too..

    class Solution {
    public:
        vector<vector<int>> ans;
        void func(vector<int> &num,vector<int> a,int visited[], int pos)
        {
            if(pos==num.size())
            {
                ans.push_back(a);
                return;
            }
            for(int i=0;i<num.size();i++)
            {
                if(visited[i]==false)
                {
                    if(a.size()<=pos)
                        a.push_back(num[i]);
                    else
                        a[pos]=num[i];
                    visited[i]=true;
                    func(num,a,visited,pos+1);
                    visited[i]=false;
                }
                
            }
        }
        vector<vector<int> > permute(vector<int> &num) {
            vector<int> a;
            int visited[1000];
            for(int i=0;i<num.size();i++) visited[i]=false;
            func(num,a,visited,0);
            return ans;
            
        }
    };

Log in to reply
 

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