0
点赞
收藏
分享

微信扫一扫

【滑动窗口】牛客网:最长不含重复字符的子字符串

你带来了我的快乐 2022-03-30 阅读 50
算法

 

 

import java.util.*;
import java.lang.Math;

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @return int整型
     */
    public int lengthOfLongestSubstring (String s) {
        // write code he
        int[] ch=new int[128];
        int i=0;
        int j=0;
        int res=0;
        int len=0;
        while(j<s.length()){
            while(j<s.length()&&ch[s.charAt(j)+0]==0){
                ch[s.charAt(j++)+0]++;
                len++;
            }
            res=Math.max(res,len);
            ch[s.charAt(i)+0]--;
            if(ch[s.charAt(i)+0]==0)len--;
            i++;
        }
        return res;
    }
}
举报

相关推荐

0 条评论