题目考察的知识点是:
字符串的遍历,双指针。
题目解答方法的文字分析:
维护记录每个字符的个数的计数器map,维护双指针,每次右指针向右移动一步,左指针不断右移找到合法的最靠左的位置,更新答案即可。
本题解析所用的编程语言:
java语言。
完整且正确的编程代码:
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return int整型
*/
public int lengthOfLongestContinuousTasks (String s) {
// write code here
Map<Character, Integer> map = new HashMap<>();
int left = 0;
int len = 0;
for (int i = 0; i < s.length(); i++) {
if (map.containsKey(s.charAt(i))) {
len = Math.max(len, i - left);
left = Math.max(left, map.get(s.charAt(i)) + 1);
}
map.put(s.charAt(i), i);
}
len = Math.max(len, s.length() - left);
return len;
}
}

京公网安备 11010502036488号