一. 题目描写叙述

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. nginx 1.15.10 前端代理转发 将多个地址,代理转发到一个地址和端口 多系统公用一个cookie 统一token

    nginx 1.15.10 前端代理转发 将多个地址,代理转发到一个地址和端口 多系统公用一个cookie 统一token 注意: proxy_pass http://192.168.40.54:22 ...

  2. input_shape { dim: 1 dim: 3 dim: 224 dim: 224 }

    http://blog.csdn.net/u010417185/article/details/52619593

  3. Eclipse Code Recommenders 自动补全(联想)神器

    Eclipse Code Recommenders 可以在eclipse市场中下载.自动补全.模糊匹配.非常有用!

  4. 【讲●解】KMP算法

    KMP算法 我们小组负责讲这个... 术语与规定 为了待会方便,所以不得不做一些看起来很拖沓的术语,但这些规定能让我们更好地理解\(KMP\)甚至\(AC\)自动机. 字符串匹配形式化定义如下: 假设 ...

  5. 自定义函数导致的sql性能问题

    同事说,某某报表跑的很慢,让我调查一下 优化前:该报表整体需要跑4小时以上. sql代码如下 SELECT /*省略多数查询字段*/ REP_FUN_REFCODEVALUE /*自定义函数*/ (P ...

  6. 在docker中部署nginx

    1端口映射 大写P  为容器暴漏的所有端口进行映射   -p ,--publish-all=true    docker run -P -it centos  /bin/bash 小写p  指定哪些容 ...

  7. LINUX:关于Redis集群搭建 、和搭建项目中遇到的问题

    文章来源:http://www.cnblogs.com/hello-tl/p/7804225.html 0.Redis的简单安装 1.安装redis依赖 # yum install gcc tcl g ...

  8. laravel使用总结(一)

    安装 composer create-project laravel/laravel learnlaravel5 --prefer-dist v5.3.* 安装成功之后会自动生成一个key > ...

  9. Ducci Sequence解题报告

    A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... ,  ...

  10. 【MFC】利用MFC写一个计时器小程序

    1整体设计 创建对话框程序,并且设计对话框相关控件如图 相应的ID和对应的成员变量如图: 我的想法是这样的,只读属性的编辑框添加有CString类型的成员变量(如s_hour),在xxxDlg.h里另 ...