题目:

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. netty与MQ使用心得

    最近在做分布式的系统,使用netty与mq进行远程RPC调用,现将心得经验总结一下. 我们公司的服务器在云端机房,在每一个店面有一个服务器,店面服务器外网无法访问. 我们的做法是店面服务器在启动时与云 ...

  2. 【aspnetcore】模拟中间件处理请求的管道

    几个核心对象: ApplicationBuilder 就是startup->Configure方法的第一个参数,请求(HttpContext) 就是由这个类来处理的 HttpContext 这个 ...

  3. 109 Convert Sorted List to Binary Search Tree 有序链表转换二叉搜索树

    给定一个单元链表,元素按升序排序,将其转换为高度平衡的BST.对于这个问题,一个高度平衡的二叉树是指:其中每个节点的两个子树的深度相差不会超过 1 的二叉树.示例:给定的排序链表: [-10, -3, ...

  4. Mysql5.7安装错误处理与主从同步及!

    basedir=/iddbs/mysql-5.7.16 datadir=/iddbs/mysql5.7/data3306 一.自定义Mysql.5.7版本免编译安装: 1.Db-server1安装前期 ...

  5. 如何正确在IDEA 里maven构建的项目中引入lib的jar包(图文详解)

    不多说,直接上干货! 问题详情 以下是我,maven构建出来的最新spark2.2.0-bin-hadoop2.6的项目. 有些依赖包,maven还是无法一次性满足,所以,得手动加入lib的jar包. ...

  6. C#的特性学习

    转自:https://www.cnblogs.com/rohelm/archive/2012/04/19/2456088.html   特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类 ...

  7. asp.net,监听输入框值的即时变化onpropertychange、oninput

    作者:自由天堂发布站点:WEB六零零 网页设计制作原文地址:http://www.web600.net/html/editor/JavaScript/201001131529.html 要达到的效果 ...

  8. 2013上半年中国CRM市场分析报告

    经过了十多年的风风雨雨,CRM度过了漫长的市场培育期,即将迎来成熟期.目前这一阶段也是CRM惨烈搏杀的一个阶段,据不完全统计,国内大大小小的CRM厂商已经超过600家,各厂商几度火拼,努力扩大自己在C ...

  9. 使用POI创建word表格-在表格单元格中创建子表格

    要实现的功能如下:表格中的单元格中有子表格 实现代码如下: XWPFParagraph cellPara = row.getCell(j).getParagraphArray(0); //row.ge ...

  10. LibreOJ #103. 子串查找

    题目描述 这是一道模板题. 给定一个字符串 A AA 和一个字符串 B BB,求 B BB 在 A AA 中的出现次数. A AA 中不同位置出现的 B BB 可重叠. 输入格式 输入共两行,分别是字 ...