Skip to content

Commit 862fe46

Browse files
author
Chihung Yu
committed
push new code
1 parent 738211f commit 862fe46

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Leetcode #144
2+
// Language: Javascript
3+
// Problem: https://leetcode.com/problems/binary-tree-preorder-traversal/
4+
// Author: Chihung Yu
5+
/**
6+
* Definition for a binary tree node.
7+
* function TreeNode(val) {
8+
* this.val = val;
9+
* this.left = this.right = null;
10+
* }
11+
*/
12+
/**
13+
* @param {TreeNode} root
14+
* @return {number[]}
15+
*/
16+
17+
var preorderTraversal = function(root) {
18+
var result = [];
19+
20+
traverse(root, result);
21+
22+
return result;
23+
};
24+
25+
function traverse(node, result) {
26+
if(!node) {
27+
return;
28+
}
29+
30+
result.push(node.val);
31+
32+
traverse(node.left, result);
33+
traverse(node.right, result);
34+
}

268 Missing Number.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Leetcode 268
2+
// Language: Javascript
3+
// Problem: https://leetcode.com/problems/missing-number/
4+
// Author: Chihung Yu
5+
/**
6+
* @param {number[]} nums
7+
* @return {number}
8+
*/
9+
10+
var missingNumber = function(nums) {
11+
var res = 0;
12+
13+
// nums = [0, 1, 3, 4, 5]
14+
// index go from 1 to 5
15+
// res starts with 0
16+
// perform xor so that index and num will cancel out leaving the odd num alone
17+
for(var i = 1; i <= nums.length; i++) {
18+
res = res ^ i ^ nums[i - 1];
19+
}
20+
21+
return res;
22+
};

27 Remove Element.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Leetcode 27
2+
// Language: Javascript
3+
// Problem: https://leetcode.com/problems/remove-element/
4+
// Author: Chihung Yu
15
/**
26
* @param {number[]} A
37
* @param {number} elem
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Leetcode 318
2+
// Language: Javascript
3+
// Problem: https://leetcode.com/problems/maximum-product-of-word-lengths/
4+
// Author: Chihung Yu
5+
/**
6+
* @param {string[]} words
7+
* @return {number}
8+
*/
9+
var maxProduct = function(words) {
10+
var processed = [];
11+
12+
for(var i = 0; i < words.length; i++) {
13+
processed.push(compute(words[i]));
14+
}
15+
16+
var result = [];
17+
var max = 0;
18+
19+
for(i = 0; i < words.length; i++) {
20+
for(var j = i + 1; j < words.length; j++) {
21+
if((processed[i] & processed[j]) === 0) {
22+
var cur = words[i].length * words[j].length;
23+
24+
if(cur > max) {
25+
max = cur;
26+
}
27+
}
28+
}
29+
}
30+
31+
return max;
32+
};
33+
34+
function compute(word) {
35+
var val = 0;
36+
var base = "a".charCodeAt(0);
37+
38+
for(i = 0; i < word.length; i++){
39+
val |= (1 << (word.charCodeAt(i) - base));
40+
41+
}
42+
return val;
43+
}

0 commit comments

Comments
 (0)