用Java编写两个关于codingBat的解释

时间:2014-06-01 04:38:18

标签: java

问题是关于在Java中解码来自codingBat的this问题。

问题陈述:

给定一个int数组,如果数组中出现的每个2都在另一个2的旁边,则返回true。

twoTwo({4,2,2,3})→true

twoTwo({2,2,4})→true

twoTwo({2,2,4,2})→false

首先通过问题陈述,数组中出现的每2个旁边是另一个2。然后

  

您认为上面显示的第一个输入的结果如上所示   应该是真的吗?

twoTwo({4,2,2,3})→true

因为正如我所看到的那样,数组中出现的前2个本身就是4而不是2

我感到困惑还是错误地提出了问题?我不得不抓住问题,以某种方式获得正确的代码来解决问题,如下所示,但这似乎是一个热门话题:< / p>

public boolean twoTwo(int[] nums) {
  if(nums.length==0)
  {
    return true;
   }
   if(nums.length==1)
  {
    return !(nums[0]==2);
   }
   if((nums.length==2))
   {
     if((nums[1]==2)&&(nums[0]==2))
       return true;
       else
     return false;
    }
   for(int i=0;i+2<nums.length;i++)
   {
     if((nums[i]!=2)&&(nums[i+1]==2)&&(nums[i+2]!=2))
      return false;
   }
  if((nums[nums.length-2]!=2)&&(nums[nums.length-1]==2))
    return false;

return true;     
}

欢迎任何有效的替代解决方案。

谢谢!

17 个答案:

答案 0 :(得分:3)

这是我怎么做的。我认为它更容易理解:

public boolean twoTwo(int[] nums)
{
   for (int i=0; i<nums.length; i++)
   {
      if (nums[i] != 2)
         continue;
      if (i >= 1 && nums[i-1] == 2)
         continue;
      if (i < (nums.length-1) && nums[i+1] == 2)
         continue;
      return false;
   }
   return true;     
}

答案 1 :(得分:2)

public boolean twoTwo(int[] nums) {

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

    if(nums[i] == 2) {                             //If current number is 2
      if (
         // if prev or next is not 2 return true

         !(i-1>=0 && nums[i-1] == 2) &&           
         !(i+1<nums.length && nums[i+1] == 2)

         )  {   return false; }
    }
  }
  return true;
}

为了简单和干净的代码,此代码强制检查

  

i-1>=0i+1<nums.length在每次迭代中。

可以通过从(1...nums.length-1)迭代并分别检查边缘情况来避免这种情况。

答案 2 :(得分:2)

我遇到的问题的解决方案如下:

public boolean twoTwo(int[] nums) {
    final int length = nums.length;
    for (int i = 0; i < length;){
        int count = 0;      // Used to count following 2's
        while(i < length && nums[i++] == 2){
            count++;
        }
        if(count == 1){ // No adjacent 2's! Set doesn't work.
            return false;
        }
    }
    return true; // Didn't come across a lone 2
}

我处理这个问题的方法是,我计算所有相邻的2个。如果计数不是1,我们就是好的。这意味着该指数中没有2,或者存在2个组。这是成立的,因为我们在一个方向上遍历数组。

这个解决方案的一个好处是它适用于任何大小的数组。请注意,即使存在2个循环,它也会具有线性复杂性。它们都只使用相同的索引值进行遍历,只扫描一次数组。

如果我们在任何时候找到2,那么只检查以下内容,发现有2个跟随2(由count表示),我们返回false。

答案 3 :(得分:1)

接下来是指之前或之后。循环遍历每个数字并检查前后的值以查看是否存在相邻的2.特殊情况是在您检查第一个和最后一个元素时因为之前不能成为一个元素或之后检查。

public boolean twoTwo(int[] nums) {

    if(nums.length == 1 && nums[0] == 2)
        return false;

    for(int i = 0; i < nums.length; i++) {
        if(nums[i] == 2) {
            if(i == 0) { // check the next element
                if(nums[i+1] != 2)
                    return false;
            }
            else if(i == (nums.length - 1)) { // check the previous element
                if(nums[i-1] != 2)
                    return false;
            }
            else { // check both
                if(nums[i+1] != 2 && nums[i-1] != 2)
                    return false;
            }
        }
    }

    return true;
}

答案 4 :(得分:1)

Here is mine solution to two two's problem。我认为我的解决方案很明确,即可以理解。

package codingbat.array2;

public class TwoTwo
{
    public static void main(String[] args) 
    {
    }

    public boolean twoTwo(int[] nums) 
    {
        boolean twoTwo = true;
        for (int i = 0; i < nums.length; i++)
        {   
            if (2 == nums[i])
            {
                if (i > 0 && 2 == nums[i - 1]
                 || nums.length > i+1 && 2 == nums[i+1])
                {   
                    twoTwo = true;   
                    i++;                
                } 
                else
                {
                    twoTwo = false;
                    break;
                }          
            }
        }  

        return twoTwo;
    }
}

答案 5 :(得分:1)

我知道这是一个老问题,但我想出了一个新的解决方案。简短,没有复杂的条件。

public boolean twoTwo(int[] nums) {
  int position = -2;
  boolean result = true;

  for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 2) {
      result = position == i - 1;
      position = i;
    }
  }

  return result;
}

答案 6 :(得分:0)

另一种选择。这是主要思想:

  1. 将数组转换为String。在字符串的开头和结尾处添加一个不同于“ 2”的字符,以避免越界。

  2. 查找独立的“ 2”-如果字符串的元素等于2,则检查紧接前后的字符是否也等于“ 2”。如果是,则意味着并非所有的“ 2”都相邻,因此方法返回false。

     public boolean twoTwo(int[] nums) {
    
         // convert array to string
         String text = "";
         for (int i = 0; i < nums.length; i++) {
             text += String.valueOf(nums[i]);
         }
    
         text = " " + text + " ";
    
         // find standalone "2"
         for (int i = 1; i < text.length() - 1; i++) {
             if (text.charAt(i) == '2' && text.charAt(i - 1) != '2' && text.charAt(i + 1) 
         != '2') {
                 return false;
             }
         }
    
         return true;
     }
    

答案 7 :(得分:0)

那个对我来说很艰难...这是我的:

public boolean twoTwo(int[] nums) {

  boolean two = false, res = true;

  for (int i : nums) {
    if (i == 2) {
      if (two)
        res = true;
      else {
        two = true;
        res = false;
      }
    } else {
      two = false;
    }
  }

  return res;
}

答案 8 :(得分:0)

如果其他建议让您感到困惑,这可能会更容易理解。

public boolean twoTwo(int[] nums) {
  int len = nums.length;
  if(len == 0) return true;   // no 2's to worry about
  if(len == 1) return nums[0] != 2;   // make sure it's not a single 2

  for(int i = 1; i < len -1; i++){    // loop for each except edge cases
    if(nums[i] == 2 && nums[i-1] != 2 && nums[i+1] != 2) return false;  // check surrounding
  }

  if(nums[len - 1] == 2) return nums[len - 2] == 2; //if last num is 2 check for 2 before it
  return true;  // made it here it's true
}

答案 9 :(得分:0)

易于理解)

static boolean twoTwo(int[] nums) {
    int len = nums.length;
    boolean result = true;
    boolean found = false;

    for(int i=0; i<len; i++){
        //if it not 2, no meaning to go true other if-s
        if(nums[i] !=2) {found = false; continue;}
        // if current element is 2 and found is true(last element is 2)
        if(nums[i] ==2 && found) result = true;
        // if current element is 2, but last element not
        if(nums[i] ==2 && !found){
            found = true;
            result = false;
        }

    }
    return result;
}

答案 10 :(得分:0)

public boolean twoTwo(int[] nums) {
  int count = 0;
  for (int i = 0; i < nums.length; i++)
    if (nums[i] == 2) count++;
    else if (count == 1) return false;
    else count = 0;
  return count != 1;
}

每次遇到2时,我们都会增加连续2的计数器。

当它不是2时 - 但是计数器表明之前只有一个2 - 我们知道我们发现了一个孤独的2

否则搜索将继续,重置2 - 计数器。

答案 11 :(得分:0)

public boolean twoTwo(int[] nums) {
  int len = nums.length;
  Boolean check = false;
  int  count = 0;

  for(int i=0; i<len ; i++){
    if(nums[i]==2){
      count++;
      if((i<len-1 && nums[i+1]==2) || (i>0 && nums[i-1]==2)) check = true;
      else check = false;
    }
  }

  if(count==0) check = true;  
 return check;
}

答案 12 :(得分:0)

这是我的解决方案。享受。

public boolean twoTwo(int[] nums) 
    {
      //If the length is 2 or more
      if (nums.length >= 2)
      {
        //If the last char is a 2, but the one before it is not a char, we return false;
        if (nums[nums.length - 1] == 2 && nums[nums.length - 2] != 2)
        {
          return false;
        }
        //If larger than three, we create a for loop to test if we have any 2s that are alone.
        if (nums.length >= 3)
        {
          for (int i = 1; i < nums.length-1; i++)
          {
            //If we find a two that is alone, we return false;
            if ((nums[i] == 2) && (nums[i-1] != 2 && nums[i+1] != 2))
            {
              return false;
            }
          }
        }
        //If we don't return false, we return true;
        return true;
      }
      //If we have less than two characters, we return true if the length is 0, or \
      //One the one number there is not a 2.
      else
      {
        return ((nums.length == 0) || !(nums[0] == 2));
      }
    }

答案 13 :(得分:0)

public boolean twoTwo(int[] nums) {
  boolean two = false;
  boolean result = true;

  for (int i=0; i<nums.length; i++) {
    if (nums[i] == 2) {
      if (two) {
        result = true;
      } else {
        result = false;
      }
    two = true;
    } else {
      two = false;
    }
  }

  return result;
}

答案 14 :(得分:0)

public boolean twoTwo(int[] nums) {

    float two = 0;
    double count = 0;

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

        if (i < nums.length - 2 && nums[i] == 2 && nums[i + 1] == 2 && nums[i + 2] == 2) {
            return true;
        }

        if (i < nums.length - 1 && nums[i] == 2 && nums[i + 1] == 2) {
            count++; //count the pair
        }

        if (nums[i] == 2) {
            two++;
        }
    }
    return ((count * 2) == two);
   //each pair contain 2 ,two"s .so pair*2=total two counts
   //count
}

答案 15 :(得分:0)

public boolean twoTwo(int[] nums) {
  for(int i = 0;i<nums.length;i++)
    if(nums[i]==2 && !isTwoBeforeOrAfter(nums,i))
      return false;
return true;
}

private boolean isTwoBeforeOrAfter(int[] nums,int i){
return i+1<nums.length && nums[i+1]==2 || i-1>=0 && nums[i-1]==2;
}

答案 16 :(得分:0)

public boolean twoTwo(int[] nums) {
  for(int i = 0 ; i < nums.length; i++ ) {
     int count = 0;
     if(nums[i] == 2 ) {

        while(i+1 < nums.length &&  nums[i+1] == 2 ) {
          count ++;
          i++;
        }

        if (count == 0 ) {
          return false;
        }
     }
  }
  return true;
}