题目:

给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号。其中,编号从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程序:

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

总耗时: 5756 ms

Python程序:

  1. class Solution:
  2. # @param {int[]} nums an integer array
  3. # @return {long} a long integer
  4. def permutationIndex(self, nums):
  5. # Write your code here
  6. index = 0
  7. position = 2
  8. factor = 1
  9. numslen = len(nums)
  10. for i in range((numslen-2),-1,-1):
  11. successors = 0
  12. for j in range((i+1),numslen):
  13. if nums[i]>nums[j]:
  14. successors+=1
  15. index += successors*factor
  16. factor*=position
  17. position+=1
  18. index +=1
  19. 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. js设计模式(3)---桥接模式

    0.前言 看设计模式比较痛苦,一则是自己经验尚浅,不能体会到使用这些设计模式的益处:二则是不能很好把握使用这些设计模式的时机.所以这一部分看得断断续续,拖拖拉拉,为了了却这快心病,决定最近一口气看完几 ...

  2. PHP获取当前日期或指定日期的所在月的第一天和最后一天

    <?php function getthemonth($date) { $firstday = date('Y-m-01', strtotime($date)); $lastday = date ...

  3. How to change comment

    AX2009 // USR Changed on 2013-07-10 at 12:57:46 by 7519 - Begin // USR Changed on 2013-07-10 at 12:5 ...

  4. How to: Enable and Disable an Action Pane Button on a List Page [AX 2012]

    Applies To: Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynami ...

  5. 在HTML中通过jQuery设置列表项符号

    在创建列表的时候,可以通过指定type来设置列表项的符号,如下所示: <body> <form id="form1" runat="server&quo ...

  6. Linux 使用退格键时出现^H解决方法

    当我们再和脚本交互的时候,在终端上输错了内容,使用退格键,屏幕上会出现乱码,比如 ^H.^H不是H键的意思,是backspace. 主要是当你的终端backspace有问题的时候才需要设置. 解决方法 ...

  7. WPF学习笔记1——XAML之1

    参考文献: http://msdn.microsoft.com/zh-cn/library/ms752059(v=vs.110).aspx <Pro WPF 4.5 in C# > 一.X ...

  8. telnet命令判断端口是否通不通

    以上得出结论80端口不通 如果连接成功,想要退出telnet的话,ctrl+],然后输入quit 查看iptables vi /etc/sysconfig/iptables   #编辑防火墙配置文件  ...

  9. 自定义的你的ubuntu鼠标右键

    首先看下效果图: 好,接下来讲下如何实现,“下一个桌面”和”在终端打开“,首先是安装必要软件 sudo apt-get -y install nautilus-open-terminal nautil ...

  10. clion windows 开发配置

    1.下载clion 并且安装. 地址 : http://download-cf.jetbrains.com/cpp/clion-1.0.1.exe 2.安装cygwin  地址: https://cy ...