* 197. Permutation Index【LintCode by java】

Description

Given a permutation which contains no repeated number, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1.

Example

Given [1,2,4], return 1.

 

解题:至今仍然不理解这个“字典中的顺序”是什么意思,做个标记日后有时间再看。照着人家的代码敲得:https://www.cnblogs.com/theskulls/p/4881142.html      代码如下:

 1 public class Solution {
 2     /**
 3      * @param A: An array of integers
 4      * @return: A long integer
 5      */
 6     public long permutationIndex(int[] A) {
 7         // write your code here
 8         long index = 0;
 9         long position = 2;
10         long factor = 1;
11         for (int p = A.length - 2; p >= 0; p--) {
12             long successors = 0;
13             for (int q = p + 1; q < A.length; q++) {
14                 if (A[p] > A[q]) {
15                     successors++;
16                 }
17             }
18         index += (successors * factor);
19         factor *= position;
20         position++;
21         }
22         index = index + 1;
23         return index;
24         }
25 }

 

转载于:https://www.cnblogs.com/phdeblog/p/9202492.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值