排列序号

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了排列序号相关的知识,希望对你有一定的参考价值。

代码;

 

class Solution {
public:
/**
* @param A an integer array
* @return a long integer
*/
long long permutationIndex(vector<int>& A) {
// Write your code here
// 找出A中每一个位置其后有多少个数比它小
// 然后再相加这些数与位对应的权
int len = A.size();
int c[len];
c[len - 1] = 0;// 最后一个数之后就没有比它小的数了
vector<int> a;
a.push_back(A[len - 1]);
for(int i = len - 2;i >= 0; --i){
auto iter = lower_bound(a.begin(), a.end(), A[i]);
c[i] = iter - a.begin();
a.insert(iter, A[i]);
}
long long ans = 1, fac = 1, cc = 1;
for(int i = len - 2;i >= 0; --i)
ans += (fac*=cc++) * c[i];
return ans;
}
};

截图:

技术分享

 


























以上是关于排列序号的主要内容,如果未能解决你的问题,请参考以下文章

如何自动排列序号?

mysql mysql 获取排列序号

排列序号

排列序号

排列序号

浅谈康托展开和其逆运算