* 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 ...
随机推荐
- Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法
Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法 从xcode4.4开始,LLVM4.0编译器为Objective-C添加一些新的特性. ...
- [SharePoint]如何去掉欢迎导航的信息?
在我们实际的开发过程中,经常会把SharePoint中的一些已有的feature去掉,如欢迎信息下面的“我的设置”,“对本页面进行个性化设置”.详细见下图: Figure 1 常见的欢迎信息的界面 ...
- C++读取字符串数据的两种方式
C++读取字符串数据的两种方式 对于同样的样例输入: ladder came tape soon leader acme RIDE lone Dreis peat ScAlE orb eye Ride ...
- mount/umount命令
挂载及卸载指定的文件系统 mount [选项] [-L<标签>] [-o<选项>] [-t<文件系统类型>] [设备名] [挂载点] umount [挂载点] -a ...
- MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述 # Time: :: # User@Host: **[**] @ [**] Id: ** # Killed: # Query_time: Rows_examined: Rows_affect ...
- Linux查看MAC地址方法
注:一般默认的网卡文件名是eth0,根据IP地址对应的实际情况区判断是ethx即可. 1. ip -a . cat /sys/class/net/ens39/address 其中 HWaddr字段就 ...
- 替代Xshell的良心国产软件 FinalShell
今年8月份NetSarang公司旗下软件家族的官方版本被爆被植入后门着实让我们常用的Xshell,Xftp等工具火了一把,很长时间都是在用Xshell,不过最近发现了一款同类产品FinalShell, ...
- js实现QQ、微信、新浪微博分享功能
微信分享需要手机扫描二维码,需要对url进行编码.在https协议下,扫描二维码时,浏览器打不开可能时安全证书导致的. var shareModel = { /** * 分享QQ好友 * @param ...
- ES6 开发规范-最佳实践
ES6 开发规范(最佳实践) 本文为开发规范,收集方便日后查看. [开发规范]https://blog.csdn.net/zzzkk2009/article/details/53171058?utm_ ...
- Linux phpmailer发送邮件失败的解决方法
(本地windows phpmailer发送ok 放到linux发送失败) 原因:linux 通过465端口进行更安全的SMTPS协议发送邮件 windows 是基于smtp 25端口的 因此 可 ...