LN : leetcode 3 Longest Substring Without Repeating Characters
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:
- Given "abcabcbb", the answer is "abc", which the length is 3.
- Given "bbbbb", the answer is "b", with the length of 1.
- 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的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
- Leetcode 3. Longest Substring Without Repeating Characters (Medium)
Description Given a string, find the length of the longest substring without repeating characters. E ...
- [Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
随机推荐
- IDEA新手下载及和eclipse,myeclipse的区别
一:下载安装包,我们可以去官网下载.下载地址:http://www.jetbrains.com/idea/download/#secation=windows 二:IDEA的安装: 1.下载安装包后双 ...
- NOIP 2010 机器翻译
P1540 机器翻译 题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于 ...
- 洛谷 P1166 打保龄球
P1166 打保龄球 题目描述 打保龄球是用一个滚球去打击十个站立的柱,将柱击倒.一局分十轮,每轮可滚球一次或多次,以击倒的柱数为依据计分.一局得分为十轮得分之和,而每轮的得分不仅与本轮滚球情况有关, ...
- 推荐IOS开发3个工具:Homebrew、TestFight、Crashlytics-b
1. Homebrew 什么是Homebrew? Homebrew is the easiest and most flexible way to install the UNIX tools App ...
- 怎样把引用的jar包和本项目一起导出成jar文件
之所以要导出Runnable JAR.是由于我们希望将引用到的Jar包与本项目一起进行导出,所以不要选Jar file 选File/Export...然后Java/Runnable JAR file, ...
- HttpURL连接远程serverGet和Post方式请求并返回数据
查看原文:http://www.ibloger.net/article/1813.html package cn.gis; import java.io.BufferedReader; import ...
- Ubuntu虚拟机安装遇到的各种坑
配置 13年Macbook Pro 虚拟机环境 Parallels Desktop Linux 版本 Ubuntu 16.04 1.分辨率问题 进入只有一种分辨率 终端输入 sudo xdiagnos ...
- java web项目的部署
java web项目的部署 我刚开始学着编写java web项目,着实遇到不少麻烦,感觉JAVA真难侍候,好多东西都是手动.手动. 就拿这个web项目在tomcat上的部署来说吧.我在项目的build ...
- gradle使用笔记
1 gradle user home 默认情况下是-/.gradle目录.可以使用gradle -g [directory]修改. 1.1 ./gradle/caches gradle下载的所有的依赖 ...
- 展开阅读全文 js 爬虫操作
from selenium import webdriver import time import random from bs4 import * browser = webdriver.Chrom ...