题目描述

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. python3编程基础之一:操作

    基本操作有:读数据.写数据.运算.控制.输入.输出.语句块 1.读取数据: num1 = 50 num2 = num1 //通过num2取得num1的值,这就是逻辑上的读取 测试数据:print(nu ...

  2. vsCode中如何根据屏幕宽度自动换行

      在设置中搜索editor.wordWrap   文章来源:外星人来地球 欢迎关注,有问题一起学习欢迎留言.评论

  3. php - ftp 上传文件到远程服务器

    ccentos7服务器 ======================== 一.安装vsftpd及ftp命令 yum install vsftpd -y yum install ftp -y 二.vsf ...

  4. sqlserver 动态sql执行execute和sp_executesql

    sp_executesql的运用 书写语法要点: exec sp_executesql @sql,N’参数1 类型1,参数2 类型2,参数3 类型3 OUTPUT’,参数1,参数2,参数3 OUTPU ...

  5. SQL-W3School-函数:SQL LEN() 函数

    ylbtech-SQL-W3School-函数:SQL LEN() 函数 1.返回顶部 1. LEN() 函数 LEN 函数返回文本字段中值的长度. SQL LEN() 语法 SELECT LEN(c ...

  6. 编译freeglut

    下载freeglut  http://freeglut.sourceforge.net/ 1>------ 已启动生成: 项目: CallbackMaker, 配置: Debug x64 --- ...

  7. 123457123457#0#-----com.yimeng.TouNaoWangZhe--前拼后广--brain游戏one

    com.yimeng.TouNaoWangZhe--前拼后广--brain游戏one

  8. 【Leetcode_easy】766. Toeplitz Matrix

    problem 766. Toeplitz Matrix solution1: class Solution { public: bool isToeplitzMatrix(vector<vec ...

  9. 配置Linux描述网络安全CIA模型之可用性案例

    在Linux中防御SYN型DOS攻击的方法比较常见的有: 1.增大队列SYN最大半链接数 2.利用SYN cookie技术   下面分别进行分析. 1.增大队列SYN最大半连接数 在LINUX中执行命 ...

  10. 神经网络与机器学习第3版学习笔记-第1章 Rosenblatt感知器

    神经网络与机器学习第3版学习笔记 -初学者的笔记,记录花时间思考的各种疑惑 本文主要阐述该书在数学推导上一笔带过的地方.参考学习,在流畅理解书本内容的同时,还能温顾学过的数学知识,达到事半功倍的效果. ...