滑动窗口+前缀和-9--LC1248.统计[优美子数组] class Solution(object): def numberOfSubarrays(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ def leK(nums, k): start = 0 odd_count = 0 count = .
滑动窗口+前缀和-8--LC930.和相同的二元子数组 class Solution(object): def numSubarraysWithSum(self, nums, goal): """ :type nums: List[int] :type goal: int :rtype: int """ # 1.前缀和 presum = 0 adict = {0:1} count = 0 for.
前缀和-LC560.和为K的子数组 class Solution(object): def subarraySum(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ # 前缀和+哈希表优化 # 使用哈希表,空间换时间 # 在遍历的过程中构建前缀和 # 使用哈希表或者字典记录每个前缀和出现的次数 .
滑动窗口+前缀和-7--LC992.K个不同整数的子数组 class Solution(object): def subarraysWithKDistinct(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ # 904题的进阶,904是寻找最长,这里限定了种类个数k # 这里涉及到状态回缩 # end在for循环里自动更新,end每次只.
滑动窗口-6--LC1004.最大连续1的个数III class Solution(object): def longestOnes(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ max_len = float('-inf') star = 0 count = 0 for end in range(len(nums.
滑动窗口-5--LC904.水果成篮 class Solution(object): def minWindow(self, s, t): """ :type s: str :type t: str :rtype: str """ # 滑动窗口,不定长问题 from collections import Counter temp = Counter(t) # 定义一个与t一样的滑动窗口字典 .
滑动窗口-4--LC76.最小覆盖子串 class Solution(object): def minWindow(self, s, t): """ :type s: str :type t: str :rtype: str """ # 滑动窗口,不定长问题 from collections import Counter temp = Counter(t) # 定义一个与t一样的滑动窗口字典 .
滑动窗口-3--LC567.字符串的排列 class Solution(object): def checkInclusion(self, s1, s2): """ :type s1: str :type s2: str :rtype: bool """ # 滑动窗口 # 这个题与28是同类的,都是定长的滑动窗口问题 # 不同在于,此题考虑到排列,排列乍一看比较麻烦,需要考虑的情况很多 # 我们可.
滑动窗口-2--LC3.无重复字符的最长子串 class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ # 滑动窗口 if not len(s): return 0 # 最大就定义极小,最小就定义极大 res = float('-inf') i .
滑动窗口-1--LC28.实现strStr() class Solution(object): def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ # API # return haystack.find(needle) # 滑动窗口 star = 0 .
双指针-2--LC977.有序数组的平方 class Solution(object): def sortedSquares(self, nums): """ :type nums: List[int] :rtype: List[int] """ # 双指针法 # 非递减顺序的整数数组有三种情况“ # 1.非负数,平方后还是非递减 # 2.负数,平方后是非递增 # 3.有正有负,要具体考虑 .
双指针-2--LC844.比较含退格的字符串 class Solution(object): def backspaceCompare(self, s, t): """ :type s: str :type t: str :rtype: bool """ # 1.用栈清除元素后比较 def fun(str_): result = list() for i in str_: .
快慢指针-1--LC26.删除有序数组中的重复项 class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ # 快慢指针,27的进阶,先做27 # 如果len(nums)=0,没有元素,return 0 # 如果len(nums)=1,也没有重复元素,return 1 # 因此sl.
二分查找法-3--LC.有效的完全平方数 class Solution(object): def isPerfectSquare(self, num): """ :type num: int :rtype: bool """ # 69的进阶,二分查找法 l, r = 1, num while l<=r: mid = l + ((r-l)>>1) if mid * .
二分查找法-2--LC69.x的平方根 class Solution(object): def mySqrt(self, x): """ :type x: int :rtype: int """ # 二分查找法 # x平方根的整数部分是满足 k^2<=x的k的最大值,可以使用二分查找法查找k l, r, num = 0, x, -1 while l <= r: mid = l.
二分查找-1--LC34.在排序数组中查找元素的第一个和最后一个位置 class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ # 二分查找法 n = len(nums) if not n: return [-1, -1].
动态规划--LC518.零钱兑换II class Solution(object): def change(self, amount, coins): """ :type amount: int :type coins: List[int] :rtype: int """ # 完全背包 # 每一种面额的硬币有无限个 # dp[j]:金额为j的组合数,最终目标是dp[amount] # 此时的金.
动态规划--LC474.一和零 class Solution(object): def findMaxForm(self, strs, m, n): """ :type strs: List[str] :type m: int :type n: int :rtype: int """ # 这个题是一个01背包问题,不一样的是,背包的容量是2维的,有m和n # 确定dp # dp[i][j.
动态规划--LC494.目标和 此类问题与传统的01背包问题稍有不同,尤其是状态转移方程。01背包问题会有max(),求一个最大;而此问题求的是能有多少种方法,面对第i个元素,存在取与不取的问题,最后加起来就是所有的方法。class Solution(object): def findTargetSumWays(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int.