您的位置 首页 编程语言

isSequencedArray

美国程序员面试题

 

Define an m-n sequenced array to be an array that contains one or more occurrences of all the integers between m and n inclusive. Furthermore, the array must be in ascending order and contain only those integers. For example, {2, 2, 3, 4, 4, 4, 5} is a 2-5 sequenced array. The array {2, 2, 3, 5, 5, 5} is not a 2-5 sequenced array because it is missing a 4. The array {0, 2, 2, 3, 3} is not a 2-3 sequenced array because the 0 is out of range. And {1,1, 3, 2, 2, 4} is not a 1-4 sequenced array because it is not in ascending order.
Write a method named isSequencedArray that returns 1 if its argument is a m-n sequenced array, otherwise it returns 0.
If you are writing in Java or C# the function signature is int isSequencedArray(int[ ] a, int m, int n)
If you are writing in C or C++ the function signature is
int isSequencedArray(int a[ ], int len, int m, int n) where len is the number of elements in the array a.
You may assume that m<=n

 

测试样例

 

 

java实现代码:

package com.zzy;

/**
 * 更多请关注: http://huamaodashu.com
 * Created by huamaodashu on 26/07/2018.
 */
public class SequencedArray {
    public static void main(String[] args) {

//        int []a={1, 2, 3, 4, 5};
//        int []a={1, 2, 3, 4, 5};
//        int []a={-5, -5, -4, -4, -4, -3, -3, -2, -2, - 2};
//        int []a={0, 1, 2, 3, 4, 5};
//        int []a={1, 2, 3, 4};
//        int []a={1, 2, 5};
        int []a={5, 4, 3, 2, 1};
        System.out.println(isSequencedArray(a,1,5));

    }
    public static int isSequencedArray(int[]a, int m, int n ){

        if(m<n && a.length>(n-m)){

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

//                if(a[i]<=a[i+1]){
//                    System.out.println("i="+i);
//                    return 1;
//                }
                if((a[i]<=a[i+1]) &&((a[i]>=m && a[i]<=n))) {
//
                }else {
                    return 0;
                }
            }
            return 1;
        }

        return 0;
    }
}

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

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

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

hadoopall

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

热门文章

发表评论

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