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.

刚开始根据归并排序写的多了一个中间数组

void MergeSortArr(int arrA[],int lA,int arrB[],int lB){
	int *trr = new int[lA+lB];
	int i = 0;
	int j = 0;
	int k = 0;
	while(i < lA && j < lB)
	{
		if(arrA[i] <= arrB[j])
			trr[k++] = arrA[i++];
		else
			trr[k++] = arrB[j++];
	}
	while(i < lA)
		trr[k++] = arrA[i++];
	while(j < lB)
		trr[k++] = arrB[j++];
	for(i = 0;i<k;++i)
	{
		arrA[i] = trr[i];
	}
	delete[] trr;
}

参考别人的写的版本从A的后面开始排序

void MergeSortArr1(int arrA[],int lA,int arrB[],int lB){
	int *trr = new int[lA+lB];
	int i = lA- 1;
	int j = lB- 1;
	int k = lA + lB - 1;
	while (i >= 0 && j >= 0)
	{
		if(arrA[i] >= arrB[j])
			arrA[k--] = arrA[i--];
		else
			arrA[k--] = arrB[j--];
	}
	while(i >= 0)
		arrA[k--] = arrA[i--];
	while(j >= 0)
		arrA[k--] = arrB[j--];

}

LeetCode (1) Merge Sorted Array

时间: 2024-11-05 18:36:35

LeetCode (1) 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: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 initialized in A and B are m

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

leetcode 【 Merge Sorted Array 】python 实现

题目: 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 initialized in A and B ar

[leetcode] 12. Merge Sorted Array

这道题的无聊之处在于题目其实给了一些很奇怪的测试用例.比如他会给一些空的数组来,但是这个是不科学的,因为在C++中不允许定义一个空的列表,我们用的又不是那种糙又快的python,所以在这里我遇到了一些问题,后来还是解决了.这道题题目如下: 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 th