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 ...
随机推荐
- es5实现一个class
es5实现一个class https://juejin.im/post/5ac1c5bf518825558949f898#heading-9
- Python性能优化方案
Python性能优化方案 从编码方面入手,代码算法优化,如多重条件判断有限判断先决条件(可看 <改进python的91个建议>) 使用Cython (核心算法, 对性能要求较大的建议使用C ...
- 进阶之路 | 奇妙的Activity之旅
前言 本文已经收录到我的Github个人博客,欢迎大佬们光临寒舍: 我的GIthub博客 本篇文章需要已经具备的知识: Activity的基本概念 AndroidManifest.xml的基本概念 学 ...
- 错误:EfficientDet网络出现"No boxes to NMS"并且mAP:0.0的解决方案
近日,在使用谷歌新推出来的一个网络EfficientDet进行目标检测训练自己的数据集的时候,出现了如下错误: 其中项目开源地址是:https://github.com/toandaominh1997 ...
- 高级UI组件(二)
1.图像视图 <ImageView android:id="@+id/imageView" android:layout_width="wrap_content&q ...
- w13scan扫描器的使用
0x01 w13scan第三方包下载 环境:python3以上 下载:pip install w13scan 0x02 利用w13scan API接口编写w13scan.py from W13SCAN ...
- jQuery---小火箭返回顶部案例
小火箭返回顶部案例 1. 滚动页面,当页面距离顶部超出1000px,显示小火箭. 封装在scroll函数里,当前页面距离顶部为$(window).scrollTop >=1000 小火箭显示和隐 ...
- day9 修改文件
# 修改文件 # 文件是不能修改 with open('小护士班主任', mode='r', encoding='utf-') as f, open('小护士班主任.bak', 'w', encodi ...
- js监听滚动结束
使用setTimeout模拟滚动结束 let scrollTimer; document.addEventListener("scroll", () => { clearTi ...
- IntelliJ IDEA 2018.3.2 永久破解
PS:动手能力强的来,手残的去淘宝买吧,大概15块钱1年.建议看完后在动手,有一个全局观,浪费不了多少时间 一. 下载破解补丁文件 链接:https://pan.baidu.com/s/1wFp14t ...