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

阅读 50

2022-03-30

 

 

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)

0 0 举报