题解 | #牛牛的协作#
牛牛的协作
https://www.nowcoder.com/practice/ae483ed148b34e14bec1451b3317984d
知识点:双指针
思路:双指针固定更新值即可,
编程语言:java
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ public int lengthOfLongestContinuousTasks(String s) { HashMap<Character, Integer> cnt = new HashMap<>(); int res = 0; int j = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); cnt.put(c, cnt.getOrDefault(c, 0) + 1); while (cnt.get(c) > 1) { char prevChar = s.charAt(j++); cnt.put(prevChar, cnt.get(prevChar) - 1); } res = Math.max(res, i - j + 1); } return res; } }