Skip to content

Commit 709d240

Browse files
committed
fd
1 parent 3355100 commit 709d240

File tree

4 files changed

+147
-1
lines changed

4 files changed

+147
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@
294294
|325|[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/MaximumSizeSubarraySumEqualsK.java)|75|这题思路有意思,多做几遍|
295295
|328|[Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/OddEvenLinkedList.java)|90|
296296
|329|[Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/LongestIncreasingPathInAMatrix.java)|70|这题要多做几遍|
297+
|332|[Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/ReconstructItinerary.java)|||
297298
|333|[Largest BST Subtree](https://leetcode.com/problems/largest-bst-subtree/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/LargestBSTSubtree.java)|50|这道题虽然不难,但是折腾了很久,一定要多做|
298299
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/IncreasingTripletSubsequence.java)|75|
299300
|335|[Self Crossing](https://leetcode.com/problems/self-crossing/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/SelfCrossing.java)|80|

solution/src/main/java/com/inuker/solution/ContainsDuplicateIII.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class ContainsDuplicateIII {
1616
/**
1717
* 这题要注意溢出问题,干脆都用long
1818
* 时间复杂度O(nlg(min(n,k))
19+
* for循环走到当前的nums[i]说明之前的都不行,因此只要判断当前
20+
* nums[i]的加入是否可以即可,因此只要判断nums[i]相邻的上下两个数是否在t范围内
1921
*/
2022
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
2123
TreeSet<Long> set = new TreeSet<>();

solution/src/main/java/com/inuker/solution/MinimumSizeSubarraySum.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
*/
88

99
public class MinimumSizeSubarraySum {
10-
10+
/**
11+
* 仍然采用滑动窗口的办法
12+
*/
1113
public int minSubArrayLen(int s, int[] nums) {
1214
int sum = 0, min = Integer.MAX_VALUE;
1315
for (int i = 0, j = 0; i < nums.length; i++) {
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package com.inuker.solution;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.HashMap;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.PriorityQueue;
11+
import java.util.Stack;
12+
13+
/**
14+
* Created by liwentian on 2017/9/2.
15+
*/
16+
/**
17+
* 这题有两个前提
18+
* 1, 必须从JFK开始
19+
* 2, 解一定存在
20+
*/
21+
22+
/**
23+
* 这题有两种解法:
24+
* 1, 常规是DFS暴力解法,面试能写出这种就行了
25+
* 2, 欧拉解法
26+
*/
27+
public class ReconstructItinerary {
28+
29+
/**
30+
* 常规DFS暴力列出所有可能的路径,可以一次走遍所有的边
31+
*/
32+
public List<List<String>> findItinerary(String[][] tickets) {
33+
HashMap<String, ArrayList<String>> map = new HashMap<>();
34+
for (String[] ticket : tickets) {
35+
map.computeIfAbsent(ticket[0], k -> new ArrayList<>()).add(ticket[1]);
36+
}
37+
List<List<String>> result = new LinkedList<>();
38+
dfs("JFK", result, map, new LinkedList<>(Arrays.asList("JFK")), tickets.length);
39+
return result;
40+
}
41+
42+
void dfs(String airport, List<List<String>> result, HashMap<String, ArrayList<String>> map, LinkedList<String> route, int n) {
43+
if (route.size() == n + 1) {
44+
result.add(new ArrayList<>(route));
45+
return;
46+
}
47+
if (!map.containsKey(airport) || map.get(airport).isEmpty()) {
48+
return;
49+
}
50+
ArrayList<String> list = map.get(airport);
51+
for (int i = 0; i < list.size(); i++) {
52+
String target = list.remove(i);
53+
54+
route.add(target);
55+
dfs(target, result, map, route, n);
56+
route.removeLast();
57+
58+
list.add(i, target);
59+
}
60+
}
61+
62+
/**
63+
* 题目要求是列出字母顺序最小的
64+
*/
65+
public List<String> findItinerary2(String[][] tickets) {
66+
HashMap<String, ArrayList<String>> map = new HashMap<>();
67+
for (String[] ticket : tickets) {
68+
map.computeIfAbsent(ticket[0], k -> new ArrayList<>()).add(ticket[1]);
69+
}
70+
for (List<String> list : map.values()) {
71+
Collections.sort(list);
72+
}
73+
LinkedList<String> result = new LinkedList<>(Arrays.asList("JFK"));
74+
dfs("JFK", map, result, tickets.length);
75+
return result;
76+
}
77+
78+
boolean dfs(String airport, HashMap<String, ArrayList<String>> map, LinkedList<String> route, int n) {
79+
if (route.size() == n + 1) {
80+
return true;
81+
}
82+
if (!map.containsKey(airport) || map.get(airport).isEmpty()) {
83+
return false;
84+
}
85+
ArrayList<String> list = map.get(airport);
86+
for (int i = 0; i < list.size(); i++) {
87+
String target = list.remove(i);
88+
89+
route.add(target);
90+
if (dfs(target, map, route, n)) {
91+
return true;
92+
}
93+
route.removeLast();
94+
95+
list.add(i, target);
96+
}
97+
return false;
98+
}
99+
100+
/**
101+
* 欧拉递归解法如下
102+
*/
103+
public List<String> findItinerary3(String[][] tickets) {
104+
for (String[] ticket : tickets)
105+
targets.computeIfAbsent(ticket[0], k -> new PriorityQueue()).add(ticket[1]);
106+
visit("JFK");
107+
return route;
108+
}
109+
110+
Map<String, PriorityQueue<String>> targets = new HashMap<>();
111+
List<String> route = new LinkedList();
112+
113+
void visit(String airport) {
114+
while (targets.containsKey(airport) && !targets.get(airport).isEmpty())
115+
visit(targets.get(airport).poll());
116+
route.add(0, airport);
117+
}
118+
119+
/**
120+
* 欧拉迭代写法
121+
*/
122+
public List<String> findItinerary4(String[][] tickets) {
123+
for (String[] ticket : tickets)
124+
targets.computeIfAbsent(ticket[0], k -> new PriorityQueue()).add(ticket[1]);
125+
126+
Map<String, PriorityQueue<String>> targets = new HashMap<>();
127+
List<String> route = new LinkedList();
128+
129+
Stack<String> stack = new Stack<>();
130+
stack.push("JFK");
131+
132+
while (!stack.isEmpty()) {
133+
String s = stack.peek();
134+
while (targets.containsKey(s) && !targets.get(s).isEmpty())
135+
stack.push(targets.get(s).poll());
136+
route.add(0, stack.pop());
137+
}
138+
139+
return route;
140+
}
141+
}

0 commit comments

Comments
 (0)