LeetCode – Refresh – Merge Sorted Array

Note:

Since it merged from end, so

1. tmp1, tmp2 = INT_MIN

2. tmp1 > tmp2

 1 class Solution {
 2 public:
 3     void merge(int A[], int m, int B[], int n) {
 4         int total = m+n-1, indexA = m-1, indexB = n-1;
 5         while (indexA >= 0 || indexB >= 0) {
 6             int tmp1 = INT_MIN, tmp2 = INT_MIN;
 7             if (indexA >= 0) {
 8                 tmp1 = A[indexA];
 9             }
10             if (indexB >= 0) {
11                 tmp2 = B[indexB];
12             }
13             if (tmp1 > tmp2) {
14                 A[total--] = A[indexA--];
15             } else {
16                 A[total--] = B[indexB--];
17             }
18         }
19     }
20 };
时间: 2024-10-16 14:22:29

LeetCode – Refresh – 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

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