题目:

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.
 

代码(C++实现):

 class Solution {
public:
int lengthOfLongestSubstring(string s)
{
// 定义一个map用来存放整个字符串s
unordered_map<char, int> unmap; // tempLength记录每次扫描位置开始的一次统计的最长字符串的长度
int tempLength = ;
// 众多tempLength中的最大值
int maxLength = ; // 第一层循环:分别以s中的每个字符为基准,进行遍历
for (int j = ; j < s.size(); j++)
{
// 第二层循环:以当前第一层循环中当前的字符为基准,进行遍历,统计以此字符为基准的tempLength
for (int i = j; i < s.size(); i++)
{
// 是否tempLength继续增加的条件是,map中没有出现过当前指向的字符
if (unmap.count(s[i]) == )
{
pair<char, int> myshopping(s[i], i);
// 如果当前的map中无此字符,将当前字符插入到map中
unmap.insert(myshopping);
tempLength++;
maxLength = maxLength > tempLength ? maxLength : tempLength;
}
// 当前字符已经在map中了,直接break,并将本次使用的map进行清除操作
else
{ tempLength = ;
unmap.clear();
break;
} }
} return maxLength;
}
};

【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters的更多相关文章

  1. 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

    这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...

  2. No.003:Longest Substring Without Repeating Characters

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

  3. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

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

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

  5. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  6. leetcode第三题--Longest Substring Without Repeating Characters

    Problem:Given a string, find the length of the longest substring without repeating characters. For e ...

  7. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  8. leetcode第三题Longest Substring Without Repeating Characters java

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

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

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

随机推荐

  1. 2018/12/20 20:52:42 螺纹钢PTA豆粕

    如期向上,但是一点办法没有:没有好的入场位,不做不算错,面对诱惑不动如山也是一种修养,今晚看M5有没有3买,有的话可以看情况考虑要不要进场 PTA M30向下一笔过程中,等待M30当前一笔下跌结束,可 ...

  2. 怎么精确控制solidworks里面的表格的位置

    手工移动是不可能的,总是有点误差,虽然有主动捕捉的功能. public void SetTablePosition(TableAnnotation table, double x, double y) ...

  3. 使用腾讯地图和js,html实现地理位置的获取

    转自http://blog.csdn.net/eele_one/article/details/64905218<!DOCTYPE html> <html> <head& ...

  4. iOS XML解析使用-韩国庆

    欢迎-------(北京-iOS移动开发金牌教师QQ:2592675215)韩老师给你带来XML解析课程 今天给大家讲解下xml解析的第三方简单用法:首先我解释下,json和xml解析格式. JSON ...

  5. java-框架-索引

    spring 整体了解 spring 入门demo Spring整体了解 spring梳理 Spring线程池的5个要素 spring的事务隔离级别以及传播性 事务4个隔离界别及脏读,不可重复读,幻读 ...

  6. Android 开发 values目录里定义数组、颜色、文本、尺寸xml配置文件并且获取数据 附录Android符号转码表

    以下xml都在res/values/文件夹下创建 创建String类型array: /app/src/main/res/values/array.xml <?xml version=" ...

  7. Tomcat 控制台出现乱码

    本地在启动tomcat时,控制台启动显示乱码 这是因为windows默认编码集为GBK,用startup.bat启动tomcat时,它会读取catalina.bat的代码并打开一个新窗口运行,打开的c ...

  8. Javascript 继承和多态

    近期通过一些巧合 或者说 思想转变吧 ... 想通过blog && 公众号 (个人公众号: KeepinJS)去记录自己的Javascript深度学习的内容,从而达到 进一步的自我提升 ...

  9. Awvs、Snort的下载安装

    学渗透测试是我对自己的奖赏. 这是Awvs环境的搭建. 推荐链接:https://www.cnblogs.com/show2008/p/10371461.html 这是Snort环境搭建. 推荐链接: ...

  10. Oracle所有分析函数<转>

    Oracle分析函数——函数列表 SUM        :该函数计算组中表达式的累积和 MIN        :在一个组中的数据窗口中查找表达式的最小值 MAX        :在一个组中的数据窗口中 ...