3. Longest Substring Without Repeating Characters
来源:互联网 发布:ftp主进程打开的端口 编辑:程序博客网 时间:2024/06/06 13: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 subsequenceand not a substring.
一道非常经典的题目 vote达到1322
首先想到的是下面这个解法
public class Solution { public int lengthOfLongestSubstring(String s) { 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; }}
[i,j]标识不重复的substring 用set来保证 当遇到重复的 计算一下当前长度 然后从上一个重复的后面开始 循环
比如输入是”abcdecde”
set里是abcde 当遇到第二个c 计算一下当前长度=3 然后i+1 也就是从第一个b开始
实际上我们不需要这样 每次i+1 对于上面这个例子 第二次计算 i直接从前一个重复字符位置后面 即d开始就可以了
因为c之前的位置都会包含c 导致重复
下面是优化后的解法
public class Solution { public int lengthOfLongestSubstring(String s) { int n = s.length(), ans = 0; Map<Character, Integer> map = new HashMap<>(); // current index of character // try to extend the range [i, j] for (int j = 0, i = 0; j < n; j++) { if (map.containsKey(s.charAt(j))) { //1. i = Math.max(map.get(s.charAt(j)), i); } ans = Math.max(ans, j - i + 1); map.put(s.charAt(j), j + 1); } return ans; }}
1.为什么是i= Math.max(map.get(s.charAt(j)),i);
而不是i = map.get(s.charAt(j));
testcase:abba
第二个a出现时 之后非重复子串的起始位置应该是第二个b 也就是i 而不是第一个a后面
所以要取i和前一个重复字符位置的最大值
阅读全文
0 0
- [LeetCode]3.Longest Substring Without Repeating Characters
- LeetCode 3.Longest Substring Without Repeating Characters
- LeetCode --- 3. Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- [Leetcode] 3. Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters
- [leetcode] 3.Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- [leetcode] 3. Longest Substring Without Repeating Characters
- 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
- 自定义View加减
- Emergency (25)
- PHP学习笔记 08
- hadoop之BlockPoolManager源码分析
- [转] ubuntu 更改文件夹权限
- 3. Longest Substring Without Repeating Characters
- Nginx配置阿里云https服务
- [c]文件内容反向输出到另一个文件(递归实现)
- HDU 2063 (二分图最大匹配)
- Now you can provide attr "wx:key" for a "wx:for" to improve performance.
- linux运维技能
- mt6735 [Audio App]拖动歌曲计时条过程中,如何显示当前快进的时间
- LDAP安装步骤
- 图像不变矩的计算