while right < len(s): if lookup[s[right]] > 0: count -= 1 lookup[s[right]] -= 1 right += 1
得到可行的窗口后,将left指针逐个右移,若得到的窗口依然可行,则更新最小窗口大小。
1 2 3 4 5 6 7 8
while count == 0: if min_len > right-left: min_len = right-left res = s[left:right] if lookup[s[left]] == 0: count += 1 lookup[s[left]] += 1 left += 1
若窗口不再可行,则跳转至 2。
1 2 3 4 5 6 7 8 9 10 11 12 13
while right < len(s): if lookup[s[right]] > 0: count -= 1 lookup[s[right]] -= 1 right += 1 while count == 0: if min_len > right-left: min_len = right-left res = s[left:right] if lookup[s[left]] == 0: count += 1 lookup[s[left]] += 1 left += 1
classSolution(object): defcombine(self, n, k): """ :type n: int :type k: int :rtype: List[List[int]] """ res = [] defhelper(i,k,tmp): if k == 0: res.append(tmp) for j inrange(i,n+1): helper(j+1,k-1,tmp+[j]) helper(1,k,[]) return res
// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝 int len = removeDuplicates(nums);
// 在函数里修改输入数组对于调用者是可见的。 // 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。 for (int i = 0; i < len; i++) { print(nums[i]); }
解答:
思路:
1 2 3 4 5 6 7 8 9 10 11 12
classSolution(object): defremoveDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ i = 0 for n in nums: if i < 2or n != nums[i-2]: nums[i] = n i += 1 return i
此题可以有通用模板,将2改成k的话:
1 2 3 4 5 6 7 8
classSolution: defremoveDuplicates(self, nums, k): i = 0 for n in nums: if i < k or n != nums[i-k]: nums[i] = n i += 1 return i