【问题标题】:Finding Consecutive Matching Elements in Arrays在数组中查找连续匹配的元素
【发布时间】:2014-06-02 07:47:29
【问题描述】:

我对这个问题有一点小问题。所有电话的所有答案都检查了,除了一个,我没有得到。

contains({1, 2, 1, 2, 3}, {1, 2, 3}) 在应该为真时返回假。

提示:

编写一个名为 contains 的静态方法,它接受两个整数数组 a1a2 作为参数,并返回一个布尔值,指示 a2 的元素序列是否出现在 a1 中(对于是的,否则为假)。 a2 中的元素序列可以出现在a1 中的任何位置,但必须以相同的顺序连续出现。例如,如果名为 list1list2 的变量存储以下值:

int[] list1 = {1, 6, 2, 1, 4, 1, 2, 1, 8};
int[] list2 = {1, 2, 1};

那么 contains(list1, list2) 的调用应该返回 true,因为 list2 的值序列 {1, 2, 1} 包含在 list1 中,从索引 5 开始。如果 list2 存储了值 {2, 1, 2},则调用 contains(list1, list2) 将返回 false,因为 list1 不包含该值序列。任何两个具有相同元素的列表都被认为是相互包含的,因此诸如 contains(list1, list1) 之类的调用应该返回 true。

您可以假设传递给您的方法的两个数组的长度都至少为 1。您不能使用任何字符串来帮助您解决这个问题,也不能使用产生字符串的方法,例如 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;
}

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    由于您构建循环的方式,您的方法返回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] 为假,那么secondCount 会被重置,而i 仍然是高级的。因此,contains({1, 2, 1, 2, 3}, {1, 2, 3}); 将始终返回 false

    first[i] == second[secondCount] 无法阻止i 前进时,请考虑将continue 添加到您的代码中。

    【讨论】:

      【解决方案2】:

      当将secondCount 从非零重置为零时,i 也会减一。

      【讨论】:

        【解决方案3】:

        工作代码-->

        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]。

        【讨论】:

          【解决方案4】:

          我见过很多类似我的代码,但我认为这是一个更好的解决方案!

          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;
          }
          

          【讨论】:

          • 如果你每次不匹配时都重置counter,而不是count,我认为你会得到误报。另外,因为你不回溯,你会错过一些匹配,例如输入是: { 1 6 2 1 2 1 2 1 3 5 } { 1 2 1 3 }
          猜你喜欢
          • 2021-03-18
          • 2017-10-07
          • 2020-02-19
          • 1970-01-01
          • 2011-11-13
          • 1970-01-01
          • 2018-12-29
          • 2015-05-23
          • 1970-01-01
          相关资源
          最近更新 更多