题目:

给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号。其中,编号从1开始。

样例

例如,排列[1,2,4]是第1个排列。

解题:

这个题目感觉很坑的。感觉这只有求出所有的排列,然后找出其对应的下标,但是怎么求出排列,在做Project Euler 时候碰到过,但是现在我又不会写了,那时候毕竟是抄别人的程序的。在geekviewpoint看到一种很厉害的解法,不需要求所有的排列,直接根据给的数组进行求解。

思路:

1.对于四位数:4213 = 4*100+2*100+1*10+3

2.4个数的排列有4!种。当我们知道第一位数的时候,还有3!种方式,当知道第二位数时候还有2!种方式,当知道第三位数的时候还有1!种方式,前面三位数都确定的时候,最后一位也确定了。<这里是按照高位到地位的顺序>

3.对4个数的排列,各位的权值为:3!,2!,1!,0!。第一位之后的数小于第一位的个数是x,第二位之后的数小于第二位的个数是y,第三位之后的数小于第三的个数是z,第四位之后的数小于第四位的个数是w,则abcd排列所在的序列号:index = x*3!+y*2!+z*1!,<0!=0>

在数的排列中,小数在前面,大数在后面,所以考虑该位数之后的数小于该为的数的个数,这里我自己理解的也不是很透,就这样。

4.例如 4213;x= 3,y = 1,z=0,index = 18+2=20

123;x = 0,y=0,index = 0

321;x= 2,y=1,index = 2*2!+1*1! = 5

这里的下标是从0开始的。

Java程序:

public class Solution {
/**
* @param A an integer array
* @return a long integer
*/
public long permutationIndex(int[] permutation) {
// Write your code here
long index = 0;
long position = 2;// position 1 is paired with factor 0 and so is skipped
long factor = 1;
for (int p = permutation.length - 2; p >= 0; p--) {
long successors = 0;
for (int q = p + 1; q < permutation.length; q++) {
if (permutation[p] > permutation[q]) {
successors++;
}
}
index += (successors * factor);
factor *= position;
position++;
}
index = index + 1;
return index;
}
}

总耗时: 5756 ms

Python程序:

class Solution:
# @param {int[]} nums an integer array
# @return {long} a long integer
def permutationIndex(self, nums):
# Write your code here
index = 0
position = 2
factor = 1
numslen = len(nums)
for i in range((numslen-2),-1,-1):
successors = 0
for j in range((i+1),numslen):
if nums[i]>nums[j]:
successors+=1
index += successors*factor
factor*=position
position+=1
index +=1
return index

总耗时: 223 ms

同时,九章上面的程序还看不懂。。。

lintcode :Permutation Index 排列序号的更多相关文章

  1. lintcode Permutation Index

    题目:http://www.lintcode.com/zh-cn/problem/permutation-index/ 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的 ...

  2. Lintcode: Permutation Index II

    Given a permutation which may contain repeated numbers, find its index in all the permutations of th ...

  3. [OJ] Permutation Index

    LintCode 197. Permutation Index (Easy) LintCode 198. Permutation Index II (Medium) 感觉这两道题主要考察计算排列组合的 ...

  4. Permutation Index I & II

    Given a permutation which contains no repeated number, find its index in all the permutations of the ...

  5. GridView控件中加自动排列序号

    GridView控件中加自动排列序号 为 Gridview 增加一个新的空白列,如下: <asp:BoundField  HeaderText="序号">    < ...

  6. * 197. Permutation Index【LintCode by java】

    Description Given a permutation which contains no repeated number, find its index in all the permuta ...

  7. 【LeetCode每天一题】Permutation Sequence(排列序列)

    The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...

  8. [CareerCup] 9.3 Magic Index 魔法序号

    9.3 A magic index in an array A[0.. .n-1] is defined to be an index such that A[i] = i. Given a sort ...

  9. [LintCode] Permuation Index

    Given a permutation which contains no repeated number, find its index in all the permutations of the ...

随机推荐

  1. JDK 动态代理实现原理

    一.引言 Java动态代理机制的出现,使得Java开发人员不用手工编写代理类,只要简单地指定一组接口及委托类对象便能动态生成代理类.代理类会负责将所有方法的调用分派到委托对象上反射执行,在分派执行的过 ...

  2. 重拾C,一天一点点_3

    按位运算 C语言提供了6个位操作运算符,只能作用于整型操作数,即只作用于带符号或无符号的char.short.int.long. &    按位与(AND) !      按位或(OR) ^  ...

  3. 给view 添加事件

    //绑定图片点击事件 UITapGestureRecognizer *g=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@select ...

  4. Laravel 5 基础(四)- Blade 简介

    在多个页面中我们可能包含相同的内容,像是文件头,链接的css或者js等.我们可以利用布局文件完成这个功能. 让我们新建一个布局文件,例如 views/layout.blade.php <!doc ...

  5. SVM入门

    前言: 又有很长的一段时间没有更新博客了,距离上次更新已经有两个月的时间了.其中一个很大的原因是,不知道写什么好-_-,最近一段时间看了看关于SVM(Support Vector Machine)的文 ...

  6. synchronized关键字使用剖析

    synchronized关键字,代表这个方法加锁,相当于不管哪一个线程(例如线程A),运行到这个方法时,都要检查有没有其它线程B(或者C. D等)正在用这个方法,有的话要等正在使用synchroniz ...

  7. AJAX异步同步

    为了更好的用户体验,AJAX的异步同步技术给了我们一个很好的用户体验下面是我做的一个例子. 1.客户端处理 UserId.HTML <!DOCTYPE html PUBLIC "-// ...

  8. 在线压缩zip

    <?php //验证密码 $password = "test"; ?> <html> <head> <meta http-equiv=&q ...

  9. php必看六本书

    php和mysql web开发 PHP高级程序设计_模式.框架与测试.pdf  PHP专业项目实例开发.pdf PHP5高级应用开发实践.pdf [深入PHP面向对象.模式与实践(第2版)].(美)赞 ...

  10. SkyDrive Pro client now available as standalone download. Hurray!

    SkyDrive Pro client now available as standalone download. Hurray! by  Todd O. Klindt  on 5/21/2013 1 ...