猿问

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

基本上我试图返回 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在此处输入图像描述


慕尼黑5688855
浏览 255回答 3
3回答

蝴蝶不菲

public boolean arrayOneTwoThree(int[] nums) {&nbsp; &nbsp; // variables declaration.&nbsp; &nbsp; boolean result = false;&nbsp; &nbsp; int counter = 0,index = 0;&nbsp; &nbsp; int arr[] = {1,2,3};&nbsp; &nbsp; &nbsp;// base condition check. like array length should not be less than 3.&nbsp; &nbsp; if(nums.length < 3){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return result;&nbsp; &nbsp; }&nbsp; &nbsp; //for loop for iterating over array and finding desired pattern&nbsp; &nbsp; for(int i = 0; i < nums.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; //pattern found in array&nbsp; &nbsp; &nbsp; &nbsp; if(counter == 3){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(nums[i] == arr[counter]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;index++;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;counter++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(counter != 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i = index-1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;counter = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; if (counter == 3) {&nbsp; &nbsp; &nbsp; &nbsp; result = true;&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; return result;}该解决方案的复杂度为 O(n)。

GCT1015

您当前的代码有很多问题。您true针对每个案件都返回。您创建了一个result变量,但从未对其执行任何操作。当您只能使用一个循环时,您就创建了三个循环。您实际上从未检查过这些值是否彼此相邻。对于第一个问题,return true&nbsp;只有当它们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) {&nbsp; &nbsp; // test cases&nbsp; &nbsp; int [] arr = {1, 1 ,2, 1, 2, 3};&nbsp; &nbsp; System.out.println(arrayOneTwoThree(arr));&nbsp; &nbsp; int [] arr2 = {3, 2, 3};&nbsp; &nbsp; System.out.println(arrayOneTwoThree(arr2));}public static boolean arrayOneTwoThree(int[] nums) {&nbsp; &nbsp; for(int i = 0; i < nums.length - 2; i++) {&nbsp; &nbsp; &nbsp; &nbsp;if(nums[i] == 1 && nums[i + 1] == 2 && nums[i + 2] == 3){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return true;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;return false;}测试运行:真的错误的注意:如果您需要在上下文中使用它,请static从 中&nbsp;删除,我曾经在我的.arrayOneTwoThree(int [] nums)non-staticstaticmain

慕森王

请尝试这个解决方案,您也许能够解决所有测试用例public static boolean find123(List<Integer> numbers) {boolean isOneExist=false;boolean isTwoExist=false;boolean isThreeExist=false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(Integer n1:numbers)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(n1==1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isOneExist=true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(n1==2 && isOneExist)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isTwoExist=true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(n1==3 && isTwoExist)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isThreeExist=true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isOneExist && isTwoExist && isThreeExist)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答