Skip to content

Commit dbf7b50

Browse files
committed
add more solution
1 parent f08fa91 commit dbf7b50

29 files changed

+966
-390
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
facebook
2+
test.js

1 Two Sum.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
// Given an array of integers, return indices of the two numbers such that they add up to a specific target.
2+
3+
// You may assume that each input would have exactly one solution.
4+
5+
// Example:
6+
// Given nums = [2, 7, 11, 15], target = 9,
7+
8+
// Because nums[0] + nums[1] = 2 + 7 = 9,
9+
// return [0, 1].
10+
// UPDATE (2016/2/13):
11+
// The return format had been changed to zero-based indices. Please read the above updated description carefully.
12+
13+
// Hide Company Tags LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
14+
// Hide Tags Array Hash Table
15+
// Hide Similar Problems (M) 3Sum (M) 4Sum (M) Two Sum II - Input array is sorted (E) Two Sum III - Data structure design
16+
17+
18+
119
/**
220
* @param {number[]} nums
321
* @param {number} target
@@ -6,19 +24,12 @@
624
var twoSum = function(nums, target) {
725
var hash = {};
826

9-
for(var i = 0; i < nums.length; i++){
10-
hash[nums[i]] = i;
11-
}
12-
13-
for(i = 0; i < nums.length; i++){
27+
for(var i = 0; i < nums.length; i++) {
1428
var num = nums[i];
15-
var diff = target - num;
16-
if(hash[diff] !== undefined && hash[diff] !== i){
17-
if(hash[diff] > i){
18-
return [i+1, hash[diff]+1];
19-
} else {
20-
return [hash[diff]+1, i+1]
21-
}
29+
if(hash[num] !== undefined) {
30+
return [hash[num], i]
31+
} else {
32+
hash[target - num] = i;
2233
}
2334
}
2435

10 Regular Expresion Matching.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@
1919
// isMatch("ab", ".*") → true
2020
// isMatch("aab", "c*a*b") → true
2121

22-
2322
/**
2423
* @param {string} s
2524
* @param {string} p
2625
* @return {boolean}
2726
*/
2827
var isMatch = function(s, p) {
29-
var m = s.length;
30-
var n = p.length;
28+
var sLen = s.length;
29+
var pLen = p.length;
3130
var dp = [];
3231

33-
for(var i = 0; i <= m; i++) {
32+
for(var i = 0; i <= sLen; i++) {
3433
var tmp = [];
3534

36-
for(var j = 0; j <= n; j++) {
35+
for(var j = 0; j <= pLen; j++) {
3736
tmp.push(false);
3837
}
3938

@@ -42,33 +41,33 @@ var isMatch = function(s, p) {
4241

4342
dp[0][0] = true;
4443

45-
for(i = 0; i <= m; i++) {
46-
for(j = 0; j <= n; j++) {
47-
if(p[j-1] !== '.' && p[j-1] !== '*') {
48-
if(i > 0 && s[i-1] === p[j-1] && dp[i-1][j-1]) {
44+
for(i = 0; i <= sLen; i++) {
45+
for(j = 0; j <= pLen; j++) {
46+
if(p[j - 1] !== '.' && p[j - 1] !== '*') {
47+
if(i > 0 && p[j - 1] === s[i - 1] && dp[i - 1][j - 1]) {
4948
dp[i][j] = true;
5049
}
51-
} else if(p[j-1] === '.') {
52-
if(i > 0 && dp[i-1][j-1]) {
53-
dp[i][j] = true;
50+
} else if(p[j - 1] === '.') {
51+
if(i > 0 && dp[i - 1][j - 1]) {
52+
dp[i][j] = true;
5453
}
55-
} else if(j > 1) { // '*' cannot be the first element
56-
if(dp[i][j-2]) { // 0 Occurance
57-
dp[i][j] = true;
58-
} else if(i > 0 && (p[j-2] == s[i-1] || p[j-2] == '.') && dp[i-1][j]) {
59-
54+
} else if(j > 1) { // '*' cannot be the first element
55+
if(dp[i][j - 2]) { // 0 occurance
56+
dp[i][j] = true;
57+
} else if(i > 0 && (p[j - 2] === s[i - 1] || p[j - 2] === '.') && dp[i - 1][j]) {
58+
6059
// example
6160
// xa and xa*
6261
// s[i-1] === a
6362
// p[j-2] === a
6463
// a === a
6564
// so we can now compare x, xa*
6665
// and x here is dp[i-1][j]
67-
dp[i][j] = true;
66+
dp[i][j] = true;
6867
}
6968
}
7069
}
7170
}
7271

73-
return dp[m][n];
72+
return dp[sLen][pLen];
7473
};

121 Best Time to Buy and Sell Stock.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1-
// Leetcode 121
2-
// Language: Javascript
3-
// Problem: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
4-
// Author: Chihung Yu
1+
// Say you have an array for which the ith element is the price of a given stock on day i.
2+
3+
// If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
4+
5+
// Example 1:
6+
// Input: [7, 1, 5, 3, 6, 4]
7+
// Output: 5
8+
9+
// max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
10+
// Example 2:
11+
// Input: [7, 6, 4, 3, 1]
12+
// Output: 0
13+
14+
// In this case, no transaction is done, i.e. max profit = 0.
15+
// Hide Company Tags Amazon Microsoft Bloomberg Uber Facebook
16+
// Hide Tags Array Dynamic Programming
17+
// Hide Similar Problems (M) Maximum Subarray (M) Best Time to Buy and Sell Stock II (H) Best Time to Buy and Sell Stock III (H) Best Time to Buy and Sell Stock IV (M) Best Time to Buy and Sell Stock with Cooldown
18+
519
/**
620
* @param {number[]} prices
721
* @return {number}

125 Valid Palindrome.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,33 @@ var isPalindrome = function(s) {
3636
}
3737
}
3838

39+
return true;
40+
};
41+
42+
43+
/**
44+
* @param {string} s
45+
* @return {boolean}
46+
*/
47+
var isPalindrome = function(s) {
48+
var head = 0;
49+
var tail = s.length - 1;
50+
51+
s = s.toLowerCase();
52+
53+
while(head < tail) {
54+
while(s[head] && !s[head].match(/[a-z0-9]/)) {
55+
head++;
56+
}
57+
while(s[tail] && !s[tail].match(/[a-z0-9]/)) {
58+
tail--;
59+
}
60+
if(head < tail && s[head] !== s[tail]) {
61+
return false;
62+
}
63+
head++;
64+
tail--;
65+
}
66+
3967
return true;
4068
};

133 Clone Graph.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,25 @@ var cloneGraph = function(graph) {
5757
}
5858
return newNode;
5959
}
60+
};
61+
62+
63+
var cloneGraph = function(graph) {
64+
if(!graph) {
65+
return graph;
66+
} else {
67+
return dfs(graph, {});
68+
}
69+
70+
function dfs(node, visited) {
71+
var newNode = visited[node.label] = visited[node.label] || new UndirectedGraphNode(node.label);
72+
73+
for(var i = 0; i < node.neighbors.length; i++) {
74+
var neighbor = node.neighbors[i];
75+
newNode.neighbors[i] = visited[neighbor.label] = visited[neighbor.label] || dfs(neighbor, visited);
76+
}
77+
78+
return newNode;
79+
}
80+
6081
};

15 3Sum.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
// Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
2+
3+
// Note: The solution set must not contain duplicate triplets.
4+
5+
// For example, given array S = [-1, 0, 1, 2, -1, -4],
6+
7+
// A solution set is:
8+
// [
9+
// [-1, 0, 1],
10+
// [-1, -1, 2]
11+
// ]
12+
// Hide Company Tags Amazon Microsoft Bloomberg Facebook Adobe
13+
// Hide Tags Array Two Pointers
14+
// Hide Similar Problems (E) Two Sum (M) 3Sum Closest (M) 4Sum (M) 3Sum Smaller
15+
16+
17+
118
/**
219
* @param {number[]} nums
320
* @return {number[][]}

157 Read N Characters Given Read4.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// The API: int read4(char *buf) reads 4 characters at a time from a file.
2+
3+
// The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file.
4+
5+
// By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.
6+
7+
// Note:
8+
// The read function will only be called once for each test case.
9+
10+
// Hide Company Tags Facebook
11+
// Hide Tags String
12+
// Hide Similar Problems (H) Read N Characters Given Read4 II - Call multiple times
13+
14+
15+
/**
16+
* Definition for read4()
17+
*
18+
* @param {character[]} buf Destination buffer
19+
* @return {number} The number of characters read
20+
* read4 = function(buf) {
21+
* ...
22+
* };
23+
*/
24+
25+
/**
26+
* @param {function} read4()
27+
* @return {function}
28+
*/
29+
var solution = function(read4) {
30+
/**
31+
* @param {character[]} buf Destination buffer
32+
* @param {number} n Maximum number of characters to read
33+
* @return {number} The number of characters read
34+
*/
35+
return function(buf, n) {
36+
var eof = false;
37+
var total = 0;
38+
var temp = Array(4);
39+
40+
while(!eof && total < n) {
41+
// read4 will populate temp with read characters, and return count ...
42+
var count = read4(temp);
43+
44+
if(count < 4) {
45+
eof = true;
46+
}
47+
48+
count = Math.min(count, n - total);
49+
50+
for(var i = 0; i < count; i++) {
51+
buf[total++] = temp[i];
52+
}
53+
}
54+
55+
return total;
56+
};
57+
};

161 One Edit Distance.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,40 @@ var isOneEditDistance = function(s, t) {
4242
}
4343

4444
return found || s.length < t.length;
45+
};
46+
47+
48+
var isOneEditDistance = function(s, t) {
49+
if(s.length > t.length) {
50+
var tmp = s;
51+
s = t;
52+
t = tmp;
53+
}
54+
55+
if(t.length - s.length > 1) {
56+
return false;
57+
}
58+
59+
60+
var i = 0;
61+
var j = 0;
62+
var diff = 0;
63+
64+
while(i < s.length && j < t.length) {
65+
if(s[i] !== t[j]) {
66+
if(diff !== 0) {
67+
return false;
68+
}
69+
diff++;
70+
71+
if(t.length !== s.length) {
72+
i--;
73+
}
74+
}
75+
76+
i++;
77+
j++;
78+
}
79+
80+
return diff === 1 || (t.length !== s.length && (t.length - j) === 1);
4581
};

0 commit comments

Comments
 (0)