算法系列——Longest Substring Without Repeating Characters
来源:互联网 发布:国家药监局数据查询 编辑:程序博客网 时间:2024/06/07 00:14
问题描述
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.
解题思路
滑动窗口+哈希表
采用双指针l,r [l,r]维护不重复字符串,哈希表记录 窗口中字符出现的频率。
程序实现
public class Solution { public int lengthOfLongestSubstring(String s) { int[] freq=new int[256]; int l=0,r=-1; int result=0; while(l<s.length()){ if(r+1<s.length()&&freq[s.charAt(r+1)]==0) freq[s.charAt(++r)]++; else freq[s.charAt(l++)]--; result=Math.max(result,r-l+1); } return result; }}
阅读全文
0 0
- 算法系列——Longest Substring Without Repeating Characters
- 算法——Longest Substring Without Repeating Characters
- LeetCode算法题——Longest Substring Without Repeating Characters
- 数据结构与算法[LeetCode]—Longest Substring Without Repeating Characters
- LeetCode03—Longest Substring Without Repeating Characters
- LeetCode3—Longest Substring Without Repeating Characters
- 【贪心算法】Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters 算法
- 算法题目--Longest Substring Without Repeating Characters
- LeetCode —— Longest Substring Without Repeating Characters
- LeetCode——Longest Substring Without Repeating Characters
- LeetCode——Longest Substring Without Repeating Characters
- [LeetCode]——Longest Substring Without Repeating Characters
- leetcode 003 —— Longest Substring Without Repeating Characters
- LeetCode——Longest Substring Without Repeating Characters
- LeetCode题解——3Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters —— Leetcode
- LeetCode3——Longest Substring Without Repeating Characters
- JAVA---关于分支结构switch和循环结构for语句的特例
- Spring学习之动态代理
- 开启我的Java学习之路
- MatLab中读取TXT文件和bin文件,存储bin文件
- Mysql锁及事务的隔离水平
- 算法系列——Longest Substring Without Repeating Characters
- android获取短信的内容
- 关于精灵图
- lua 人机交互(二)
- 高级IO(文件的读写)——并发式IO的解决方案(解决多路阻塞式IO的方案)
- 反射、代理学习笔记
- Unity3D Attributes用法小结(一)
- Python :滑动窗口的最大值
- Latex 论文elsevier,手把手如何用Latex写论文