# 88 Merge Sorted Array

## 88. [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/)

## 1. Question

Given two sorted integer arraysnums1andnums2, mergenums2intonums1as one sorted array.

**Note:**\
You may assume thatnums1has enough space (size that is greater or equal tom+n) to hold additional elements fromnums2. The number of elements initialized in nums1 and nums2 are m and n respectively.

## 2. Implementation

**(1) Two Pointers**

```java
public class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int index1 = m - 1, index2 = n - 1;
        int k = m + n - 1;

        while (index1 >= 0 && index2 >= 0) {
            nums1[k--] = nums1[index1] > nums2[index2] ? nums1[index1--] : nums2[index2--];
        }

        while (index2 >= 0) {
            nums1[k--] = nums2[index2--];
        }
    }
}
```

## 3. Time & Space Complexity

时间复杂度O(m + n), m为nums1的长度，n为nums2的长度，空间复杂度O(1)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://protegejj.gitbook.io/algorithm-practice/leetcode/two-pointers/88-merge-sorted-array.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
