【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters
题目:
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的更多相关文章
- 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)
这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...
- No.003:Longest Substring Without Repeating Characters
问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- 【leetcode刷题笔记】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
- leetcode第三题--Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters. For e ...
- 【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 ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
随机推荐
- 4、redis 分布式锁
1. 前言 关于分布式锁的实现,目前常用的方案有以下三类: 数据库乐观锁: 基于分布式缓存实现的锁服务,典型代表有 Redis 和基于 Redis 的 RedLock: 基于分布式一致性算法实现的锁服 ...
- dotnet core调试docker下生成的dump文件
最近公司预生产环境.net core应用的docker容器经常出现内存暴涨现象,有时会突然吃掉几个G,触发监控预警,造成容器重启. 分析了各种可能原因,修复了可能发生的内存泄露,经测试本地正常,但是发 ...
- 检查邮箱IP是否在国际反垃圾邮件组织的黑名单中
有时候邮件发不出去,很有可能就是邮件服务器的IP被国际上一些反垃圾组织列入黑名单了,这时你可以通过返回的邮件判断是否进入黑名单,或者通过以下查询地址看是否被列入,然后一个个申请移除: http://m ...
- Flask--WTForms
简介 WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 安装:pip3 install wtforms 用户登录注册示例 1. 用户登录 当用户登录时候,需要对用 ...
- react-native shadow失效
做边框阴影,但是有时候会失效,内容产生阴影,而边框无效,今天发现了原因,没错,就是没有设置背景颜色导致的.如图
- python pdfkit html转pdf响应式轮子 django例
pip install pdfkit 本例用django做的请求,换成对应框架即可 此方法可将html页面转成pdf下载 #!/usr/bin/env python # coding:utf-8 im ...
- vue双向绑定的简单实现(原创)
简单模拟一下vue的双向绑定实现,代码比较粗糙,菜鸟一枚,欢迎各位大佬斧正 1.实验环境: 利用a.b两个input,a代表页面中的数据,b代表data中的数据 2.原理: 利用Object.defi ...
- RobotFramework - AppiumLibrary 之关键字Open Application使用
- 2008R2 部署 aspnetcore repair failed 函数不正确
vc_redist.x64
- orcal - 伪列
数据伪劣 行号 ROWNUM SELECT ROWNUM, empno,ename,sal from emp; 取出第一行数据 SELECT ROWNUM, empno,ename,sal from ...