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 - ...
针对第一种解法,我们还可以将HashSet换成数组,用数组来判断字符是否重复出现,其他思路一样。 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 && ++se...
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...
AI代码解释 classSolution{publicintlengthOfLongestSubstring(String s){if(s==null||s.length()==0)return0;int from=0,to=1,length=1,maxLength=0;// to遍历直到字符串末尾while(to<s.length()){int site=s.substring(from,to).indexOf(s.charAt(to));if(site!=-1){// to指向的字符已存在length...
1 <= s.length <= 10 ^ 5 s 仅由英文小写字母组成 样例 思路:贪心 对于字母序连续字符串来说,第一个字母确定后,后续的字母都会确定。 所以我们可以维护当前字母序连续字符串的长度 cnt ,以及所有 cnt 的最大值 ans ,然后遍历 s 中的第 i 个字母,进行如下处理: s[i - 1] + 1 == s[i]: 贪心...
* https://leetcode.com/problems/longest-substring-without-repeating-characters * brutal force * @param {string} s * @return {number} */ var lengthOfLongestSubstring = function(s) { var n = s.length, ans = 0; /** * @param s String ...
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. ...
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 sconsist of only digits and English letters. ...
1 <= s.length <= 105 s由小写英文字母组成 publicintlongestContinuousSubstring(String s){// 最长的字母序 连续子字符串长度// 问题等价于,求s和t,s和t的最长公共子串// 考虑动态规划,无后效性、最优子结构、重复子问题// 令dp[i][j]表示以i结尾的s和以j结尾的t,最长公共子串的长度// 状态转移...
Given a string, find the length of thelongest substringwithout repeating characters. 给定字符串,找到最大无重复子字符串。 样例 Input:"abcabcbb"Output:3Explanation:The answeris"abc",withthe length of3.Input:"bbbbb"Output:1Explanation:The answeris"b",withthe length of1.Input:"pwwkew"Output:3...