LeetCode Longest Substring Without Repeating Characters 最长不重复子串

题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复。字符可能包含标点之类的,不仅仅是字母。按ASCII码算,就有2^8=128个。
思路:从左到右扫每个字符,判断该字符距离上一次出现的距离是多少,若大于max,则更新max。若小于,则不更新。每扫到一个字符就需要更新他的出现位置了。这里边还有个注意点,举例说明:
假如有长为16串 s="arbtbqwecpoiuyca"
当扫到第2个b时,距离上一个b的距离是2;(直接减)
当扫到第2个c时,距离上一个c的距离是6;(直接减)
但是!当扫到第2个a时,距离上一个a的距离是15,可是这串里面已经有b和c都有重复的了,是不符合的。真正的长 = 第2个a的位置 - 第1个c的位置。
假设当前扫到的字符为'A',其实求长的式子应该这样的:len = i - max(cur,pos[A])
这里的cur是指一个字符的位置,该字符是距离A最近的,并且在该字符与A之间还会出现该字符一次,(也就是在两个A之间,如果有出现次数为两次的字符,记录下第1个字符的位置,若多次出现,记录从右数第2次出现该字符的位置)这个cur的值要随时更新。
注:坑!这个算法的复杂度完全的O(n),实在强大。想了2天,我本来想到用哈希来做,感觉有点麻烦,一直在想更简单的,下面这个别人的代码实在简洁到没办法了,佩服。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int locs[];//保存字符上一次出现的位置
memset(locs, -, sizeof(locs));
int idx = -, max = ;//idx为当前子串的开始位置-1
for (int i = ; i < s.size(); i++)
{
if (locs[s[i]] > idx)//如果当前字符出现过,那么当前子串的起始位置为这个字符上一次出现的位置+1
{
idx = locs[s[i]];
}
if (i - idx > max)
{
max = i - idx;
}
locs[s[i]] = i;
}
return max;
}
};
Longest Substring Without Repeating Characters
上面代码一字不差复制过来了。
LeetCode Longest Substring Without Repeating Characters 最长不重复子串的更多相关文章
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] 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 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- [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. Examples: ...
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 003 Longest Substring Without Repeating Characters 最长不重复子串
Given a string, find the length of the longest substring without repeating characters.Examples:Given ...
- Longest Substring Without Repeating Characters 最长不重复子串
只遍历一次字符串即可求出最长不重复子串的长度. int lengthOfLongestSubstring(string s) { vector<,-); //记录字符上一次出现的位置,ASCII ...
随机推荐
- C# 写 LeetCode Medium #2 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- The Little Prince
Chapter 1 That is why, at the age of six, I gave up what might have been a magnificant career as a p ...
- 2017BAPC初赛A(思维,无序图,向量)
#include<bits/stdc++.h>using namespace std;string goods,sister[100010];int x,m;unordered_map&l ...
- Ocelot(二)- 请求聚合与负载均衡
Ocelot(二)- 请求聚合与负载均衡 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/10865511.html 源码地址: ...
- c# Relative Path convert to Absolute Path
Reference: http://stackoverflow.com/questions/4796254/relative-path-to-absolute-path-in-c http://sta ...
- Java基础--正则表达式的规则
注意:正则表达式只关注格式是否正确,不关注内容是否有效. 一.字符集合, []表示一个字符. 1.[abc] :指a,b,c中的任意一个字符. 2.[^abc]:指除了a,b,c外的任意字符. 3.[ ...
- 微信小程序-修改单选框和复选框大小的方法
方法有两种: 一:采用css的zoom属性 zoom缩放会将元素保持在左上角,并且会有毛边,可能会稍稍改变元素原来的形状. 二:采用css3的transform:scale属性 zoom缩放会将元素保 ...
- n个点的基环树数量
某裴姓蒟蒻上午提了一个小问题(rt)..然后他升华了..升华之前感受到了神犇的力量... 方法一: g[n][k]表示n个点,k条边的无向图(不一定连通) f[n][k]表示表示n个点,k条边的无向连 ...
- docker jvm 占用高的问题定位
定位流程 先使用一些轻便的工具查看总体情况, 如果情况糟糕, 再使用重量级的工具 jstack 查看线程数是否过多 jstat -gc -gcutil 查看gc次数和时间是否过多, 各个分 ...
- html 页面跳转方式
网页的跳转在很多时候都非常的有用,下面的是两个简单的例子.仅供参考. 一.用<meta>里直接写刷新语句:5秒后跳转到百度 <html> <head> <me ...