题目

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

解法

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

代码

 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. Notepad++ 书签

      Notepad++,有一个书签功能,指定书签是Ctrl+F2,在书签之间移动是按F2来切换,这个可以在几个想查看的数据之间进行快速切换,所以看起来就很方便.

  2. tar 解压缩命令

    tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...

  3. hdu 4403

    水水的dfs #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath& ...

  4. BZOJ 3533 sdoi 2014 向量集

    设(x,y)为Q的查询点,分类讨论如下:1.y>0:  最大化a*x+b*y,维护一个上凸壳三分即可 2.y<0:最大化a*x+b*y  维护一个下凸壳三分即可 我们考虑对时间建出一棵线段 ...

  5. iOS开发--使用RSA加密

    在iOS中使用RSA加密解密,需要用到.der和.p12后缀格式的文件,其中.der格式的文件存放的是公钥(Public key)用于加密,.p12格式的文件存放的是私钥(Private key)用于 ...

  6. POJ 1879 Tempus et mobilius Time and motion 队列和栈

    很简单的队列和栈的应用,不过读明白题意非常重要:(直接引用白书的题解)三个轨道,一个库.分别是分钟单位的轨道,5min单位的轨道,一小时单位的轨道,还有就是n容量的库.每过一分钟,一个小球从库里面出来 ...

  7. Android ActionBar通过Tab进行不同的Fragment之间的交换

    ActionBar的使用常见于4.0系统,其Tab的使用挺广泛的. 在ActionBar中添加标签(Tabs),每个标签对应的是一个Fragment,点击不同的Tab时,就会切换到对应的Fragmen ...

  8. java遍历树(深度遍历和广度遍历

    java遍历树如现有以下一颗树:A     B          B1               B11          B2               B22     C          C ...

  9. MapReduce编程系列 — 3:数据去重

    1.项目名称: 2.程序代码: package com.dedup; import java.io.IOException; import org.apache.hadoop.conf.Configu ...

  10. 安装Hadoop系列 — 新建MapReduce项目

    1.新建MR工程 依次点击 File → New → Ohter…  选择 “Map/Reduce Project”,然后输入项目名称:mrdemo,创建新项目:     2.(这步在以后的开发中可能 ...