查找Array中元素前后元素的出现次数

时间:2014-06-07 15:18:05

标签: java arrays

我正在写一个算法:

在此算法中,如果数组包含元素3,则元素3不能位于两个1元素之间,如下所示:

  

int array = {5,2,10,' 3',15,' 1',2,2} //'符号只是用于突出显示有问题的元素。

上面的数组包含元素3,请注意在3之前没有元素1。 但是在元素3之后有一个元素1,在这种情况下它应该返回True。 它应该返回true,因为元素3不被"包围"由两个元素1

组成
  

int array = {' 3',2,18,>' 1' ,0,#3#, - 11,' 1'< ,' 3'} //',<,#符号只是突出了caracteres。

3的第一个元素之后的这个数组中,有两个元素1,围绕一个元素3,因此它应该返回False

我尝试过以下代码:

public class Third 
{  
    public static void main(String[] args)
    {  
        int[] array = {1, 2, 4, 3, 1}; //should return false, '3' contained in '1' elements.

        for(int i=0; i<array.length; i++)
        {
            if(array[i]==3)
            {
                for(int j=0;j<array[i];j++)
                {
                    if(array[j]==1)
                    {

                         System.out.println("I foud One before "+array[j]); 


                    }else
                    {
                        break;
                    }

                        System.out.println("yes i found the array:"+array[i]);
                }

                for(int z=0;z>array[i];z++)
                {
                    if(array[z]==1)
                    {
                             System.out.println("I found after 3 is :"+array[z]);
                    }
                    break;
                }
            }
        }
    }
}  

我没有得到我想要的上述代码的确切结果。

3 个答案:

答案 0 :(得分:0)

如何简单地遍历数组?:

List<Integer> nums = new ArrayList<>();
List<Integer> target = new ArrayList<>(Arrays.asList(new int[]{1, 3, 1}));
for (int i : array) {
    if (i == 3 || i == 1) {
        nums.add(i);
    }
}
return Collections.indexOfSubList(nums, target) >= 0;

或者,使用正则表达式:

int test[] = {1, 5, 3, 2, 5, 67, 8, 1};
return Arrays.toString(test).matches(".*1.*3.*1.*"); //greedy search

答案 1 :(得分:0)

 public class Third {  
 public static void main(String[] args){  
    int[] array = {1,2,4,3, 1}; 

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

        if(array[i]==3)
        {
            for(int j=0;j<=i;j++)
            {
                if(array[j]==1)
                {

                     System.out.println("I foud One before "+array[j]); 


                }else
                {
                    break;
                }


            System.out.println("yes i found the array:"+array[i]);

        }

            for(int z=i;z>array.length;z++)
            {
                if(array[z]==1)
                {
                    System.out.println("I found after 3 is :"+array[z]);
                }
                break;
            }
    }

}

答案 2 :(得分:0)

根据你的问题,如果你只是试图检查3是否被1包围,你可以这样做。这是根据你的假设你的数组中没有多个3的问题。

public static void main(String[] args) 
{
    int[] array = {3,1,2,4,3,1,3,1};
    int posOfThree = 0;
    int posOfFirstThree = 0;
    boolean noLeft = true;
    boolean noRight = true;
    boolean firstThreeFound = false;
    //Get position of 3
    for (int x=0; x<array.length; x++) 
    {
        if (array[x] == 3)
        {
            if (firstThreeFound == false)
            {   
                posOfFirstThree = x; //Position of the first occurred 3
                firstThreeFound = true;     
            }       
            posOfThree = x;          //Position of the last occurred 3
        }       
    }   

    //Check if there is 1 on left hand side of first 3
    int left = 0;
    while (posOfFirstThree - left > 0)  
    {
        left ++;
        if (array[posOfFirstThree-left] == 1)
            noLeft = false;
    }   

    //Check if there is 1 on right hand side of last 3
    int right = 0;
    while (posOfThree + right < array.length-1) 
    {
        right ++;
        if (array[posOfThree + right] == 1)
            noRight = false;
    }

    System.out.println("Outcome: " + (noLeft || noRight));
}

节目输出: Outcome: True

由于您只能使用普通循环和简单数组来执行此操作。这很简单,足以满足您的要求。当然,我相信这个解决方案可以进一步改进,但是,这段代码可以完全满足您的需求(即使增加或减少数组大小)。

编辑:编辑后的版本可以处理数组中的多个3,并检查是否所有3都被1包围。