[LeetCode]3. Longest Substring Without Repeating Characters 最长无重复子串解析。java
来源:互联网 发布:c语言linuxsleep 编辑:程序博客网 时间:2024/06/11 04:02
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", 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.
这里用的不是native method, 因为它虽然直接但是太慢了。O(n3). 因为找的是无重复的子串,因此我们使用Hashset结构来存放已经找到的字符,HashSet中保存的是从某个位置pre到另一个位置res中间的所有字符。hashset 作为一个滑动窗口, 使用这个hashset保存在当前的滑动窗口(i,j)的字符。然后我们向右滑动j,If it is not in the HashSet, we slide j further. Doing so until s[j] is already in the HashSet. At this point, we found the maximum size of substrings without duplicate characters start with index ii. If we do this for all i, we get our answer.
public class Solution { public int lengthOfLongestSubstring(String s) { if(s == null || s.length() < 1) return 0;int n = s.length(); Set<Character> set = new HashSet<>(); int ans = 0, i = 0, j = 0; while (i < n && j < n) { // try to extend the range [i, j] if (!set.contains(s.charAt(j))){ set.add(s.charAt(j++)); ans = Math.max(ans, j - i); } else { set.remove(s.charAt(i++)); } } return ans;}}
- [LeetCode]3. Longest Substring Without Repeating Characters 最长无重复子串解析。java
- LeetCode OJ-3.Longest Substring Without Repeating Characters(最长无重复子串)
- leetcode:Longest Substring Without Repeating Characters (寻找最长无重复字符的子串)
- LeetCode OJ 之 Longest Substring Without Repeating Characters (无重复的最长子串)
- LeetCode-3 Longest Substring Without Repeating Characters(最长无重复子串)
- 【LeetCode】4. Longest Substring Without Repeating Characters 最长无重复子串
- leetcode 3 : Longest Substring Without Repeating Characters 最长无重复子串 (C# 语言版)
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
- [Leetcode #3]Longest Substring Without Repeating Characters 无重复字符的最长子串
- LeetCode-3:Longest Substring Without Repeating Characters (最长无重复字符的子串) --medium
- leetcode 3. Longest Substring Without Repeating Characters 最长不重复子串和重复子串
- LeetCode 3. Longest Substring Without Repeating Characters(最长不重复子串)
- LeetCode 3. Longest Substring Without Repeating Characters--不包含重复字符的最长子串长度
- Leetcode#3. Longest Substring Without Repeating Characters(最长不重复子串-哈希))
- Leetcode 3. Longest Substring Without Repeating Characters(最长不重复子串)
- leetcode解题之3. Longest Substring Without Repeating Characters Java版(最长子串长度)
- 3. Longest Substring Without Repeating Characters 无重复字符的最长子字符串
- 寻找最长无重复字符子串 Longest Substring Without Repeating Characters
- 1308:Is It A Tree?
- 建立和配置Mavenweb 项目方法
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛b题Coin(矩阵快速幂)
- 08. 无脚本的JSP
- 1.卷2(进程间通信)---简介
- [LeetCode]3. Longest Substring Without Repeating Characters 最长无重复子串解析。java
- 随机生成测试数据(rand(),srand())
- 访今日头条频道管理
- bzoj3339 Rmq Problem(离线+线段树)
- 转载:如何做一个聪明的程序员
- js/java 页面url传值中文编码&解码问题
- 大数据学习[02]:hadoop安装配置
- 《C++ Concurrency in Action》笔记7 mutex(2)pop和top问题
- C++ 关键字——friend