Skip to content

Commit 4f98d15

Browse files
committed
update a O(1) space and O(n) time for DisappearElementSln.cs
1 parent dc1735a commit 4f98d15

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Array/Array.Lib/DisappearElementsSln.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ public IList<int> FindDisappearedNumbers(int[] nums)
2727

2828
public IList<int> FindDisappearedNumbers2(int[] nums)
2929
{
30-
System.Array.Sort(nums);
3130
IList<int> rtn = new List<int>();
3231
for (int i = 0; i < nums.Length; i++)
3332
{
34-
int val = i + 1;
35-
36-
33+
int index = Math.Abs(nums[i]) - 1;
34+
if (nums[index] > 0)
35+
nums[index] = -nums[index];
36+
}
37+
for (int i = 0; i < nums.Length; i++)
38+
{
39+
if(nums[i]>0)
40+
rtn.Add(i+1);
3741
}
3842
return rtn;
3943
}

0 commit comments

Comments
 (0)