Skip to content

Commit a18ffd5

Browse files
committedFeb 27, 2017
add: 2 solutions
1 parent e20d6c1 commit a18ffd5

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed
 

‎README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
This is the solution collection of my LeetCode problems, most of them are programmed in JavaScript. All JavaScript codes are wrote in ECMAScript 6 standard, each solution file will contain a problem description in the beginning.
44

5-
**Progress: 28/**
5+
**Progress: 33/**
66

77
| ID | Title | Solution | Difficulty |
88
|---| ----- | -------- | ---------- |
99
|1|[Two Sum](https://leetcode.com/problems/two-sum/) | [JavaScript](./src/two-sum/res.js)|Easy|
1010
|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [JavaScript](./src/add-two-numbers/res.js)|Medium|
1111
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [JavaScript](./src/longest-substring-without-repeating-characters/res.js) |Medium|
1212
|7|[Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [JavaScript](./src/reverse-integer/res.js)|Easy|
13+
|11|[Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [JavaScript](./src/container-with-most-water/res.js)|Medium|
1314
|13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [JavaScript](./src/roman-to-integer/res.js)|Easy|
15+
|17|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [JavaScript](./src/letter-combinations-of-a-phone-number/res.js)|Medium|
1416
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | [JavaScript](./src/remove-nth-node-from-end-of-list/res.js)|Medium|
1517
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) | [JavaScript](./src/generate-parentheses/res.js)|Medium|
1618
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [JavaScript](./src/remove-duplicates-from-sorted-array/res.js)|Easy|

‎src/container-with-most-water/res.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* res.js
3+
* @authors Joe Jiang (hijiangtao@gmail.com)
4+
* @date 2017-02-27 21:00:54
5+
* @version $Id$
6+
*
7+
* Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
8+
*
9+
* Note: You may not slant the container and n is at least 2.
10+
*
11+
* Solution Idea: https://discuss.leetcode.com/topic/503/anyone-who-has-a-o-n-algorithm/3
12+
*
13+
* @param {number[]} height
14+
* @return {number}
15+
*/
16+
let maxArea = function(height) {
17+
let size = height.length,
18+
maxVal = 0,
19+
left = 0,
20+
right = size-1;
21+
22+
while (left < right) {
23+
let lVal = height[left],
24+
rVal = height[right],
25+
currentVal = minVal(lVal, rVal)*(right-left);
26+
if (maxVal < currentVal) {
27+
maxVal = currentVal;
28+
}
29+
if (lVal > rVal) {
30+
right -= 1;
31+
} else {
32+
left += 1;
33+
}
34+
}
35+
36+
return maxVal;
37+
};
38+
39+
let minVal = function(a, b) {
40+
if (a>b) {
41+
return b;
42+
}
43+
44+
return a;
45+
};
46+
47+
let maxVal = function (a, b) {
48+
if (a>b) {
49+
return a;
50+
}
51+
52+
return b;
53+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* res.js
3+
* @authors Joe Jiang (hijiangtao@gmail.com)
4+
* @date 2017-02-27 22:30:32
5+
* @version $Id$
6+
*
7+
* Given a digit string, return all possible letter combinations that the number could represent.
8+
*
9+
* A mapping of digit to letters (just like on the telephone buttons) is given below.
10+
*
11+
* Input:Digit string "23"
12+
*
13+
* Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
14+
* Note:
15+
* Although the above answer is in lexicographical order, your answer could be in any order you want.
16+
*
17+
* @param {string} digits
18+
* @return {string[]}
19+
*/
20+
let letterCombinations = function(digits) {
21+
let size = digits.length,
22+
mapping = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"],
23+
res = [''];
24+
25+
for (let i=0; i<size; i++) {
26+
let currentVal = digits[i]-'0';
27+
if (currentVal > 1 && currentVal < 10) {
28+
let tmp = [],
29+
maplen = mapping[currentVal].length,
30+
reslen = res.length;
31+
32+
for (let j=0; j<reslen; j++) {
33+
tmp.push(res[j]);
34+
}
35+
res = [];
36+
37+
for (let j=0; j<reslen; j++) {
38+
for (let k=0; k<maplen; k++) {
39+
res.push(tmp[j] + mapping[currentVal][k]);
40+
}
41+
}
42+
}
43+
}
44+
45+
if (size < 1 || (res.length === 1 && res[0] === '')) {
46+
return [];
47+
}
48+
49+
return res;
50+
};

0 commit comments

Comments
 (0)
Please sign in to comment.