算法-滑动窗口
作者:互联网
左神
给你一个字符串s 一个字符串t
返回s中涵盖t所有字符的最短子串
class Solution { int f(String s, String t) { //1 int ans = Integer.MAX_VALUE; char[] chs = s.toCharArray(); char[] cht = t.toCharArray(); int[] debt = new int[256]; for (int i = 0; i < t.length(); i++) { debt[cht[i]]++; } int debtSum = t.length(); int l = 0, r = 0; //2 while (r < s.length()) { //r++ util fill while (debtSum > 0) { debt[chs[r]]--; if (debt[chs[r]] >= 0) debtSum--; r++; } //l++ util miniest fill while (debtSum <= 0) { debt[chs[l]]++; if (debt[chs[l]] > 0) { ans = Math.min(ans, r - l + 1); debtSum++; } l++; } } return ans; } }
标签:窗口,int,ans,while,++,算法,debtSum,滑动,debt 来源: https://www.cnblogs.com/t1314/p/15757089.html