Skip to content

Commit 36fd6ef

Browse files
committed
README + next permutation + subset
1 parent cad4f11 commit 36fd6ef

File tree

6 files changed

+141
-329
lines changed

6 files changed

+141
-329
lines changed

Array/31. Next Permutation/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Time spent : 10 min(self)
2+
3+
GIVEN: an array
4+
5+
RETURNS: rearrange numbers into the lexicographically next greater permutation of numbers
6+
7+
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
8+
9+
EXAMPLES:
10+
11+
`1,2,3``1,3,2`
12+
`3,2,1``1,2,3`
13+
`1,1,5``1,5,1`
14+
15+
[Algorithm][https://leetcode.com/problems/global-and-local-inversions/solution/]:
16+
17+
1. Traverse the array from right to left, find the first index i, that num[i] > nums[i - 1] or i == 0.
18+
2. When i is not 0, use j to traverse from right to left again, find the first index j that nums[j] > nums[i - 1], then exchange nums[j] and nums[i - 1].
19+
3. Reverse the nums from i to the end of array.
20+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 2018.4.8
2+
class Solution {
3+
public void nextPermutation(int[] nums) {
4+
if (nums == null || nums.length == 0) {
5+
return;
6+
}
7+
int n = nums.length;
8+
if (n == 1) {
9+
return;
10+
}
11+
12+
int i = n - 1;
13+
while (i > 0 && nums[i] <= nums[i - 1]) {
14+
i--;
15+
}
16+
if (i != 0) {
17+
int j = n - 1;
18+
while (nums[j] <= nums[i - 1]) {
19+
j--;
20+
}
21+
swap(nums, i - 1, j);
22+
}
23+
swapList(nums, i, n - 1);
24+
}
25+
26+
public void swapList(int[] nums, int start, int end) {
27+
while (start < end) {
28+
swap(nums, start++, end--);
29+
}
30+
}
31+
32+
public void swap(int[] nums, int i, int j) {
33+
int temp = nums[i];
34+
nums[i] = nums[j];
35+
nums[j] = temp;
36+
}
37+
}
File renamed without changes.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// 2018.4.7 DFS
2+
class Solution {
3+
public List<List<Integer>> subsets(int[] nums) {
4+
List<List<Integer>> result = new LinkedList<>();
5+
if (nums == null) {
6+
return result;
7+
}
8+
Arrays.sort(nums);
9+
dfs(nums, 0, new LinkedList<>(), result);
10+
return result;
11+
}
12+
13+
// 1. Definition of DFS
14+
private void dfs(int[] nums,
15+
int index,
16+
List<Integer> subset,
17+
List<List<Integer>> result) {
18+
// 3. Exit of DFS
19+
if (index == nums.length) {
20+
result.add(new LinkedList<Integer>(subset));
21+
return;
22+
}
23+
24+
// 2. Recursion Division
25+
// Pick
26+
subset.add(nums[index]);
27+
dfs(nums, index + 1, subset, result);
28+
29+
// Not pick
30+
subset.remove(subset.size() - 1);
31+
dfs(nums, index + 1, subset, result);
32+
}
33+
}
34+
35+
// 2018.4.7 Genreral DFS
36+
class Solution {
37+
public List<List<Integer>> subsets(int[] nums) {
38+
List<List<Integer>> result = new LinkedList<>();
39+
if (nums == null) {
40+
return result;
41+
}
42+
dfs(nums, 0, new LinkedList<Integer>(), result);
43+
return result;
44+
}
45+
46+
private void dfs(int[] nums,
47+
int startIndex,
48+
List<Integer> subset,
49+
List<List<Integer>> result) {
50+
result.add(new LinkedList<Integer>(subset));
51+
52+
for (int i = startIndex; i < nums.length; i++) {
53+
subset.add(nums[i]);
54+
dfs(nums, i + 1, subset, result);
55+
subset.remove(subset.size() - 1);
56+
}
57+
}
58+
}
59+
60+
class Solution {
61+
public List<List<Integer>> subsets(int[] nums) {
62+
List<List<Integer>> result = new LinkedList<>();
63+
if (nums == null) {
64+
return result;
65+
}
66+
dfs(nums, 0, new LinkedList<Integer>(), result);
67+
return result;
68+
}
69+
70+
private void dfs(int[] nums,
71+
int startIndex,
72+
List<Integer> subset,
73+
List<List<Integer>> result) {
74+
result.add(subset);
75+
76+
for (int i = startIndex; i < nums.length; i++) {
77+
List<Integer> newSet = new LinkedList<>(subset);
78+
newSet.add(nums[i]);
79+
dfs(nums, i + 1, newSet, result);
80+
}
81+
}
82+
}

DFS/78. Subsets/Solution.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)