题目

给定一个字符串,返回其中不包含重复字符的最长子串长度。

解法

维持两个指针,第一个指向子串开始,第二个负责遍历,当遍历到的字符出现在子串内时,应计算当前子串长度,并更新最长值;然后第一个指针更新为出现位置的下一个。

代码

 class Solution {
public:
int lengthOfLongestSubstring(string s) {
int last_pos[]; //记录每个字符最后一次出现位置
fill(last_pos, last_pos + , -); int start = , max_len = ; //start指向当前子串第一个字符
int slen = s.size(); for(int stop = ; stop < slen; ++stop) //stop用于遍历
{
char c = s[stop]; if(last_pos[c] >= start) //stop指向的字符出现在子串内部
{
max_len = max(stop - start, max_len); //更新最长值
start = last_pos[c] + ; //更新第一个字符指针
}
last_pos[c] = stop; //更新当前字符最后出现位置
} return max(max_len, slen - start); //循环停止时,最后一个子串没有参与计算,所以在这里计算其长度并对比
}
};

LeetCode题解——Longest Substring Without Repeating Characters的更多相关文章

  1. [LeetCode 题解]: Longest Substring Without Repeating Characters

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

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

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

  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. 使用Yeoman搭建 AngularJS 应用 (8) —— 让我们搭建一个网页应用

    原文地址:http://yeoman.io/codelab/write-app.html 创建一个新的模板来显示一个todo的列表 打开views/main.html 为了从一个干净的模板开始,删除m ...

  2. 你不需要jQuery(五)

    什么不用jQuery?因为它让你的网站体积变得臃肿.你的网站并不真的需要jQuery,不需要它带来的额外体积.带宽和加载时间. 用原生JavaScript简单实现jQuery提供的功能和方法 查找.选 ...

  3. C#学习笔记(二)

    1.注释:注销,解释2.单行://多行:/**/文档注释:///按enter主食要保证 别人一看就明白3.快速对期待吗:ctrl+k+d,按住ctrl不放,按k,迅速抬起,再按d(按D得时候k已经抬起 ...

  4. PAT-乙级-1008. 数组元素循环右移问题 (20)

    1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...

  5. WCF服务的创建和发布到IIS

    一. WCF服务的创建 有两种创建方式: 1.WCF服务库 2.WCF服务应用程序 如下图所示: 这里选择WCF服务库.注意事项: 1.WCF服务库是一个类库项目,这里选择.net 3.5版本(版本高 ...

  6. hdu 1213

    简单并查集 #include <cstdio> #include <cstring> #define maxn 30005 int fa[maxn],ans[maxn],n,m ...

  7. IIC驱动分析

    IIC设备是一种通过IIC总线连接的设备,由于其简单性,被广泛引用于电子系统中.在现代电子系统中,有很多的IIC设备需要进行相互之间通信 IIC总线是由PHILIPS公司开发的两线式串行总线,用于连接 ...

  8. linux cd命令不带路径参数

    #切换到当前用户的主目录.若为root用户,则切换到/root,若普通用户,则切换到/home/username $ cd

  9. ubuntu下如何设置主机名

    方法如下: 在终端输入 hostname 查看主机名主机名存放在/etc/hostname中 ,sudo gedit /etc/hostname 修改后保存/etc/hosts 还有一份 , sudo ...

  10. DBNull.Value 字段的用法

    DBNull 是一个单独的类,这意味着该类只能存在此实例.它指数据库中数据为空(<NULL>)时,在.net中的值 如果数据库字段的数据缺失,则您可以使用 DBNull.Value 属性将 ...