-
Notifications
You must be signed in to change notification settings - Fork 149
074-week4 第四周作业 #678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
074-week4 第四周作业 #678
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import java.util.*; | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
注释有点少,想学一下解题思路