# 1089.Duplicate Zeros

**1089.Duplicate Zeros**

难度:Easy

> 给你一个长度固定的整数数组 arr，请你将该数组中出现的每个零都复写一遍，并将其余的元素向右平移。

注意：请不要在超过该数组长度的位置写入元素。

要求：请对输入的数组 就地 进行上述修改，不要从函数返回任何东西。

```
示例 1：

输入：[1,0,2,3,0,4,5,0]
输出：null
解释：调用函数后，输入的数组将被修改为：[1,0,0,2,3,0,0,4]
示例 2：

输入：[1,2,3]
输出：null
解释：调用函数后，输入的数组将被修改为：[1,2,3]
 

提示：

1 <= arr.length <= 10000
0 <= arr[i] <= 9
```

使用一个堆栈来保留前N项，最后复制到数组中即可。注意堆栈的大小。

```
class Solution {
public:
    void duplicateZeros(vector<int>& arr) {
        if(arr.size()==1) return ;
        stack<int> num;
        for(auto a: arr)
        {
            num.push(a);
            if(!a) num.push(a);
            
            if(num.size()>=arr.size()) break;
        }
        if(num.size()>arr.size()) num.pop();
        int start=num.size()-1;
     //   cout<<start<<endl;
        while(!num.empty())
        {
            arr[start--]=num.top();
            num.pop();
        }
        
    }
};
```

> 执行用时 :28ms, 在所有 C++ 提交中击败了91.37%的用户\
> 内存消耗 :9.4MB, 在所有 C++ 提交中击败了100%的用户


---

# 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://dfine.gitbook.io/leetcode/1089.duplicate_zeros.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.
