diff --git a/sliding window/438. Find All Anagrams in a String.md b/sliding window/438. Find All Anagrams in a String.md index 429e3f9..9b7ff6f 100644 --- a/sliding window/438. Find All Anagrams in a String.md +++ b/sliding window/438. Find All Anagrams in a String.md @@ -45,25 +45,25 @@ class Solution { for(int i = 0; i < windowSize; ++ i) { ++ charToCountMap[pArray[i]-'a']; } - int left = 0, count = windowSize; + int left = 0, diff = windowSize; for(int right = 0; right < sArray.length; ++ right) { -- charToCountMap[sArray[right]-'a']; if(charToCountMap[sArray[right]-'a'] >= 0) { // 右指针指向的字符, 在窗口中出现过 - -- count; + -- diff; } if(right >= windowSize) { - // 右指针超过窗口宽度后,每次都同步移动下左指针,并更新计数map和出现个数count + // 右指针超过窗口宽度后,每次都同步移动下左指针,并更新计数map和差异个数diff - // 将左指针指向的字符计数还原,如果它在窗口中不存在,则还原后为0,否则说明之前在窗口中出现过,需要把count也增加1 + // 将左指针指向的字符计数还原,如果它在窗口中不存在,则还原后为0,否则说明之前在窗口中出现过,需要把diff也增加1 ++ charToCountMap[sArray[left]-'a']; if(charToCountMap[sArray[left]-'a'] > 0) { - ++ count; + ++ diff; } ++ left; } - if(count == 0) { - // 只要count=0,就说明找到了一个解;因为右指针和左指针维护的窗口没到长度时,不可能出现count=0;之后右指针和左指针之差一直等于窗口长度 + if(diff == 0) { + // 只要diff=0,就说明找到了一个解;因为右指针和左指针维护的窗口没到长度时,不可能出现diff=0;之后右指针和左指针之差一直等于窗口长度 res.add(left); }