Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2c4372c

Browse files
committed
Merge branch 'master' of github.com:hijiangtao/LeetCodeOJ
2 parents4ce9160 +a18ffd5 commit2c4372c

File tree

4 files changed

+177
-1
lines changed
  • src
    • container-with-most-water
    • letter-combinations-of-a-phone-number
    • longest-substring-without-repeating-characters

4 files changed

+177
-1
lines changed

‎README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +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|
11+
|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|
1112
|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|
1214
|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|
1316
|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|
1417
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)|[JavaScript](./src/generate-parentheses/res.js)|Medium|
1518
|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+
letmaxArea=function(height){
17+
letsize=height.length,
18+
maxVal=0,
19+
left=0,
20+
right=size-1;
21+
22+
while(left<right){
23+
letlVal=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+
returnmaxVal;
37+
};
38+
39+
letminVal=function(a,b){
40+
if(a>b){
41+
returnb;
42+
}
43+
44+
returna;
45+
};
46+
47+
letmaxVal=function(a,b){
48+
if(a>b){
49+
returna;
50+
}
51+
52+
returnb;
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+
letletterCombinations=function(digits){
21+
letsize=digits.length,
22+
mapping=["","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"],
23+
res=[''];
24+
25+
for(leti=0;i<size;i++){
26+
letcurrentVal=digits[i]-'0';
27+
if(currentVal>1&&currentVal<10){
28+
lettmp=[],
29+
maplen=mapping[currentVal].length,
30+
reslen=res.length;
31+
32+
for(letj=0;j<reslen;j++){
33+
tmp.push(res[j]);
34+
}
35+
res=[];
36+
37+
for(letj=0;j<reslen;j++){
38+
for(letk=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+
returnres;
50+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Given a string, find the length of the longest substring without repeating characters.
3+
*
4+
* Examples:
5+
*
6+
* Given "abcabcbb", the answer is "abc", which the length is 3.
7+
*
8+
* Given "bbbbb", the answer is "b", with the length of 1.
9+
*
10+
* Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
11+
*
12+
* res.js
13+
*@authors Joe Jiang (hijiangtao@gmail.com)
14+
*@date 2017-02-27 20:52:27
15+
*@version $Id$
16+
*
17+
*@param {string} s
18+
*@return {number}
19+
*/
20+
letlengthOfLongestSubstring=function(s){
21+
letstrlen=s.length;
22+
23+
// 字符串长度小于2
24+
if(strlen<2){
25+
returnstrlen;
26+
}
27+
28+
letmaxString='',maxLen=1;
29+
30+
for(leti=0;i<strlen;i++){
31+
letsubstr=[s[i]],continueJud=false;
32+
33+
// 剩余字符串即使全部不重复也不可能长于现有最长的字符串
34+
if(strlen-i<=maxLen){
35+
break;
36+
}
37+
38+
// 如果遇到连续相等的字符,则跳到本次连续出现的最后一个该字符上
39+
while(i+1<strlen&&s[i]===s[i+1]){
40+
i+=1;
41+
continueJud=true;
42+
}
43+
if(continueJud){
44+
continueJud=false;
45+
i-=1;
46+
continue;
47+
}
48+
49+
// 从第二个字符开始遍历,直到字符串结尾或者出现重复字符的情况
50+
for(letj=i+1;j<strlen;j++){
51+
if(substr.indexOf(s[j])!==-1){
52+
if(substr.length>maxLen){
53+
maxLen=substr.length;
54+
// maxString = ''.join(substr);
55+
}
56+
57+
break;
58+
}
59+
60+
substr.push(s[j]);
61+
if(j===strlen-1){
62+
if(substr.length>maxLen){
63+
maxLen=substr.length;
64+
}
65+
}
66+
}
67+
}
68+
69+
returnmaxLen;
70+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp