在各种情况下我多次遇到这个问题.虽然我对C或Java感到满意,但它对所有编程语言都是通用的.
让我们考虑两个数组(或集合):
char[] A = {'a', 'b', 'c', 'd'};
char[] B = {'c', 'd', 'e', 'f'};
Run Code Online (Sandbox Code Playgroud)
如何将两个数组之间的公共元素作为新数组?在这种情况下,阵列A和B的交点是char[] c = {'c', 'd'}
.
我想避免在另一个数组内重复迭代一个数组,这将增加执行时间(A长度为B的长度),这对于大型数组来说太多了.
有没有什么办法可以在每个数组中单独传递以获得公共元素?
我有两个int型List
像List A
和List B
.我要检查多少项目List A
是那里List B
.我能够做到这一点,但是foreach
因为优化是我的代码中的主要目标,所以我可以避免这种有效方式.
List<int> A = new List<int>;
List<int> B = new List<int>;
// Some logic....item added in both lists. Then
foreach(var item in A)
{
if (B.Contains(item))
{
// Subtract number of duplicates
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用Intersect
和Any
,但是返回bool
所以我无法完全应用它们.
我有一系列for循环,它们在原始的字符串列表上工作,然后逐渐过滤列表,例如:
import re
# Regex to check that a cap exist in string.
pattern1 = re.compile(r'\d.*?[A-Z].*?[a-z]')
vocab = ['dog', 'lazy', 'the', 'fly'] # Imagine it's a longer list.
def check_no_caps(s):
return None if re.match(pattern1, s) else s
def check_nomorethan_five(s):
return s if len(s) <= 5 else None
def check_in_vocab_plus_x(s,x):
# s and x are both str.
return None if s not in vocab else s+x
slist = ['the', 'dog', 'jumps', 'over', 'the', 'fly']
# filter with check_no_caps
slist = [check_no_caps(s) …
Run Code Online (Sandbox Code Playgroud) 我正在寻找在列表或字符串数组中找到匹配模式的方法,特别是在.NET中,但是来自其他语言的算法或逻辑会很有帮助.
假设我有3个数组(或在此特定情况下List(Of String))
Array1
"Do"
"Re"
"Mi"
"Fa"
"So"
"La"
"Ti"
Array2
"Mi"
"Fa"
"Jim"
"Bob"
"So"
Array3
"Jim"
"Bob"
"So"
"La"
"Ti"
Run Code Online (Sandbox Code Playgroud)
我想报告一下比赛的发生情况
("Mi", "Fa") In Arrays (1,2)
("So") In Arrays (1,2,3)
("Jim", "Bob", "So") in Arrays (2,3)
("So", "La", "Ti") in Arrays (1, 3)
Run Code Online (Sandbox Code Playgroud)
......和其他任何人.
我用它来解决一个问题,而不是专门制作它的商业产品,而不是手工做(有110个约100-200项的清单).
是否有任何算法,现有代码或想法可以帮助我找到所描述的结果?
有必须是一个更好的办法来做到这一点,我敢肯定...
// Simplified code
var a = new List<int>() { 1, 2, 3, 4, 5, 6 };
var b = new List<int>() { 2, 3, 5, 7, 11 };
var z = new List<int>();
for (int i = 0; i < a.Count; i++)
if (b.Contains(a[i]))
z.Add(a[i]);
// (z) contains all of the numbers that are in BOTH (a) and (b), i.e. { 2, 3, 5 }
Run Code Online (Sandbox Code Playgroud)
我不介意使用上述技术,但我想要快速有效的东西(我需要多次比较非常大的列表<>),这似乎都不是!有什么想法吗?
编辑:因为它有所不同 - 我使用的是.NET 4.0,初始数组已经排序并且不包含重复项.
对于任何三个给定的集合A,B和C:是否有一种方法可以(以编程方式)确定是否存在A的元素,它是B和C的连接(编辑:交集)的一部分?
例如:
A:所有大于3
B的数字:所有数字小于7
C:所有数字等于5
在这种情况下,集合A中的元素是数字5,适合.我将其作为规范实现,因此这个数值范围只是一个例子.A,B,C可以是任何东西.
给定多个项目列表,找到具有匹配项目的列表.
这个问题的强力伪代码如下所示:
foreach list L
foreach item I in list L
foreach list L2 such that L2 != L
for each item I2 in L2
if I == I2
return new 3-tuple(L, L2, I) //not important for the algorithm
Run Code Online (Sandbox Code Playgroud)
我可以想到许多不同的方法 - 创建列表列表并在搜索其他候选列表后删除每个候选列表 - 但我想知道是否有更好的算法?
我正在使用Java,如果这对您的实现有所影响.
谢谢
从这个,我们知道要解决两个排序的数组的交叉方法。那么如何得到多个排序数组的交集呢?
根据两个已排序数组的答案,我们可以将其应用于多个数组。这里是代码
vector<int> intersectionVector(vector<vector<int> > vectors){
int vec_num = vectors.size();
vector<int> vec_pos(vec_num);// hold the current position for every vector
vector<int> inter_vec; // collection of intersection elements
while (true){
int max_val = INT_MIN;
for (int index = 0; index < vec_num; ++index){
// reach the end of one array, return the intersection collection
if (vec_pos[index] == vectors[index].size()){
return inter_vec;
}
max_val = max(max_val, vectors[index].at(vec_pos[index]));
}
bool bsame = true;
for (int index = 0; index < vec_num; ++index){
while …
Run Code Online (Sandbox Code Playgroud)