题目:

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. C#后台接java接口传输字节数组(byte[])

    事情是这样的C#t代码之前接的WCF接口,后来那边统一改为java的接口,我是用的HttpClient从后台发请求调用的java接口,其他接口都很顺利,是的....知道遇到一个需要传byte[]类型数 ...

  2. Centos7修改默认网卡名(改为eth0)以及网卡启动报错RTNETLINK answers: File exists处理

    安装好centos7版本的系统后,发现默认的网卡名字有点怪,为了便于管理,可以手动修改.下面对centos7版本下网卡重命名操作做一记录:1)编辑网卡信息[root@linux-node2~]# cd ...

  3. MySQL查询语句报错 sql_mode=only_full_group_by 问题

    升级MySQL到5.7后,查询语句总是报sql_mode=only_full_group_by问题,总结归纳了两种解决方案,推存第二种解决方案. 报错信息: [Err] 1055 - Expressi ...

  4. windows2008 apache2.4 tomcat-7多域名绑定环境配置

    =====================软件清单Apache2.4.33apache-tomcat-7.0.85===================== 1.安装apache    1.1下载ap ...

  5. Win8 & WP8.1 轻型数据库

    自Win8 和 WP8.1 以来,MS把SQL CE去掉了.为了方便自己的APP升级,减少代码改动量,所以写了个小型的数据库管理,参考WP8.1之前的DataContext. 用时较短,花了几天的时间 ...

  6. linux Ubuntu 16.04安装 postgresql

    两次的重装系统安装数据库折磨的死去活来. 安装步骤: 1,运行  sudo apt-get update     --这个用来查找数据源 2 ,运行  apt install postgresql  ...

  7. iptables 配置说明

    #配置,禁止进,允许出,允许回环网卡 iptables -P INPUT DROP iptables -P OUTPUT ACCEPT iptables -A INPUT -i lo -j ACCEP ...

  8. 69.纯 CSS 创作一个单元素抛盒子的 loader

    原文地址:https://segmentfault.com/a/1190000015470411#articleHeader0 HTML code: <div class="loade ...

  9. 视频剪辑软件调研:Adobe Premiere、会声会影、抖音短视频

    Adobe Premiere.会声会影.抖音短视频基本功能特点对比: 特点 Adobe Premiere 会声会影 抖音短视频 运行平台 Win7/Win8/Win10.macOS  Win7/Win ...

  10. flask sqlchemy 多对多的自引用关系定义

    多对多的定义可以使用关联表,或者重新定义一个模型,通过模型定义多对多的自引用在flask web开发书里有讲到,这里主要演示用关联表定义的方法. from flask_sqlalchemy impor ...