Description

Given a permutation which contains no repeated number, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1.

Example

Given [1,2,4], return 1.

解题:至今仍然不理解这个“字典中的顺序”是什么意思,做个标记日后有时间再看。照着人家的代码敲得:https://www.cnblogs.com/theskulls/p/4881142.html      代码如下:

 public class Solution {
/**
* @param A: An array of integers
* @return: A long integer
*/
public long permutationIndex(int[] A) {
// write your code here
long index = 0;
long position = 2;
long factor = 1;
for (int p = A.length - 2; p >= 0; p--) {
long successors = 0;
for (int q = p + 1; q < A.length; q++) {
if (A[p] > A[q]) {
successors++;
}
}
index += (successors * factor);
factor *= position;
position++;
}
index = index + 1;
return index;
}
}

* 197. Permutation Index【LintCode by java】的更多相关文章

  1. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  2. 211. String Permutation【LintCode by java】

    Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...

  3. 212. Space Replacement【LintCode by java】

    Description Write a method to replace all spaces in a string with %20. The string is given in a char ...

  4. 165. Merge Two Sorted Lists【LintCode by java】

    Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new so ...

  5. 158. Valid Anagram【LintCode by java】

    Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...

  6. 157. Unique Characters 【LintCode by java】

    Description Implement an algorithm to determine if a string has all unique characters. Example Given ...

  7. 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】

    Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...

  8. 173. Insertion Sort List【LintCode by java】

    Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...

  9. 172. Remove Element【LintCode by java】

    Description Given an array and a value, remove all occurrences of that value in place and return the ...

随机推荐

  1. x+=i和x = x+i比较 -- 简单赋值和复合赋值

    这两个赋值方式其实是有区别的,如果最后结果的类型和左操作数的类型一样,那么这两个表达式就完全等价. 下面来看看两个例子来理解它们的区别: 编写一个程序,使得x+=i合法, x = x+i: 不合法. ...

  2. HDU 1290 献给杭电五十周年校庆的礼物(面分割空间 求得到的最大空间数目)

    传送门: 献给杭电五十周年校庆的礼物 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. php auto_prepend_file和auto_append_file的妙用

    这是一个鲜为人知的设置! auto_prepend_file 和 auto_append_file 是在php.ini中进行配置的2个参数,auto_prepend_file 表示在php程序加载第一 ...

  4. Linux下Git远程仓库的使用详解

    Git远程仓库Github 提示:Github网站作为远程代码仓库时的操作和本地代码仓库一样的,只是仓库位置不同而已! 准备Git源代码仓库 https://github.com/ 准备经理的文件 D ...

  5. Redis Sentinel 集群安装 step by step

    一. 准备材料 服务器 IP address 操作系统 位数 Redis 版本   CNT06CAH05 192.168.3.47 CentOS 6.5 x64 Redis-3.2.6 sentine ...

  6. Python入门(案例)

    Python入门(案例) #一.上课案例: #输出hello wordprint('hello word') #python注释有两种#1.单行注释#这是单行注释#2.多行注释'''这是多行注释''' ...

  7. 解决 LLVM 错误提示 may only occur zero or one times!

    使用 LLVM 混淆器添加参数进行编译提示如下错误:clang (LLVM option parsing): for the -bcf option: may only occur zero or o ...

  8. 高性能MySQL--innodb中事务的隔离级别与锁的关系

    最近买了<高性能MySQL>这本书回来看,从中收益颇多!我来一吐为快! 我们都知道事务,那么在什么情况下我们需要使用事务呢? 银行应用是解释事务的一个经典例子.假设一个银行的数据库有两张表 ...

  9. Java : Netty 入门案例

    接收端代码: public class IOServer { public static void main(String[] args) throws IOException, Interrupte ...

  10. python学习笔记:第13天 内置函数(一)

    详细文件查看点击这里:详细地址