题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复。字符可能包含标点之类的,不仅仅是字母。按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. .net core webapi +ddd(领域驱动)+nlog配置+swagger配置 学习笔记(2)

    DDD领域驱动模型设计 什么是DDD 软件开发不是一蹴而就的事情,我们不可能在不了解产品(或行业领域)的前提下进行软件开发,在开发前,通常需要进行大量的业务知识梳理,而后到达软件设计的层面,最后才是开 ...

  2. OC官方文档翻译-Values-and-Collections-值与集合类型

    查看全部文档翻译,请浏览https://github.com/L1l1thLY/Programming-with-Objective-C-in-Chinese,blog仅收录本人翻译的两章. 简述 O ...

  3. java线程并发工具类CyclicBarrier、CountDownLatch及Semaphore

    一.CyclicBarrier   (原文链接:http://www.studyshare.cn/blog-front/blog/index ) 1.定义 CyclicBarrier是线程并发工具类之 ...

  4. C++ List的用法(转载)

    Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢. assign() 给list赋值  back() 返回最后一个元素  begin ...

  5. 【转至hejinde的专栏】Axure RP 8最新激活码(可用注册码)

    Licensee:米 业成 (STUDENT)Key:nFmqBBvEqdvbiUjy8NZiyWiRSg3yO+PtZ8c9wdwxWse4WprphvSu9sohAdpNnJK5 亲测可用

  6. Jmeter report优化

    优化大致过程 生成并的报告模板: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet ...

  7. 洛谷P5279 [ZJOI2019]麻将

    https://www.luogu.org/problemnew/show/P5279 以下为个人笔记,建议别看: 首先考虑如何判一个牌型是否含有胡的子集.先将牌型表示为一个数组num,其中num[i ...

  8. PullToRefreshListView

    @Override protected void onRefreshing(final boolean doScroll) { /** * If we're not showing the Refre ...

  9. Ubuntu apt-get update中断的时候会出现一个错误导致不能再apt-get update

    错误描述为:Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable) E: Un ...

  10. 牛客网训练赛26D(xor)

    题目链接:https://www.nowcoder.com/acm/contest/180/D 线性基的学习:https://www.cnblogs.com/vb4896/p/6149022.html ...