From 43e5fae3399b27b6841226e808be84ea61a95027 Mon Sep 17 00:00:00 2001 From: null Date: Sun, 12 May 2019 06:16:17 +0800 Subject: [PATCH] =?UTF-8?q?074-week4=20=E7=AC=AC=E5=9B=9B=E5=91=A8?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_04/id_74/LeetCode_211_74.java | 66 ++++++++++++++++++++++++++++++ Week_04/id_74/LeetCode_241_74.java | 32 +++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 Week_04/id_74/LeetCode_211_74.java create mode 100644 Week_04/id_74/LeetCode_241_74.java diff --git a/Week_04/id_74/LeetCode_211_74.java b/Week_04/id_74/LeetCode_211_74.java new file mode 100644 index 00000000..f88020d8 --- /dev/null +++ b/Week_04/id_74/LeetCode_211_74.java @@ -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); + */ \ No newline at end of file diff --git a/Week_04/id_74/LeetCode_241_74.java b/Week_04/id_74/LeetCode_241_74.java new file mode 100644 index 00000000..43822c20 --- /dev/null +++ b/Week_04/id_74/LeetCode_241_74.java @@ -0,0 +1,32 @@ +import java.util.*; + +class Solution { + public List diffWaysToCompute(String input) { + List 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 result1 = diffWaysToCompute(p1); + List 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; + } +} \ No newline at end of file