跳至主要內容
Code Snappet

Binary Search Example

下述代码是为了将相邻的 PC 进行合并,并产生新的 PC 和计数,比如说:

image

有一系列的 PC 列表,此时我们需要将连续的 PC 合并到一起,它们后面的技术也增加到合并的结果中。

def merge_continuous_addresses(address_list, count_list):
    result = {}

    i = 0
    while i < len(address_list):
        address = address_list[i]
        count = count_list[i]

        j = i + 1
        current_sum = count

        while j < len(address_list) and int(address_list[j], 16) == int(address, 16) + 4 * (j - i):
            current_sum += count_list[j]
            j += 1

        result[address] = current_sum
        i = j

    return result

Someone小于 1 分钟Algorithmalgorithmother
Knapsack

概览

背包问题可以大致分为三类,分别是:

  1. 背包组合问题
  2. True/False 问题
  3. 最大最小问题

其基础的背包问题一般由两个模型演变而来:

  1. 0-1 背包问题
  2. 完全背包问题

本文先研究 0-1 背包和完全背包,而后对其他问题进行研究。

例题索引

0-1 背包

问题 类型 递推公式 备注
例题 LC474:1和0 0-1 背包最大最小值问题 dp[i] = max(dp[i], dp[i-num] + 1) 两个背包
例题 LC416:分割等和子集 0-1 背包True/False问题 dp[i] = dp[i] or dp[i - num]
例题 LC494:目标和 0-1 背包组合问题 dp[i] += dp[i - num]
例题 LC1049:最后一块石头的重量 III 0-1 背包最大最小值问题 dp[i] = max(dp[i], dp[i-stone] + stone)

Someone大约 16 分钟Algorithmalgorithmdp
Binary Search

Summary

1. 二分搜索模板

1.1 基本的二分搜索算法

  1. 手工实现

    class Solution:
        def search(self, nums: List[int], target: int) -> int:
            if not nums:
                return -1
            l, r = 0, len(nums) - 1
            while l <= r:
                mid = l + (r - l) // 2
                if nums[mid] < target:
                    l = mid + 1
                elif nums[mid] > target:
                    r = mid - 1
                else:
                    return mid
    
            return -1
    
  2. 使用 Python bisect

        def search_2(self, nums: List[int], target: int) -> int:
            res = bisect.bisect_left(nums, target)
            if res != len(nums) and nums[res] == target:
                return res
            return -1
    

Someone大约 9 分钟Algorithmalgorithmbinary_search
LCS

Summary

LCS 问题有很多的子问题,大致包括以下:

  • 最长公共子串,要求子串在原字符串中是连续

  • 最长公共子序列

  • 最大子序列

  • 最长递增子序列

最长公共子串

暴力求解

def LCSubStr(str1, str2, m, n):
    LCSuff = [[0 for i in range(n + 1)] for j in range(m + 1)]

    # 最长公共子串的长度
    result = 0

    # str1 中的最后相同的位置
    p = 0

    for i in range(m + 1):
        for j in range(n + 1):
            if(i == 0 or j == 0):
                LCSuff[i][j] = 0
            elif(str1[i - 1] == str2[j - 1]):
                LCSuff[i][j] = LCSuff[i-1][j-1] + 1
                result = max(result, LCSuff[i][j])
                p = i
            else:
                LCSuff[i][j] = 0

    return str1[p - result:p], result


X = 'OldSite:GeeksforGeeks.org'
Y = 'NewSite:GeeksQuiz.com'

m = len(X)
n = len(Y)

print('Length of Longest Common Substring is',
      LCSubStr(X, Y, m, n))

Someone小于 1 分钟Algorithmalgorithmleetcodelcs
String

String

Python String

求 String 长度

C String

Problems

在算法中,字符串的操作和数组一样,都是很热门的考察点,这篇文章将总结一下常见的有关 string 的算法,方便查阅学习、总结。

判断两个字符串是否只相差一个字母

注意,针对已经排序的字符串,判断这两个字符串是否只相差一个字母(也就是说第二个字符串比第一个字符串多最后一个字母,如 abcabcd 这种)。


Someone大约 3 分钟Algorithmalgorithmleetcodestring