Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Week_04/id_74/LeetCode_211_74.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class WordDictionary {
public class TrieNode {
public char data;
public TrieNode[] children = new TrieNode[26];
public boolean isEndingChar = false;
public TrieNode(char data) {
this.data = data;
}
}

private TrieNode root;

/** Initialize your data structure here. */
public WordDictionary() {
root = new TrieNode('/');
}

/** Adds a word into the data structure. */
public void addWord(String word) {
TrieNode n = root;
for (char c : word.toCharArray()) {
int index = c - 'a';
if (n.children[index] == null) {
n.children[index] = new TrieNode(c);
}
n = n.children[index];
}
n.isEndingChar = true;
}

/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
public boolean search(String word) {
return match(word, 0, root);
}

private boolean match(String word, int start, TrieNode node) {
if (start == word.length()) {
return node.isEndingChar;
}

char c = word.charAt(start);
if (c == '.') {
for (TrieNode n : node.children) {
if (n != null) {
if (match(word, start + 1, n)) {
return true;
}
}
}
} else {
if (node.children[c - 'a'] == null) {
return false;
}
return match(word, start + 1, node.children[c - 'a']);
}

return false;
}
}

/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/
32 changes: 32 additions & 0 deletions Week_04/id_74/LeetCode_241_74.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.*;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释有点少,想学一下解题思路

class Solution {
public List<Integer> diffWaysToCompute(String input) {
List<Integer> result = new ArrayList<>();
for (int i = 0; i < input.length(); i++) {
char op = input.charAt(i);
if (op == '-' || op == '*' || op == '+') {
String p1 = input.substring(0, i);
String p2 = input.substring(i+1);
List<Integer> result1 = diffWaysToCompute(p1);
List<Integer> result2 = diffWaysToCompute(p2);

for (Integer r1 : result1) {
for (Integer r2 : result2) {
if (op == '+') {
result.add(r1 + r2);
} else if (op == '-') {
result.add(r1 - r2);
} else if (op == '*') {
result.add(r1 * r2);
}
}
}
}
}
if (result.size() == 0) {
result.add(Integer.valueOf(input));
}
return result;
}
}