Use The Stream Of Java 8 Just For Fun


  • 0
    F

    user the IntStream:

    I take the advice of @StefanPochmann, and modify the code. It becomes:

    public class Solution {
        public void merge(int[] nums1, int m, int[] nums2, int n) {
            int[] ans = IntStream.concat(getStream(nums1, m), getStream(nums2, n))
                    .sorted()
                    .toArray();
                    
            // for(int i=0; i<ans.length && i<m+n; i++)   
                // nums1[i] = ans[i];
            System.arraycopy(ans, 0, nums1, 0, ans.length);
        }
        
        private IntStream getStream(int[] nums, int len){
            // return Arrays.stream( Arrays.copyOf(nums, len) );
            return Arrays.stream(nums, 0, len);
        }
    }

  • 0

    Could also use Arrays.stream(array, start, end) and System.arraycopy.


  • 0
    F

    Thank you, I get it.


Log in to reply
 

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