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

Commit e20d6c1

Browse files
add: Longest Substring Without Repeating Characters
1 parent 282e5e3 commit e20d6c1

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This is the solution collection of my LeetCode problems, most of them are progra
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|
1213
|13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [JavaScript](./src/roman-to-integer/res.js)|Easy|
1314
|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|
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+
let lengthOfLongestSubstring = function(s) {
21+
let strlen = s.length;
22+
23+
// 字符串长度小于2
24+
if (strlen < 2) {
25+
return strlen;
26+
}
27+
28+
let maxString = '', maxLen = 1;
29+
30+
for (let i=0; i<strlen; i++) {
31+
let substr = [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 (let j=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+
return maxLen;
70+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /