排列序号

代码:

public class Solution { 

        /**

         * @param A an integer array

         * @return a long integer

         */ 

        public long permutationIndex(int[] A) { 

            // Write your code here 

            long R=0; 

            int low[] = new int[A.length]; 

            long weight[] = new long[A.length]; 

            for(int i=A.length-1;i>=0;--i){ 

                for(int j=i+1;j<A.length;++j) 

                    if(A[j]<A[i]) 

                        ++low[i]; 

                weight[i] = (i>=A.length-2)?(A.length-1-i):weight[i+1]*(A.length-i-1); 

                 

                R += weight[i]*low[i]; 

            } 

            return R+1; 

        } 

    }  

lintcode截图:

原文地址:https://www.cnblogs.com/aly15109725486/p/7235725.html