java 判断一个数组中的数值是否连续相邻

来源:互联网 发布:淘宝页面找不 编辑:程序博客网 时间:2024/05/17 20:31
*  判断一个数组中的数值是否连续相邻
 *  满足以下条件:   
 *              1.0是例外可以反复出现  0可以通配任何字符
 *              2.相同的数值不会重复出现
 *              3.该数组可以是乱序的
 *  当数组不含有0时满足最大值-最小值=n(数组长度)-1
 *  当数组数组含有0时.满足最大值-最小值<n(数组长度)-1

 *  所以,当最大值最大值-最小值>n(数组长度)-1时,一定不是连续相邻数组

package datastruct.usearray;public class JudgeAdjacent {   private static boolean judege(int a[]) {   int min=Integer.MAX_VALUE;   int max=Integer.MIN_VALUE;    for (int i = 0; i < a.length; i++) {  if (a[i]!=0) { if (min>a[i]) {min=a[i];} if (maxa.length-1) {return false;}else {return true;}   }        public static void main(String[] args) {         int a[]={8,5,0,10,6,7,0,0};if (judege(a)) {System.out.println("该数组是相邻的!");}else {System.out.println("该数组不是相邻的!");}}}

1 0