您的位置 首页 编程语言

SystematicallyIncreasing

美国程序员面试题

An array is called systematically increasing if it consists of increasing sequences of the numbers from 1 to n.
The first six (there are over 65,000 of them) systematically increasing arrays are:

 

 

 

Write a function named isSystematicallyIncreasing which returns 1 if its array argument is systematically increasing. Otherwise it returns 0.
If you are programming in Java or C#, the function signature is
int isSystematicallyIncreasing(int[ ] a)
If you are programming in C or C++, the function signature is
int isSystematicallyIncreasing(int a[ ], int len) where len is the number of elements in the array a.

 

 

 

java实现代码:

package com.zzy;

/**
 * 更多请关注: http://huamaodashu.com
 * Created by huamaodashu on 07/08/2018.
 */
public class SystematicallyIncreasing {
    public static void main(String[] args) {
//        int[] a ={1, 1, 2};
//        int[] a ={1, 1, 2, 1, 2, 3};
//        int[] a ={1, 1, 2, 1, 2, 3, 1, 2, 3, 4};
//        int[] a ={1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5};
//        int[] a ={1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6};
//        int[] a ={1, 2, 1, 2, 3};
//        int[] a ={1, 1, 3};
//        int[] a ={1, 2, 1, 2, 1, 2};
//        int[] a ={1, 2, 3, 1, 2, 1};
        int[] a ={1, 1, 2, 3};
        System.out.println(isSystematicallyIncreasing(a));

    }
    public static  int isSystematicallyIncreasing(int [] a){

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


        if(a.length > 2){
            if(a[0] != 1 || a[1] !=1){
                return 0;
            }
            int count = 2;
            for (int i = 1; i < a.length-1; i++) {
                if(a[i+1] - a[i] !=1){
                    System.out.println("count"+count);
                    if(a[i] != count){
                        return 0;
                    }
                    count ++;

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

            }

        }
        return 1;
    }
}

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

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

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

hadoopall

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

热门文章

发表评论

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