Leetcode, leetcodeoj
Two methods are used:
(1) I tried to write a Prefix Tree, 352 ms.
The largest trick is to use an array boolean [] searchFlag to record the substring starting from s [I] (s [I... n-1]) whether it has been searched (s [I... n-1] can it be composed of dict words ). If it is searched, you do not need to search again (because the search fails, if it is successful, the program has already ended ).
import java.util.*;public class Solution {class TrieNode {char ch;ArrayList<TrieNode> children = new ArrayList<TrieNode>();TrieNode(char c) {ch = c;}TrieNode() {ch = '\0';}}public void insert(String word, int ind, TrieNode node) {if (ind == word.length()) {TrieNode nNode = new TrieNode('\0');node.children.add(nNode);return;}int i;for (i = 0; i < node.children.size(); i++) {if (node.children.get(i).ch == word.charAt(ind)) {insert(word, ind + 1, node.children.get(i));break;}}if (i == node.children.size()) {TrieNode nNode = new TrieNode(word.charAt(ind));node.children.add(nNode);insert(word, ind + 1, nNode);}}public boolean search(String s, int ind, TrieNode root, TrieNode node, boolean[] searchFlag) {if (ind == s.length()) {for (int i = 0; i < node.children.size(); i++)if (node.children.get(i).ch == '\0')return true;return false;}if(node == root && searchFlag[ind] == true)//the sub string[i...n-1] has been searched beforereturn false;int i;boolean flag = false;for (i = 0; i < node.children.size(); i++) {if (node.children.get(i).ch == '\0') {// end of word, try to search a new word againflag = search(s, ind, root, root, searchFlag);searchFlag[ind] =true;} else if (node.children.get(i).ch == s.charAt(ind)) {// go on iterate the wordflag = search(s, ind + 1, root, node.children.get(i), searchFlag);}if (flag == true)return true;}return false;}public boolean wordBreak(String s, Set<String> dict) {TrieNode root = new TrieNode();for (String word : dict) {insert(word, 0, root);}boolean[] searchFlag = new boolean[s.length()];for(int i=0;i<s.length();i++)searchFlag[i] = false;boolean flag = search(s, 0, root, root, searchFlag);return flag;}public static void main(String args[]) {Solution sol = new Solution();String s = "abcd";Set<String> dict = new HashSet<String>(Arrays.asList("a","abc","b","cd"));System.out.println(sol.wordBreak(s, dict));}}
(2) search directly in the set without using the Prefix Tree, which is faster due to the small amount of data. 336 ms
public class Solution {public boolean search(String s, int beg, Set<String> dict, boolean[] searchFlag){if(beg == s.length())return true;if(searchFlag[beg] == true)return false;boolean flag = false;for(int e = beg;e<=s.length();e++){String subStr = s.substring(beg,e);if(dict.contains(subStr)){flag = search(s,e,dict,searchFlag);if(flag == true)return true;searchFlag[e] = true;}}return false;}public boolean wordBreak(String s, Set<String> dict) {boolean[] searchFlag = new boolean[s.length()+1];for(int i=0;i<s.length()-1;i++)searchFlag[i] = false;boolean flag = search(s,0,dict, searchFlag);return flag;}}
What is leetcode?
There are many programming and interview questions, which can be compiled and run online. It is difficult. If you can do it all by yourself, it is very helpful for large companies. I just did the questions there.
How does leetcode oj submit code?
You cannot write the main function. What you need is to implement a member function based on the interface provided by class Solution.
Give a reference answer
# Include <sstream> using namespace std; class Solution {public: void reverseWords (string & s) {string ans = "", temp; stringstream sin (s ); while (sin> temp) {if (ans! = "") {Ans = temp + "" + ans;} else {ans = temp ;}} s = ans ;}};