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 ...
随机推荐
- cogs 1446. [Commando War,Uva 11729]突击战
1446. [Commando War,Uva 11729]突击战 ★ 输入文件:commando.in 输出文件:commando.out 简单对比时间限制:1 s 内存限制:64 ...
- (七十一)关于UITableView退出崩溃的问题和滚动究竟部的方法
[TableView退出崩溃的问题] 近期在使用TableView时偶然发如今TableView中数据较多时,假设在滚动过程中退出TableView到上一界面.会引起程序的崩溃.经过网上查阅和思考我发 ...
- ethercat主站控制软件TwinCAT的安装
TwinCAT软件系统是基于PC兼容机的自己主动化系统,全称是"The Windows Control and Automation Technology".它把不论什么PC兼容机 ...
- OpenCASCADE Extended Data Exchange - XDE
OpenCASCADE Extended Data Exchange - XDE eryar@163.com Abstract. OpenCASCADE Data Exchange allows de ...
- Java的线程机制
一.Java中实现多线程的两种方式1) 继承Thread类 Thread类包括了包括和创建线程所需的一切东西. Thread 最重要的方法是 run().编写线程程序时须要覆盖 run() 方法,ru ...
- jquery源码10-提交的数据和ajax()
{ var r20 = /%20/g, //全部空格 rbracket = /\[\]$/, //结尾位置匹配中括号 rCRLF = /\r?\n/g, rsubmitterTypes = /^(?: ...
- 76.Nodejs Express目录结构
转自:https://blog.csdn.net/xiaoxiaoqiye/article/details/51160262 Express是一个基于Node.js平台的极简.灵活的web应用开发框架 ...
- sql创建外键
建立外键关系:先建主表再见从表:主表:create table zhu(code int parimary key,name varchar(20)) ;从表:create table cong(co ...
- idea配置spark运行模式
1. 配置运行参数: Menu -> Run -> Edit Configurations -> 选择 + -> Application -Dspark.master=lo ...
- PHP 7.1安装xhprof进行性能分析
安装扩展该 xhprof扩展版本是从 https://github.com/longxinH/xhprof 获取的(第三方的一个库,官方版本不支持php7) 下载并编译xhprof扩展在web的htm ...