-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1089.复写零.cs
72 lines (71 loc) · 1.44 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* @lc app=leetcode.cn id=1089 lang=csharp
*
* [1089] 复写零
*
* https://leetcode.cn/problems/duplicate-zeros/description/
*
* algorithms
* Easy (58.15%)
* Likes: 121
* Dislikes: 0
* Total Accepted: 23.6K
* Total Submissions: 40.5K
* Testcase Example: '[1,0,2,3,0,4,5,0]'
*
* 给你一个长度固定的整数数组 arr,请你将该数组中出现的每个零都复写一遍,并将其余的元素向右平移。
*
* 注意:请不要在超过该数组长度的位置写入元素。
*
* 要求:请对输入的数组 就地 进行上述修改,不要从函数返回任何东西。
*
*
*
* 示例 1:
*
* 输入:[1,0,2,3,0,4,5,0]
* 输出:null
* 解释:调用函数后,输入的数组将被修改为:[1,0,0,2,3,0,0,4]
*
*
* 示例 2:
*
* 输入:[1,2,3]
* 输出:null
* 解释:调用函数后,输入的数组将被修改为:[1,2,3]
*
*
*
*
* 提示:
*
*
* 1 <= arr.length <= 10000
* 0 <= arr[i] <= 9
*
*
*/
// @lc code=start
public class Solution {
// 12 69 160 42.6;
public void DuplicateZeros(int[] arr) {
int size = arr.Length;
for(int i = 0; i < size-1; ++i){
if(arr[i] != 0){
continue;
}
for(int j = size-2; j > i; --j){
arr[j+1] = arr[j];
}
arr[++i] = 0;
}
}
}
// @lc code=end
/*
[1,0,2,3,0,4,5,0]\n
[1,2,3]\n
[1,2,3,0]\n
[0]\n
[0,0]
*/