Skip to content

Commit 89f7cb2

Browse files
author
liwentian
committed
fd
1 parent 1db1868 commit 89f7cb2

8 files changed

Lines changed: 100 additions & 38 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
|44|[Wildcard Matching](https://leetcode.com/problems/wildcard-matching/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/WildcardMatching.java)|60|和#10多做几遍|
6464
|45|[Jump Game II](https://leetcode.com/problems/jump-game-ii/#/description)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/JumpGameII.java)||
6565
|46|[Permutations](https://leetcode.com/problems/permutations/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/Permutations.java)||
66+
|47|[Permutations II](https://leetcode.com/problems/permutations-ii/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/PermutationsII.java)|70|多做几遍|
6667
|48|[Rotate Image](https://leetcode.com/problems/rotate-image/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/RotateImage.java)|100|
6768
|49|[Group Anagrams](https://leetcode.com/problems/anagrams/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/GroupAnagrams.java)|95|
6869
|50|[Pow(x, n)](https://leetcode.com/problems/powx-n/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/Pow.java)|80|
@@ -72,7 +73,7 @@
7273
|55|[Jump Game](https://leetcode.com/problems/jump-game/#/description)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/JumpGame.java)||
7374
|56|[Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/MergeIntervals.java)|70|
7475
|57|[Insert Interval](https://leetcode.com/problems/insert-interval/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/InsertInterval.java)|75|这题要多做几遍|
75-
|60|[Permutation Sequence](https://leetcode.com/problems/permutation-sequence/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/PermutationSequence.java)||
76+
|60|[Permutation Sequence](https://leetcode.com/problems/permutation-sequence/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/PermutationSequence.java)|70|这题思路巧妙,多做两遍|
7677
|66|[Plus One](https://leetcode.com/problems/plus-one/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/PlusOne.java)|100|这题多看一遍|
7778
|67|[Add Binary](https://leetcode.com/problems/add-binary/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/AddBinary.java)|100|
7879
|68|[Text Justification](https://leetcode.com/problems/text-justification/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/TextJustification.java)||

‎ebook/Backtracking.tex‎

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,36 @@ \subsubsection{Description}
278278
\subsubsection{Solution}
279279

280280
\begin{Code}
281+
public List<List<Integer>> permuteUnique(int[] nums) {
282+
List<List<Integer>> result = new ArrayList<List<Integer>>();
283+
permute(nums, result, 0);
284+
return result;
285+
}
281286

287+
public void permute(int[] nums, List<List<Integer>> result, int start) {
288+
if (start >= nums.length) {
289+
List<Integer> list = new ArrayList<Integer>();
290+
for (Integer n : nums) {
291+
list.add(n);
292+
}
293+
result.add(list);
294+
}
295+
296+
HashSet<Integer> set = new HashSet<Integer>();
297+
for (int i = start; i < nums.length; i++) {
298+
if (set.add(nums[i])) {
299+
swap(nums, start, i);
300+
permute(nums, result, start + 1);
301+
swap(nums, start, i);
302+
}
303+
}
304+
}
305+
306+
public static void swap(int[] nums, int left, int right) {
307+
int temp = nums[left];
308+
nums[left] = nums[right];
309+
nums[right] = temp;
310+
}
282311
\end{Code}
283312

284313
\newpage
@@ -307,25 +336,26 @@ \subsubsection{Solution}
307336

308337
\begin{Code}
309338
public String getPermutation(int n, int k) {
310-
int[] nums = new int[n];
339+
int[] fac = new int[n + 1];
340+
fac[0] = 1;
311341

312-
for (int i = 0; i < n; i++) {
313-
nums[i] = i + 1;
314-
}
342+
List<Integer> list = new LinkedList<>();
315343

316-
for (int i = 1; i < k; i++) {
317-
nextPermutation(nums);
344+
for (int i = 1; i <= n; i++) {
345+
fac[i] = fac[i - 1] * i;
346+
list.add(i);
318347
}
319348

349+
k--;
350+
320351
StringBuilder sb = new StringBuilder();
321-
for (int i = 0; i < n; i++) {
322-
sb.append(nums[i]);
352+
for (int i = 1; i <= n; i++) {
353+
int ind = k / fac[n - i];
354+
k %= fac[n - i];
355+
sb.append(list.remove(ind));
323356
}
324-
return sb.toString();
325-
}
326-
327-
private void nextPermutation(int[] nums) {
328357

358+
return sb.toString();
329359
}
330360
\end{Code}
331361

@@ -435,11 +465,6 @@ \subsubsection{Solution}
435465
}
436466
trie.word = word;
437467
}
438-
\end{Code}
439-
440-
\newpage
441-
442-
\begin{Code}
443468

444469
public List<String> findWords(char[][] board, String[] words) {
445470
Trie trie = new Trie();
@@ -454,7 +479,11 @@ \subsubsection{Solution}
454479
}
455480
return new LinkedList<String>(set);
456481
}
482+
\end{Code}
457483

484+
\newpage
485+
486+
\begin{Code}
458487
private void dfs(Set<String> set, char[][] board, int i, int j, Trie trie) {
459488
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) {
460489
return;

‎ebook/leetcode.log‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex 2017.9.4) 6 SEP 2017 22:08
1+
This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex 2017.9.4) 7 SEP 2017 18:53
22
entering extended mode
33
restricted \write18 enabled.
44
%&-line parsing enabled.

‎ebook/leetcode.pdf‎

595 Bytes
Binary file not shown.

‎ebook/leetcode.synctex.gz‎

2.05 KB
Binary file not shown.
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
package com.inuker.solution;
22

3+
import java.util.LinkedList;
4+
import java.util.List;
5+
36
/**
47
* Created by dingjikerbo on 2016/12/17.
58
*/
69

710
public class PermutationSequence {
811

912
public String getPermutation(int n, int k) {
10-
int[] nums = new int[n];
13+
int[] fac = new int[n + 1];
14+
fac[0] = 1;
1115

12-
for (int i = 0; i < n; i++) {
13-
nums[i] = i + 1;
14-
}
16+
List<Integer> list = new LinkedList<>();
1517

16-
for (int i = 1; i < k; i++) {
17-
nextPermutation(nums);
18+
for (int i = 1; i <= n; i++) {
19+
fac[i] = fac[i - 1] * i;
20+
list.add(i);
1821
}
1922

23+
k--;
24+
2025
StringBuilder sb = new StringBuilder();
21-
for (int i = 0; i < n; i++) {
22-
sb.append(nums[i]);
26+
for (int i = 1; i <= n; i++) {
27+
int ind = k / fac[n - i];
28+
k %= fac[n - i];
29+
sb.append(list.remove(ind));
2330
}
24-
return sb.toString();
25-
}
26-
27-
private void nextPermutation(int[] nums) {
2831

32+
return sb.toString();
2933
}
3034
}

‎solution/src/main/java/com/inuker/solution/PermutationsII.java‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,44 @@
44
* Created by dingjikerbo on 2016/12/17.
55
*/
66

7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.HashSet;
10+
import java.util.List;
11+
712
/**
813
* 需要研究
914
*/
1015
public class PermutationsII {
1116

12-
/**
13-
public List<List<Integer>> permute(int[] nums) {
17+
public List<List<Integer>> permuteUnique(int[] nums) {
18+
List<List<Integer>> result = new ArrayList<List<Integer>>();
19+
permute(nums, result, 0);
20+
return result;
21+
}
22+
23+
public void permute(int[] nums, List<List<Integer>> result, int start) {
24+
if (start >= nums.length) {
25+
List<Integer> list = new ArrayList<Integer>();
26+
for (Integer n : nums) {
27+
list.add(n);
28+
}
29+
result.add(list);
30+
}
1431

32+
HashSet<Integer> set = new HashSet<Integer>();
33+
for (int i = start; i < nums.length; i++) {
34+
if (set.add(nums[i])) {
35+
swap(nums, start, i);
36+
permute(nums, result, start + 1);
37+
swap(nums, start, i);
38+
}
39+
}
1540
}
1641

17-
*/
42+
public static void swap(int[] nums, int left, int right) {
43+
int temp = nums[left];
44+
nums[left] = nums[right];
45+
nums[right] = temp;
46+
}
1847
}

‎test/src/main/java/com/example/Main.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.inuker.solution.FactorCombinations;
44
import com.inuker.solution.FlipGame;
5+
import com.inuker.solution.PermutationSequence;
56
import com.inuker.solution.RestoreIPAddresses;
67
import com.inuker.solution.ValidWordAbbreviation;
78
import com.leetcode.google.DecodeString;
@@ -30,10 +31,8 @@
3031
public class Main {
3132

3233
public static void main(String[] args) {
33-
List<String> list = new FlipGame().generatePossibleNextMoves("++++");
34-
for (String s : list) {
35-
System.out.println(s);
36-
}
34+
String s = new PermutationSequence().getPermutation(4, 5);
35+
System.out.println(s);
3736
}
3837
}
3938

0 commit comments

Comments
 (0)