Skip to content

Commit 3292641

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent ac0dd47 commit 3292641

File tree

4 files changed

+192
-1
lines changed

4 files changed

+192
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,12 @@
361361

362362
[207. Course Schedule](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/207.%20Course%20Schedule.md)
363363

364+
[208. Implement Trie (Prefix Tree)](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/208.%20Implement%20Trie%20(Prefix%20Tree).md)
365+
366+
[209. Minimum Size Subarray Sum](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/209.%20Minimum%20Size%20Subarray%20Sum.md)
367+
368+
[210. Course Schedule II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/210.%20Course%20Schedule%20II.md)
369+
364370
## Algorithm(4th_Edition)
365371
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
366372
All java realization codes are placed in different packages.

leetcode/208. Implement Trie (Prefix Tree).md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,76 @@ class Trie {
7979
* boolean param_2 = obj.search(word);
8080
* boolean param_3 = obj.startsWith(prefix);
8181
*/
82-
```
82+
```
83+
84+
### 二刷
85+
1. 最典型的字典树,将每个字符作为一个结点,我们通过遍历的方式,将一个字符串的所有字符添加进入当前的树中。
86+
2. 如果已经到了最后一个字符,我们将当前节点的isLeaf设置成true。
87+
88+
```Java
89+
class Trie {
90+
private static class TrieNode{
91+
boolean isLeaf;
92+
TrieNode[] childs;
93+
public TrieNode(){
94+
childs = new TrieNode[26];
95+
}
96+
}
97+
private TrieNode root;
98+
/** Initialize your data structure here. */
99+
public Trie() {
100+
this.root = new TrieNode();
101+
}
102+
103+
/** Inserts a word into the trie. */
104+
public void insert(String word) {
105+
int len = word.length();
106+
TrieNode temp = root;
107+
for(int i = 0; i < len; i++){
108+
int c = word.charAt(i) - 'a';
109+
if(temp.childs[c] == null){
110+
temp.childs[c] = new TrieNode();
111+
}
112+
temp = temp.childs[c];
113+
}
114+
temp.isLeaf = true;
115+
}
116+
117+
/** Returns if the word is in the trie. */
118+
public boolean search(String word) {
119+
if(word == null || word.length() == 0) return true;
120+
int len = word.length();
121+
TrieNode temp = this.root;
122+
for(int i = 0; i < len; i++){
123+
int c = word.charAt(i) - 'a';
124+
if(temp.childs[c] == null) return false;
125+
else temp = temp.childs[c];
126+
}
127+
return temp.isLeaf;
128+
}
129+
130+
/** Returns if there is any word in the trie that starts with the given prefix. */
131+
public boolean startsWith(String prefix) {
132+
if(prefix == null || prefix.length() == 0) return false;
133+
int len = prefix.length();
134+
TrieNode temp = root;
135+
for(int i = 0; i < len; i++){
136+
int c = prefix.charAt(i) - 'a';
137+
if(temp.childs[c] == null) return false;
138+
temp = temp.childs[c];
139+
}
140+
return true;
141+
}
142+
}
143+
144+
/**
145+
* Your Trie object will be instantiated and called as such:
146+
* Trie obj = new Trie();
147+
* obj.insert(word);
148+
* boolean param_2 = obj.search(word);
149+
* boolean param_3 = obj.startsWith(prefix);
150+
*/
151+
```
152+
153+
### Reference
154+
1. [Trie Tree 字典树](https://seanforfun.github.io/datastructure/2018/11/07/TrieTree.html)

leetcode/209. Minimum Size Subarray Sum.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,34 @@ class Solution {
6363
return min == Integer.MAX_VALUE ? 0: min;
6464
}
6565
}
66+
```
67+
68+
### 二刷
69+
1. 还是使用了双指针解决了这个问题。
70+
2. 如果sum >= s, 右移左指针。
71+
3. 如果sum < s, 右移右指针。
72+
```Java
73+
class Solution {
74+
public int minSubArrayLen(int s, int[] nums) {
75+
int len = nums.length;
76+
int left = -1, right = -1;
77+
int result = nums.length;
78+
int sum = 0;
79+
boolean flag = false;
80+
while(left < len){
81+
if(sum < s && right < len){
82+
if(++right >= len) break;
83+
sum += nums[right];
84+
}
85+
if(sum >= s){
86+
flag = true;
87+
result = Math.min(result, right - left);
88+
if(left < right){
89+
sum -= nums[++left];
90+
}else return 1;
91+
}
92+
}
93+
return flag ? result : 0;
94+
}
95+
}
6696
```

leetcode/210. Course Schedule II.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,87 @@ class Solution {
6868
return result;
6969
}
7070
}
71+
```
72+
73+
### 二刷
74+
1. Method 1: Khan 排序
75+
```Java
76+
class Solution {
77+
public int[] findOrder(int numCourses, int[][] prerequisites) {
78+
int[] indegree = new int[numCourses];
79+
Map<Integer, List<Integer>> pre = new HashMap<>();
80+
for(int[] p : prerequisites){
81+
indegree[p[0]]++;
82+
List<Integer> temp = pre.containsKey(p[1]) ? pre.get(p[1]) : new ArrayList<>();
83+
temp.add(p[0]);
84+
pre.put(p[1], temp);
85+
}
86+
int[] order = new int[numCourses];
87+
int count = 0;
88+
LinkedList<Integer> queue = new LinkedList<>();
89+
for(int i = 0; i < numCourses; i++){
90+
if(indegree[i] == 0){
91+
queue.add(i);
92+
}
93+
}
94+
while(!queue.isEmpty()){
95+
int cur = queue.poll();
96+
order[count++] = cur;
97+
if(pre.containsKey(cur)){
98+
List<Integer> temp = pre.get(cur);
99+
for(Integer t : temp){
100+
indegree[t]--;
101+
if(indegree[t] == 0) queue.add(t);
102+
}
103+
}
104+
}
105+
for(int i = 0; i < numCourses; i++){
106+
if(indegree[i] != 0){
107+
return new int[0];
108+
}
109+
}
110+
return order;
111+
}
112+
}
113+
```
114+
115+
2. Method 2: dfs
116+
```Java
117+
class Solution {
118+
List<Integer> order = new LinkedList<>();
119+
public int[] findOrder(int numCourses, int[][] prerequisites) {
120+
int[] indegree = new int[numCourses];
121+
Map<Integer, List<Integer>> pre = new HashMap<>();
122+
for(int[] p : prerequisites){
123+
indegree[p[0]]++;
124+
List<Integer> temp = pre.containsKey(p[1]) ? pre.get(p[1]) : new ArrayList<>();
125+
temp.add(p[0]);
126+
pre.put(p[1], temp);
127+
}
128+
boolean[] visited = new boolean[numCourses];
129+
for(int i = 0; i < numCourses; i++){
130+
if(!visited[i] && indegree[i] == 0)
131+
dfs(i, pre, indegree, visited);
132+
}
133+
for(boolean b : visited)
134+
if(!b) return new int[0];
135+
int[] orders = new int[numCourses];
136+
for(int i = 0; i < this.order.size(); i++){
137+
orders[i] = this.order.get(i);
138+
}
139+
return orders;
140+
}
141+
private void dfs(int cur, Map<Integer, List<Integer>> pre, int[] indegree, boolean[] visited){
142+
visited[cur] = true;
143+
this.order.add(cur);
144+
if(pre.containsKey(cur)){
145+
List<Integer> temp = pre.get(cur);
146+
for(Integer t :temp){
147+
if((--indegree[t]) == 0 && !visited[t]){
148+
dfs(t, pre, indegree, visited);
149+
}
150+
}
151+
}
152+
}
153+
}
71154
```

0 commit comments

Comments
 (0)