Simple Java solution with given constraints


  • 0
    L
      public List<Integer> findDuplicates(int[] nums)
      {
        if (nums.length <= 1)
        {
          return Collections.emptyList();
        }
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= nums.length; i++)
        {
          int val = nums[i - 1];
          int otherval = nums[Math.abs(val) - 1];
          if (otherval < 0)
          {
            list.add(Math.abs(val));
          }
          else
          {
            nums[Math.abs(val) - 1] = -otherval;
          }
        }
        return list;
      }
    

Log in to reply
 

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