美国程序员面试题
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;
}
}