LeetCode Algorithm 03_Longest Substring Without Repeating Characters
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.
Tags: Hash Table, Two Pointers, String
class Solution {
public:
int lengthOfLongestSubstring(string s) {
//从前往后开始,后面一旦碰到『当前字符串中』前面出现过的字符,则直接跳到前面那个重复字符的下一个字符开始新字符串统计。
//既然是比较当前字符串,必须要有一个值来保存当前字符串从什么位置之后开始。可以初始化一个int curSubStartPosPre = -1表示
//一开始是从-1后面也就是0开始统计的,字符串长度就是后面的index-(-1),如1-(-1)=2表示当前字符串长度达到了2。
//重复出现的判断方法:构建一个以字符为键的数组,因为字符不超过256,可构建int lastAppearPos[256],存储每个字符上一次
//出现的位置,当扫描到一个字符时,查看数组中对应值lastAppearPos[字符],如果大于curSubStartPosPre,说明这个字符上一次
//出现位置在当前字符串中,也就意味着当前字符串出现重复。
//随着字符的移动,当前的长度为index-curSubStartPosPre。保存一个max来表示最大值,如果当前长度大于max则更新max。
//lastAppearPos[256]:本来是index对应字符,现在以字符为下标对应index,完成hash table的构建。因为直接比较的是字符,
//用hash table可以实现o(1)的复杂度。
int curSubStartPosPre = -;
int lastAppearPos[];
int max=;
memset(lastAppearPos, -, sizeof(lastAppearPos));
int i=;
while(i<s.length()){
if(lastAppearPos[s[i]] > curSubStartPosPre){
curSubStartPosPre = lastAppearPos[s[i]];
}
if(i-curSubStartPosPre > max){
max = i-curSubStartPosPre;
}
lastAppearPos[s[i]] = i;
i++;
}
return max;
}
};
LeetCode Algorithm 03_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 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- [Algorithm] Longest Substring Without Repeating Characters?
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- 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 ...
随机推荐
- 手把手教你用vue-cli构建一个简单的路由应用
上一章说道:十分钟上手-搭建vue开发环境(新手教程)https://www.jianshu.com/p/0c6678671635 开发环境搭建好之后,那么开始新添加一些页面,构建最基本的vue项目, ...
- 00076_BigDecimal
1.在程序中执行下列代码,会出现什么问题? System.out.println(0.09 + 0.01); System.out.println(1.0 - 0.32); System.out.pr ...
- PatentTips - Virtual translation lookaside buffer
BACKGROUND OF THE INVENTION A conventional virtual-machine monitor (VM monitor) typically runs on a ...
- tcp_tw_recycle检查tcp_timestamps的内核代码
注意:本文档中的内核代码的版本号:linux-4.0.5 /************************************************* * Author : Samson * ...
- RvmTranslator6.3 is released
RvmTranslator6.3 is released eryar@163.com RvmTranslator can translate the RVM file exported by AVEV ...
- 60.浅谈nodejs中的Crypto模块
转自:https://www.cnblogs.com/c-and-unity/articles/4552059.html node.js的crypto在0.8版本并没有改版多少,这个模块的主要功能是加 ...
- Javascript函数的基本概念+匿名立即执行函数
函数声明.函数表达式.匿名函数 函数声明:function fnName () {…};使用function关键字声明一个函数,再指定一个函数名,叫函数声明. 函数表达式 var fnName = f ...
- 混合式框架-AngularJS
简单介绍 AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门非常好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也能够认 ...
- 安装 VNC 和 XRDP
安装 VNC 和 XRDPapt-get install vnc4server tightvncserver xrdp fluxbox xtermcat >vncstop.sh << ...
- 根证书 CA
根证书 CA 密钥没有密码,使用下面的指令添加密码 openssl rsa -aes256 -in cakey_nopw.pem -out cakey_pw.pem cacert.pem -----B ...