Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在数组中查找不相邻的三个元素_Java - Fatal编程技术网

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

Java 在数组中查找不相邻的三个元素,java,Java,这是我必须回答的问题- 给定一个整数数组,如果值3在数组中正好出现3次,并且没有3个相邻,则返回true 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.leng

这是我必须回答的问题-

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

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;
}
public boolean haveThree(int[]nums){
整数计数=0;

对于(int i=0;i,该示例失败,因为您没有检查最后一个索引,可能是为了修复检查两个3是否相邻的越界错误。此外,第二个if语句中的or条件是冗余的

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;
}
public boolean haveThree(int[]nums){
整数计数=0;

对于(int i=0;i您需要一直循环到
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++;
    } 
}
for(int i=0;i
因为您没有比较最终值,所以无法判断最后一个数组元素是否为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;
}
public boolean haveThree(int[]nums){
整数计数=0;
布尔标志=假;
对于(int i=0;i
for
语句还设计用于通过集合和数组进行迭代,并可用于使循环更加紧凑和易于阅读

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

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

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

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);
}
public boolean haveThree(int[]nums){
int-threeCount=0;
布尔值successive3s=false;
对于(int i=0;i
公共布尔值三(int[]nums){
整数计数=0;
如果(nums.length>=1&&nums[0]==3)
计数++;
对于(int i=1;i
我为我的做了一个跟踪计数器

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;
  }
}
public boolean haveThree(int[]nums)
{
//我们检查是否有可能在不连续的情况下得到3。
//在这种情况下,它是最小的五个字符
//所以如果我们有任何尺寸小于这个,我们就知道它是假的。
如果(nums.length>=5)
{
//创建一个计数器来跟踪一行中有多少个3,
//以及我们总共有多少。
int counterInRow=0;
int counterThrees=0;
//检查是否有3个
对于(int i=0;i=2)
{
返回false;
}
}
//如果计数器三的数量等于或不等于3,则返回。
返回(对三==3);
}
//如果我们少于5个字符,这是不可能的。然后,
//返回false;
其他的
{
返回false;
}
}

您是否调试了该程序以查看您得到的
计数值?否-我使用的是编码bat IDE
if
条件的意义是什么?为什么需要else if语句?只需比较nums[I]和nums[I+1],如果它们都不是3,则再次递增计数并循环。如果有2个3并排,则自动失败。好的,我了解了最后一个索引,这很有意义,谢谢。@JerryMurphy当然!请注意,我更改了
if
语句,您不需要像以前那样多的条件,而且使用
el也没有意义se if
。谢谢这是一个帮助我更好地理解我错在哪里的答案,希望@Keppil在他的答案更优雅时不要太沮丧!@JerryMurphy:当然不会。:)你应该总是选择你觉得对你帮助最大的答案。谢谢keppil你的解决方案更简洁,但我花了几分钟才弄明白,仍然是个谜。谢谢你的帮助这个旗帜生意让我的大脑受伤,我要花一点时间弄明白。谢谢你的投入。事实上,现在我明白了,从来没有想过像那样处理它。干杯。没问题!是的,这只是处理问题的另一种方式;它不是展望下一个值,而是多少记得我们看到的上一个值。
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;
  }
}