题目

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

解法

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

代码

  1. class Solution {
  2. public:
  3. int lengthOfLongestSubstring(string s) {
  4. int last_pos[]; //记录每个字符最后一次出现位置
  5. fill(last_pos, last_pos + , -);
  6.  
  7. int start = , max_len = ; //start指向当前子串第一个字符
  8. int slen = s.size();
  9.  
  10. for(int stop = ; stop < slen; ++stop) //stop用于遍历
  11. {
  12. char c = s[stop];
  13.  
  14. if(last_pos[c] >= start) //stop指向的字符出现在子串内部
  15. {
  16. max_len = max(stop - start, max_len); //更新最长值
  17. start = last_pos[c] + ; //更新第一个字符指针
  18. }
  19. last_pos[c] = stop; //更新当前字符最后出现位置
  20. }
  21.  
  22. return max(max_len, slen - start); //循环停止时,最后一个子串没有参与计算,所以在这里计算其长度并对比
  23. }
  24. };

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. hadoop Safe mode is ON 的解决办法

    hadoop Safe mode is ON 的解决办法 搭了一个hadoop集群环境,近期总是出现读写文件错误的情况,查看name node的日志显示 (Safe mode is ON) Safe ...

  2. xfire for web-Service

    1.0 XFire XFire是codeHaus组织提供的一个开源框架,它构建了POJO和SOA之间的桥梁,主要特性就是支持将POJO通过非常简单的方式发布成Web服务,这种处理方式不仅充分发挥了PO ...

  3. ExtJS4.2学习(二)Ext统一组件模型——Panel

    鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-06/171.html --------------- ...

  4. 用 OneAPM Cloud Insight 监控 Docker 性能

    Docker 是构建和部署软件的一个新兴的轻量级的平台,也是一个减轻替代虚拟机的容器.Docker 通过给开发者提供兼容不同环境的镜像,成为解决现代基础设施的持续交付的一个流行的解决方案. 和虚拟机一 ...

  5. CodeForces 300A Array

    http://codeforces.com/problemset/problem/300/A 题意 :给你n个数字,让你分成3组,第一组各个数之积要小于0,第二组要大于0,第三组要等于0,符合要求的答 ...

  6. iOS开发--绘图教程

    本文是<Programming iOS5>中Drawing一章的翻译,考虑到主题完整性,翻译版本中加入了一些书中未涉及到的内容.希望本文能够对你有所帮助. 本文由海水的味道翻译整理,转载请 ...

  7. Oracle 学习笔记 常用查询命令篇

    1.查询某个用户下有多少张表 有时候很有用  select count(*) from dba_tables t where t.owner='SCOTT';

  8. Spring RestTemplate介绍

    http://www.cnblogs.com/rollenholt/p/3894117.html RestTemplate 这篇文章打算介绍一下Spring的RestTemplate.我这边以前设计到 ...

  9. linux netcat命令

    netcat是网络工具中的“瑞士军刀”,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它.使用netcat命令所能完成的事情令人惊讶. netcat所 ...

  10. php 23种设计模式的趣味解释

    http://wenku.baidu.com/link?url=GwvuvSOdJneZQc-DSKoGmPcxTtzn3cdtIp3fRaCNbkg1zJDZZZTx2NwEK5IsqU996fG3 ...