FTPrep, 30 Substring with Concatenation of All Words

来源:互联网 发布:国内人脸数据库 编辑:程序博客网 时间:2024/06/14 22:17

写这到hard之前,据说要先写一个道 简单对照物 来热身,于是5mins 完成了这段断代码,是LC第3题,Longest Substring Without Repeating Characters

public class Solution {    public int lengthOfLongestSubstring(String s) {        int len = s.length();        if(len==0|| len==1) return len;        int back=0;        int maxLen=1;        HashSet<Character> set= new HashSet<>();        for(int front=0; front<len; ++front){            if(!set.add(s.charAt(front))){                maxLen=Math.max(maxLen, front-back);                while(s.charAt(back)!=s.charAt(front)){                    set.remove(s.charAt(back));                    back++;                }                back++;            }        }        return maxLen=Math.max(maxLen, len-1-back+1);    }}




阅读全文
0 0