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. vue列表渲染,以及鼠标点击改变样式的问题

    在实际项目中,我们进场会遇到鼠标点击该表某个DOM元素的样式,在原生的js或者jquery中,我们会比较熟练的实现这个需求,但是在vue中怎么实现呢? 直接操作DOM?NO!NO! 既然我们的项目使用 ...

  2. 51nod1128 正整数分组V2

    [题解] 二分一个最大值,check一下分出来的组数是否小于等于k即可. #include<cstdio> #include<algorithm> #define LL lon ...

  3. 关于 startup_stm32f10x_hd.s 这个文件的一些说明

    关于 startup_stm32f10x_hd.s 这个文件的一些说明 startup_stm32f10x_hd.s 是一个启动文件,里面是使用汇编语言写好的基本程序,当STM32 芯片上电启动的时候 ...

  4. nyoj 19 擅长排列的小明(深搜,next_permutation)

    擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...

  5. MongoDB 数据文件备份与恢复

    备份与恢复数据对于管理任何数据存储系统来说都是非常重要的. 1.冷备份与恢复——创建数据文件的副本(前提是要停止MongoDB服务器),也就是直接copy MongoDB将所有数据都存储在数据目录下, ...

  6. Codeforces Round #240 (Div. 2) C Mashmokh and Numbers

    , a2, ..., an such that his boss will score exactly k points. Also Mashmokh can't memorize too huge ...

  7. 布局(codevs 1242)

    题目描述 Description 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们 ...

  8. sql自增长和占位符?"相矛盾"的问题

    1.对于sql server数据当数据被定义为自增长时,插入,无法将那个位置用字符占位,我们可以使用部分插入的方法来做.  insert into users (username,email,grad ...

  9. Non-inclusive cache method using pipelined snoop bus

    A non-inclusive cache system includes an external cache and a plurality of on-chip caches each havin ...

  10. 5-14 电话聊天狂人 (25分) HASH

    给定大量手机用户通话记录,找出其中通话次数最多的聊天狂人. 输入格式: 输入首先给出正整数NN(\le 10^5≤10​5​​),为通话记录条数.随后NN行,每行给出一条通话记录.简单起见,这里只列出 ...