* 滿足以下條件:
* 1.0是例外可以反復出現 0可以通配任何字符
* 2.相同的數值不會重復出現
* 3.該數組可以是亂序的
* 當數組不含有0時滿足最大值-最小值=n(數組長度)-1
* 當數組數組含有0時.滿足最大值-最小值<n(數組長度)-1
* 所以,當最大值最大值-最小值>n(數組長度)-1時,一定不是連續相鄰數組
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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 (max< a target = "_blank" >a.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("該數組不是相鄰的!"); } } } </ a > |
以上這篇java 判斷一個數組中的數值是否連續相鄰的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/qq_32106517/article/details/71304045