public class Solution { public int lengthOfLongestSubstring(String s) { int n = s.length(); Set<Character> set = new HashSet<>(); int ans = 0, i = 0, j = 0; while (i < n && j < n) { // try to extend the range [i, j] if (!set.contains(s.charAt(j))){ set.add...
题解:滑动窗口 classSolution{public:intlengthOfLongestSubstring(strings){unordered_map<char,int>window;intlen=0;intleft=0,right=0;while(right<s.size()){charc=s[right];right++;window[c]++;while(window[c]>1){chard=s[left];left++;window[d]--;}len=max(len,right-left);}returnlen;}};...
classSolution{publicintlengthOfLongestSubstring(Strings){intlen=s.length();if(len==0){return0;}intres=0;for(inti=0;i<len;++i){HashMap<Character,Integer>map=newHashMap<>();intj=i;intmax=0;while(j<len){if(map.containsKey(s.charAt(j))){break;}map.put(s.charAt(j),1);j++;}res=...
参考代码如下: 1classSolution {2public:3intlengthOfLongestSubstring(strings) {4constintsize =s.size();5if(s.empty())return0;6if(size<=1)returnsize;7intdp[size];//dp[i]表示以s[i]结尾的不重复子串8memset(dp,0,size);9intres =1;10dp[0]=1;//dp初始值11for(inti =1;i<size;++i)12...
publicintlengthOfLongestSubstring2(String s){int[] set =newint[256];intn=s.length(), left =0, right =0;intresult=0;for(inti=0; i<n; i++) { left = i; right = i;while(right < n && ++set[s.charAt(right)] <2) {
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串的长度。 示例1: 输入: s = "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 2、代码: deflengthOfLongestSubstring(self,s):"""思路 滑动窗口,暴力求解 ...
funclengthOfLongestSubstring(s string)int{// guard clauses,也就是卫语句,在递归中非常重要iflen(s)<=1{returnlen(s)}// 全局的字符串,用于保存varmp=make(map[byte]int)vari intfori=0;i<len(s);i++{if_,ok:=mp[s[i]];ok{break// 找到重复的,直接退出}else{mp[s[i]]=i// 没找到重复...
1publicclassSolution {2publicintlengthOfLongestSubstringKDistinct(String s,intk) {3intl=0, r=0;4HashMap<Character, Integer> map =newHashMap<Character, Integer>();5intlongest = 0;6if(s==null|| s.length()==0 || k<=0)returnlongest;7while(r <s.length()) {8charcur =s.charAt(r)...
Given a string, find the length of thelongest substringwithout repeating characters. Example 1: Input:"abcabcbb"Output:3Explanation:The answer is "abc", with the length of 3. Example 2: Input:"bbbbb"Output:1Explanation:The answer is "b", with the length of 1. ...
0 <= s.length <= 5 * 104 s 由英文字母、数字、符号和空格组成 代码实现: class Solution: def lengthOfLongestSubstring(self, s: str) -> int: length = 0 for i in range(len(s)): slist = [] for j in range(i, len(s)):