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
substring is "b", with the length of 1.

题解:

使用贪心策略,Hash记录字符上一次出现的位置,并记录起点。

每次更新的时候,记得将start checkpoint 之间出现的字符location清空,以免重复。


 1 class Solution {
2 public:
3 int lengthOfLongestSubstring(string s) {
4 int location[128],i,index,j;
5 for(i=0;i<128;i++) location[i]=-1;
6 int start=0,maxLength=0,templ=0;
7 for(i=0;i<s.size();i++)
8 {
9 index = s[i];
10 if(location[index]== -1)
11 templ++;
12 else
13 {
14 if(templ>maxLength)
15 {
16 maxLength = templ;
17 // cout<<s.substr(start,templ)<<endl;
18 }
19 for(j=start;j<location[index];j++)
20 location[s[j]]=-1;
21 templ =templ - (location[index]-start);
22 start = location[index]+1;
23 }
24 location[index]=i;
25 }
26 if(i==s.size() && templ>maxLength) maxLength=templ;
27 return maxLength;
28 }
29 };

转载请注明出处: http://www.cnblogs.com/double-win/

LeetCode: Longest Substring Without Repeating Characters
题解,布布扣,bubuko.com

LeetCode: Longest Substring Without Repeating Characters
题解

时间: 2024-10-03 02:59:13

LeetCode: Longest Substring Without Repeating Characters 题解的相关文章

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

leetcode——Longest Substring Without Repeating Characters 求链表中无重复字符的最大字串长度(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

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

[LeetCode] Longest Substring Without Repeating Characters [15]

题目 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 su

[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

[LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++语言 java语言实现

Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Explana

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

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

[Leetcode] Longest Substring Without Repeating Characters (C++)

题目: 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 : 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.