Skip to content

Commit 067b35b

Browse files
committed
add more solutions
1 parent f77e6fd commit 067b35b

10 files changed

+499
-50
lines changed

150 Evaluate Reverse Polish Notation.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ var evalRPN = function(tokens) {
3939
}
4040

4141
return stack.pop();
42-
};
42+
};
43+
44+
45+
console.log(evalRPN([ 12, 12, 12, '*', '+', 3, 4, '-', '+' ] ));
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* initialize your data structure here
3+
* @constructor
4+
*/
5+
var TwoSum = function() {
6+
this.hashmap = new Map();
7+
};
8+
9+
/**
10+
* Add the number to an internal data structure.
11+
* @param {number} input
12+
* @returns {void}
13+
*/
14+
TwoSum.prototype.add = function(input) {
15+
this.hashmap[input] = this.hashmap[input] || 0;
16+
this.hashmap[input]++;
17+
};
18+
19+
/**
20+
* Find if there exists any pair of numbers which sum is equal to the value.
21+
* @param {number} val
22+
* @returns {boolean}
23+
*/
24+
TwoSum.prototype.find = function(val) {
25+
for(var key in this.hashmap) {
26+
var diff = val - parseInt(key);
27+
28+
if(diff === parseInt(key)){
29+
if(this.hashmap[diff] >= 2) {
30+
return true;
31+
}
32+
} else if(this.hashmap[diff] >= 1) {
33+
return true;
34+
}
35+
}
36+
37+
return false;
38+
};
39+
40+
/**
41+
* Your TwoSum object will be instantiated and called as such:
42+
* var twoSum = new TwoSum();
43+
* twoSum.add(number);
44+
* twoSum.find(value);
45+
*/

205 Isomorphic Strings.js

+32-19
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,49 @@
1-
// Leetcode #205
2-
// Language: Javascript
3-
// Problem: https://leetcode.com/problems/isomorphic-strings/
4-
// Author: Chihung Yu
1+
// Given two strings s and t, determine if they are isomorphic.
2+
3+
// Two strings are isomorphic if the characters in s can be replaced to get t.
4+
5+
// All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
6+
7+
// For example,
8+
// Given "egg", "add", return true.
9+
10+
// Given "foo", "bar", return false.
11+
12+
// Given "paper", "title", return true.
13+
14+
// Note:
15+
// You may assume both s and t have the same length.
16+
17+
// Hide Company Tags LinkedIn
18+
// Hide Tags Hash Table
19+
// Hide Similar Problems (E) Word Pattern
20+
21+
22+
523
/**
624
* @param {string} s
725
* @param {string} t
826
* @return {boolean}
927
*/
1028
var isIsomorphic = function(s, t) {
11-
if(s === null || t === null){
12-
return true;
29+
if(s.length !== t.length) {
30+
return false;
1331
}
1432

1533
var hash1 = {};
1634
var hash2 = {};
1735

18-
for(var i = 0; i < s.length; i++){
19-
var sc = s[i];
20-
var tc = t[i];
21-
22-
hash1[sc] = tc;
23-
hash2[tc] = sc;
36+
for(var i = 0; i < s.length; i++) {
37+
hash1[s[i]] = t[i];
38+
hash2[t[i]] = s[i];
2439
}
2540

26-
var result1 = "";
27-
var result2 = "";
41+
var result1 = '';
42+
var result2 = '';
2843

29-
for(i = 0; i < s.length; i++){
30-
sc = s[i]
31-
tc = t[i];
32-
result1 += hash1[sc];
33-
result2 += hash2[tc];
44+
for(i = 0; i < s.length; i++) {
45+
result1 += hash1[s[i]];
46+
result2 += hash2[t[i]];
3447
}
3548

3649
return result1 === t && result2 === s;

224 Basic Calculator.js

+91-28
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,102 @@ Note: Do not use the eval built-in library function.
1818
* @param {string} s
1919
* @return {number}
2020
*/
21-
var calculate = function(s) {
22-
var stack = [],
23-
len = s.length,
24-
sum = 0,
25-
num,
26-
ch,
27-
j,
28-
i;
21+
// var calculate = function(s) {
22+
// var stack = [],
23+
// len = s.length,
24+
// sum = 0,
25+
// num,
26+
// ch,
27+
// j,
28+
// i;
2929

30-
stack.push(1);
31-
stack.push(1);
30+
// stack.push(1);
31+
// stack.push(1);
3232

33-
for (i = 0; i < len; i++) {
34-
ch = s.charAt(i);
33+
// for (i = 0; i < len; i++) {
34+
// ch = s.charAt(i);
3535

36-
if (!isNaN(parseInt(ch))) {
37-
num = parseInt(ch);
36+
// if (!isNaN(parseInt(ch))) {
37+
// num = parseInt(ch);
3838

39-
for (j = i + 1; j < len && !isNaN(parseInt(s.charAt(j))); j++) {
40-
num = num * 10 + parseInt(s.charAt(j));
41-
}
39+
// for (j = i + 1; j < len && !isNaN(parseInt(s.charAt(j))); j++) {
40+
// num = num * 10 + parseInt(s.charAt(j));
41+
// }
4242

43-
sum += stack.pop() * num;
43+
// sum += stack.pop() * num;
4444

45-
i = j - 1;
46-
} else if (ch === '+' || ch === '(') {
47-
stack.push(stack[stack.length - 1]);
48-
} else if (ch === '-') {
49-
stack.push(stack[stack.length - 1] * (-1));
50-
} else if (ch === ')') {
51-
stack.pop();
45+
// i = j - 1;
46+
// } else if (ch === '+' || ch === '(') {
47+
// stack.push(stack[stack.length - 1]);
48+
// } else if (ch === '-') {
49+
// stack.push(stack[stack.length - 1] * (-1));
50+
// } else if (ch === ')') {
51+
// stack.pop();
52+
// }
53+
// }
54+
55+
// return sum;
56+
// };
57+
58+
59+
60+
/**
61+
Implement a basic calculator to evaluate a simple expression string.
62+
63+
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
64+
65+
You may assume that the given expression is always valid.
66+
67+
Some examples:
68+
"1 + 1" = 2
69+
" 2-1 + 2 " = 3
70+
"(1+(4+5+2)-3)+(6+8)" = 23
71+
Note: Do not use the eval built-in library function.
72+
*/
73+
/**
74+
* @param {string} s
75+
* @return {number}
76+
*/
77+
78+
79+
80+
console.log(calculate("10+20-((2-4)*6-5)*6"));
81+
var calculate = function(s) {
82+
83+
84+
85+
86+
87+
}
88+
89+
function helper(s, i, sign, finalSum, currentSum) {
90+
while(i < s.length) {
91+
if(s[i].match(/[0-9]/)) {
92+
var num = 0;
93+
94+
while(i < s.length) {
95+
if(s[i].match(/[0-9]/)) {
96+
num *= 10;
97+
num += parseInt(s[i]);
98+
i++
99+
} else {
100+
break;
101+
}
102+
}
103+
} else if(s[i] === '-') {
104+
105+
} else if(s[i] === '+') {
106+
return num + helper(s, i, sign, finalSum, currentSum);
107+
} else if(s[i] === '*') {
108+
109+
} else if(s[i] === '/') {
110+
111+
} else if(s[i] === '(') {
112+
113+
} else if(s[i] === ')') {
114+
52115
}
116+
117+
i++;
53118
}
54-
55-
return sum;
56-
};
119+
}

261 Graph Valid Tree.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var validTree = function(n, edges) {
3232
}
3333
}
3434

35-
return unionFind.unionCount() === 1;
35+
return unionFind.count() === 1;
3636
};
3737

3838
// reference: http://blog.csdn.net/dm_vincent/article/details/7655764
@@ -75,7 +75,7 @@ class UnionFind {
7575
return this.find(m) === this.find(n);
7676
}
7777

78-
unionCount() {
78+
count() {
7979
return this.count;
8080
}
8181
}

349 Intersection of Two Arrays.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Given two arrays, write a function to compute their intersection.
2+
3+
// Example:
4+
// Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
5+
6+
// Note:
7+
// Each element in the result must be unique.
8+
// The result can be in any order.
9+
// Hide Tags Binary Search Hash Table Two Pointers Sort
10+
// Hide Similar Problems (E) Intersection of Two Arrays II
11+
12+
13+
/**
14+
* @param {number[]} nums1
15+
* @param {number[]} nums2
16+
* @return {number[]}
17+
*/
18+
var intersection = function(nums1, nums2) {
19+
var hash = {};
20+
var result = [];
21+
var i = 0;
22+
while(i < nums1.length || i < nums2.length) {
23+
if(i < nums1.length) {
24+
hash[nums1[i]] = hash[nums1[i]] || [];
25+
hash[nums1[i]][0] = true;
26+
}
27+
28+
if(i < nums2.length) {
29+
hash[nums2[i]] = hash[nums2[i]] || [];
30+
hash[nums2[i]][1] = true;
31+
}
32+
33+
i++
34+
}
35+
36+
for(i in hash) {
37+
if(hash[i][0] && hash[i][1]) {
38+
result.push(parseInt(i));
39+
}
40+
}
41+
42+
return result;
43+
};

350 Intersection of Two Arrays II.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Given two arrays, write a function to compute their intersection.
2+
3+
// Example:
4+
// Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
5+
6+
// Note:
7+
// Each element in the result should appear as many times as it shows in both arrays.
8+
// The result can be in any order.
9+
// Follow up:
10+
// What if the given array is already sorted? How would you optimize your algorithm?
11+
// What if nums1's size is small compared to nums2's size? Which algorithm is better?
12+
// What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
13+
// Hide Tags Binary Search Hash Table Two Pointers Sort
14+
// Hide Similar Problems (E) Intersection of Two Arrays
15+
16+
17+
18+
/**
19+
* @param {number[]} nums1
20+
* @param {number[]} nums2
21+
* @return {number[]}
22+
*/
23+
var intersect = function(nums1, nums2) {
24+
var hash = {};
25+
var arr1, arr2;
26+
27+
if(nums1.length > nums2.length) {
28+
arr1 = nums2;
29+
arr2 = nums1;
30+
} else {
31+
arr1 = nums1;
32+
arr2 = nums2;
33+
}
34+
35+
var count = arr1.length;
36+
var result = [];
37+
38+
for(var i = 0; i < arr1.length; i++) {
39+
hash[arr1[i]] = hash[arr1[i]] || 0;
40+
hash[arr1[i]]++;
41+
}
42+
43+
for(i = 0; i < arr2.length && count !== 0; i++) {
44+
if(hash[arr2[i]] > 0) {
45+
hash[arr2[i]]--;
46+
count--;
47+
result.push(arr2[i]);
48+
}
49+
}
50+
51+
return result;
52+
};

0 commit comments

Comments
 (0)