摘要:我覺(jué)得雖然在里分類(lèi)是,但其實(shí)是一道難題。思路如下搞一個(gè)哈希表,存儲(chǔ)數(shù)組中每一位的后面小于它的數(shù)的個(gè)數(shù)。與上一題的不同之處時(shí)會(huì)有重復(fù)的數(shù)。當(dāng)然,每個(gè)重復(fù)數(shù)的都要階乘,例如有個(gè),個(gè),就是。是所有排列的次數(shù)和,返回下一次。
Permutation Index Problem
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.
ExampleGiven [1,2,4], return 1.
Note我覺(jué)得雖然在LC里分類(lèi)是Easy,但其實(shí)是一道難題。思路如下:
搞一個(gè)哈希表,存儲(chǔ)數(shù)組A中每一位A[i]的后面小于它的數(shù)的個(gè)數(shù)count。
為什么這樣做呢,因?yàn)榘凑?strong>lexicographical order,小的數(shù)應(yīng)該排在前面。那么A[i]后面小于A[i]的數(shù)有count個(gè),而i前面又應(yīng)該有n-i-1位,有(n-1-i)的階乘種排列的可能,所以應(yīng)該排在A[i]之前的可能排列就有count * (n-1-i)!個(gè):所以遍歷A[]中每一個(gè)數(shù),計(jì)算在其之前的自然排列的數(shù)目,這些數(shù)目相加之和存入res,那么res的下一個(gè)數(shù)字就是現(xiàn)在數(shù)組A[]的排列。
對(duì)題目有了思索和理解之后,可以找找簡(jiǎn)化一點(diǎn)的辦法。有沒(méi)有可能不使用HashMap,也能夠記錄階乘呢?只要從最后一位fact = 1開(kāi)始, 向高位階乘,直到最高位fact = A.length!。
Solution1.
public class Solution { public long permutationIndex(int[] A) { int n = A.length; long res = 0; HashMapmap = new HashMap (); for (int i = 0; i < n; i++) { int count = 0; for (int j = i+1; j < n; j++) { if (A[i] > A[j]) count++; } map.put(A[i], count); } for (int i = 0; i < n; i++) { long fact = 1; for (int j = n-1-i; j > 0; j--) { fact *= j; } res += map.get(A[i]) * fact; } return ++res; } }
2.
public class Solution { public long permutationIndex(int[] A) { if (A == null || A.length == 0) return 0; long fact = 1, index = 0; for (int i = A.length-1; i >= 0; i--) { int rank = 0; for (int j = i+1; j < A.length; j++) { if (A[j] < A[i]) rank++; } index += rank * fact; fact *= (A.length-i); } return index+1; } }
vs. i increases in for loop
//這種思路是從高位向低位計(jì)算當(dāng)前位剩余排列總數(shù),階乘逐位減小,理解起來(lái)就沒(méi)有那么直觀了。
public class Solution { public long permutationIndex(int[] A) { if (A == null || A.length == 0) return 0; long index = 0, fact = 1; for (int i = 1; i <= A.length; i++) fact *= i; for (int i = 0; i < A.length; i++) { int rank = 0; for (int j = i+1; j < A.length; j++) { if (A[j] < A[i]) rank++; } fact /= (A.length-i); index += rank * fact; } return index+1; } }Permutation Index II Problem
Given a permutation which may contain repeated numbers, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1.
ExampleGiven the permutation [1, 4, 2, 2], return 3.
Note與上一題的不同之處時(shí)會(huì)有重復(fù)的數(shù)。那么,只要在發(fā)現(xiàn)是重復(fù)數(shù)的那一位用rank * fact的結(jié)果除以重復(fù)的次數(shù)dup再加入index就可以了。當(dāng)然,每個(gè)重復(fù)數(shù)的dup都要階乘,例如有3個(gè)2,4個(gè)8,dup就是3! * 4! = 144。index是所有previous排列的次數(shù)和,返回下一次index+1。
Solutionpublic class Solution { public long permutationIndexII(int[] A) { long index = 0, fact = 1, dup = 1; HashMapmap = new HashMap (); for (int i = A.length-1; i >= 0; i--) { if (!map.containsKey(A[i])) map.put(A[i], 1); else { map.put(A[i], map.get(A[i])+1); dup *= map.get(A[i]); } int rank = 0; for (int j = i+1; j < A.length; j++) { if (A[j] < A[i]) rank++; } index += rank * fact / dup; fact *= (A.length - i); } return index+1; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/66193.html
摘要:從末位向前遍歷,假設(shè)循環(huán)開(kāi)始全是倒序排列,如當(dāng)?shù)谝淮纬霈F(xiàn)正序的時(shí)候,如的和此時(shí)從數(shù)列末尾向前循環(huán)到,找到第一個(gè)比大的交換這兩個(gè)數(shù),變成倒置第位到末位的數(shù)為正序排列這里的是完全倒置的排列,如,即上面循環(huán)的情況完全沒(méi)有出現(xiàn), Problem Implement next permutation, which rearranges numbers into the lexicographic...
Problem Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first strings permutations is the substring of the second string. ...
摘要:做法先把這個(gè)數(shù)放入一個(gè)數(shù)組里,同時(shí)計(jì)算出的階乘。假設(shè)這一組是第組,第一個(gè)數(shù)就是,同時(shí)刪去這個(gè)數(shù),并讓除以取余作為新的。如此循環(huán),這樣,下一組的成員數(shù)減少了,要找的位置也更為精確了。 Problem Given n and k, return the k-th permutation sequence. Example For n = 3, all permutations are li...
Problem Given a list of integers, which denote a permutation. Find the previous permutation in ascending order. Notice The list may contains duplicate integers. Example For [1,3,2,3], the previous per...
摘要:謎題三階幻方。試將這個(gè)不同整數(shù)填入一個(gè)的表格,使得每行每列以及每條對(duì)角線(xiàn)上的數(shù)字之和相同。列出所有的整數(shù)填充方案,然后進(jìn)行過(guò)濾。 /* * 謎題--三階幻方。 * 試將1~9這9個(gè)不同整數(shù)填入一個(gè)3×3的表格,使得每行、每列以及每條對(duì)角線(xiàn)上的數(shù)字之和相同。 * 策略 * 窮舉搜索。列出所有的整數(shù)填充方案,然后進(jìn)行過(guò)濾。 * 亮點(diǎn)為遞歸函數(shù)getPermut...
閱讀 954·2019-08-30 15:55
閱讀 551·2019-08-26 13:56
閱讀 2080·2019-08-26 12:23
閱讀 3295·2019-08-26 10:29
閱讀 600·2019-08-26 10:17
閱讀 2868·2019-08-23 16:53
閱讀 697·2019-08-23 15:55
閱讀 2814·2019-08-23 14:25