import java.util.HashMap; public static int lengthOfLongestSubstring(String s) { if (s == null || s.length() == 0) return 0; int len = s.length(); int max = 0, leftBound = 0; HashMap<Character, Integer> map = new HashMap<>(); for (int i = 0; i < len; i++) { char c = s.charAt(i); int tempBound = map.containsKey(c) ? map.get(c) + 1 : 0; leftBound = Math.max(leftBound, tempBound);// 滑动窗口的左门限 max = Math.max(max, i - leftBound + 1); map.put(c, i); } return max; }
点赞 1

相关推荐

07-14 13:37
重庆大学 C++
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务