题解 | #最长不含重复字符的子字符串#

最长不含重复字符的子字符串

http://www.nowcoder.com/practice/48d2ff79b8564c40a50fa79f9d5fa9c7

//和NC41 最长无重复子数组是一样的解题思路

public class Solution {

public int lengthOfLongestSubstring (String s) {
    // write code here
    char[] charArray = s.toCharArray();
    if(charArray.length==0){
        return 0;
    }
    int max = 0;
    Map<Character,Integer> result = new HashMap<>();
    for(int i=0,j=0;i< charArray.length;i++){
        if(result.containsKey(charArray[i])){
            j = Math.max(j,result.get(charArray[i])+1);
        }
        result.put(charArray[i],i);
        max=Math.max(max,i-j+1);
    }
    return max;
}

}

全部评论

相关推荐

评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务