排列序号

代码:

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截图:

 

posted on 2017-07-25 18:16  这位黑洞同学  阅读(126)  评论(0编辑  收藏  举报

导航