Longest Substring Without Repeating Characters -- LeetCode
来源:互联网 发布:手机淘宝6.10旧版本 编辑:程序博客网 时间:2024/06/14 04:26
原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/
这道题用的方法是在LeetCode中很常用的方法,对于字符串的题目非常有用。 首先brute force的时间复杂度是O(n^3), 对每个substring都看看是不是有重复的字符,找出其中最长的,复杂度非常高。优化一些的思路是稍微动态规划一下,每次定一个起点,然后从起点走到有重复字符位置,过程用一个HashSet维护当前字符集,认为是constant操作,这样算法要进行两层循环,复杂度是O(n^2)。
这道题用的方法是在LeetCode中很常用的方法,对于字符串的题目非常有用。 首先brute force的时间复杂度是O(n^3), 对每个substring都看看是不是有重复的字符,找出其中最长的,复杂度非常高。优化一些的思路是稍微动态规划一下,每次定一个起点,然后从起点走到有重复字符位置,过程用一个HashSet维护当前字符集,认为是constant操作,这样算法要进行两层循环,复杂度是O(n^2)。
最后,我们介绍一种线性的算法,也是这类题目最常见的方法。基本思路是维护一个窗口,每次关注窗口中的字符串,在每次判断中,左窗口和右窗口选择其一向前移动。同样是维护一个HashSet, 正常情况下移动右窗口,如果没有出现重复则继续移动右窗口,如果发现重复字符,则说明当前窗口中的串已经不满足要求,继续移动有窗口不可能得到更好的结果,此时移动左窗口,直到不再有重复字符为止,中间跳过的这些串中不会有更好的结果,因为他们不是重复就是更短。因为左窗口和右窗口都只向前,所以两个窗口都对每个元素访问不超过一遍,因此时间复杂度为O(2*n)=O(n),是线性算法。空间复杂度为HashSet的size,也是O(n). 代码如下:
public int lengthOfLongestSubstring(String s) { if(s==null || s.length()==0) return 0; HashSet<Character> set = new HashSet<Character>(); int max = 0; int walker = 0; int runner = 0; while(runner<s.length()) { if(set.contains(s.charAt(runner))) { if(max<runner-walker) { max = runner-walker; } while(s.charAt(walker)!=s.charAt(runner)) { set.remove(s.charAt(walker)); walker++; } walker++; } else { set.add(s.charAt(runner)); } runner++; } max = Math.max(max,runner-walker); return max;}这道题思想在字符串处理的题中还是比较重要的,实现上主要是HashSet和数组index的操作。扩展的题目有Substring with Concatenation of All Words,Minimum Window Substring,思路是非常接近的,只是操作上会更加繁琐一些。
56 0
- 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 23: Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- LeetCode 3 - 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] Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [leetcode] Longest Substring Without Repeating Characters
- LeetCode - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- leetcode之Longest Substring Without Repeating Characters
- win7中VS2010中安装CSS3.0问题解决方法
- uboot源码中start.S里中断向量表中不直接LDR PC ,"异常地址",而是使用一个标号
- [Unity3d]刀斧武器砍击的攻击碰撞判定
- 【整理】关于角色攻击范围判定和攻击判定
- java算法面试题-----1
- Longest Substring Without Repeating Characters -- LeetCode
- codeforces#231_div2_B Very Beautiful Number 高精度枚举
- ldr和adr在使用标号表达式作为操作数的区别
- 面试题
- java面试题---算法2
- [LeetCode] Add Binary
- 网站许久不管为什么排名越来越好
- Stanford - Cryptography I - Week 1-2 Programming Assignment
- Access denied for user 'root'@'localhost' (using password: YES)