3. Longest Substring Without Repeating Characters
来源:互联网 发布:枪口动能计算器软件 编辑:程序博客网 时间:2024/05/29 02:47
s[j,i]表示以字母i结尾的最长的无字母重复的substring;
public class Solution { public int lengthOfLongestSubstring(String s) { HashMap<Character, Integer> charPosition = new HashMap<Character, Integer>(); int max = 0; for (int i = 0, j = 0; i < s.length(); i ++){ if (charPosition.containsKey(s.charAt(i))){ j = Math.max(j, charPosition.get(s.charAt(i)) + 1); } charPosition.put(s.charAt(i), i); max = Math.max(max, i-j+1); } return max; }}
注意:
j的更新不能直接:j = charPosition.get(s.charAt(i)) + 1;
例如测试用例“abba”,当i=3时,j将被从2更新为1.
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
- 剑指offer_整数中1出现的次数
- Linux嵌入式开发常见面试题
- 同义词总结
- HDU 5918 Sequence I kmp算法+虽然暴力也能过
- 2016华为校招上机试题2——洞穴逃生
- 3. Longest Substring Without Repeating Characters
- 51.[Python]使用multiprocessing进行多进程编程
- 51nod 1007 正整数分组(01背包)
- Integer Replacement
- C 重写power函数 递归与循环
- Visual Basic 总结
- 有关指针(1)
- Android Studio导入SlidingMenu库
- hdu1241油田