【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: ...
随机推荐
- 简单Hash函数LongHash
import java.security.SecureRandom; import java.util.Random; public class LongHash { private static l ...
- 关于new你应当知道的一切
new在C++中是一个我们经常用到的运算符.由它所创建的变量会被分配在堆中,并且在程序结束之前应当将分配的内存delete掉,否则就会导致内存泄漏.但是除此之外,你对new有更深入的了解吗?本篇文章将 ...
- 安装Git Bash图文教程
1.下载Git Bash,下载地址 https://pan.baidu.com/s/1sllsi0d 2.双击Git-2.9.2-64-bit.exe,运行,进行安装:点击“Next” 3.设置安装路 ...
- JAVA中通过Jaxp操作XML文件基础
Java中有多种方式操作XML文件,目前讲一讲以SUN公司提供的DocumentBuilderFactory工厂类对象操作XML. 使用XML基本操作就是需要CRUD(增删改查),那么首先通过一个查询 ...
- 开发环境 pyenv
pyeny githup地址:https://github.com/pyenv/pyenv/ 安装时使用pyeny提供安装工具进行安装 githup 地址:https://github.com/pye ...
- MyBatis注解-动态SQL 一个 SqlProvider的demo
Provider动态语言注解 MyBatis提供了多个注解如:@InsertProvider,@UpdateProvider,@DeleteProvider和@SelectProvider,这些都是建 ...
- flutter 入口文件配置路由+加载页面
入口文件配置路由 1.路由信息 -- 加载页面 ,通常用于显示新的内容或者广告,加载完成之后进入主页面 -- 主页面 /app 2.配置页面 main.dart main.dart // main ...
- 爬虫之scrapy
一.项目简单流程 1.创建项目 scrapy startproject 项目名 2.创建Spider cd 项目名 scrapy genspider 爬虫名 域名 class YokaSpider(s ...
- MySql CURD操作(数据的增删改查)
1.增 格式 insert into 表名字 (列名) values(...); 两种方式 1.直接insert into 表名字 values(...); 全部插入 2.insert into ...
- oss对象云存储
import qiniu import uuidimport config def qn_upload_voice(fileData): '''上传语音到七牛云 @arg: fileData - 编码 ...