classSolution{publicintlengthOfLongestSubstring(String s){intans=0; Map<Character, Integer> map =newHashMap<>();for(intl=0, r =0; r < s.length(); ++r) {charc=s.charAt(r);if(map.containsKey(c) && l <= map.get(c)) { l = map.get(c) +1; }else{ ans = Math.max(r - ...
参考代码如下: 1classSolution {2public:3intlengthOfLongestSubstring(strings) {4vector<int> hashtab(128,-1);5intstart = -1,index =0, maxlen =0;6for(charc:s)7{8if(hashtab[c]>start)9start =hashtab[c];10hashtab[c] = index++;11maxlen = max(maxlen,index - start -1);12}13returnm...
题目地址:https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 题目描述 Given a string, find the length of thelongest substringwithout repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", which the length is 3. 1. ...
别人非常棒的做法:sliding window,while loop里面每次循环都会尝试更新max,所以每次更新之前都是把l, r调整到合适的位置,也就是说,一旦r移动到超出K distinct char限制,l也要更新使k distinct char重新满足,l更新是在max更新之前 1publicclassSolution {2publicintlengthOfLongestSubstringKDistinct(String s,intk) {...
【LeetCode】5. Longest Palindromic Substring 陌儿的百科全书 来自专栏 · LeetCode 题目: Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: "babad" Output: "bab" Note: "aba" is also a valid answer....
Can you solve this real interview question? Longest Substring Without Repeating Characters - Given a string s, find the length of the longest substring without duplicate characters. Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer
原题链接 :leetcode.com/problems/l Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. 给定字符串 s,找到 s 中最长的回文字符串,可以假定 s 的最大长度是 1000。 Example 1: Input: "babad" Output: "bab" Note: "aba" is...
5. Longest Palindromic Substring Given a strings, returnthe longestpalindromicsubstringins. Example 1: Input:s = "babad"Output:"bab"Explanation:"aba" is also a valid answer. Example 2: Input:s = "cbbd"Output:"bb" Constraints: 1 <= s.length <= 1000...
publicclassSolution{publicstaticintlengthOfLongestSubstring(Strings){inti=0,j=0;intmax=0;Set<Character>set=newHashSet<>();while(j<s.length()){if(!set.contains(s.charAt(j))){set.add(s.charAt(j++));max=Math.max(max,set.size());}else{set.remove(s.charAt(i++));}}returnmax;}} ...
public class Solution { public int lengthOfLongestSubstring(String s) { int max = 0; int count = 0; int index = 0; //[index,i-1]中是否有与s[i]重复的字符 boolean flag = false; for(int i = 0; i < s.length(); i++) { ...