一. 题目描写叙述

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.

二. 题目分析

题目的大意是。给出一串字符串,找出无反复字符的最长子串,输出其长度。能够使用两个指针,一个指向当前子串的头,一个指向尾,尾指针不断往后扫描,当有字符前面出现过,记录当前子串长度和最优解的比較结果。然后头指针不断往后扫描。直到扫描到一个字符和尾指针同样,则尾指针继续扫描。当尾指针到达字符串结尾时算法结束。算法复杂度O(n) + O(n) = O(n)。

三. 演示样例代码

class Solution {
private:
bool canUse[256];
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
memset(canUse, true, sizeof(canUse)); int count = 0;
int start = 0;
int ret = 0;
for(int i = 0; i < s.size(); i++)
{
if (canUse[s[i]])
{
canUse[s[i]] = false;
count++;
}
else
{
ret = max(ret, count);
while(true)
{
canUse[s[start]] = true;
count--;
if (s[start] == s[i])
break;
start++;
}
start++;
canUse[s[i]] = false;
count++;
}
} ret = max(ret, count); return ret;
}
};

四. 小结

參考:http://www.cnblogs.com/remlostime/archive/2012/11/12/2766530.html

leetcode笔记:Longest Substring Without Repeating Characters的更多相关文章

  1. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  2. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  3. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  4. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  5. LeetCode之Longest Substring Without Repeating Characters

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

  6. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

    Description Given a string, find the length of the longest substring without repeating characters. E ...

  7. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

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

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  9. LeetCode[3] Longest Substring Without Repeating Characters

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

  10. 【leetcode】Longest Substring Without Repeating Characters

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

随机推荐

  1. 2019年今日头条机试_JAVA后台岗_第一题

        广度优先遍历: import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; class Nod ...

  2. 《少年先疯队》第九次团队作业:Beta冲刺第三天

    3.1 今日完成任务情况 姚玉婷:酒店系统中剩余功能的完善 马丽莎:酒店系统中管理员功能的测试 张   琼:酒店系统中会员功能的测试 孙苗坤:酒店系统中其余管理功能的测试文档的编写 3.2 成员贡献时 ...

  3. _ 下划线 vue mixins 混入 变量前有下划线 变量不起作用

    _ 下划线 vue mixins 混入 变量前有下划线 变量不起作用

  4. HTML5新特性之History

    几年前,Ajax的兴起给互联网带来了新的生机,同时也使用户体验有了质的飞跃,用户无需刷新页面即可获取新的数据,而页面也以一种更具有交互性的形式为用户展现视图,可以说这种变化对互联网发展的贡献是史无前例 ...

  5. NET实现谷歌OCR的使用记录(CLOUD VISION API)

    1)购买VPS 2)配置一VPN 建议使用 cisco anycounect  |   ***会连接失败(切记,祭奠浪费的一天)大神可以帮我看下是什么问题 3)进入https://cloud.goog ...

  6. 5.1 qbxt 一测 T1

    禁咒检验 (3MB / 2s)[问题描述] 在古老的世界里,有一个神奇的职业叫做魔法师. 魔法师的特点是会魔法,施放魔法需要念咒语. 在古老的世界里,有一个神奇的职业叫做码农.码农的工作是帮助魔法师记 ...

  7. 9. InnoDB通用表空间

    9. InnoDB通用表空间 通用表空间是InnoDB 使用CREATE TABLESPACE语法创建的共享表空间.本节中的以下主题描述了常规表空间功能和功能: 通用表空间功能 创建通用表空间 将表添 ...

  8. float 和 clear

    float 特性1:可以为行内浮动元素设置宽高 <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  9. docker:安装

    文章来源:http://www.cnblogs.com/hello-tl/p/8901132.html 0.卸载旧版本 # yum remove docker \ docker-client \ do ...

  10. js总结(四):关于高性能

    参考<高性能网站建设进阶指南> 不仅仅关注页面加载时间,也要关注下页面操作时的相应速度.页面操作是我们写程序中 实实在在需要的 1.使用局部变量 任何非局部变量,在函数中使用次数超过一次时 ...