My Solution With Java passes All Test Cases in ( permutation I , permutation II ) Work 100%


  • 0
    Y

    My Solution Work 100%
    With java :

    boolean next_permutation(int[] p) {
      for (int a = p.length - 2; a >= 0; --a)
        if (p[a] < p[a + 1])
          for (int b = p.length - 1;; --b)
            if (p[b] > p[a]) {
              int t = p[a];
              p[a] = p[b];
              p[b] = t;
              for (++a, b = p.length - 1; a < b; ++a, --b) {
                t = p[a];
                p[a] = p[b];
                p[b] = t;
              }
              return true;
            }
      return false;
    }
    
    List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        Arrays.sort(nums);
        res.add(new ArrayList<Integer>(builderArray(nums)));
        while (next_permutation(nums))
            res.add(new ArrayList<Integer>(builderArray(nums)));
        return res;
    }
    
    public List<Integer> builderArray(int arr[]){
        List<Integer> res = new LinkedList<>();
        for(int i=0 ; i < arr.length ; i++){
            res.add(arr[i]);
        }
        return res;
    }
    

    ***And if you have any suggestions for optimization the code please tell me, thank you***


Log in to reply
 

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