Leetcode(JAVA)
題目 : https://leetcode.com/problems/design-add-and-search-words-data-structure/ 。
想法 :
Trie + Backtrack。
時間複雜度 :
程式碼 : (JAVA)
class WordDictionary {
class Node{
Node[] child;
boolean isEnd;
Node(){
child = new Node[26];
}
}
Node root;
public WordDictionary() {
root = new Node();
}
public void addWord(String word) {
Node curr = root;
for(int i = 0 ; i < word.length() ; i++){
char ch = word.charAt(i);
if(curr.child[ch - 'a'] == null){
curr.child[ch - 'a'] = new Node();
}
curr = curr.child[ch - 'a'];
}
curr.isEnd = true;
}
public boolean travel(Node node, String word, int index){
if(node == null) return false;
if(index == word.length()) return node.isEnd;
if(word.charAt(index) == '.'){
for(int i = 0 ; i < 26 ; i++){
if(travel(node.child[i], word, index + 1)) return true;
}
return false;
}
else{
return travel(node.child[word.charAt(index) - 'a'], word, index + 1);
}
}
public boolean search(String word) {
Node curr = root;
return travel(root, word, 0);
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/
題目 : https://leetcode.com/problems/richest-customer-wealth/ 。 想法 : 語法。 時間複雜度 : O(n*m)。 程式碼 : (JAVA)
Feb 5, 2022題目 : https://leetcode.com/problems/coin-change-2/ 。 想法 : 無窮背包問題。 時間複雜度 : O(n*m)。 程式碼 : (JAVA)
Feb 5, 2022題目 : https://leetcode.com/problems/integer-break/ 。 想法 : 規律就在2跟3身上(?),把一個數字分解成2跟3的總和就好。 時間複雜度 : O(1)。 程式碼 : (JAVA)
Feb 5, 2022題目 : https://leetcode.com/problems/longest-common-subsequence/ 。 想法 : LCS。 if(str[i] == str[j]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j], dp[i][j-1]);
Feb 5, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up