Skip to content

Commit ff7c465

Browse files
committed
作业
1 parent 01ec2d0 commit ff7c465

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

Week07/GoStairs.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.algorithm.ok.week_7;
2+
3+
/***
4+
* 70. 爬楼梯
5+
*/
6+
public class GoStairs {
7+
8+
public int climbStairs(int n) {
9+
int[] dp = new int[n + 1];
10+
dp[0] = 1;
11+
dp[1] = 1;
12+
for(int i = 2; i <= n; i++) {
13+
dp[i] = dp[i - 1] + dp[i - 2];
14+
}
15+
return dp[n];
16+
}
17+
18+
/***
19+
* 可知本题是斐波那契数列,那么用斐波那契数列的公式即可解决问题
20+
*/
21+
public int ss(int n) {
22+
double sqrt_5 = Math.sqrt(5);
23+
double fib_n = Math.pow((1 + sqrt_5) / 2, n + 1) - Math.pow((1 - sqrt_5) / 2,n + 1);
24+
return (int)(fib_n / sqrt_5);
25+
}
26+
}

Week07/Trie.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.algorithm.ok.week_7;
2+
3+
/***
4+
* 208. 实现 Trie (前缀树)
5+
* Trie trie = new Trie();
6+
*
7+
* trie.insert("apple");
8+
* trie.search("apple"); // 返回 true
9+
* trie.search("app"); // 返回 false
10+
* trie.startsWith("app"); // 返回 true
11+
* trie.insert("app");
12+
* trie.search("app"); // 返回 true
13+
*/
14+
public class Trie {
15+
TrieNode root;
16+
public Trie() {
17+
root = new TrieNode();
18+
}
19+
20+
/** Inserts a word into the trie. */
21+
public void insert(String word) {
22+
if(word == null || word.equals("")) return ;
23+
TrieNode node = root;
24+
for(int i = 0; i<word.length(); i++){
25+
char ch = word.charAt(i);
26+
if(!node.next.containsKey(ch)) {
27+
node.next.put(ch,new TrieNode());
28+
}
29+
node = node.next.get(ch);
30+
node.path++;
31+
}
32+
node.end++;
33+
34+
}
35+
36+
/** Returns if the word is in the trie. */
37+
public boolean search(String word) {
38+
if(word == null || word.equals("")) return false;
39+
TrieNode node = root;
40+
for(int i = 0; i<word.length(); i++){
41+
char ch = word.charAt(i);
42+
if(!node.next.containsKey(ch)) return false;
43+
node = node.next.get(ch);
44+
}
45+
if(node.end == 0) return false;
46+
return true;
47+
}
48+
49+
/** Returns if there is any word in the trie that starts with the given prefix. */
50+
public boolean startsWith(String word) {
51+
if(word == null || word.equals("")) return false;
52+
TrieNode node = root;
53+
for(int i = 0; i<word.length(); i++){
54+
char ch = word.charAt(i);
55+
if(!node.next.containsKey(ch)) return false;
56+
node = node.next.get(ch);
57+
}
58+
return true;
59+
}
60+
}

Week07/TrieNode.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.algorithm.ok.week_7;
2+
3+
import java.util.HashMap;
4+
5+
public class TrieNode{
6+
public int path;
7+
public int end;
8+
public HashMap<Character, TrieNode> next;
9+
10+
public TrieNode(){
11+
path = 0;
12+
end = 0;
13+
next = new HashMap<>();
14+
}
15+
}

0 commit comments

Comments
 (0)