-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo26_Array.cs
63 lines (59 loc) · 2.09 KB
/
No26_Array.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
using System;
using System.Collections.Generic;
using System.Text;
namespace LeetCode_26
{
//static void Main(string[] args)
//{
// var solution = new Solution();
// while (true)
// {
// //int input = int.Parse(Console.ReadLine());
// //int input2 = int.Parse(Console.ReadLine());
// //int input3 = int.Parse(Console.ReadLine());
// string input = Console.ReadLine();
// //string input2 = Console.ReadLine();
// int[] intArr = input.Split(',').Select(s => int.Parse(s)).ToArray();
// //int input = int.Parse(input2);
// var res = solution.RemoveDuplicates(intArr);
// Console.WriteLine(res);
// }
//}
/// <summary>
/// Experience: C#的内存消耗和运行时间也太不稳定了吧,我去看了运行时间第一名的答案,没发现有多大不同。
/// 然后直接粘贴提交,结果,您猜这么着,还比我原来的还慢,嗨,您说说。
/// </summary>
public class Solution
{
/// <summary>
/// 第一反应解,就单纯的循环交换(原来官方还有个名字叫做快慢指针)
/// 时间复杂度:O(n)
/// 空间复杂度:O(1)
/// </summary>
/// <param name="nums"></param>
/// <returns></returns>
public int RemoveDuplicates(int[] nums)
{
if (nums.Length == 0)
return 0;
int cur_index = 0;
for (int i = 1; i < nums.Length; i++)
{
if (nums[i] > nums[cur_index])
{
cur_index++;
//Swap(nums, i, cur_index);
nums[cur_index] = nums[i];//他不要求保留数组,直接覆盖更简单一些
}
}
return cur_index + 1;
}
//private void Swap(int[] nums, int source, int target)
//{
// int temp;
// temp = nums[source];
// nums[source] = nums[target];
// nums[target] = temp;
//}
}
}