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. mac 使用iTerm2快捷登录远程服务器

    mac 使用iTerm2快捷登录远程服务器 正常使用ssh登录服务器: ssh -p port user@host user@host's password: 输入端口,用户名,服务器ip地址后,还需 ...

  2. 集训队日常训练20181201 E 1005 : 小蝌蚪

    时间限制(普通/Java):500MS/1500MS     内存限制:65536KByte总提交: 25            测试通过:5 描述 有 n 个装着小蝌蚪的水缸排成一排,你拥有一个无限 ...

  3. 在CentOS6.9上Shell脚本定时释放内存cache

    一.写Shell脚本 mkdir -p /var/script/ vim /var/script/freemem.sh 写入以下Shell脚本: #!/bin/bash # 当前已使用的内存大小 us ...

  4. 简单H5单页面真机调试

    1.安装Node.js 这个没什么好说的,直接去官网下载安装就好了. Node.js官网:https://nodejs.org 2.安装http-server 直接在命令行中安装到全局(-g表示安装到 ...

  5. PPI | protein-protein interaction | 蛋白互作分析

    STRING database的挖掘 这个数据库绝对是做实验人的宝藏,里面包含了各种蛋白互作关系,不用做实验就有一大堆证据. IPA了解一下,收费的高端分析软件,大部分就是整合的这个数据库,很多大佬喜 ...

  6. spoj839Optimal Marks

    题意:略 怎样判断属于S,T集合. 如果从S出发到不了某点,该点出发也到不了T,那么割给那边都行. 如果S出发能到该点,该点出发也能到T,这种情况下dinic没结束. 只能从S到该点:只能分到S集.只 ...

  7. Django-models & QuerySet API

    django中配置mysql数据库 1,首先配置settings.py. 一是在INSTALLED_APPS里面加入app名称: 二是配置数据库相关信息 INSTALLED_APPS = [ 'dja ...

  8. LCSS最长公共子序列算法

    0.论文基本介绍以及相关内容 分析移动用户位置的相似性,提取移动用户的相似路径在出行路径预测.兴趣区域发现.轨迹聚类.个性化路径推荐等领域具有广泛的应用. 重点:利用移动用户定位数据找到合适轨迹的表示 ...

  9. jquery横向纵向鼠标滚轮全屏切换

    html <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF- ...

  10. python web1(解析url)

    环境:pycharm 尝试对地址进行切片 去掉头 http 或 https a.遇到了一些问题 url = 'https://www.cnblogs.com/derezzed/articles/811 ...