-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
29 lines (25 loc) · 814 Bytes
/
Solution.cs
File metadata and controls
29 lines (25 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace LeetCode.Problem1089{
//1089. Duplicate Zeros
//https://leetcode.com/problems/duplicate-zeros/
/*
Given a fixed-length integer array arr, 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 and do not return anything.
*/
public class Solution {
public void DuplicateZeros(int[] arr) {
Queue<int> values = new Queue<int>(arr);
int[] anss = new int [arr.Length];
for (int i = 0; i < anss.Length; i++)
{
anss[i] = values.Dequeue();
if (anss[i] == 0 && i < anss.Length-1)
{
anss[i + 1] = 0;
i++;
}
}
Array.Copy(anss,arr,arr.Length);
}
}
}