题目描述

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. [转载]workbench分网---mapped face meshing面映射网格划分

    原文地址:face meshing面映射网格划分">workbench分网---mapped face meshing面映射网格划分作者:一丝尘埃 face meshing面映射网格划 ...

  2. Java 面向对象(十四)

    反射 反射是框架设计的灵魂 一.类的加载时机 当程序要使用某个类时,如果该类还未被加载到内存中,系统会通过加载,连接,初始化三步来实现对这个类进行初始化. 加载 :就是指将class文件读入内存,并为 ...

  3. legend3---lamp.sh常用操作

    legend3---lamp.sh常用操作 一.总结 一句话总结: 1.github上下载代码 2.修改项目数据库配置,修改文件权限等操作:chown -R apache:apache /data/w ...

  4. P5662 纪念品

    P5662 纪念品 题解 拿到题目想到DP,但是就是不知道咋写 后来证实这是个背包DP(最近整理背包白整了 我们观察这道题目的特殊之处: 也就是说,对于手中的物品,我们可以今天买了然后明天早上接着卖出 ...

  5. OpenJudge计算概论-人民币支付

    /*========================================================== 人民币支付 总时间限制: 1000ms 内存限制: 65536kB 描述 从键 ...

  6. OS X环境下如何搭建编译Cocos2D-X v3.x的Android Studio工程

    Cocos2D-X官网已经简单介绍了如何在OS X环境下搭建Cocos2D-X v2.x和v3.x的指南.具体链接为:http://www.cocos.com/doc/article/index?ty ...

  7. C++11消息队列 + Qt线程池 + QRunnable执行任务简单模型

    1.模板类queue,包含头文件<queue>中,是一个FIFO队列. queue.push():在队列尾巴增加数据 queue.pop():移除队列头部数据 queue.font():获 ...

  8. Qwidget::update

    void QWidget::update ()分析重绘事件激活 1看看手册中这段话 void QWidget::update () [slot] Updates the widget unless u ...

  9. [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/nginx/conf/nginx.conf:1

    带有sudo 权限执行就可以了

  10. Java下载HTTP URL链接示例

    这里以下载迅雷U享版为例. 示例代码: package com.zifeiy.snowflake.handle.filesget; import java.io.File; import java.i ...