Longest Substring Without Repeat Characters

 1 var lengthOfLongestSubstring = function(s) {
 2     if (s === ‘‘) {
 3         return 0;
 4     }
 5
 6     var lenMax = 1,
 7         lenCurr = 1,
 8         i, repeat;
 9
10     for (i = 1; i < s.length; i++) {
11         repeat = s.substr(i - lenCurr, lenCurr).indexOf(s.substr(i, 1));
12
13         if (repeat == -1) {
14             lenCurr++;
15         } else {
16             lenCurr -= repeat;
17         }
18
19         if (lenCurr > lenMax) {
20             lenMax = lenCurr;
21         }
22     }
23
24     return lenMax;
25 };
时间: 2024-11-14 23:16:35

Longest Substring Without Repeat Characters的相关文章

leetcode3 Longest substring whitout repeat characters

求字符串中的最长无重复子串的长度,例如"abcabcbb",最长无重复子串为"abc",长度为3.因为要求无重复,因此想到要用HashMap来保存,因为HashMap的键值不能重复.将要存入的字符作为key,字符在字符串中的下标作为value,如果map中已经存有该字符,则删掉该字符以及字符串中该字符之前的所有字符,然后再存入.例如字符串为"abcbd",如已存入abc,现在要存b,则删掉ab,存入(b,3):max为map中存放最多字符时的数值

leetcode4 ---Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

LeetCode: Longest Substring Without Repeating Characters 题解

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

LeetCode3 Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s

[LeetCode] 3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

LeetCode Problem 3.Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

Leetcode 03 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

Java for LeetCode 003 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

LeetCode Longest Substring Without Repeating Characters 最长不重复子串

题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离是多少,若大于max,则更新max.若小于,则不更新.每扫到一个字符就需要更新他的出现位置了.这里边还有个注意点,举例说明: 假如有长为16串 s="arbtbqwecpoiuyca" 当扫到第2个b时,距离上一个b的距离是2:(直接减) 当扫到第2个c时,距离上一个c的距离是6:(直接减