Skip to content

Commit f08fa91

Browse files
committed
add more solutions
1 parent 893650b commit f08fa91

6 files changed

+281
-37
lines changed

173 Binary Search Tree Iterator.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
2+
3+
// Calling next() will return the next smallest number in the BST.
4+
5+
// Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.
6+
7+
// Credits:
8+
// Special thanks to @ts for adding this problem and creating all test cases.
9+
10+
// Hide Company Tags LinkedIn Google Facebook Microsoft
11+
// Hide Tags Tree Stack Design
12+
// Hide Similar Problems (M) Binary Tree Inorder Traversal (M) Flatten 2D Vector (M) Zigzag Iterator (M) Peeking Iterator (M) Inorder Successor in BST
13+
14+
15+
116
/**
217
* Definition for binary tree
318
* function TreeNode(val) {

209 Minimum Size Subarray Sum.js

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,58 @@
11
// http://blog.csdn.net/lisonglisonglisong/article/details/45666975
2+
// http://www.cnblogs.com/grandyang/p/4501934.html
3+
4+
// Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.
5+
6+
// For example, given the array [2,3,1,2,4,3] and s = 7,
7+
// the subarray [4,3] has the minimal length under the problem constraint.
8+
9+
// click to show more practice.
10+
11+
// Credits:
12+
// Special thanks to @Freezen for adding this problem and creating all test cases.
13+
14+
// Hide Company Tags Facebook
15+
// Hide Tags Array Two Pointers Binary Search
16+
// Hide Similar Problems (H) Minimum Window Substring (M) Maximum Size Subarray Sum Equals k
17+
218

319
/**
420
* @param {number} s
521
* @param {number[]} nums
622
* @return {number}
723
*/
24+
25+
// O(n) solution
826
var minSubArrayLen = function(s, nums) {
927
var sum = 0;
1028
var left = 0;
11-
var right = -1;
12-
var len = nums.length;
29+
var right = 0;
1330
var minLen = Infinity;
1431

15-
while(right < len) {
16-
while(right < len && sum < s) {
17-
sum += nums[++right];
18-
}
19-
if(sum >= s) {
20-
minLen = Math.min(right - left + 1, minLen);
21-
sum -= nums[left];
22-
left++;
32+
while(right < nums.length) {
33+
while(right < nums.length && sum < s) {
34+
sum += nums[right++];
2335
}
2436

37+
while(sum >= s) {
38+
minLen = Math.min(minLen, right - left);
39+
sum -= nums[left++];
40+
}
2541
}
2642

27-
28-
29-
return minLen > len ? 0 : minLen;
30-
};
43+
return minLen > nums.length ? 0 : minLen;
44+
};
45+
46+
// The O(NlogN) solution is to sum up the array
47+
// [1,2,3,4,5] becomes [1,3,6,10,15]
48+
// then iterate through array from index 0 to nums.length - 1
49+
// for each value in the summed array
50+
// binary search values after that index so the difference becomes greater than s
51+
// example
52+
// s = 8
53+
// at index 0 with value 1 look between [3,6,10,15] using binary search.
54+
// we can find that at value 10 the difference is 10 - 1 = 9 the minLen is index 3 - 1 + 1 = 3
55+
// then we check index 1 with value 3 and binary search [6,10,15] we can find that at value 15 we have difference 15 - 3 = 12
56+
// the distance is index 4 - 1 + 1 = 4
57+
58+
// console.log(minSubArrayLen(11, [1,2,3,4,5]));

282 Expression Add Operators.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or * between the digits so they evaluate to the target value.
2+
3+
// Examples:
4+
// "123", 6 -> ["1+2+3", "1*2*3"]
5+
// "232", 8 -> ["2*3+2", "2+3*2"]
6+
// "105", 5 -> ["1*0+5","10-5"]
7+
// "00", 0 -> ["0+0", "0-0", "0*0"]
8+
// "3456237490", 9191 -> []
9+
// Credits:
10+
// Special thanks to @davidtan1890 for adding this problem and creating all test cases.
11+
12+
// Hide Company Tags Google Facebook
13+
// Hide Tags Divide and Conquer
14+
// Hide Similar Problems (M) Evaluate Reverse Polish Notation (H) Basic Calculator (M) Basic Calculator II (M) Different Ways to Add Parentheses
15+
16+
17+
// reference: http://blog.csdn.net/pointbreak1/article/details/48596115
18+
19+
var addOperators = function(num, target) {
20+
function opRecur(num, target, lastOp, result, expression, results) {
21+
if(num.length === 0) {
22+
if(target === result) {
23+
results.push(expression);
24+
}
25+
return;
26+
}
27+
28+
for(var i = 1; i <= num.length; i++) {
29+
var curr = num.substring(0, i);
30+
if(curr.length > 1 && curr[0] === '0') {
31+
continue;
32+
}
33+
34+
var rest = num.substring(i);
35+
var currVal = parseInt(curr);
36+
37+
if(expression.length === 0) {
38+
opRecur(rest, target, currVal, currVal, expression + curr, results);
39+
} else {
40+
opRecur(rest, target, currVal, result + currVal, expression + "+" + curr, results);
41+
opRecur(rest, target,-currVal, result - currVal, expression + "-" + curr, results);
42+
opRecur(rest, target, currVal * lastOp, result - lastOp + lastOp * currVal, expression + "*" + curr, results);
43+
}
44+
}
45+
}
46+
47+
var results = [];
48+
opRecur(num, target, 0, 0, '', results);
49+
return results;
50+
};
51+
52+
console.log(addOperators('01023', 3));

311 Sparse Matrix Multiplication.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Given two sparse matrices A and B, return the result of AB.
2+
3+
// You may assume that A's column number is equal to B's row number.
4+
5+
// Example:
6+
7+
// A = [
8+
// [ 1, 0, 0],
9+
// [-1, 0, 3]
10+
// ]
11+
12+
// B = [
13+
// [ 7, 0, 0 ],
14+
// [ 0, 0, 0 ],
15+
// [ 0, 0, 1 ]
16+
// ]
17+
18+
19+
// | 1 0 0 | | 7 0 0 | | 7 0 0 |
20+
// AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
21+
// | 0 0 1 |
22+
// Hide Company Tags LinkedIn Facebook
23+
// Hide Tags Hash Table
24+
25+
26+
/**
27+
* @param {number[][]} A
28+
* @param {number[][]} B
29+
* @return {number[][]}
30+
*/
31+
32+
// normal matrix mulitplication
33+
// slower version
34+
var multiply = function(A, B) {
35+
var result = [];
36+
37+
var rowA = A.length;
38+
var colA = A[0].length;
39+
var rowB = B.length;
40+
var colB = B[0].length
41+
42+
for(var i = 0; i < rowA; i++) {
43+
result.push(Array(colB).fill(0));
44+
45+
for(var j = 0; j < colB; j++) {
46+
47+
for(var k = 0; k < colA; k++) {
48+
result[i][j] += A[i][k]*B[k][j]
49+
}
50+
51+
}
52+
}
53+
54+
return result;
55+
};
56+
57+
// faster
58+
// skip
59+
multiply = function(A, B) {
60+
var result = [];
61+
var i,j,k;
62+
63+
var rowA = A.length;
64+
var colA = A[0].length;
65+
var colB = B[0].length
66+
67+
for(var i = 0; i < rowA; i++) {
68+
result.push(Array(colB).fill(0));
69+
}
70+
71+
for(i = 0; i < rowA; i++) {
72+
for(k = 0; k < colA; k++) {
73+
if(A[i][k] !== 0) {
74+
for(j = 0; j < colB; j++) {
75+
if(B[k][j] !== 0) {
76+
result[i][j] += A[i][k]*B[k][j];
77+
}
78+
}
79+
}
80+
}
81+
}
82+
83+
return result;
84+
};
85+
86+
87+
88+
// var data1 = [[0,1],[0,0],[0,1]];
89+
// var data2 = [[1,0],[1,0]];
90+
91+
// var data1 = [[1,0,0],[-1,0,3]];
92+
// var data2 = [[7,0,0],[0,0,0],[0,0,1]];
93+
94+
var data1 = [[1,-5]];
95+
var data2 = [[12],[-1]];
96+
97+
console.log(multiply(data1,data2));
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead.
2+
3+
// Example 1:
4+
// Given nums = [1, -1, 5, -2, 3], k = 3,
5+
// return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is the longest)
6+
7+
// Example 2:
8+
// Given nums = [-2, -1, 2, 1], k = 1,
9+
// return 2. (because the subarray [-1, 2] sums to 1 and is the longest)
10+
11+
// Follow Up:
12+
// Can you do it in O(n) time?
13+
14+
// Hide Company Tags Palantir Facebook
15+
// Hide Tags Hash Table
16+
// Hide Similar Problems (M) Minimum Size Subarray Sum (E) Range Sum Query - Immutable
17+
18+
19+
/**
20+
* @param {number[]} nums
21+
* @param {number} k
22+
* @return {number}
23+
*/
24+
var maxSubArrayLen = function(nums, k) {
25+
var maxLen = 0;
26+
var currSum = 0;
27+
var dict = { 0: -1 };
28+
29+
for(var i = 0; i < nums.length; i++) {
30+
currSum += nums[i];
31+
32+
// since we are looking for the maxlen, dict is used to store the very first
33+
// location where currSum occurred
34+
if(dict[currSum] === undefined) {
35+
dict[currSum] = i;
36+
}
37+
38+
if(dict[currSum - k] !== undefined) {
39+
maxLen = Math.max(maxLen, i - dict[currSum - k]);
40+
}
41+
}
42+
43+
return maxLen;
44+
};

43 Multiply Strings.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,52 @@
1+
// Given two numbers represented as strings, return multiplication of the numbers as a string.
2+
3+
// Note:
4+
// The numbers can be arbitrarily large and are non-negative.
5+
// Converting the input string to integer is NOT allowed.
6+
// You should NOT use internal library such as BigInteger.
7+
// Hide Company Tags Facebook Twitter
8+
// Hide Tags Math String
9+
// Hide Similar Problems (M) Add Two Numbers (E) Plus One (E) Add Binary
10+
111
/**
212
* @param {string} num1
313
* @param {string} num2
414
* @return {string}
515
*/
616
var multiply = function(num1, num2) {
7-
if(num1 === null || num2 === null || num1.length === 0 || num2.length === 0){
8-
return 0;
17+
if(num1 === null || num2 === null || num1.length === 0 || num2.length === 0 || num1 === '0' || num2 === '0') {
18+
return '0';
919
}
1020

1121
var arr1 = num1.split('').reverse();
1222
var arr2 = num2.split('').reverse();
13-
1423
var result = [];
1524

16-
for(var i = 0; i < arr1.length; i++){
25+
for(var i = 0; i < arr1.length; i++) {
1726
var carry = 0;
27+
var val1 = parseInt(arr1[i]);
1828

19-
for(var j = 0; j < arr2.length; j++){
20-
var n1 = parseInt(arr1[i]);
21-
var n2 = parseInt(arr2[j]);
22-
var exist = parseInt(result[i+j] || 0);
23-
var total = n1*n2+carry+exist;
24-
25-
var remain = total%10 + '';
26-
carry = parseInt(total/10);
27-
result[i+j] = remain;
29+
for(var j = 0; j < arr2.length; j++) {
30+
var val2 = parseInt(arr2[j]);
31+
var product = val1*val2 + carry;
32+
var exist = result[i+j] || 0;
33+
var sum = product + exist;
34+
var digit = sum%10;
35+
carry = Math.floor(sum/10);
36+
result[i+j] = digit;
2837
}
2938

30-
if(carry > 0){
31-
result[i + j] = carry + '';
39+
if(carry > 0) {
40+
result[i+j] = carry;
3241
}
3342
}
3443

35-
result = result.reverse();
44+
result.reverse();
3645
result = result.join('');
37-
result = result.replace(/^0+/g,'');
46+
result = result.replace(/^0+/, '');
3847

39-
if(result.length === 0){
40-
return "0";
41-
} else {
42-
return result;
43-
}
44-
};
48+
49+
return result;
50+
};
51+
52+
multiply('123', '456')

0 commit comments

Comments
 (0)