LeetCode longest substring without repeating characters 题解 Hash表
题目
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.
思路——Hash大法好
双指针移动。
Hash表查询字符是否出现。
时间复杂度O(L)O(L)O(L),空间复杂度O(L)O(L)O(L)
结果
Runtime: 27 ms, faster than 70.11% of Java online submissions for Longest Substring Without Repeating Characters.
Memory Usage: 37.5 MB, less than 100.00% of Java online submissions for Longest Substring Without Repeating Characters.
源码
class Solution {
public int lengthOfLongestSubstring(String s) {
Set<Character> set = new HashSet<Character>();
int ans = 0;
int l, r = 0;
Character c;
int len = s.length();
for (l = 0; l < len; ++l) {
while (r < len) {
c = s.charAt(r);
if (set.contains(c)) {
ans = Math.max(ans,r-l);
break;
} else {
set.add(c);
++r;
}
}
if (r == len) {
ans = Math.max(ans, r - l);
break;
}
set.remove(s.charAt(l));
}
return ans;
}
}
LeetCode longest substring without repeating characters 题解 Hash表的更多相关文章
- [LeetCode]Longest Substring Without Repeating Characters题解
Longest Substring Without Repeating Characters: Given a string, find the length of the longest subst ...
- C++ leetcode Longest Substring Without Repeating Characters
要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...
- [LeetCode] 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
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- [Leetcode] Longest Substring Without Repeating Characters (C++)
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- Leetcode:Longest Substring Without Repeating Characters 解题报告
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. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
随机推荐
- Zookeeper 介绍 原理
简介: ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务. 它Google的Chubby一个开源的实现,在分布式协调技术方面做得比较好的就是Google的Chubby还有 ...
- Django请求过程
- Mysql 在线新建或重做主从
1. 前言 以前给 Mysql 数据库做主从,都是在主服务器停服的情况下做的.但是最近有一个项目,已经上线几天了,数据库也单服务器跑了几天,才确定要给 Mysql 服务器做一个主从架构,简单的一主一从 ...
- 杭电-------2055An Easy Problem(C语言)
#include<stdio.h> int main() { int m; int i; scanf("%d", &m); ]; int y; int z; ; ...
- tomcat+memcached+nginx部署文档(附完整部署包直接运行即可)
1 前言 1.1 目的 为了正确的部署“ngix+memcached”特编写此部署手册,使安装人员可以通过部署手册知道如何部署系统,也为需要安装该系统的安装人员正确.快速的部署本系统提供帮助. 1.2 ...
- Pyinstaller打包exe,丢失图标等问题
Pyinstaller打包exe,丢失图标等问题 一.原因 exe运行时会解压一个名为'_MEI*'的资源文件夹到电脑的临时目录,程序结束时删除. 程序里使用'\图标.png'这样的路径,exe运行时 ...
- MySQL真正的UTF-8字符集utf8mb4
MySQL有个utf-8的坑 MySQL 的 utf8 实际上不是真正的 UTF-8.utf8 只支持每个字符最多三个字节,而真正的 UTF-8 是每个字符最多四个字节. MySQL 一直没有修复这个 ...
- hadoop之HDFS核心类Filesystem的使用
1.导入jar包,要使用hadoop的HDFS就要导入hadoop-2.7.7\share\hadoop\common下的3个jar包和lib下的依赖包.hadoop-2.7.7\share\hado ...
- 使用HSSFWorkbook导出、操作excel
原文地址:https://www.jianshu.com/p/dd1e4f28757b 在实际开发中我们经常需要导入数据,统计数据,并且将统计好的数据导出excel,今天分享一个导出学生信息的方法. ...
- 极具性价比优势的工业控制以及物联网解决方案-米尔MYD-C8MMX开发板测评
今天要进行测评的板子是来自米尔电子的MYD-C8MMX开发板.MYD-C8MMX开发板是米尔电子基于恩智浦,i.MX 8M Mini系列嵌入式应用处理器设计的开发套件,具有超强性能.工业级应用.10年 ...