为了账号安全,请及时绑定邮箱和手机立即绑定

给定一个整数数组,如果数字序列 1、2、3 出现在数组中的某处,则返回 true

给定一个整数数组,如果数字序列 1、2、3 出现在数组中的某处,则返回 true

慕尼黑5688855 2023-08-23 17:20:07
基本上我试图返回 true,如果数组中按特定顺序排列 1、2、3,我无法弄清楚如何做到这一点。我已经尝试过使用一些 for 循环和 if 语句,但我不知道这实际上是否是最好的方法public static boolean arrayOneTwoThree(int[] nums) {    for(int i = 0; i < nums.length - 2; i++) {       if(nums[i] == 1 && nums[i + 1] == 2 && nums[i + i] == 3){           return true;       }   }   return false;}仅当 1、2、3 位于数组中时才返回 true 我希望仅当 1、2、3 处于特定顺序时才返回 true在此处输入图像描述
查看完整描述

3 回答

?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

public boolean arrayOneTwoThree(int[] nums) {


    // variables declaration.

    boolean result = false;

    int counter = 0,index = 0;

    int arr[] = {1,2,3};


     // base condition check. like array length should not be less than 3.

    if(nums.length < 3){

         return result;

    }


    //for loop for iterating over array and finding desired pattern

    for(int i = 0; i < nums.length; i++){


        //pattern found in array

        if(counter == 3){

          result = true;

          return result;

        }


        if(nums[i] == arr[counter]){

             index++; 

             counter++;

        }

        else if(counter != 0){

             i = index-1;

             counter = 0;

        }

    }     

    if (counter == 3) {

        result = true;

        return result;

    }  

    return result;

}

该解决方案的复杂度为 O(n)。


查看完整回答
反对 回复 2023-08-23
?
GCT1015

TA贡献1827条经验 获得超4个赞

您当前的代码有很多问题。

  • true针对每个案件都返回。

  • 您创建了一个result变量,但从未对其执行任何操作。

  • 当您只能使用一个循环时,您就创建了三个循环。

  • 您实际上从未检查过这些值是否彼此相邻。

对于第一个问题,return true 只有当它们1, 2, 3彼此相邻时我们才会这样做,return false;对于其他所有情况也是如此。这是通过使用return false;after 循环来完成的。

对于下一个问题,result不需要,你实际上不需要计算任何东西,所以删除它。

对于第三个问题,将所有循环合并为一个循环。但是,我们需要循环到 的条件nums.length - 2而不是 ,length因为我们将同时比较 3 个值,并且我们不想得到ArrayOutOfBoundsException

最后,要检查所有值是否彼此相邻,只需将当前数组索引处的值、下一个索引值以及索引上的两个值分别与 1、2 和 3 进行比较即可。

这看起来像if (nums[i] == 1 && nums[i + 1] == 2 && nums[i + 2] == 3)。如果是这种情况true,我们会return true立即。

以下是经过所有这些修复后代码的外观:

public static void main(String[] args) {

    // test cases

    int [] arr = {1, 1 ,2, 1, 2, 3};

    System.out.println(arrayOneTwoThree(arr));


    int [] arr2 = {3, 2, 3};

    System.out.println(arrayOneTwoThree(arr2));

}


public static boolean arrayOneTwoThree(int[] nums) {

    for(int i = 0; i < nums.length - 2; i++) {

       if(nums[i] == 1 && nums[i + 1] == 2 && nums[i + 2] == 3){

           return true;

       }

   }

   return false;

}

测试运行:

真的

错误的

注意:如果您需要在上下文中使用它,请static从 中 删除,我曾经在我的.arrayOneTwoThree(int [] nums)non-staticstaticmain


查看完整回答
反对 回复 2023-08-23
?
慕森王

TA贡献1777条经验 获得超3个赞

请尝试这个解决方案,您也许能够解决所有测试用例


public static boolean find123(List<Integer> numbers) {


boolean isOneExist=false;

boolean isTwoExist=false;

boolean isThreeExist=false;

                        for(Integer n1:numbers)

            {

                    if(n1==1)

                {

                        isOneExist=true;

                }

                if(n1==2 && isOneExist)

                {

                    isTwoExist=true;

                }

                if(n1==3 && isTwoExist)

                {

                    isThreeExist=true;

                }

            

            }

            

            if(isOneExist && isTwoExist && isThreeExist)

            {

            return true;

            }

            

        return false;

    }


查看完整回答
反对 回复 2023-08-23
  • 3 回答
  • 0 关注
  • 169 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信