一:题目描述
题目链接:. - 力扣(LeetCode)
二:算法原理分析
三:代码编写
void duplicateZeros3(vector<int>& arr)
{int dest = -1, cur = 0, n = arr.size();//1.找到要复写的最后一个数字while (cur < n){if (arr[cur]) dest++;else dest += 2;if (dest >= n - 1)break;cur++;}//处理越界问题if (dest == n){arr[n - 1] = 0;dest -= 2;cur--;}while (cur >= 0){if (arr[cur]){arr[dest--] = arr[cur--];}else{arr[dest--] = 0;arr[dest--] = 0;cur--;}}}