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

说明:无

实现:

精简实现:

 1 // 时间复杂度 O(m+n),空间复杂度 O(1)
 2 class Solution {
 3 public:
 4     void merge(int A[], int m, int B[], int n) {
 5         int i=m-1,j=n-1,k=m+n-1;
 6         while(i>=0&&j>=0)//从后面开始比较归并,直到有一个数组归并完
 7         {
 8           A[k--]=A[i]>B[j]?A[i--]:B[j--];//将大数赋给A[k]
 9         }
10         while(j>=0)//若B还没归并完,直接归并到A
11             A[k--]=B[j--];
12     }
13 };

leetcode 题解:Merge Sorted Array(两个已排序数组归并)

时间: 2024-11-03 16:53:37

leetcode 题解:Merge Sorted Array(两个已排序数组归并)的相关文章

LeetCode第[88]题(Java):Merge Sorted Array(合并已排序数组)

题目:合并已排序数组 难度:Easy 题目内容: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 翻译: 给定两个排序的整数数组nums1和nums2,将nums2合并到nums1中作为一个排序数组. 注意: nums1和nums2中初始化的元素数量分别为m和n. nums1有足够的空间(大小大于或等于m+n)来容纳nums2中的额外元素. 我的思路:此处和归

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]. 说明: 1)设个标志可实现 实现: 1 class Solution { 2 public

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

<LeetCode OJ> 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

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 (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(88)题解-- 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 additi