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;
}
}
```