题目:

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

    Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言.可基于服务器级的(httpd.conf)和目录级的(.htaccess)两种方式.如果要想用到rewrite模块,必须先 ...

  2. C# CacheHepler Class

    internal class CacheHelper { /// <summary> /// Insert value into the cache using /// appropria ...

  3. impdp ORA-29913: error in executing ODCIEXTTABLEOPEN callout

    1.数据导出时的日志 ;;; Export: Release :: Copyright (c) , , Oracle and/or its affiliates. All rights reserve ...

  4. C# 缓存学习总结

    昨天整理了一下缓存的基本用法,和缓存依赖类 CacheDependency类的使用,今天整理一下缓存的数据库依赖类SqlCacheDependency 1.数据库依赖类SqlCacheDependen ...

  5. 源码编译安装MySQL 5.7.9

    安装CentOS 6.3 配置yum:[root@hank-yoon ~]# cd /etc/yum.repos.d/ [root@hank-yoon yum.repos.d]# mkdir a [r ...

  6. WPF中的DataTemplate

    <Window x:Class="DateTemplate应用.MainWindow" xmlns="http://schemas.microsoft.com/wi ...

  7. 0x01第一个汇编程序

    ;将由text db 10,20,30,40定义的4个数相加,并输出其和. .386    ;指明指令集 .model flat,stdcall ;平坦模式,函数右边的参数先入栈 option cas ...

  8. ubuntu 14.04 安装 Quartus II 13.1 过程

    神奇的linux! 第一步去官网注册然后下载对应的linux版本,包括软件和设备文件两部分,软件也就是quartus II nios ide,modelsim-altera这些,设备就是具体alter ...

  9. Eclipse启动的时候窗口一闪就关的解决办法(转)

    有时候会碰到如题这种问题,从网上查知解决办法,非常管用 为eclipse.exe创建一个快捷方式,然后快捷方式上右键-属性,在目标栏填入 E:\eclipse\eclipse.exe -vm &quo ...

  10. SQLServer2005:在执行批处理时出现错误。错误消息为: 目录名无效

    删除数据时忘了想delete删除的话会记录日志,更何况是我删除百万条数据,结果还没删完服务器内存就占慢了,一切数据都进不来了,估计这种情况导致我的数据库有问题了,右键打开表提示:目录名无效,执行SQL ...