查找数组中不相邻的三个元素

时间:2013-12-11 21:07:38

标签: java

这是我必须回答的问题 -

  

给定一组int,如果值3在数组中出现正好3次,则返回true,并且没有3个是彼此相邻的。

haveThree({3, 1, 3, 1, 3}) → true
haveThree({3, 1, 3, 3}) → false
haveThree({3, 4, 3, 3, 4}) → false

这是我的解决方案:

public boolean haveThree(int[] nums) {
  int count = 0;
  for (int i=0;i<nums.length-1;i++) {
      if (nums[i] == 3 && nums[i+1] ==3) {
          return false;
      } 
      else
         if ((nums[i]==3 && nums[i+1]!=3)||(nums[i]==3 && nums[i+1]!=3)) {
            count ++;
         }
  }
  return count ==3;
}

某些测试失败了。例如,{3,1,3,1,3}应该返回true;然而,返回false,我无法弄清楚原因。

7 个答案:

答案 0 :(得分:4)

您需要一直循环到nums.length以计算所有出现次数。此外,不需要else语句。我会做类似的事情:

for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 3) {
        if ((i < nums.length - 1) && (nums[i + 1] == 3)) {
            return false;
        }
        count++;
    } 
}

答案 1 :(得分:2)

这个例子失败了,因为你没有检查最后一个索引,可能是为了修复越界错误,检查两个3是否相邻。第二个if语句中的条件或条件也是多余的。

public boolean haveThree(int[] nums) {
    int count = 0;
    for (int i=0;i<nums.length-1;i++) {
        if (nums[i] == 3 && nums[i+1] ==3) {
            return false;
        } 
        if ((nums[i]==3)) { //removed redundant condition and doesn't need to be an else
            count ++;
        }
    }
    // check the last index, you've already ensured the second to last is not also a 3
    if(nums[nums.length-1] == 3) {
        count++;
    }
    return count == 3;
}

答案 2 :(得分:2)

因为您没有比较最终值,所以无法判断最后一个数组元素是否为3。我要做的是(保证根据需要遍历每个元素)是添加一个标志布尔值,让你知道前一个值是否为3(如果当前值不是3,则将其重置为false)。

我的例子:

public boolean haveThree(int[] nums) {
   int count = 0;
   boolean flag = false;

   for(int i = 0; i < nums.length; i++) {
      if(nums[i] == 3) { // The current value is a 3

         if(flag) { // Previous value was a 3, rejecting.
            return false; 
         }
         else { // We have another 3, set the flag
            count++;
            flag = true; 
         }
      }
      else { // Since this wasn't a 3, we can set the flag back to false
         flag = false;
      }
   }
   return count == 3;
}

答案 3 :(得分:0)

for语句还设有another form,用于迭代集合和数组,可用于使循环更紧凑,更易于阅读。

boolean haveThree(int[] nums) {
    int count = 0, prevNum = 0;
    for (int i : nums){
        if (i==3) {
            count++;
            if (prevNum == i)
                return false;
        }
        prevNum = i;
    }
    return count == 3;
}

答案 4 :(得分:0)

正如一些人已经指出的那样,你没有计算数组中存在的所有3个。你的循环在最后一个元素之前结束,以避免ArrayIndexOutOfBoundsException

这是一个逻辑错误。对于您提到的测试用例,您的代码失败,因为if时第一个false条件返回i = 0。我练习时编写了以下代码片段。希望它有所帮助。

public boolean haveThree(int[] nums) {
  int threeCount = 0;
  boolean successive3s = false;

  for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 3) { 
      threeCount++;
    }
    if (nums[i] == 3 && (i + 1) < nums.length && nums[i + 1] == 3) 
      successive3s = true;      
  }

  return (!successive3s && threeCount == 3);
}

答案 5 :(得分:0)

public boolean haveThree(int[] nums) {
   int count = 0;

   if(nums.length >= 1 && nums[0] == 3)
        count++;

    for(int i = 1; i < nums.length; i++) {
        if(nums[i - 1] == 3 && nums[i] == 3)
            return false;

        if(nums[i] == 3)
            count++;
    }

    return count == 3;
}

答案 6 :(得分:0)

我为我做了一个拖尾计数器。

public boolean haveThree(int[] nums) 
{
  //We check to see if it is possible to get 3 without being in a row.
  //In this case, it is the smallest at five chars
  //E.G 31313, so if we have any size less than this, we know it to be false.
  if (nums.length >= 5)
  {
    //Create a counter to track how many 3's we have in a row, 
    //as well as how many we have total.
    int counterInRow = 0;
    int counterThrees = 0;
    //Check for 3's
    for (int i = 0; i < nums.length; i++)
    {
      //If a number is 3, we increment both;
      if (nums[i] == 3)
      {
        counterInRow++;
        counterThrees++;
      }
      //Otherwise, we reset the amount in a row to 0;
      else
      {
        counterInRow = 0;
      }
      //If we have 2 or more in a row, we return false.
      if (counterInRow >= 2)
      {
        return false;
      }
    }
    //Return if the amount of the counterThrees equals 3 or not.
    return (counterThrees == 3);
  }
  //If we have less than 5 characters, it isn't possible. We then,
  //Return false;
  else
  {
    return false;
  }
}