003Longest Substring Without Repeating Characters
来源:互联网 发布:excel找两列不同的数据 编辑:程序博客网 时间:2024/06/07 05:48
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.
求最长子字符串的长度,注意是连续的子字符串。双指针维护一个动态窗口,遍历一个长度即可,时间复杂度O(n),用一个hashSet判断动态窗口中是否能加上,即字母是否重复,遍历一圈后记录最大的窗口长度。
看了讨论区的代码,思路是一样的,只不过用的是hashMap记录是否重复,看起来简洁一些
代码:
class Solution { public int lengthOfLongestSubstring(String s) { int left = 0, right = 0, max = 0;if (s == null || s.length() == 0) {return 0;}Set<Character> set = new HashSet<>();while (right < s.length()) { //能添加上 没有重复,右指针走一个while (right < s.length() && set.add(s.charAt(right))) {right++;}max = Math.max(max, right - left);set.remove(s.charAt(left)); //有重复的话向左移,直到重复字符被剔除 移动到了下一个字母位置left++;}return max; }}
阅读全文
0 0
- LeetCode 003 Longest Substring Without Repeating Characters
- LeetCode-003 Longest Substring Without Repeating Characters
- 【leetcode】003 Longest Substring Without Repeating Characters
- LeetCode 003 Longest Substring Without Repeating Characters
- LeetCode 003-Longest Substring Without Repeating Characters
- [LeetCode]-003-Longest Substring Without Repeating Characters
- LeetCode #003 Longest Substring Without Repeating Characters
- LeetCode 003 Longest Substring Without Repeating Characters
- 003 Longest Substring Without Repeating Characters [Leetcode]
- 003 - Longest Substring Without Repeating Characters
- [LeetCode]003-Longest Substring Without Repeating Characters
- 003Longest Substring Without Repeating Characters (C)
- LeetCode 003 Longest Substring Without Repeating Characters
- LeetCode 003: Longest Substring Without Repeating Characters
- leetcode-003 Longest Substring Without Repeating Characters
- 003 Longest Substring Without Repeating Characters
- LeetCode-003 Longest Substring Without Repeating Characters
- leetcode 003 Longest Substring Without Repeating Characters
- 如何下载IAR历史版本
- transition
- 追逐物联网浪潮,LoRa定位技术多场景应用
- Educational Codeforces Round 34 (Rated for Div. 2) D. Almost Difference(高精度)
- HBuilder 入门(5) 编写一个登录页面
- 003Longest Substring Without Repeating Characters
- mybatis多表查询
- spring线程池ThreadPoolTaskExecutor应用
- #Import #include @class区别
- Android 应用程序没有权限时跳到应用的权限管理页面,让用户手动打开需要的权限
- 分类器准确率评估
- 文件流的基本使用方法
- Lintcode 35.翻转链表
- 数据结构与算法_插入排序