码迷,mamicode.com
首页 > 编程语言 > 详细

算法 - 最长无重复子串

时间:2017-10-18 16:16:17      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:hashmap   des   get   math   character   har   esc   int   参考   

原题:

https://leetcode.com/problems/longest-substring-without-repeating-characters/description/

 

参考:

[LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

 

解法:

class Solution {
    public int lengthOfLongestSubstring(String s) {
        HashMap<Character,Integer> map = new HashMap<>();
        
        int res = 0, i = 0,left=0;
        
        while(i<s.length())
        {
            char ch = s.charAt(i);
            if(map.containsKey(ch) && map.get(ch)>=left) left = map.get(ch)+1;
            else res = Math.max(res,i-left+1);
            map.put(ch,i++);
        }
        return res;
    }
}

 

算法 - 最长无重复子串

标签:hashmap   des   get   math   character   har   esc   int   参考   

原文地址:http://www.cnblogs.com/qlky/p/7686951.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!