最长无重复子串

longest-substring-without-repeating-characters

http://www.nowcoder.com/questionTerminal/5947ddcc17cb4f09909efa7342780048

public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0) return 0;
        Map<Character, Integer> map = new HashMap<>();
        int start = 0;
        int result = 0;
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i))) {
                start = Math.max(map.get(s.charAt(i)) + 1 , start); // 如果重复位置大于start则需要更新start,若小于则不用
            }
            map.put(s.charAt(i), i);
            result = Math.max(result, i - start + 1);
        }
        return result;
    }

全部评论

相关推荐

点赞 评论 收藏
分享
04-18 00:32
已编辑
中南大学 Java
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务