lintcode :Permutation Index 排列序号
题目:
给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号。其中,编号从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 排列序号的更多相关文章
- lintcode Permutation Index
题目:http://www.lintcode.com/zh-cn/problem/permutation-index/ 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的 ...
- Lintcode: Permutation Index II
Given a permutation which may contain repeated numbers, find its index in all the permutations of th ...
- [OJ] Permutation Index
LintCode 197. Permutation Index (Easy) LintCode 198. Permutation Index II (Medium) 感觉这两道题主要考察计算排列组合的 ...
- Permutation Index I & II
Given a permutation which contains no repeated number, find its index in all the permutations of the ...
- GridView控件中加自动排列序号
GridView控件中加自动排列序号 为 Gridview 增加一个新的空白列,如下: <asp:BoundField HeaderText="序号"> < ...
- * 197. Permutation Index【LintCode by java】
Description Given a permutation which contains no repeated number, find its index in all the permuta ...
- 【LeetCode每天一题】Permutation Sequence(排列序列)
The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...
- [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 ...
- [LintCode] Permuation Index
Given a permutation which contains no repeated number, find its index in all the permutations of the ...
随机推荐
- js设计模式(3)---桥接模式
0.前言 看设计模式比较痛苦,一则是自己经验尚浅,不能体会到使用这些设计模式的益处:二则是不能很好把握使用这些设计模式的时机.所以这一部分看得断断续续,拖拖拉拉,为了了却这快心病,决定最近一口气看完几 ...
- PHP获取当前日期或指定日期的所在月的第一天和最后一天
<?php function getthemonth($date) { $firstday = date('Y-m-01', strtotime($date)); $lastday = date ...
- 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 ...
- 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 ...
- 在HTML中通过jQuery设置列表项符号
在创建列表的时候,可以通过指定type来设置列表项的符号,如下所示: <body> <form id="form1" runat="server&quo ...
- Linux 使用退格键时出现^H解决方法
当我们再和脚本交互的时候,在终端上输错了内容,使用退格键,屏幕上会出现乱码,比如 ^H.^H不是H键的意思,是backspace. 主要是当你的终端backspace有问题的时候才需要设置. 解决方法 ...
- WPF学习笔记1——XAML之1
参考文献: http://msdn.microsoft.com/zh-cn/library/ms752059(v=vs.110).aspx <Pro WPF 4.5 in C# > 一.X ...
- telnet命令判断端口是否通不通
以上得出结论80端口不通 如果连接成功,想要退出telnet的话,ctrl+],然后输入quit 查看iptables vi /etc/sysconfig/iptables #编辑防火墙配置文件 ...
- 自定义的你的ubuntu鼠标右键
首先看下效果图: 好,接下来讲下如何实现,“下一个桌面”和”在终端打开“,首先是安装必要软件 sudo apt-get -y install nautilus-open-terminal nautil ...
- clion windows 开发配置
1.下载clion 并且安装. 地址 : http://download-cf.jetbrains.com/cpp/clion-1.0.1.exe 2.安装cygwin 地址: https://cy ...