Skip to content

Commit 0ba640f

Browse files
committed
remove ele
1 parent 878cd9f commit 0ba640f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Array/Array.Lib/RemoveElementSln.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Array.Lib
7+
{
8+
public class RemoveElementSln
9+
{
10+
public int RemoveElement(int[] nums, int val)
11+
{
12+
int i = 0; //指向不等于元素val
13+
for (int j = 0; j < nums.Length; j++)
14+
{
15+
while (j < nums.Length && nums[j] == val)
16+
j++;
17+
if (i < j && j < nums.Length)
18+
nums[i] = nums[j];
19+
if (j < nums.Length)
20+
i++;
21+
}
22+
return i;
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)