面试题 10.01. Sorted Merge LCCI(Leetcode每日一题-2020.03.03)

Problem

You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order.

Initially the number of elements in A and B are m and n respectively.

Example

Input:
A = [1,2,3,0,0,0], m = 3
B = [2,5,6], n = 3
Output:
[1,2,2,3,5,6]

Solution

从后往前遍历,三个下标

idx指向合并后数组的当前值
a_idx指向A数组的当前值
b_idx指向A数组的当前值
只要b_idx>=0,就进行循环

如果a_idx不为零,就比较A[a_idx]和B[b_idx]的值,把较大的放在A[idx]处,并减小相应的下标值
如果a_idx为0,那么只需将B[b_idx]放到A[idx]处,并减小相应的下标

class Solution {
public:
    void merge(vector<int>& A, int m, vector<int>& B, int n) {

        if(m == 0 && n == 0)
            return;

        int a_idx = -1;
        int b_idx = -1;
        
        if(m > 0)
            a_idx = m-1;
        if(n > 0)
            b_idx = n-1;
        
        int idx = m+n-1;

        while(b_idx >= 0)
        {
            if(a_idx >= 0)
            {
                if(A[a_idx] > B[b_idx])
                {
                    A[idx] = A[a_idx];
                    --a_idx;
                }
                else
                {
                    A[idx] = B[b_idx];
                    --b_idx;
                }
                
            }
            else
            {
                A[idx] = B[b_idx];
                --b_idx;
            }
            --idx;
        }

    }
};
发布了496 篇原创文章 · 获赞 215 · 访问量 53万+

猜你喜欢

转载自blog.csdn.net/sjt091110317/article/details/104640347
今日推荐