Longest Substring Without Repeating Characters

来源:互联网 发布:2017淘宝双11销量排名 编辑:程序博客网 时间:2024/06/05 04:27
public class Solution {    public int lengthOfLongestSubstring(String s) {        if (s == null || s.length() < 1) {            return 0;        }        char[] chars = s.toCharArray();        int prevMax = 0;        Map<Character/*char of chars*/,Integer/*index of char*/> map = new HashMap<>();         for (int i = 0; i < chars.length; i++) {            if (map.containsKey(chars[i])) {                prevMax = Math.max(map.size(), prevMax);                i = map.get(chars[i]);                map.clear();            } else {                map.put(chars[i], i);            }        }        return Math.max(map.size(), prevMax);    }}

0 0
原创粉丝点击