题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复。字符可能包含标点之类的,不仅仅是字母。按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 最长不重复子串的更多相关文章

  1. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  2. [LeetCode] Longest Substring Without Repeating Characters最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  3. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  4. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  5. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  6. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  7. leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  8. 003 Longest Substring Without Repeating Characters 最长不重复子串

    Given a string, find the length of the longest substring without repeating characters.Examples:Given ...

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

    只遍历一次字符串即可求出最长不重复子串的长度. int lengthOfLongestSubstring(string s) { vector<,-); //记录字符上一次出现的位置,ASCII ...

随机推荐

  1. 介绍两款常用的“图表统计图"的插件

    一.相信朋友们在开发的过程中都会使用到“数据统计”的功能,图表的统计更为直观,在这里就介绍两款插件:fusionChart.DataVisualization. 1.fusionChart实际项目中用 ...

  2. centos运行netcore error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.

    Error: Another program is already listening on a port that one of our HTTP servers is configured to ...

  3. es6实现类的多重继承

    1.类的多种继承,将多个类的接口“混入”(mix in)另一个类. function mix(...mixins) { class Mix { // 如果不需要拷贝实例属性下面这段代码可以去掉 // ...

  4. 洛谷P1072 Hankson 的趣味题

    P1072 Hankson 的趣味题 题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson 正在思考一 ...

  5. CF987C Three displays 暴力

    题意翻译 题目大意: nnn个位置,每个位置有两个属性s,cs,cs,c,要求选择3个位置i,j,ki,j,ki,j,k,使得si<sj<sks_i<s_j<s_ksi​< ...

  6. Codeforces Round #499 (Div. 1)部分题解(B,C,D)

    Codeforces Round #499 (Div. 1) 这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解. B ...

  7. 虚拟机上安装Cell节点(12.1.2.3.3)

    安装介质下载 打开firefox,输入:https://edelivery.oracle.com 点击"Sign In",输入帐号.密码,登陆edelivery网站.       ...

  8. java 使用 spirng 监控 cpu 使用 状态。。。。

    首先..使用 sigar ,sigar 使用  要 注意区分 是 web 还是 本地..最好 在 WEB-INF 下 复制 dll 文件..因为WEB-INF 不会被压缩... try { //如果是 ...

  9. js——移动端js事件、zepto.js

    1. touchstart : 手指放到屏幕上时触发 2. touchmove : 手指在屏幕上滑动时触发 3. touched : 手指离开屏幕时触发 4. touchcancel : 系统取消to ...

  10. linux读取yaml文件的某个属性值

    trigger=$(cat test.yaml | grep "trigger" | awk '{print $2}') 该条命令的意思是:读取test.yaml文件中的trigg ...