题目:

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.
提示标签: hashtable 、 Two Pointers 、 String

分析:

该题目是一个求最长连续子串的长度问题,根据提示标签,题解应该涉及到哈希表的应用,严格控制每个字符的出现下标,以及更新子串位置同时对比当前最长子串的位置。

算法:

class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s.empty())
return 0; int hash_tab[256]; //保存当前字符上一次出现的下标
memset(hash_tab , -1 , sizeof(hash_tab)); //hash_tab中初始化所有值为-1
int max_len = 0, pos = -1;//max_len即是最长子串长度 , pos为当前子串的开始位置 for(int i=0 ; i<s.length() ; i++)
{
//每个字符的元位置初始化为-1 ,当当前字符是重复字符时,改变子串的开始位置
if(hash_tab[s[i]] > pos)
{
pos = hash_tab[s[i]];
}//if if(i-pos > max_len)
{
max_len = i-pos;
}//if //更改首次出现字符的位置
hash_tab[s[i]] = i;
}//for
return max_len;
}
};

LeetCode03 AC代码下载


LeetCode(3)Longest Substring Without Repeating Characters的更多相关文章

  1. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  2. LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)

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

  3. Leetcode经典试题:Longest Substring Without Repeating Characters解析

    题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...

  4. 【LeetCode】3 、Longest Substring Without Repeating Characters

    题目等级:Medium 题目描述:   Given a string, find the length of the longest substring without repeating chara ...

  5. leetcode第三题--Longest Substring Without Repeating Characters

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

  6. leetcode第三题Longest Substring Without Repeating Characters java

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

  7. LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters

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

  8. LeetCode Hash Table 3. Longest Substring Without Repeating Characters

    HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...

  9. LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

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

随机推荐

  1. MDX之百分比

    MDX的几种百分比的计算方法 实际应用中,特别是一些分析报表,经常需要计算数据百分比.份额.平均值.累计占比等,在数据仓库飞速发展的今天,我们需要了解一些经常编写的MDX语句的写法,以满足工作中的需要 ...

  2. oracle GROUP BY rollup

    1.ROW_NUMBER() OVER函数的基本用法用法 http://www.cnblogs.com/fxgachiever/archive/2010/09/15/1826792.html 2.De ...

  3. 牛客网Java刷题知识点之什么是死锁、死锁产生的4个必要条件、死锁的解除与预防

    不多说,直接上干货! https://www.nowcoder.com/ta/review-java/review?query=&asc=true&order=&page=16 ...

  4. node项目 Error: Cannot find module 'mongoose'

    这是因为你部署的项目没有添加mongoose,使用 在自己项目的根目录下:npm install mongoose --save

  5. echarts 添加Loading 等待。

    capturedsDetailsEcharts: function(id) { if (!id) { id = mini.get("chnNameCaptureds").getVa ...

  6. Django框架和前端的的基本结合

    1 昨日回顾 a socket b 路由关系 c 模板字符串替换(模板语言) 主流web框架总结: django a用别人的 b自己写的 c自己写的 flask a用别人的 b自己写的 c用别人的(j ...

  7. 【读书笔记】构建之法(CH4~CH6)

    从chapter4至chapter6,围绕着构建过程的合作讨论构建之法,而合作与个人工作的区别却以一个微妙的问题为开端:阅读别人的代码有多难? 两人合作:(驾驶员与领航员) 合作要注意代码风格规范与设 ...

  8. LR中排序脚本

    /* * LoadRunner Java script. (Build: 670) * * Script Description: * */ import lrapi.lr; public class ...

  9. JS 语言基础

    两个变量 相加 var s="今天下雨了"; var i=10; alert(i+s); 这里的i+s是拼接的意思   显示出来是  今天下雨了10 假设我改  s="2 ...

  10. ubuntu 14.04 安装tomcat服务器 配置图片路径和文件路径

    root@hett-PowerEdge-T30:/usr/local/src# lltotal 235956drwxr-xr-x  6 root root      4096  3月 26 14:48 ...