复写零
- 题目描述
- 算法描述
- 编程代码
链接: 复写零
题目描述
算法描述
编程代码
class Solution {
public:void duplicateZeros(vector<int>& arr) {int n = arr.size();int dest = -1,cur = 0;while(cur < n){if(arr[cur]){dest++;}else{dest+=2;}cur++;if(dest >= n-1){break;}}if(dest == n){dest-=2;arr[n-1] = 0;cur--;}cur--;while(cur>=0){if(arr[cur]){arr[dest--] = arr[cur--];}else{arr[dest--] = 0;arr[dest--] = 0;--cur;}}}
};