-
Notifications
You must be signed in to change notification settings - Fork 0
/
1089.复写零.cs
39 lines (38 loc) · 1.01 KB
/
1089.复写零.cs
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
30
31
32
33
34
35
36
37
38
39
/*
* @lc app=leetcode.cn id=1089 lang=csharp
*
* [1089] 复写零
*/
// @lc code=start
// 双指针
// 30/30 cases passed (120 ms)
// Your runtime beats 100 % of csharp submissions
// Your memory usage beats 68.42 % of csharp submissions (42.8 MB)
public class Solution
{
public void DuplicateZeros(int[] arr)
{
int len = arr.Length;
int count = 0;
bool flag = true;
for (int i = 0; i < len - count; i++)
{
if (arr[i] == 0 && i < len - count - 1) count++;
else if (arr[i] == 0 && i == len - count - 1) flag = false;
}
int idx = len - 1 - count;
int temp = idx;
for (int i = len - 1; i >= 0; i--)
{
if (arr[idx] == 0 && count > 0 && (idx != temp || idx == temp && flag))
{
arr[i] = 0;
arr[--i] = 0;
count--;
}
else arr[i] = arr[idx];
idx--;
}
}
}
// @lc code=end