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清空,以免重复。

 class Solution {
public:
int lengthOfLongestSubstring(string s) {
int location[],i,index,j;
for(i=;i<;i++) location[i]=-;
int start=,maxLength=,templ=;
for(i=;i<s.size();i++)
{
index = s[i];
if(location[index]== -)
templ++;
else
{
if(templ>maxLength)
{
maxLength = templ;
// cout<<s.substr(start,templ)<<endl;
}
for(j=start;j<location[index];j++)
location[s[j]]=-;
templ =templ - (location[index]-start);
start = location[index]+;
}
location[index]=i;
}
if(i==s.size() && templ>maxLength) maxLength=templ;
return maxLength;
}
};

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

[LeetCode 题解]: Longest Substring Without Repeating Characters的更多相关文章

  1. LeetCode题解 || Longest Substring Without Repeating Characters (O(n)算法)问题

    problem: Given a string, find the length of the longest substring without repeating characters. For ...

  2. LeetCode题解——Longest Substring Without Repeating Characters

    题目: 给定一个字符串,返回其中不包含重复字符的最长子串长度. 解法: 维持两个指针,第一个指向子串开始,第二个负责遍历,当遍历到的字符出现在子串内时,应计算当前子串长度,并更新最长值:然后第一个指针 ...

  3. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  4. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

  5. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  6. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  7. [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  8. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  9. LeetCode之Longest Substring Without Repeating Characters

    [题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...

随机推荐

  1. 19_java之List和Set

    01List接口的特点 A:List接口的特点: a:它是一个元素存取有序的集合. 例如,存元素的顺序是11.22.33.那么集合中,元素的存储就是按照11.22.33的顺序完成的). b:它是一 ...

  2. react之本地图片引用

    react之本地图片引用 <img src="../images/photo.png"/> 这种写法在react中是不支持的,所以引用本地图片需要用import或者re ...

  3. Linux虚机密码破解

    1 重启机器,在机器读秒时按回车键 2 选择要启动的操作系统按 e 3 选择kernel所在行按 e 4 末尾输入空格  single 5 敲回车 在按 b 系统将进入单用户模式 然后 可以 通过 p ...

  4. LeetCode之数组处理题java

    342. Power of Four Total Accepted: 7302 Total Submissions: 21876 Difficulty: Easy Given an integer ( ...

  5. SPI子系统分析之二:数据结构

    内核版本:3.9.5 spi_master struct spi_master用来描述一个SPI主控制器,我们一般不需要自己编写spi控制器驱动. /*结构体master代表一个SPI接口,或者叫一个 ...

  6. Phong & BlinnPhong Specular Shader

    [Phong Specular Shader] 如果物体离摄像机很远,或者不需要高精度镜面反射,则Phong模型适用. Phong模型如下: 使用前必须指定使用自定义Phong. [BlinnPhon ...

  7. markdown转HTML,目录生成

    1.首先,准备好已经编辑好的markdown文件放到指定目录下. 2.下载node.js,下载地址:https://nodejs.org/en/download/ 3.下载好node.js文件后,配置 ...

  8. [C++] How to prevent memory leaks

    How to prevent memory leaks ? overload new/delete

  9. 2.spark-streaming实战

    park Streaming--实战篇 摘要:      Sprak Streaming属于Saprk API的扩展,支持实时数据流(live data streams)的可扩展,高吞吐(hight- ...

  10. code1091 传染病控制

    1.读入图,边是双向的 2.递归建树,同时确定每一层的节点 3.dfs按层搜索,先把这一层所有被传染的(die[pa[k]]=true的)的die置为true 然后循环,每次把一个die为true的变 ...