Why my solution exceeds time limit?


  • 0
    S

    I don't understand why my solution exceeds time limit? Can someone give me a hint?

    public class Solution {
        public List<List<Integer>> permuteUnique(int[] num) {
             if(num == null || num.length == 0)
                return null;
            List<List<Integer>> res = new ArrayList<List<Integer>>();  
            Arrays.sort(num);
            doPermute(num, 0, res);  
            return res;
        }
        public static void doPermute(int[] num, int k, List<List<Integer>> res){
            if(k == num.length){
                res.add(convertArrayToList(num));
                return;
            }
            for(int i = k; i < num.length; i++){
                swap(num, k, i);
    		    doPermute(num, k+1, res);
    		    swap(num, k, i);
    		    if(i>0 && num[i]==num[i-1])
                    continue;
            }
        }
        public static List<Integer> convertArrayToList(int[] num){
            List<Integer> res = new ArrayList<Integer>();
            for(int i = 0; i < num.length; i++){
                res.add(num[i]);
            }
            return res;
        }
        public static void swap(int[] num, int i, int j){
            int temp = num[i];
            num[i] = num[j];
            num[j] = temp;
        }
    }
    

    Time limit is exceeded on this test case: [-1,-1,3,-1]


  • 0
    P

    i met the similar problem with you, and i tested it on my eclipse, and worked fine. Same test case.


Log in to reply
 

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