* 197. Permutation Index【LintCode by java】
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】的更多相关文章
- 156. Merge Intervals【LintCode by java】
Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...
- 211. String Permutation【LintCode by java】
Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...
- 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 ...
- 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 ...
- 158. Valid Anagram【LintCode by java】
Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...
- 157. Unique Characters 【LintCode by java】
Description Implement an algorithm to determine if a string has all unique characters. Example Given ...
- 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 ...
- 173. Insertion Sort List【LintCode by java】
Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...
- 172. Remove Element【LintCode by java】
Description Given an array and a value, remove all occurrences of that value in place and return the ...
随机推荐
- 【UVA11806 Cheerleaders】 题解
题目链接:https://www.luogu.org/problemnew/show/UVA11806 容斥原理+组合数 正着找合♂fa的不好找,那就用总方案数-不合♂fa的 #include < ...
- ETL测试小结
一.ETL测试的重要性: ETL(Extract-Transform-Load的缩写,即数据抽取.转换.装载的过程)作为BI/DW(Business Intelligence)的核心和灵魂,能够按照统 ...
- Google Guava -缓存cache简单使用
package guavacache; import java.util.concurrent.ExecutionException; import java.util.concurrent.Time ...
- VS进行调试时IIS Express显示Access Define-坑爹的腾讯TGP助手
今天在家使用VS进行调试的时候发现IIS Express死活启动不了,改用IIS也是不行,尝试了网上所说的所有办法,改了各种权限,找了各种注册表,最终未果,然后我想起之前被腾讯坑过的那次:http:/ ...
- JQuery给一个元素绑定两次点击事件(第二次点击事件)
由于项目的要求,需要给复选框设置样式,初始样式:,第一次点击的时候显示,第二次点击时候需要改变该样式:. 设计思路: 当点击次数为奇数时显示带有颜色的图片 当点击次数为偶数时显示没有颜色的图片 下边是 ...
- iOS之改变UIAlertViewController字体的颜色
NSString *message = @"请确认信息是否正确?"; NSString *title = @"提示"; UIAlertController *a ...
- asp.net core上使用Redis demo
整体思路:(1.面向接口编程 2.Redis可视化操作IDE 3.Redis服务) [无私分享:ASP.NET CORE 项目实战(第十一章)]Asp.net Core 缓存 MemoryCache ...
- 昊合数据整合平台HHDI常见问题
Q: HaoheDI和Informatica PowerCenter.IBM DataStage的区别在哪里? A: Informatica和DataStage是比较重量级的ETL平台,其自身就是比较 ...
- Python学习 :装饰器
装饰器(函数) 装饰器作为一个函数,可以为其他函数在不修改原函数代码的前提下添加新的功能 装饰器的返回值是一个函数对象.它经常用于有切面需求的场景,比如:插入日志.性能测试.事务处理.缓存.权限校验等 ...
- 说一说MySQL的锁机制
锁概述 MySQL的锁机制,就是数据库为了保证数据的一致性而设计的面对并发场景的一种规则. 最显著的特点是不同的存储引擎支持不同的锁机制,InnoDB支持行锁和表锁,MyISAM支持表锁. 表锁就是把 ...