forked from chihungyu1116/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chihung Yu
committed
Jan 21, 2016
1 parent
73fca78
commit a12c922
Showing
4 changed files
with
300 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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,86 @@ | ||
/** | ||
* @constructor | ||
* Initialize your data structure here. | ||
*/ | ||
// http://www.cnblogs.com/Liok3187/p/4626730.html | ||
var TrieNode = function(key) { | ||
return { | ||
key: key, | ||
isWord: false, | ||
children: {} | ||
} | ||
}; | ||
|
||
var Trie = function() { | ||
this.root = TrieNode(); | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {void} | ||
* Inserts a word into the trie. | ||
*/ | ||
Trie.prototype.insert = function(word) { | ||
var tree = this.root; | ||
var i, curr; | ||
|
||
for(i = 0; i < word.length; i++) { | ||
curr = word[i]; | ||
if(!tree.children[curr]) { | ||
tree.children[curr] = TrieNode(curr); | ||
} | ||
tree = tree.children[curr]; | ||
} | ||
|
||
tree.isWord = true; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {boolean} | ||
* Returns if the word is in the trie. | ||
*/ | ||
Trie.prototype.search = function(word) { | ||
var tree = this.root; | ||
|
||
for(var i = 0; i < word.length; i++) { | ||
var curr = word[i]; | ||
|
||
if(!tree.children[curr]) { | ||
return false; | ||
} | ||
|
||
tree = tree.children[curr]; | ||
} | ||
|
||
return tree.isWord; | ||
}; | ||
|
||
/** | ||
* @param {string} prefix | ||
* @return {boolean} | ||
* Returns if there is any word in the trie | ||
* that starts with the given prefix. | ||
*/ | ||
Trie.prototype.startsWith = function(prefix) { | ||
var tree = this.root; | ||
|
||
for(var i = 0; i < prefix.length; i++) { | ||
var curr = prefix[i]; | ||
|
||
if(!tree.children[curr]) { | ||
return false; | ||
} | ||
|
||
tree = tree.children[curr]; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
/** | ||
* Your Trie object will be instantiated and called as such: | ||
* var trie = new Trie(); | ||
* trie.insert("somestring"); | ||
* trie.search("key"); | ||
*/ |
183 changes: 183 additions & 0 deletions
183
297 Serialize and Deserialize Binary Tree My Submissions Question.js
This file contains 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,183 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val) { | ||
* this.val = val; | ||
* this.left = this.right = null; | ||
* } | ||
*/ | ||
|
||
/** | ||
* Encodes a tree to a single string. | ||
* | ||
* @param {TreeNode} root | ||
* @return {string} | ||
*/ | ||
// http://fisherlei.blogspot.com/2013/03/interview-serialize-and-de-serialize.html | ||
var serialize = function(root) { | ||
var result = []; | ||
serializer(root, result); | ||
|
||
return result.join(","); | ||
}; | ||
|
||
var serializer = function(node, output) { | ||
if(node === null) { | ||
output.push("#"); | ||
return; | ||
} | ||
|
||
output.push(node.val); | ||
serializer(node.left, output); | ||
serializer(node.right, output); | ||
} | ||
|
||
/** | ||
* Decodes your encoded data to tree. | ||
* | ||
* @param {string} data | ||
* @return {TreeNode} | ||
*/ | ||
var deserialize = function(data) { | ||
data = data.split(","); | ||
var index = 0; | ||
|
||
function deserializer(data) { | ||
if(index > data.length || data[index] === "#") { | ||
return null; | ||
} | ||
|
||
var node = new TreeNode(parseInt(data[index])); | ||
index++; | ||
node.left = deserializer(data, index); | ||
index++; | ||
|
||
node.right = deserializer(data, index); | ||
return node; | ||
} | ||
|
||
return deserializer(data); | ||
}; | ||
|
||
|
||
|
||
/** | ||
* Your functions will be called as such: | ||
* deserialize(serialize(root)); | ||
*/ | ||
|
||
|
||
/* | ||
* Definition for a binary tree node. | ||
* function TreeNode(val) { | ||
* this.val = val; | ||
* this.left = this.right = null; | ||
* } | ||
*/ | ||
|
||
/** | ||
* Encodes a tree to a single string. | ||
* | ||
* @param {TreeNode} root | ||
* @return {string} | ||
*/ | ||
// var serialize = function(root) { | ||
// if(!root) { | ||
// return ""; | ||
// } | ||
|
||
// var result = []; | ||
// var curLvl = []; | ||
// var nextLvl = []; | ||
// curLvl.push(root); | ||
|
||
// while(curLvl.length) { | ||
// var node = curLvl.shift(); | ||
|
||
// if(node) { | ||
// result.push(node.val); | ||
|
||
// if(node.left) { | ||
// nextLvl.push(node.left); | ||
// } else { | ||
// nextLvl.push(null); | ||
// } | ||
|
||
// if(node.right) { | ||
// nextLvl.push(node.right); | ||
// } else { | ||
// nextLvl.push(null); | ||
// } | ||
// } else { | ||
// result.push(null); | ||
// } | ||
|
||
// if(curLvl.length === 0) { | ||
// curLvl = nextLvl; | ||
// nextLvl = []; | ||
// } | ||
// } | ||
|
||
// console.log('serialize: ',result) | ||
|
||
// return result.join(','); | ||
// }; | ||
|
||
/** | ||
* Decodes your encoded data to tree. | ||
* | ||
* @param {string} data | ||
* @return {TreeNode} | ||
*/ | ||
// var deserialize = function(data) { | ||
// if(data === "") { | ||
// return null | ||
// } | ||
|
||
// data = data.split(','); | ||
|
||
// var val = data.shift(); | ||
// var root = new TreeNode(val); | ||
|
||
// var curLvlCnt = 1; | ||
// var nextLvlCnt = 0; | ||
// var lvl = []; | ||
// lvl.push(root); | ||
|
||
// while(data.length) { | ||
// var node = lvl.shift(); | ||
// curLvlCnt--; | ||
|
||
// var leftVal = data.shift(); | ||
// var rightVal = data.shift(); | ||
|
||
// if(leftVal) { | ||
// node.left = new TreeNode(leftVal); | ||
// nextLvlCnt++; | ||
// lvl.push(node.left); | ||
// } else { | ||
// node.left = null; | ||
// } | ||
|
||
// if(rightVal) { | ||
// node.right = new TreeNode(rightVal); | ||
// nextLvlCnt++; | ||
// lvl.push(node.right); | ||
// } else { | ||
// node.right = null; | ||
// } | ||
|
||
// if(curLvlCnt === 0) { | ||
// curLvlCnt = nextLvlCnt; | ||
// nextLvlCnt = 0; | ||
// } | ||
// } | ||
|
||
// console.log('deserialize: ',root) | ||
|
||
// return root; | ||
// }; | ||
|
||
/** | ||
* Your functions will be called as such: | ||
* deserialize(serialize(root)); | ||
*/ |
This file contains 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,31 @@ | ||
/** | ||
* @param {number[]} coins | ||
* @param {number} amount | ||
* @return {number} | ||
*/ | ||
var coinChange = function(coins, amount) { | ||
var dp = [0]; | ||
for(var i = 1; i <= amount; i++) { | ||
dp.push(-1); | ||
} | ||
|
||
for(var a = 0; a < amount; a++) { | ||
if(dp[a] < 0) { | ||
continue; | ||
} | ||
|
||
for(var c = 0; c < coins.length; c++) { | ||
var coin = coins[c]; | ||
|
||
if((a + coin) > amount) { | ||
continue; | ||
} | ||
|
||
if(dp[a + coin] < 0 || dp[a + coin] > dp[a] + 1) { | ||
dp[a+coin] = dp[a] + 1; | ||
} | ||
} | ||
} | ||
|
||
return dp[amount]; | ||
}; |