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.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

解题思路是建立一个映射表,检查两个重复字符之间的间隔长度。

int lengthOfLongestSubstring(string str)
{
// create table to store all ASCII
vector<int> vecTable(256,-1);
int nMaxLen = -1;
int nStart = -1;
for (int i = 0; i < str.length(); ++i)
{
// Target character's position is after previous location
if (vecTable[str[i]] > nStart)
{
nStart = vecTable[str[i]];
}
// Update target character's position;
vecTable[str[i]] = i;
nMaxLen = max(nMaxLen, i - nStart);
} return nMaxLen;
}

  

Leet Code 3. Longest Substring Without Repeating Characters的更多相关文章

  1. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  2. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  3. [leetcode]3. Longest Substring Without Repeating Characters无重复字母的最长子串

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  4. Leetcode:Longest Substring Without Repeating Characters 解题报告

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  5. LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

    题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...

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

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

  7. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  8. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

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

  9. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

随机推荐

  1. js switch 字符串

    //字符串var day="dd";switch (day){case "dd": x="day it's dd"; break;case ...

  2. 迁移学习 transferlearning

    2019-04-08 13:25:17 在实践中,很少有人从头开始训练整个卷积网络(随机初始化),因为拥有足够大小的数据集是相对罕见的.相反,通常在非常大的数据集(例如ImageNet,其包含具有10 ...

  3. 3D视图的2D展示

    效果图:预览 :预览 如何在2d界面显示3d图形? 如果把屏幕的中心作为视点的中心位置,那由远及近的物体应该是逐渐缩小的,而且是逐渐模糊的, 我们首先获取元素相对于中心点的距离,然后抽取这个距离的百分 ...

  4. Android从启动到程序运行整个过程的整理

    1Android是基于Linux的一个操作系统,它可以分为五层,下面是它的层次架构图,可以记一下,因为后面应该会总结到SystemServer这些Application Framework层的东西 A ...

  5. Monkey工具

    Android自动化测试方法,对其中的一些工具.方法和框架做了一些简单的整理,其中包括android测试框架.CTS.Monkey. Monkeyrunner.benchmark.其它test too ...

  6. mysql、nginx、php-fpm的启动与关闭

    mysql 一.启动方式 1.使用 service 启动:service mysqld start 2.使用 mysqld 脚本启动:/etc/inint.d/mysqld start 3.使用 sa ...

  7. 记一次mybatis bindingexception 问题排查

    看到的错误信息如出一辙都是这样的:Method threw 'org.apache.ibatis.binding.BindingException' exception.Invalid bound s ...

  8. 生成器的认识及其思考:VAE, GAN, Flow-based Invertible Model

    生成器对应于认知器的逆过程. 这一切的起源都是当初一个极具启发性的思想:Sleep-wake algorithm——人睡眠时整理记忆做梦,是一个生成的过程,即通过最终的识别结果企图恢复接收到的刺激,当 ...

  9. hdu多校第4场E. Matrix from Arrays HDU 二维前缀和

    Problem E. Matrix from Arrays Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total S ...

  10. java通过HttpClient方式和HttpURLConnection方式调用WebService接口

    1.引入maven依赖: <dependency> <groupId>org.apache.httpcomponents</groupId> <artifac ...