[英]Finding Consecutive Matching Elements in Arrays
我對此問題有輕微的疑問。 所有答案都將檢查所有呼叫,但不包括一個。
當它為true時, contains({1, 2, 1, 2, 3}, {1, 2, 3})
返回false。
提示:
編寫一個名為contains的靜態方法,該方法接受兩個整數a1
和a2
數組作為參數,並返回一個布爾值,該值指示a2
的元素序列是否出現在a1
(“是”為true,“否”為false)。 a2
的元素序列可以出現在a1
任何位置,但必須連續且以相同的順序出現。 例如,如果名為list1
和list2
變量存儲以下值:
int[] list1 = {1, 6, 2, 1, 4, 1, 2, 1, 8};
int[] list2 = {1, 2, 1};
然后,對contains(list1,list2)的調用應返回true,因為list2的值序列{1, 2, 1}
1、2、1 {1, 2, 1}
包含在list1中,從索引5開始。如果list2已存儲值{2, 1, 2}
2、1、2 {2, 1, 2}
,則該調用contains(list1,list2)的值將返回false,因為list1不包含該值序列。 任何兩個具有相同元素的列表都被視為包含彼此,因此諸如contains(list1,list1)之類的調用應返回true。
您可能會假定傳遞給您的方法的兩個數組的長度都至少為1。您可能無法使用任何String來幫助解決此問題,也不會使用產生String的方法(例如Arrays.toString)。
碼:
public static boolean contains(int[] first, int[] second) {
int secondCount = 0; // Indicates where to start on second array
if (first.length < second.length) { // If second array is longer than first
return false;
}
for (int i=0; i<first.length; i++) { // goes through each element in first array
if (first[i] == second[secondCount]) { // If elements match
secondCount++; // increment by one and move onto next elem on second
if (secondCount == second.length) { // If complete match
return true;
}
} else { // resets count
secondCount = 0;
}
}
return false;
}
由於您構造循環的方式,您的方法返回false
。 下面,我概述了方法的運行時。
i = 0; secondCount = 0;
{1, 2, 1, 2, 3} {1, 2, 3}
^ ^
1 == 1 ? true
i = 1; secondCount = 1;
{1, 2, 1, 2, 3} {1, 2, 3}
^ ^
2 == 2 ? true
i = 2; secondCount = 2;
{1, 2, 1, 2, 3} {1, 2, 3}
^ ^
1 == 3 ? false
i = 3; secondCount = 0;
{1, 2, 1, 2, 3} {1, 2, 3}
^ ^
2 == 1 ? false
i = 3; secondCount = 0;
{1, 2, 1, 2, 3} {1, 2, 3}
^ ^
3 == 1 ? false
return false;
問題是,如果布爾方程first[i] == second[secondCount]
為假,則在i
仍為Advanced時將重置secondCount
。 因此, contains({1, 2, 1, 2, 3}, {1, 2, 3});
將始終返回false
。
每當first[i] == second[secondCount]
阻止i
前進時,請考慮在代碼中添加一個continue
。
當將secondCount
從非零重置為零時, i
也會減一。
工作代碼->
public static boolean contains(int[] first, int[] second) {
int secondCount = 0; // Indicates where to start on second array
if (first.length < second.length) { // If second array is longer than first
return false;
}
for (int i=0; i<first.length; i++) { // goes through each element in first array
if (first[i] == second[secondCount]) { // If elements match
secondCount++; // increment by one and move onto next elem on second
if (secondCount == second.length) { // If complete match
return true;
}
} else { // resets count
secondCount = 0;
i--; //ADD THIS
}
}
return false;
}
(使用您的list1 = {1,2,1,2,3}和list2 = {1,2,3}的示例)'i'必須遞減,因為初始時list1 [0]和list1 [1]相等分別與list2 [0]和list2 [1]進行比較,並且將list1 [2]與list2 [2]進行比較,這是錯誤的,您只遞減第二個計數,然后執行i ++,因此,再次啟動時,您將比較list1 [ 3]的list2 [0]為假,但是您應該比較list1 [2]而不是list1 [3]。
我已經看到很多類似於我的代碼,但是我認為這是一個更好的解決方案!
public static void main(String[] args){
int[] list1 = {1, 6, 2, 1, 4, 1, 2, 1, 8};
int[] list2 = {1, 2, 1};
boolean presence = contains(list1, list2);
System.out.println(presence);
}
public static boolean contains(int[] list1, int[] list2){
if (list1.length < list2.length){
return false;
}
int count = 0;
int counter = 0;
for (int i = 0; i < list1.length; i++){
if (list1[i] == list2[counter]){
count++;
counter++;
if (count == list2.length){
return true;
}
}else{
counter = 0;
}
}
return false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.