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 ...
随机推荐
- web安全:防范点击劫持的两种方式
防范点击劫持的两种方式 什么点击劫持?最常见的是恶意网站使用 <iframe> 标签把我方的一些含有重要信息类如交易的网页嵌入进去,然后把 iframe 设置透明,用定位的手段的把一些引诱 ...
- 【Uva 1627】Team them up!
[Link]: [Description] 给你n个人; 有一些人之间有认识关系 a认识b,b不一定认识a 让你把这n个人分成两组 使得这两组中的每一组: 组内的人与人之间都相互认识. 并且,使得两组 ...
- 动态规划 LCS,LIS
1.最大连续子序列 dp[i]=max(dp[i-1]+a[i],a[i]) 以i为结尾 2.最大不连续子序列 dp[i]=max(dp[j]+a[i],dp[j]) 3.最大连续递增子序列 if a ...
- 一次失败的PHP扩展开发之旅
一次失败的PHP扩展开发之旅 By warezhou 2014.11.19 缘起 经过不断的持续迭代.我们部门的协程版网络框架(CoSvrFrame)最终出炉了!这本来是件喜大普奔的事情.可是随着新业 ...
- Java的IO流架构
输入输出是指应用程序与外部设备及其他计算机进行数据交流的操作,如读写硬盘数据.向显示器输出数据.通过网络读取其他节点的数据等.任何一种编程语言必须拥有输入输出的处理方式,Java语言也不例外.Java ...
- 在电子商务里,一般会提到这样几个词:商品、单品、SPU、SKU
简单理解一下,SPU是标准化产品单元,区分品种:SKU是库存量单位,区分单品:商品特指与商家有关的商品,可对应多个SKU. 首先,搞清楚商品与单品的区别.例如,iphone是一个单品,但是在淘宝上当很 ...
- RPC简易学习
0.RPC简介 RPC, 英文全称:Remote Process Call. 中文全称:远程过程调用. 客户端通过网络请求调用远程服务端对外暴露服务.常用的两种RPC协议:TCP.HTTP. ...
- Ubuntu+PyQt5+Python3.6+Qt Designer 实现可视化窗口的编辑
一.为什么写这片博文 近期将实验室的电脑的OS换成了ubuntu,想对linux进一步的了解和使用.在使用的过程中想用python+pyqt5写一个音乐播放器和视频播放器(这也是linux的乐趣所在) ...
- 数据库事务及其EF中如何处理事务
一.基础知识 1) 使用事务级别ReadUnCommited 会产生脏读现像,意味着读取到的为UnCommited(未提交)的数据.怎么理解呢?在使用该隔离级别的事务开始后.更新了数据 ...
- IOS越狱开发错误解决
Questions: haseScriptExecution Run\ Script /Users/jun/Library/Developer/Xcode/DerivedData/ButtonMa ...