[Leetcode] 3.Longest Substring Without Repeating Characters(unordered_map)

通过把未访问的结点放到unordered_map中来判断是否重复,代码如下:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s == "")
return ;
unordered_map<char,int> hash;
int len = ;
int res = ;
for(int i = ;i < s.length();i ++)
{
if(hash.find(s[i]) != hash.end())
{
i = hash[s[i]];
hash.clear();
len = ;
}
else
{
hash[s[i]] = i;
len ++;
res = max(len, res);
}
}
return res;
}
};
[Leetcode] 3.Longest Substring Without Repeating Characters(unordered_map)的更多相关文章
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode #003# Longest Substring Without Repeating Characters(js描述)
索引 思路1:分治策略 思路2:Brute Force - O(n^3) 思路3:动态规划? O(n^2)版,错解之一:420 ms O(n^2)版,错解之二:388 ms O(n)版,思路转变: 1 ...
- 「LeetCode」0002-Longest Substring Without Repeating Characters(C++)
分析 贪心思想.注意更新每次判断的最长不同子串的左区间的时候,它是必须单调增的(有时候会在这里翻车). 代码 关掉流同步能有效提高速度. static const auto io_sync_off = ...
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)
题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)
题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
随机推荐
- 20145234黄斐《Java程序设计》第二周学习总结
教材学习内容总结 类型 Java可区分为基本类型(Primitive Type)和类类型(Class Type),其中类类型也叫参考类型(Reference Type). 字节类型,也叫byte类型, ...
- ROS Twist和Odometry消息类型使用(Python)
消息类型: 1. Twist - 线速度角速度 通常被用于发送到/cmd_vel话题,被base controller节点监听,控制机器人运动 geometry_msgs/Twist geometry ...
- Ruby 基础教程1-9
异常 1.异常结构 [ begin] ... rescue [retry] ... [ensure] . ...
- 写了个汉字转G代码工具,无描边的那种,市面上没有类似的小软件
学了不少G代码知识, 将公司废旧的三轴非标设备改造成了一个雕刻机,市面上的小软件不好用 网上下的软件有描边的,字体刻起来太粗,这个比较好用,看图应该都能明白吧, 就自己写了个,“少于150字的随笔不允 ...
- 创建并运行第一个Django项目
首先, 添加Django模块: 在CMD命令行输入 python -m django --version 查看Django版本: 创建第一个Django项目: 整个工程的目录结构: mysite目录是 ...
- centos7下搭建django
安装环境:centos7.4 1 安装nginx yum install nginx 注:尝试过在本地和腾讯云上安装,使用同一条命令:在本地安装提示没有可用安装包,云上安装正常 启动nginx,并启用 ...
- Python3.6+selenium3.8+Firefox5.7 环境搭建
大家好,我是阿哲,本人是从php转岗过来学习测试的一名小菜! 在学习selenium过程中,发现运行selenium有很多的问题. 我们在利用pip install selenium 安装的最新版后, ...
- es6笔记4^_^function
一.function默认参数 现在可以在定义函数的时候指定参数的默认值了,而不用像以前那样通过逻辑或操作符来达到目的了. es5 function sayHello(name){ //传统的指定默认参 ...
- react在安卓下输入框被手机键盘遮挡问题
问题概述 今天遇到了一个问题,在安卓手机上,当我要点击输入"店铺名称"时,手机软键盘弹出来刚好把输入框挡住了:挡住就算了,关键是页面还不能向上滑动,整个手机窗口被压为原来的二分 ...
- (python)leetcode刷题笔记 02 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...