JAVA use sort directly


  • 0
    Z

    class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
    for(int i=m;i<m+n;i++){
    nums1[i]=nums2[i-m];
    }
    Arrays.sort(nums1);

    }
    

    }


  • 0
    M

    I'd say it fails inputs like this:

    [-1,0,0]
    1
    [1]
    1

    You end up with [-1,0,1] instead of [-1,1,0].


  • 0
    Z

    thanks,the solution is ac by leetcode.i dont really understand what's m & n means, if m&n is smaller than nums1&nums2's length ,i think this question is really bad.


  • 0
    M

    Yeah, I know it gets AC, and the poor problem text is why I said "I'd say" it fails that case. I'm pretty sure the problem is consistently using prefixes of the arrays, i.e., we're supposed to use the first m values in nums1 and the first n values in nums2 and then end up with the merged values as the first m+n values in nums1. You clearly do interpret it that way for the input, but for the output you don't, which is inconsistent.

    Edit: I added assert m+n == nums1.length; to your code and it still got accepted (and with < instead it didn't, showing that assertions work). So the problem text saying nums1's size is "greater or equal to m + n" is bad, or the test suite is bad because it doesn't actually have such a case.


Log in to reply
 

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