编程模板总结

这篇博客探讨了滑动窗口在解决信息技术问题中的应用,包括寻找满足阈值的最大区间、判断区间重叠和处理碰撞的 asteroids 问题。通过使用双指针技巧,滑动窗口有效地遍历数组或字符串,寻找符合条件的子区间。此外,还涉及了回溯法在还原 IP 地址问题中的使用。
摘要由CSDN通过智能技术生成

1 滑窗

参考模板:

def findSubArray(nums):
    N = len(nums)                    # 数组/字符串长度
    left, right = 0, 0                 # 双指针,表示当前遍历的区间[left, right],闭区间
    sums = 0                       # 用于统计 子数组/子区间 是否有效,根据题目可能会改成求和/计数
    res = 0                        # 保存最大的满足题目要求的 子数组/子串 长度
    while right < N:                  # 当右边的指针没有搜索到 数组/字符串 的结尾
        sums += nums[right]            # 增加当前右边指针的数字/字符的求和/计数
        while 区间[left, right]不符合题意: # 此时需要一直移动左指针,直至找到一个符合题意的区间
            sums -= nums[left]        # 移动左指针前需要从counter中减少left位置字符的求和/计数
            left += 1              # 真正的移动左指针,注意不能跟上面一行代码写反
        # 到 while 结束时,我们找到了一个符合题意要求的 子数组/子串
        res = max(res, right - left + 1)  # 需要更新结果
        right += 1                 # 移动右指针,去探索新的区间
    return res

滑动窗口中用到了左右两个指针,它们移动的思路是:以右指针作为驱动,拖着左指针向前走。右指针每次只移动一步,而左指针在内部while循环中每次可能移动多步。右指针是主动前移动,探索未知的新区域;左指针是被迫移动,负责寻找满足题意的区间。

模板的整体思想是:

  1. 定义两个指针left和right,分别指向区间的开头和结尾,注意是闭区间;定义sums用来统计该区间内的各个字符出现次数;

  2. 第一重while循环是为了判断right指针的位置是否超出了数组边界;当right每次到了新位置,需要增加right指针的求和或计数;

  3. 第二重while循环是让left指针向右移动到[left,right]区间符合题意的位置;当left每次移动到新位置,需要减少left指针的求和或计数;

  4. 在第二重while循环之后,成功找到一个符合题意的[left,right]区间,题目要求最大的区间长度,因此更新res为max(res,当前区间的长度);

  5. right指针每次向右移动一步,开始探索新的区间;



示例:找满足阈值的最大区间

func getBestTimeWindow(usersPerHour []int, threshold int) []int {
	// 初始相关变量定义,如双指针、sum、res
	thisPtr := ptr{left:0, right:0, sum:0}
	thisRes := re{indexFore:0, indexBack:0, res:0}

	for thisPtr.right < len(usersPerHour) {
		thisPtr.sum += usersPerHour[thisPtr.right]  

		for thisPtr.sum > threshold {  // 若不满足条件,缩小滑窗
			thisPtr.sum -= usersPerHour[thisPtr.left]
			thisPtr.left++
		}
		// 这里处理我们要的信息
		if thisRes.res < thisPtr.right - thisPtr.left {
			thisRes.res  = max(thisRes.res, thisPtr.right - thisPtr.left)
			thisRes.indexFore, thisRes.indexBack = thisPtr.left, thisPtr.right
		}

		thisPtr.right++  // 开始扩大滑窗
	}
	// 处理不满足条件的情况
	if thisRes.res == 0 {
		return []int{-1, -1}
	}
	return []int{thisRes.indexFore, thisRes.indexBack}


区间判断与合并

判断区间是否重叠
func isIntersecting(arr [][2]int, startTime, endTime int) bool {
	for i := 0; i < len(arr); i++ {
		if arr[i][0] >= startTime && arr[i][0] < endTime {
			return false
		}
		if arr[i][0] <= startTime && arr[i][1] > startTime {
			return false
		}
	}
	return true
}


https://leetcode-cn.com/problems/asteroid-collision/submissions/

func asteroidCollision(asteroids []int) []int {
    stack := make([]int, 0, 8)
    for i := 0; i < len(asteroids); i++ {
        if len(stack) == 0 || stack[len(stack)-1] < 0 {
            stack = append(stack, asteroids[i])
        } else if asteroids[i] < 0 {
            stack = task(stack, asteroids, asteroids[i])
        } else if asteroids[i] > 0 {
            stack = append(stack, asteroids[i])
        }
    }
    return stack
}

func task(stack []int, asteroids []int, dest int) []int {
    for len(stack) != 0 {
        num := stack[len(stack)-1]

        if num < 0 {
            stack = append(stack, dest)
            return stack
        }

        stack = append(stack[:len(stack)-1])
        if num > int(math.Abs(float64(dest))) {
            stack = append(stack, num)
            return stack
        } else if num == int(math.Abs(float64(dest))) {
            return stack
        }
    }
    stack = append(stack, dest)
    return stack
}


回溯

https://leetcode-cn.com/problems/restore-ip-addresses/

func isValid1(temp []int, s string) bool {
    num := 0
    for i := 0; i < len(temp); i++ {
        num += temp[i]
    }
    if num == len(s) {
        return true
    }
    return false
}

func isValid2(temp []int, s string) bool {
    num := 0
    for i := 0; i < len(temp); i++ {
        str := s[num : temp[i]+num]
        if n, _ := strconv.Atoi(str); n > 255 {
            return false
        }
        if len(str) > 1 && str[0] == '0' {
            return false
        }
        num += temp[i]
    }
    return true
}

func task(temp []int, s string, res []string) string {
    num := 0
    r := ""
    for i := 0; i < len(temp); i++ {
        str := s[num : temp[i]+num]
        r += str + "."
        num += temp[i]
    }
    return r[:len(r)-1]
}

func backTrack(temp []int, s string, res *[]string, nums []int) {
    if len(temp) > 4 {
        return 
    }
    if len(temp) == 4 && isValid1(temp, s) && isValid2(temp, s) {
        *res = append(*res, task(temp, s, *res))
        return 
    }
    for i := 0; i < len(nums); i++ {
        temp = append(temp, nums[i])
        backTrack(temp, s, res, nums)
        temp = append(temp[:len(temp)-1])
    }
}

func restoreIpAddresses(s string) []string {
    if len(s) < 4 {
        return []string{}
    }
    nums := []int{1, 2, 3}
    res := make([]string, 0, 8)
    backTrack([]int{}, s, &res, nums)
    return res
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

华为云计算搬砖工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值