马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 赚小钱 于 2020-5-12 02:20 编辑
Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right.
Note that elements beyond the length of the original array are not written.
Do the above modifications to the input array in place, do not return anything from your function.
Example 1:
- Input: [1,0,2,3,0,4,5,0]
- Output: null
- Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
复制代码
Example 2:
- Input: [1,2,3]
- Output: null
- Explanation: After calling your function, the input array is modified to: [1,2,3]
复制代码
Note:
- 1 <= arr.length <= 10000
- 0 <= arr[i] <= 9
测试
输入
- [1,0,2,3,0,4,5,0]
- [1, 0, 0, 0]
- [1, 1, 1, 1]
- [1, 0, 0, 1]
- [1, 1, 1, 0]
- [1, 0, 0, 0, 0]
- [1, 1, 1, 1, 0]
- [1, 0, 0, 1, 0]
- [1, 1, 1, 0, 0]
- [1, 0, 0, 0, 1]
- [1, 1, 1, 1, 1]
- [1, 0, 0, 1, 1]
- [1, 1, 1, 0, 1]
复制代码
输出
- [1,0,0,2,3,0,0,4]
- [1,0,0,0]
- [1,1,1,1]
- [1,0,0,0]
- [1,1,1,0]
- [1,0,0,0,0]
- [1,1,1,1,0]
- [1,0,0,0,0]
- [1,1,1,0,0]
- [1,0,0,0,0]
- [1,1,1,1,1]
- [1,0,0,0,0]
- [1,1,1,0,0]
复制代码
进阶1
空间复杂度 O(1)。
进阶2
时间复杂度 O(n)。
https://leetcode.com/problems/duplicate-zeros |