codility 刷题 PermCheck

还是看清楚题目,这题有句话搞晕了:

permutation is a sequence containing each element from 1 to N once, and only once.

其实是说什么样的是个permutation.而不是说真实情况下也只会有一次出现.

            int maxValue =0;;
            for (int i =0; i < A.length; i++) {
                if (A[i] > maxValue){
                    maxValue = A[i];
                }
            }
            if ( maxValue !=A.length ) {
                return 0;
            }
            int B[]  = new int[A.length];
            for (int i =0; i < A.length; i++) {
                if(A[i]-1!=0) {
                    return 7;
                }
                B[A[i]-1] = A[i];
            }
            for (int i =0; i <B.length; i++) {
                if (B[i]==0) {
                    return 0;
                }
            }
            return 1;

 

你可能感兴趣的:(codility)