来接分,顺便留下你的算法【求任意长度的两个正整数数组中 重复数字的个数】
假设有两个整数数组A、B,已知这两个数组长度任意,并且每个数组中的数字都是惟一的。
要求用较快的算法求解A和B中重复的数字个数。
[解决办法]
先排序,再比较.
[解决办法]
帮顶一下
[解决办法]
var n1 = new[] { 1,2,3,4,5,6,7,8,9,10 };
var n2 = new[] { 0,2,4,6,8 };
var count=0;
foreach (var i in n1.Intersect(n2))
count++;
Console.Write(count);
Console.ReadLine();
[解决办法]
用一个 HashSet 装入数组 A 的值,然后再循环 B,判断 B 中的数字是否在 HashSet 中,若在,则加入重复数字的列表,这是否会快一些呢?
[解决办法]
看看我写得对不对int[] Array1={1,2,3,4,5,6}int[] Array2={11,2,3,4,5,6,7,8}int len1=Array1.lengthint len2=Array2.lengthlist<int> a=new list<int>for (int i=0,i<len1,i++){ a.add(Array1(i))} for (int j=0,j<len2,j++) { if (!a.Contains(Array2(j))) { a.add(Array2(j)) } }int len3=a.lengthreturn len1+len2-len3
[解决办法]
仔细看下啊。
[解决办法]
考虑七楼的建议,另外如果数组元素范围不是太大的话也可以直接用数组
开一个和元素范围一样大的数组(用位数组就可以),先遍历一个数组,在元素出现的位置标为1,再遍历第二个数组,如果元素相应的位置是1则说明重复
[解决办法]
int[m] m1= new int{a1,a2,...,am};int[n] m2= new int{b1,b2,...,bn};int iEqualCount = 0 ;for(int i =0;i<m;i++){int iValue = m1[i];for(int j=0;j<n;j++){if(ivalue==m2[j])iEqualCount ++;}}
[解决办法]
把二个数组合成一个数组,然后排序,然后比较A[i]和A[i+1]是否相同,这样就行了.
[解决办法]
JF
[解决办法]
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace DemoInrtArray
{
class Program
{
static void Main(string[] args)
{
int count = 0;
int[] aArray = new int[] { 1, 2, 3, 45, 6, 7, 890 };
int[] bArray = new int[] { 10, 20, 3, 6, 890, 100, 30, 1 };
int lenA = aArray.Length;
int lenB = bArray.Length;
List <int> cpArray = new List <int>();
if (lenA > lenB)
{
foreach (int a in aArray)
{
cpArray.Add(a);
}
foreach (int b in bArray)
{
if (cpArray.Contains(b))
{
count++;
cpArray.Remove(b);
}
}
}
else
{
foreach (int b in bArray)
{
cpArray.Add(b);
}
foreach (int a in aArray)
{
if (cpArray.Contains(a))
{
count++;
cpArray.Remove(a);
}
}
}
Console.Write(count);
Console.ReadLine();
}
}
}
using System;using System.Collections.Generic;using System.Text;using System.Collections;namespace DemoInrtArray{ class Program { static void Main(string[] args) { int count = 0; int[] aArray = new int[] { 1, 2, 3, 45, 6, 7, 890 }; int[] bArray = new int[] { 10, 20, 3, 6, 890, 100, 30, 1 }; int maxLength = aArray.Length + bArray.Length; int[] cArray = new int[maxLength]; aArray.CopyTo(cArray, 0); bArray.CopyTo(cArray, aArray.Length); int temp = 0; for (int i = 0; i < cArray.Length - 1; i++) { for (int j = 0; j < cArray.Length - i - 1; j++) { if (cArray[j] > cArray[j + 1]) { temp = cArray[j]; cArray[j] = cArray[j + 1]; cArray[j + 1] = temp; } } } for (int i = 0; i < cArray.Length; i++) { if (cArray[i] == cArray[i + 1]) { count++; i++; } } Console.Write(count); Console.ReadLine(); } }}
[解决办法]
是不是可以使用类似STL的泛型算法来解决。
假设 roster1 和 roster2 是两个存放名字的 list 对象,可使用 find_first_of 统计有多少个名字同时出现在这两个列表中:
// program for illustration purposes only:
// there are much faster ways to solve this problem
size_t cnt = 0;
list<string>::iterator it = roster1.begin();
// look in roster1 for any name also in roster2
while ((it = find_first_of(it, roster1.end(),
roster2.begin(), roster2.end()))
!= roster1.end()) {
++cnt;
// we got a match, increment it to look in the rest of roster1
++it;
}
cout << "Found " << cnt
<< " names on both rosters" << endl;
[解决办法]
up
[解决办法]
算法真厉害
[解决办法]
额 直接想的两个循环....不知道那个快
[解决办法]
[解决办法]
hash表应该快
[解决办法]
Intersect(),Contains(),这些都是方法,不是算法,你们把工作全扔给了引擎。
14楼的想法也是无视的数组长度和排序所耗资源。
16楼也有删除重复记录,缩小数组,减少循环的思路,而且还争对AB数组大小来做不同的循环。美中不除还是用到了list,如果A有500万条,B有500万条,恭喜你,获得了一个1000万条的list。
[解决办法]
有 code=C/C++ 的不??
关注。
[解决办法]
djqualls
[解决办法]
up
[解决办法]
用1位来表示一个数,当该数在数组中时该位位置1,否则为0.
如果是两层循环的话,代价太高.
[解决办法]
//建立二叉树 public class Node { public int data; public Node left; public Node right; public Node() { data = -1; } //添加Node public void AddNode(int value) { if (data == -1) { data = value; return; } if (value < data) { if (left == null) { left = new Node(); } left.AddNode(value); } else { if (right == null) { right = new Node(); } right.AddNode(value); } } //在二叉数里查询Node是否存在 public bool SelectNode(int value) { //总共查询次数 Form1.totalCount++; if (value == data) { return true; } else { if (value < data) { if (left == null) { return false; } return left.SelectNode(value); } else { if (right == null) { return false; } return right.SelectNode(value); } } } } // 程序主体 ------------ private void button2_Click(object sender, EventArgs e) { int[] a = { 9, 3, 2, 1, 7, 5, 8, 4, 6 }; int[] b = { 6, 3, 0, 11 ,1,13,2}; Node n = new Node(); for (int i = 0; i < a.Length; i++) { n.AddNode(a[i]); } int count = 0; for (int j = 0; j < b.Length; j++) { if (n.SelectNode(b[j])) { count++; } } //显示相同个数 MessageBox.Show(count.ToString()); }
[解决办法]
jf
[解决办法]
二分查找的min会逐渐往后移
复杂度应该也是 Nlog(N)
所以此算法的复杂度为 Nlog(N)
[解决办法]
先排序,后删除重复的个数,最后比较,折半查找
[解决办法]
合并数组,然后像画点一样, 填另外一个同样大小的空数组。
为空的个数就是重复的个数
[解决办法]
假设有两个整数数组A、B,已知这两个数组长度任意,并且每个数组中的数字都是惟一的。
要求用较快的算法求解A和B中重复的数字个数。
每组每个数字都是唯一的。