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 ...
随机推荐
- 彻底来理解下hashmap吧
1.什么叫hashmap? 答:首先是一种map集合,其次呢,它是一种利用hash表来存储的数据结构.所以叫hashmap. 2.hashmap的特点是什么? 答:hashmap的特点是key值不能重 ...
- springMVC多数据源使用 跨库跨连接
原文:http://blog.itpub.net/9399028/viewspace-2106641/ http://blog.csdn.net/a973893384/article/details/ ...
- Android系统开发(4)——Autotools
Autotools工具的构成 1.autoscan autoscan是用来扫描源码文件夹生成configure.san文件的,configure.san包括了系统配置的基本选项.里面都是一些宏定义,我 ...
- AutoCAD如何方便截图放到Word文档,改成白底黑字
将模型视图切换到布局2即可 比如下图所示的效果 先回到模型视图把所有线条颜色都改成白色,然后添加适当的标注(比如要受力分析,则在CAD中绘制箭头也很方便的),文字说明.然后切换到布局2就OK ...
- vuex 存值 及 取值 的操作
1.传值 // 定义参数 let params = { workItemId: workItemId, flowInstId: flowInstId, itemStatus: itemStatus, ...
- MySQL Study之--MySQL压力測试工具mysqlslap
MySQL Study之--MySQL压力測试工具mysqlslap 一.Mysqlslap介绍 mysqlslap是MySQL5.1之后自带的benchmark基准測试工具,类似Apache ...
- nginx负载均衡向后台传递參数方法(后端也是nginxserver)
做了一个站点是用nginx 做的负载均衡.后端也是多个nginxserver 遇到了一个问题.当做SSL支持时 前端nginx分发到 后端nginx后就成 http形式了(这样后台php用$_SERV ...
- Android学习笔记-tween动画之java实现
Android动画分为Tween动画和Frame动画,近期学习了,体tween动画,现在讲学习的心得以及相关知识介绍如下. Tween又称为补间动画,可以把对象进行缩小.放大.旋转和渐变等操作. ...
- [办公自动化]excel工作簿内的表无法删除,单击右键无删除键
今天同事问,我自己的工作簿,没有设置保护,但是就是无法删除其中的工作表. 后来发现,她的excel工作簿打开的文件名后面显示[共享]. 原因找到了. 取消共享就可以了.
- SQl 事物+视图+游标+索引+锁
一:事务: 是访问并可能更新数据库中各种数据项的一个程序执行单元(unit),事务是恢复和并发控制的基本单位. 事务的四个特性:ACID A:atomicity 原子性,事务里的所有操作都是一体的,要 ...