Skip to content

Commit 50f19f8

Browse files
committed
update three array questions
1 parent 3efa555 commit 50f19f8

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* ==============================================================================
2+
* 功能描述:ContainsDuplicateIISln
3+
* 创 建 者:gz
4+
* 创建日期:2017/5/3 18:25:02
5+
* ==============================================================================*/
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
11+
namespace Array.Lib
12+
{
13+
/// <summary>
14+
/// ContainsDuplicateIISln
15+
/// </summary>
16+
public class ContainsDuplicateIISln
17+
{
18+
public bool ContainsNearbyDuplicate(int[] nums, int k)
19+
{
20+
Dictionary<int, int> dict = new Dictionary<int, int>(); //元素值,索引
21+
for (int i = 0; i < nums.Length; i++)
22+
{
23+
if (dict.ContainsKey(nums[i]))
24+
{
25+
if (Math.Abs(i - dict[nums[i]]) <= k)
26+
return true;
27+
dict.Remove(nums[i]); //移调
28+
dict.Add(nums[i], i);//添加最新的
29+
continue;
30+
}
31+
dict.Add(nums[i], i);
32+
}
33+
return false;
34+
}
35+
36+
}
37+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* ==============================================================================
2+
* 功能描述:Class1
3+
* 创 建 者:gz
4+
* 创建日期:2017/5/3 18:55:55
5+
* ==============================================================================*/
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
11+
namespace Array.Lib
12+
{
13+
//Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
14+
15+
//Find all the elements of [1, n] inclusive that do not appear in this array.
16+
17+
//Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
18+
public class DisappearElementsSln
19+
{
20+
public IList<int> FindDisappearedNumbers(int[] nums)
21+
{
22+
IList<int> rtn = new List<int>();
23+
for (int i = 0; i < nums.Length; i++)
24+
rtn.Add(i + 1);
25+
return rtn.Except(nums).ToList(); //求差集应用在这个场合效率不好,为充分挖掘题目
26+
}
27+
28+
public IList<int> FindDisappearedNumbers2(int[] nums)
29+
{
30+
System.Array.Sort(nums);
31+
IList<int> rtn = new List<int>();
32+
for (int i = 0; i < nums.Length; i++)
33+
{
34+
int val = i + 1;
35+
36+
37+
}
38+
return rtn;
39+
}
40+
41+
}
42+
}

Array/Array.Lib/RotateArraySln.cs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* ==============================================================================
2+
* 功能描述:RotateArraySln
3+
* 创 建 者:gz
4+
* 创建日期:2017/5/3 16:03:03
5+
* ==============================================================================*/
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Globalization;
9+
using System.Linq;
10+
using System.Text;
11+
12+
namespace Array.Lib
13+
{
14+
/// <summary>
15+
/// RotateArraySln
16+
/// </summary>
17+
public class RotateArraySln
18+
{
19+
public void Rotate(int[] nums, int k)
20+
{
21+
int n = nums.Length;
22+
if (n == 0 || n == 1) return;
23+
if ((double)k / n > 1)
24+
k = k % n;
25+
if (k == 0) return;
26+
System.Array.Reverse(nums, 0, n - k); //index, length
27+
System.Array.Reverse(nums, n - k, k);
28+
System.Array.Reverse(nums);
29+
}
30+
31+
public void Rotate2(int[] nums, int k)
32+
{
33+
int n = nums.Length;
34+
if (n == 0 || n == 1) return;
35+
if ((double)k / n > 1)
36+
k = k % n;
37+
if (k == 0) return;
38+
Reverse(nums, 0, n - k); //index, length
39+
Reverse(nums, n - k, k);
40+
Reverse(nums, 0, n);
41+
}
42+
43+
private void Reverse(int[] nums, int index, int length)
44+
{
45+
int i = 0;
46+
int loop = length/2;
47+
while (i < loop)
48+
{
49+
swap(ref nums[index + i], ref nums[length + index - i - 1]);
50+
i++;
51+
}
52+
}
53+
54+
private void swap(ref int a, ref int b)
55+
{
56+
int tmp = a;
57+
a = b;
58+
b = tmp;
59+
}
60+
61+
}
62+
}

0 commit comments

Comments
 (0)