Java for LeetCode 088 Merge Sorted Array

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

解题思路一:

使用一个nums1Copy把nums1的前m个元素拷贝出来,然后把nums1Copy和nums2放进nums1中,JAVA实现如下:

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int[] nums1Copy=new int[m];
    	for(int i=0;i<m;i++)
    		nums1Copy[i]=nums1[i];
    	int i=0,j=0,k=0;
    	while(i<m&&j<n){
    		if(nums1Copy[i]<nums2[j])
    			nums1[k++]=nums1Copy[i++];
    		else nums1[k++]=nums2[j++];
    	}
    	if(i>=m-1)
    		while(j<n)
    			nums1[k++]=nums2[j++];
    	if(j>=n-1)
    		while(i<m)
    			nums1[k++]=nums1Copy[i++];
    }

解题思路二:

思路一会有一定的空间复杂度,可以直接把nums1中的前m个元素往右平移n个单位,然后进行合并,JAVA实现如下:

    public void merge(int[] nums1, int m, int[] nums2, int n) {
    	for(int i=m+n-1;i>=n;i--)
    		nums1[i]=nums1[i-n];
    	int i=n,j=0,k=0;
    	while(i<m+n&&j<n){
    		if(nums1[i]<nums2[j])
    			nums1[k++]=nums1[i++];
    		else nums1[k++]=nums2[j++];
    	}
    	if(i>=m+n)
    		while(j<n)
    			nums1[k++]=nums2[j++];
    }
时间: 2024-10-05 05:01:49

Java for LeetCode 088 Merge Sorted Array的相关文章

LeetCode --- 88. Merge Sorted Array

题目链接:Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements init

LeetCode OJ - Merge Sorted Array

原地归并. 下面是AC代码: 1 public void merge(int A[], int m, int B[], int n) { 2 3 int len = A.length; 4 //first copy m elements of A to the end of A 5 for(int i=m-1,j = len-1;i>=0;i--){ 6 A[j--] = A[i]; 7 } 8 //merge the A and B 9 int startA = len - m; 10 int

【leetcode】Merge Sorted Array

Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialize

leetCode 88. Merge Sorted Array 有序数组

88. Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The n

&lt;LeetCode OJ&gt; Merge Sorted Array【88】

88. Merge Sorted Array My Submissions Question Total Accepted: 80830 Total Submissions: 274120 Difficulty: Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough sp

LeetCode (1) Merge Sorted Array

题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. 刚开始根据归并排序写的多了一个中

[LeetCode]题解(python):088 Merge Sorted Array

题目来源 https://leetcode.com/problems/merge-sorted-array/ Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold addit

[LeetCode] 88. Merge Sorted Array Java

题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements i

LeetCode 88. Merge Sorted Array(合并有序数组)

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initi