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 andn respectively.

原题链接:https://oj.leetcode.com/problems/merge-sorted-array/

题目:给定两个有序数组A和B,把B归并到A中为一个有序数组。

之前做过了类似的题,只是之前的题是新建一个数组,而这题是在原数组之上归并。

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

LeetCode——Merge Sorted Array

时间: 2024-10-05 18:18:02

LeetCode——Merge Sorted Array的相关文章

[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 归并排序

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: 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 a

[Leetcode] Merge Sorted Array (C++)

题目: 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 (合并有序数组)

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 --- 归并数组

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—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

Array——LeetCode——Merge Sorted Array

[学到的知识点--当有两个判断时,谁在外面谁在里面]1.两个判断 判断一:a和b是否会超出m和n 判断二:nums1[a]大,还是nums2[b]大2.很显然判断一的优先级大于判断二-----------------------------------------------------------------------------------------------------[反思]1.审题,题目说了nums1有足够的空间----------------------------------