lc 3 Longest Substring Without Repeating Characters


3 Longest Substring Without Repeating Characters

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

Examples:

  1. Given "abcabcbb", the answer is "abc", which the length is 3.
  2. Given "bbbbb", the answer is "b", with the length of 1.
  3. Given "pwwkew", the answer is "wke", with the length of 3. Note that

    the answer must be a substring, "pwke" is a subsequence and not a

    substring.

动态规划 Accepted##

index数组用来标记该位是否第一次出现,invalid变量用来表示当前研究的子串头部的前一位,length用来表示当前研究的子串的长度。用动态规划的方法可以把时间复杂度降到最低的O(n)。

class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> index(256, -1);
int length =0;
for (int invalid = 0, i = 0; i < s.length(); i++) {
invalid = max(index[s[i]]+1, invalid);
index[s[i]] = i;
length = max(length, i-invalid+1);
}
return length;
}
};

LN : leetcode 3 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 ...

随机推荐

  1. Codeforces Educational Round 23

    A emmmmmmmmm B emmmmmmmmm C(套路) 题意: 给定n和s(n,s<=1e18),计算n以内有多少个数x满足(x-x的各个位置数字之和)>=s 分析: 容易想到如果 ...

  2. 怎么让Excel显示时间时候能把秒显示出来

    Excel显示时间一般只显示年月日小时分钟怎么能够把秒也显示出来既如下显示 2007-04-11 12:00:00 将单元格格式设为"自定义",在"类型"框中输 ...

  3. mysql导入大型sql文件时注意事项

    原文:http://blog.csdn.net/k21325/article/details/70808563 大型sql文件,需要在my.ini(windows)或者my.cnf(Linux)中设置 ...

  4. Django学习系列之路由系统

    一.基于App的路由 作用: 根据App对路由规则进行分类,在全局urls.py包含某个项目的urls.py 示例 定义全局urls.py(全局路由系统) #导入include from django ...

  5. A. Polo the Penguin and Strings

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  6. Nginx + FastCgi + Spawn-fcgi + C 架构的server环境搭建

    1.Nginx 1.1.安装 Nginx 的中文维基 http://wiki.codemongers.com/NginxChs 下载 Nginx 0.6.26(开发版)(请下载最新版本号) tar z ...

  7. hdu 1258 Sum It Up (dfs+路径记录)

    pid=1258">Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  8. 2016/1/6 输出菱形 while语句计算阶乘分数之和

    public class LingXing { public static void main(String[] args) { //打印菱形 for (int x=1;x<6;x++){ fo ...

  9. (一)Java 入门教程

    Java 入门教程 Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言. Java可运行于多个平台,如Windows, Mac OS,及其他多种UNIX版本的系统 ...

  10. [翻译]NUnit---Condition Asserts && Comparisons Asserts && Type Asserts (五)

    网址:http://www.cnblogs.com/kim01/archive/2013/03/31/2991597.html Condition Asserts 测试指定条件的方法称作条件测试,测试 ...