您的位置 首页 编程语言

Define an array to be a railroad-tie array if the following three conditions hold

美国面试题:

欢迎关注 : huamaodashu.com

 

3. Define an array to be a railroad-tie array if the following three conditions hold a. The array contains at least one non-zero element
b. Every non-zero element has exactly one non-zero neighbor
c. Every zero element has two non-zero neighbors.
For example, {1, 2, 0, 3, -18, 0, 2, 2} is a railroad-tie array because a[0] = 1 has exactly one non-zero neighbor (a[1])
a[1] = 2 has exactly one non-zero neighbor (a[0]) a[2] = 0 has two non-zero neighbors (a[1] and a[3]) a[3] = 3 has exactly one non-zero neighbor (a[4]) a[4] = -18 has exactly one non-zero neighbor (a[3]) a[5] = 0 has two non-zero neighbors (a[4] and a[6]) a[6] = 2 has exactly one non-zero neighbor (a[7]) a[7] = 2 has exactly one non-zero neighbor (a[6]) The following are not railroad-tie arrays
{1, 2, 3, 0, 2, 2}, because a[1]=2 has two non-zero neighbors.
{0, 1, 2, 0, 3, 4}, because a[0]=0 has only one non-zero neighbor (it has no left neighbor)
{1, 2, 0, 0, 3, 4}, because a[2]=0 has only one non-zero neighbor (a[1])
{1}, because a[0]=1 does not have any non-zero neighbors.
{}, because the array must have at least one non-zero element
{0}, because the array must have at lease one non-zero element.
Write a function named isRailroadTie which returns 1 if its array argument is a railroad-tie array; otherwise it returns 0.
If you are writing in Java or C#, the function signature is
int isRailroadTie(int[ ] a)
If you are writing in C or C++, the function signature is
int isRailroadTie(int a[ ], int len) where len is the number of elements in the array a

测试样例:

 

java代码实现:

 

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

//        int [] a={1, 2, 0, 3, -18, 0, 2, 2};
//        int [] a={1, 2, 3, 0, 2, 2};
//        int [] a={0, 1, 2, 0, 3, 4};
//        int [] a={1, 2, 0, 0, 3, 4};
//        int [] a={1};
//        int [] a={};
//        int [] a={0};
//        int [] a={1, 2};
//        int [] a={1, 2, 0, 1, 2, 0, 1, 2};
//        int [] a={3, 3, 0, 3, 3, 0, 3, 3, 0, 3, 3};
//        int [] a={0, 0, 0, 0};
//        int [] a={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int [] a={1, 3, 0, 3, 5, 0};
        System.out.println(isRailroadTie(a));

    }


    /**
     * 1.至少有一个非零数
     * 2.每个非0元素相邻有一个非零
     * 3.每个0元素有2个非零相邻
     * @param a
     * @return
     */
    public static int isRailroadTie (int [] a) {

        if(a.length <=1){
            return 0;// 至少有个元素
        }

        if(a[0]==0||a[a.length-1]==0){
            return 0;
        }

        for (int i = 1; i < a.length-1; i++) {

            if(a[i]==0){
                if(a[i-1]==0||a[i+1]==0){
                    return 0;
                }
            }else {
               if( !(a[i-1]==0 || a[i+1]==0)){
                   return 0;
               }
            }

        }

        return 1;

    }
}

猫叔总结了 适合新手操作的副业 《淘宝虚拟产品月入2万的 6个 细分类目》的电子书 仅供参考

如果你对虚拟产品比较感兴趣,可以点击:

淘宝卖什么虚拟产品赚钱(月入2万+)

hadoopall

关于花猫大叔短视频创业 作者: hadoopall

热门文章

发表评论

电子邮件地址不会被公开。