2015-09-22 51 views
-3

给定一个ints数组,如果值3正好在数组中出现3次,并且没有3相邻,则返回true。我想在ecllipse中运行此代码,它在codingBat.com上运行正常

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; 
      boolean isPerv3 = false; 
      for(int i = 0 ; i < nums.length && count <= 3; i++) 
      { 
      if(nums[i] == 3) 
      { 
       if(isPerv3) 
        return false; 
       else 
       { 
        count++; 
        isPerv3 = true; 
       } 
      } 
      else 
       isPerv3 = false; 
      } 
      return (count == 3); 
} 
+2

那么,什么是您的实际问题? –

+0

如果你复制/粘贴你发布到eclipse中的代码,点击运行,然后预期结果,请参考[this](http://www.tutorialspoint.com/java/) – csmckelvey

回答

0

使用ListIterator对象将是理想的这种情况。这样您就可以将List中的下一个值与之前的值进行比较。你可以保留一个柜台来检查是否连续有三个3。

import java.util.ArrayList; 
import java.util.ListIterator; 

public class Test { 

    public static void main(String[] args) { 

     int[] test1 = { 3, 2, 5, 11, 11, 11, 233, 22, 3, 3, 3, 5, 5, 5 }; // false 
     int[] test2 = { 3, 3, 3, 8, 3, 11, 233, 22, 3, 3, 3, 5, 5, 5 };  // false 
     int[] test3 = { 3, 2, 5, 8, 3, 11, 233, 22, 3, 3, 4, 3, 3, 3 };  // false 
     int[] test4 = { 3, 2, 5, 8, 3, 11, 233, 22, 3, 3, 5, 5, 5 };  // true 
     System.out.println(haveThree(test1)); 
     System.out.println(haveThree(test2)); 
     System.out.println(haveThree(test3)); 
     System.out.println(haveThree(test4)); 

    } 

    public static boolean haveThree(int[] nums) { 

     int totalNumberOfThrees = 0; 
     int threeInSuccessionCount = 0; 
     boolean result = false; 

     // convert to ArrayList 
     ArrayList<Integer> arrayList = new ArrayList<Integer>(); 
     for (int i = 0; i < nums.length; i++) { 
      arrayList.add(nums[i]); 
     } 

     // use ListIterator to compare next value with previous value 
     ListIterator<Integer> iterator = arrayList.listIterator(); 

     while (iterator.hasNext()) { 
      Integer next = iterator.next(); 
      Integer previous = iterator.previous(); 
      // counts total number of 3's 
      if (next.intValue() == 3) { 
       totalNumberOfThrees++; 
      } 
      // if next and previous values == 3 then increase count by 1 
      if (next.intValue() == 3 && previous.intValue() == 3) { 
       threeInSuccessionCount++; 
      } 
      // if next and previous values != 3 then reset counter 
      else { 
       threeInSuccessionCount = 0; 
      } 
      // if there are three consecutive 3's set threeInSuccessionCount == 3 
      // and then break from loop 
      if (threeInSuccessionCount == 3) { 
       break; 
      } 
      iterator.next(); 
     } 

     if (threeInSuccessionCount != 3 && totalNumberOfThrees >= 3) { 
      result = true; 

     } else { 
      result = false; 
     } 

     return result; 
    } 
} 

输出:

false 
false 
false 
true 

编辑:

要回答你的问题“我怎么叫haveThree()如果声明为static(main方法中)?',你可以在main方法中声明一个新的Test实例,然后用它来调用haveThree()。

像这样:

public static void main(String[] args) { 

    int[] test1 = { 3, 2, 5, 11, 11, 11, 233, 22, 3, 3, 3, 5, 5, 5 }; 
    int[] test2 = { 3, 3, 3, 8, 3, 11, 233, 22, 3, 3, 3, 5, 5, 5 }; 
    int[] test3 = { 3, 2, 5, 8, 3, 11, 233, 22, 3, 3, 4, 3, 3, 3 }; 
    int[] test4 = { 3, 2, 5, 8, 3, 11, 233, 22, 3, 3, 5, 5, 5 }; 

    Test test = new Test(); 
    System.out.println(test.haveThree(test1)); 
    System.out.println(test.haveThree(test2)); 
    System.out.println(test.haveThree(test3)); 
    System.out.println(test.haveThree(test4)); 

} 
+0

如果我制作方法,我该如何调用它非静态... –

+0

@VikasBhardwaj我编辑了我的答案 – smoggers

+0

嗨,我是新来这个网站我不知道如何做upvoting或ticking.when我尝试做投票它说你不能投票自己的帖子..我是一个在java j2EE中寻找载体的求职者,我应该如何让自己的逻辑java强大。每次当我去面试时,我发现一个程序是差异化,非常困难,我无法解决这个问题..如何使核心Java更强大.. –

相关问题