题目描述

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

Example 1:

Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

参考答案

 class Solution {
public:
int lengthOfLongestSubstring(string s) { unordered_map<char,int> map;
int res = ;
int start = -; // 存储开始的位置,如果完全没有重复的字符,那么开始位置永远是-1 for(int i = ;i<s.length();i++){
if(map.find(s[i]) != map.end()){
start = max(start,map[s[i]]);
// 如果通过find 找到了一个重复的
// 那么就把start给替换成 上一个重复字符的index
}
map[s[i]] = i; // map[char] = index;
res = max(res,i-start); // 获得最大值
}
return res;
}
};

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

Example 1:

Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

LC 3. Longest Substring Without Repeating Characters的更多相关文章

  1. LN : leetcode 3 Longest Substring Without Repeating Characters

    lc 3 Longest Substring Without Repeating Characters 3 Longest Substring Without Repeating Characters ...

  2. LeetCode[3] 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 example, ...

  4. Longest Substring Without Repeating Characters

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

  5. 3. Longest Substring Without Repeating Characters(c++) 15ms

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

  6. 【leetcode】Longest Substring Without Repeating Characters

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

  7. Longest Substring Without Repeating Characters(C语言实现)

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

  8. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  9. [LeetCode_3] Longest Substring Without Repeating Characters

    LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...

随机推荐

  1. linux 编写定时任务,查询服务是否挂掉

    shell 脚本 #!/bin/bash a=`netstat -unltp|grep fdfs|wc -l` echo "$a" if [ "$a" -ne ...

  2. CSS 之实现单行、多行文本溢出显示省略号

    一.单行文本省略 实现方法: overflow: hidden; text-overflow:ellipsis; white-space: nowrap; 二.多行文本省略 实现方法: display ...

  3. 为Python配置Vim编辑器(GUI/非GUI皆可)

    原文地址:https://blog.csdn.net/alanzjl/article/details/49383943 Vim as a python IDE ** 最近一直在写Python,但一直没 ...

  4. case设计及验证:入口+页面+展示

    测试个性CB问题, 功能整体结构为:入口+页面+展示 总结: 1. 产品文档为主,其次是服务端接口返回.数据结构及字段值确认.结合实际场景检查是否有遗漏或不合理. 2. 以字段为维度,每个字段的检查点 ...

  5. 一键分享QQ、微信、微博等

    github上找到的,合并了一个二维码在线支持API,直接修改样式可用. 二维码API说明网址:http://www.liantu.com/pingtai/ <html> <head ...

  6. 为什么ArcGIS 10.3导出 Shapefile的字段名会被截断成3个汉字?解决方法如下

    为什么ArcGIS 10.3导出 Shapefile的字段名会被截断成3个汉字?低版本中不是至少可以存储4个汉字吗?原因这个问题仍然与编码类型有关.ArcGIS 10.2 以及更早的版本,ArcGIS ...

  7. <JavaScript>几道javascript练习题

    问题1: 作用域(Scope) 考虑以下代码: (function() { var a = b = 5; })(); console.log(b); 控制台(console)会打印出什么? 答案 上述 ...

  8. C#-片段-插入片段:Visual C#

    ylbtech-C#-片段-插入片段:Visual C# 1.返回顶部 ·#if #if true #endif ·#region #region MyRegion #endregion · 2.返回 ...

  9. Ionic4.x Javascript 扩展 ActionSheet Alert Toast Loading 以及 ionic 手势相 关事件

    1.ActionSheet 官方文档:https://ionicframework.com/docs/api/action-sheet <ion-header> <ion-toolb ...

  10. android滑动标题栏渐变实现

    import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.sup ...