111,092
社区成员




static void Main(string[] args)
{
int[] a = new int[] { 1, 5, 8, 4, 2, 9 };
int[] b = new int[] { 2, 4, 6, 7, 8 };
List<int> c = new List<int>(a);
List<int> d = new List<int>(b);
foreach (int n in b)//O(a.Length)
{
c.Remove(n);
}
foreach (int n in a)//O(b.Length)
{
d.Remove(n);
}
//至此c表示a中有,b中没有的
//至此d表示b中有,a中没有的
c.AddRange(d);//功能至此结束下面是显示
//排序一下好看点
c.Sort();
c.ToList().ForEach(c1 => Console.Write(c1+" "));
Console.ReadKey();
}
static void Main(string[] args)
{
int[] a = new int[] { 1, 5, 8, 4, 2, 9 };
int[] b = new int[] { 2, 4, 6, 7, 8 };
List<int> c = new List<int>(a);
List<int> d = new List<int>(b);
foreach (int n in b)//O(a.Length)
{
c.Remove(n);
}
foreach (int n in a)//O(b.Length)
{
d.Remove(n);
}
c.AddRange(d);//功能至此结束下面是显示
//排序一下好看点
c.Sort();
c.ToList().ForEach(c1 => Console.Write(c1+" "));
Console.ReadKey();
}
for (int i = 0; i < LENGTH; i++)
{
if(p>=LENGTH)
break;
while (p<LENGTH)
.....
static void Main(string[] args)
{
const int MAX = 10000;
const int LENGTH = 1000000;
int[] array1 = new int[LENGTH];
int[] array2 = new int[LENGTH];
//Fill
Random rd = new Random();
for (int t = 0; t < LENGTH; t++)
{
array1[t] = rd.Next(MAX);
array2[t] = rd.Next(MAX);
}
//Begin proccessing
//Sort
Quicksort(ref array1, 0, LENGTH - 1);
Quicksort(ref array2, 0, LENGTH - 1);
IList<int> result = new List<int>();
int p = 0;
for (int i = 0; i < LENGTH; i++)
{
while (p<LENGTH)
{
if (array2[i] < array1[p])
{
result.Add(array2[i]);
break;
}
else if (array2[i] > array1[p])
{
p++;
}
else
{
p++;
break;
}
}
}
}
private static void Quicksort(ref int[] array, int left, int right)
{
if (left < right)
{
int middle = array[(left + right) / 2];
int i = left - 1;
int j = right + 1;
while (true)
{
while (array[++i] < middle) ;
while (array[--j] > middle) ;
if (i >= j)
break;
Swap(ref array, i, j);
}
Quicksort(ref array, left, i - 1);
Quicksort(ref array, j + 1, right);
}
}
Int32[] a = new Int32[] { 1, 5, 8, 4, 2, 9 };
Int32[] b = new Int32[] { 2, 4, 6, 7, 8 };
Dictionary<Int32, String> d = new Dictionary<Int32, String>();
for (Int32 i = 0; i < a.Length; i++)
{
d.Add(a[i], "a");
}
for (Int32 j = 0; j < b.Length; j++)
{
if (d.ContainsKey(b[j]))
{
d[b[j]] = "c";
}
else
{
d.Add(b[j], "b");
}
}
//value为a则是数组A有,数组B没有;为b则是数组B有,数组A没有;为c则是都存在的,即交集
foreach (KeyValuePair<Int32, String> kvp in d)
{
if(kvp.Value == "a")
Console.WriteLine("{0} is just in Array A", kvp.Key);
if (kvp.Value == "b")
Console.WriteLine("{0} is just in Array B", kvp.Key);
}
string[] strOne = { "1", "2", "3", "4", "5" };
string[] strTwo = { "1", "3", "5", "7", "9" };
List<string> l = strOne.Except<string>(strTwo).ToList<string>();
foreach(string s in l)
{
Console.WriteLine(s.ToString());
}
//结果:
//2
//4