public class symm
{
/*
* Returns true if array A is symmetric.
* Returns false otherwise.
* n is the number of elements A contains.
*
* The running time of your algorithm is O( ).
* You may add a brief explanation here if you wish.
*/
public static boolean symmetric( int[] A, int n )
{
return symmHelper(A, n, 0);
}
private static boolean symmHelper(int[] A, int n, int i) {
if(n==1)
return true;
if((n==2) && (A[i] == A[n-1-i]))
return true;
if((i == n-1-i) && (A[i] == A[n-1-i] ))
return true;
if(A[i] == A[n-1-i] && i < n/2 )
return symmHelper(A, n, i+1);
return false;
}
}
テストケース: 適合を除くすべてのテストに合格しました。実行するたびに結果が得られません。問題は、真ん中に 2 が 2 つあることだと思います。コードについてはよくわかりませんが、単純化できると思います。実行時間は o(log n) ですか?
5 8 2 2 8 5 はい
10 7 50 16 20 16 50 7 10 はい
5 8 5 はい
1000 1000 はい
6000 はい
10 7 50 16 20 16 50 7 1000 いいえ
10 7 50 16 20 16 50 700 10 いいえ
10 7 50 16 20 16 5000 7 10 いいえ
10 7 50 16 20 1600 50 7 10 いいえ
10 7 50 16 1600 50 7 10 いいえ