题目
给定一个字符串
s
,请你找出其中不含有重复字符的 最长子串 的长度。
解题思路
- 遍历字符串,遇到重复字符计算进行下一个子串的统计;
- 从发生重复的字符第一次出现的位置进行继续循环;
- 输出最长子串的长度。
代码展示
class Solution {public int lengthOfLongestSubstring(String s) {int max = 0;int count = 0;Map<Character,Integer> store = new HashMap<>();for (int i = 0; i < s.length(); i++){if (store.get(s.charAt(i)) == null) {store.put(s.charAt(i), i);count++;} else {max = Math.max(max, count);i = store.get(s.charAt(i));store.clear();count = 0;}}return Math.max(max, count);}
}