Description:

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.

给定一个字符串,求字符串中最长不重复的字串长度。

又是一道没有数据范围的题目,这里字符串长可以超过3w,所以暴力不可搞。最后用很巧妙的运用起点和重点位置的 顺序选择,和对字符先后出现位置的统计,来求得结果。

class Solution {

public:

int lengthOfLongestSubstring(string s) {

int maxLen = 0, start = -1;

map<char, int>mapChar;

for (int i = 0; s[i]; i ++) {

if (mapChar.count(s[i])) {//这里学了一招,map的count方法可以统计对应类型出现的次数,不用再dt的初始化了

start = max(start, mapChar[s[i]]);

}

mapChar[s[i]] = i;

maxLen = max(maxLen, i - start);

}

return maxLen;

}

};

【Leetcode 3】Longest Substring Without Repeating Characters0的更多相关文章

  1. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

  2. 【LeetCode】Longest Substring Without Repeating Characters 解题报告

    [题意] Given a string, find the length of the longest substring without repeating characters. For exam ...

  3. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

  4. 【leetcode】Longest Substring Without Repeating Characters (middle)

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

  5. 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

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

  6. 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

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

  7. 【Leetcode】【Medium】Longest Substring Without Repeating Characters

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

  8. 【leetcode刷题笔记】Longest Substring Without Repeating Characters

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

  9. leetcode题解 3. Longest Substring Without Repeating Characters

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

随机推荐

  1. 判断项目中是否有slf4j的实现类

    /** * 判断项目中是否有slf4j的实现类 */ @org.junit.Test public void test() { try { Enumeration<URL> resourc ...

  2. 【Codeforces 1118D1】Coffee and Coursework (Easy version)

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 从小到大枚举天数. 然后贪心地,从大到小分配a[i]到各个天当中. a[n]分配到第1天,a[n-1]分配到第2天,...然后a[n-x]又分 ...

  3. https://blog.csdn.net/u011495642/article/details/79958444

    https://blog.csdn.net/u011495642/article/details/79958444--- 一文读懂大数据平台——写给大数据开发初学者的话! 史上最详细的Hadoop环境 ...

  4. 撸呀撸的左手(KMP+DP)

    题目描述 撸呀撸很迷茫,因为他的左手总是不受控制,做一些不雅的事情.于是撸呀撸一狠心,决定戒撸.没想到,他的左手受不了寂寞,一闲下来就在键盘上各种乱敲. 唔,神奇的左手表示,safasfasaafaf ...

  5. 矩形面积求并(codevs 3044)

    题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行 ...

  6. Prim算法和Dijkstra算法的异同

    Prim算法和Dijkstra算法的异同 之前一直觉得Prim和Dijkstra很相似,但是没有仔细对比: 今天看了下,主要有以下几点: 1: Prim是计算最小生成树的算法,比如为N个村庄修路,怎么 ...

  7. NOIP2013 提高组合集

    NOIP 2013 提高组 合集 D1 T1 转圈游戏 快速幂裸题 #include <iostream> #include <cstdio> #include <cst ...

  8. HDU——4738 Caocao's Bridges

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. POJ 1475 推箱

    推箱 时限:n.2000MS   内存限制:n.131072K 提交材料共计: 6600   接受: 2263   特别法官 描述 想象一下你站在一个二维迷宫里,由方形细胞组成,它们可能或可能不会充满 ...

  10. 14、Java并发性和多线程-Java ThreadLocal

    以下内容转自http://ifeve.com/java-theadlocal/: Java中的ThreadLocal类可以让你创建的变量只被同一个线程进行读和写操作.因此,尽管有两个线程同时执行一段相 ...