【LeetCode】- Merge Sorted Array (合并有序数组)

[ 问题: ]

Given two sorted integer arrays A and B, merge B into A as one sorted array.

直译:给定两个排好序的整形数组,将数组B合并到数组A,形成一个新的数组。

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 and n respectively.

[ 解法: ]

题解:从结尾开始归并,不会覆盖元素又能满足题意。

参数m:数组m位置后面元素会被数组B中元素替换

参数n:从第一个开始,将数组B中n个元素会被合并到数组A

public class Solution {
	public static void main(String[] args) {
		int[] A = { 1, 2, 4, 5, 6, 8 };
		int[] B = { 3, 9, 10 };
		new Solution().merge(A, 3, B, 2);  // 1 2 3 4 9 8
	}

	public void merge(int A[], int m, int B[], int n) {
		int i, j, k;
		for (i = m - 1, j = n - 1, k = m + n - 1; k >= 0; --k) {
			if (i >= 0 && (j < 0 || A[i] > B[j])) {
				A[k] = A[i--];
			} else {
				A[k] = B[j--];
			}
		}
	}
}

时间: 2024-08-04 17:59:21

【LeetCode】- Merge Sorted Array (合并有序数组)的相关文章

(每日算法)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] Merge Sorted Array [22]

题目 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: Merge Sorted Array [088]

[题目] 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

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:Merge Sorted Array 归并排序

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

[LeetCode]6. Merge Sorted Arrays合并排序数组

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两个有序数组排序

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] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this

(LeetCode)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 init

[LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 这道题是之前那道Remove Duplicates from Sorted Array 有序数组中