如何在整数数组中查找整数的重复序列?
00将重复,123123也将重复,但01234593623将不会重复。
我对如何执行此操作有一个想法,但是我的想法很模糊,因此我的实现并没有走多远。
我的主意是
在Java中,我到此为止:
String[] p1 = new String[nDigitGroup];
String[] p2 = new String[nDigitGroup];
for (int pos = 0; pos < number.length - 1; pos++)
{
System.out.println("HERE: " + pos + (nDigitGroup - 1));
int arrayCounter = -1;
for (int n = pos; n < pos + nDigitGroup ; n++)
{
System.out.printf("\nPOS: %d\nN: %d\n", pos, n);
arrayCounter++;
p1[arrayCounter] = number[n];
System.out.println(p1[arrayCounter]);
}
pos += nDigitGroup;
arrayCounter = -1;
System.out.println("SWITCHING");
for (int n = pos; n < pos + nDigitGroup ; n++)
{
System.out.printf("\nPOS: %d\nN: %d\n", pos, n);
arrayCounter++;
p2[arrayCounter] = number[n];
System.out.println(p2[arrayCounter]);
}
if (p1[0].equals(p2[0]) && p1[1].equals(p2[1])) System.out.println("MATCHING");
}
使用以下参数运行时:
repeatingSeqOf(2, new String[] {"1", "2", "3", "4", "5", "6", "7", "7" });
我正确地填充了节数组,但是它在索引超出范围时中断。
@MiljenMikic的答案很好,尤其是因为语法实际上不是常规的。:D
如果您想一般地在一个数组上进行操作,或者想了解它,那么可以做到正则表达式几乎完全可以做到:
public static void main(String[] args) {
int[] arr = {0, 1, 2, 3, 2, 3}; // 2, 3 repeats at position 2.
// for every position in the array:
for (int startPos = 0; startPos < arr.length; startPos++) {
// check if there is a repeating sequence here:
// check every sequence length which is lower or equal to half the
// remaining array length: (this is important, otherwise we'll go out of bounds)
for (int sequenceLength = 1; sequenceLength <= (arr.length - startPos) / 2; sequenceLength++) {
// check if the sequences of length sequenceLength which start
// at startPos and (startPos + sequenceLength (the one
// immediately following it)) are equal:
boolean sequencesAreEqual = true;
for (int i = 0; i < sequenceLength; i++) {
if (arr[startPos + i] != arr[startPos + sequenceLength + i]) {
sequencesAreEqual = false;
break;
}
}
if (sequencesAreEqual) {
System.out.println("Found repeating sequence at pos " + startPos);
}
}
}
}
问题内容: 我有一个字母数字字符串,我想检查其中是否有整数重复的模式。而且它们应该是连续的。 例 12341234q我们 应该告诉我重复 1234 。 1234qwe1234 应该 不 告诉我, 1234 ,因为它不是连续重复。 12121212 应该被视为重复 12 ,因为这是第一个重复的集合。但是,如果有一种算法可以找到 1212 作为 12 之前的重复集,那么我想它必须在 1212上 再次执
问题内容: 查找整数数组中的第一个重复元素。 例如: 问题答案: 简单的解决方案是使用两个循环。外循环将遍历循环,内循环将检查元素是否重复,但此解决方案的时间复杂度为 o(n^2)。 另一种解决方案是创建另一个数组并对其进行排序。从原始数组中选取元素并使用二进制搜索在排序数组中查找元素,但此解决方案的时间复杂度为 o(n^logn)。 我们能做得更好吗? 是的,我们可以从右到左迭代并使用HashS
我试图创建一个优先级队列,根据第一个元素的值对整数数组进行排序,但我遇到了一个问题,编译器抱怨在我的编译器lambda表达式中需要一个数组。知道我搞砸了什么吗?
我想找出一个整数部分的一个平方根的数字在python与pylab扩展然而,long(sqrt(n))不适用于大整数。有没有什么方法可以非常快地找到一个非常大的数的平方根的整数部分?我是新的Python和编程。我所知道的是当循环和如果语句。谢谢你们
问题内容: 我有一个列表列表,每个列表都有一个重复序列。我正在尝试计算列表中重复整数序列的长度: 哪个会返回: 任何建议或技巧都将受到欢迎。我现在正在尝试使用re.compile来解决这个问题,但是它不太正确。 问题答案: 通过迭代2到一半序列长度之间的猜测来猜测序列长度。如果未发现任何模式,则默认返回1。 得到(如预期): 根据要求,此替代方法可提供最长的重复序列。因此,它将为list_b返回4
问题内容: 改写: 在我的项目中,我有图像。每个图像有5个标签,范围为[1,10]。我用Elasticsearch上传了这些标签: 我将这些文件加载到类型为“ img”的索引“ my_project”中的elasticsearch中: 我上传的其他示例文件: 在我的应用程序中,向量要长得多,但是具有固定数量的唯一元素。我喜欢这些文件中的20M。 现在,我想找到给定向量的相似文档。向量具有更多公
在SDA中输入std代码时,我输入了带有零的std代码,但为什么零不在显示数组时的值中?
请帮忙!!