当前位置: 首页 > news >正文

手撕算法(1)

IP属地归属(双指针法)

输出最长回文子串

这段代码的目的是找到字符串 s 中的最长回文子串。回文子串是指正读和反读都相同的子串。代码的核心思想是通过遍历字符串中的每一个字符,尝试以该字符为中心扩展,找到最长的回文子串。

代码解析

1. for i in range(len(s)):
  • 这行代码遍历字符串 s 中的每一个字符,i 是当前字符的索引。
  • 对于每一个字符,代码尝试找到以该字符为中心的最长回文子串。
2. start = max(i - len(res) -1, 0)
  • start 是当前考虑的子串的起始索引。
  • i - len(res) - 1 是为了确保当前考虑的子串长度至少比已经找到的最长回文子串 res 长 1。这是因为如果当前子串的长度不大于 res,那么它不可能是更长的回文子串。
  • max(..., 0) 是为了确保 start 不会小于 0,即不会超出字符串的起始位置。
3. temp = s[start: i+1]
  • temp 是从 start 到 i 的子串(包括 i)。
  • 这个子串的长度是 i - start + 1
4. if temp == temp[::-1]:
  • 这行代码检查 temp 是否是回文子串。temp[::-1] 是 temp 的反转。
  • 如果 temp 是回文子串,那么更新 res 为 temp
5. else: temp = temp[1::]
  • 如果 temp 不是回文子串,那么代码尝试去掉 temp 的第一个字符,得到一个新的子串 temp[1::]
  • 这个新的子串是从 start + 1 到 i 的子串。
6. if temp == temp[::-1]: res = temp
  • 再次检查新的子串 temp 是否是回文子串。
  • 如果是,更新 res 为 temp

重点解析

start = max(i - len(res) -1, 0)
  • 这个公式的目的是为了减少不必要的检查。如果当前已经找到的最长回文子串 res 的长度是 len(res),那么只有当新的子串长度大于 len(res) 时,才有可能找到更长的回文子串。
  • i - len(res) - 1 是为了确保新的子串长度至少比 res 长 1。
  • max(..., 0) 是为了防止 start 为负数,确保子串不会超出字符串的起始位置。
else: temp = temp[1::]
  • 如果当前子串 temp 不是回文子串,代码尝试去掉 temp 的第一个字符,得到一个新的子串 temp[1::]
  • 这个操作相当于将子串的起始位置向右移动一位,继续检查新的子串是否是回文子串。
  • 这个操作是为了确保即使当前子串不是回文子串,仍然有可能在去掉一个字符后找到更长的回文子串。

总结

这段代码通过遍历字符串中的每一个字符,尝试以该字符为中心扩展,找到最长的回文子串。start = max(i - len(res) -1, 0) 是为了减少不必要的检查,而 else: temp = temp[1::] 是为了确保即使当前子串不是回文子串,仍然有可能在去掉一个字符后找到更长的回文子串。

class Solution(object):def longestPalindrome(self, s):""":type s: str:rtype: str"""res = ' 'for i in range(len(s)):start = max(i-len(res)-1, 0)temp = s[start: i+1]if temp == temp[::-1]:res = tempelse:temp = temp[1::]if temp == temp[::-1]:res = tempreturn res

最长连续子序列

股票买卖的最佳时机

合并两个有序数组

如果 p1 == m,说明 nums1 已经遍历完毕,直接将 nums2 的剩余部分添加到 sorted_list。
如果 p2 == n,说明 nums2 已经遍历完毕,直接将 nums1 的剩余部分添加到 sorted_list。
如果 nums1[p1] < nums2[p2],将 nums1[p1] 添加到 sorted_list,并移动 p1 指针。
否则,将 nums2[p2] 添加到 sorted_list,并移动 p2 指针。

class Solution(object):def merge(self, nums1, m, nums2, n):""":type nums1: List[int]:type m: int:type nums2: List[int]:type n: int:rtype: None Do not return anything, modify nums1 in-place instead."""sorted_list = []p1, p2 = 0, 0while p1 < m or p2 < n:if p1 == m:sorted_list.append(nums2[p2])p2 += 1elif p2 == n:sorted_list.append(nums1[p1])p1 += 1elif nums1[p1] < nums2[p2]:sorted_list.append(nums1[p1])p1 += 1else:sorted_list.append(nums2[p2])p2 += 1nums1[:] = sorted_list

快手测开

http://www.xdnf.cn/news/304741.html

相关文章:

  • 使用 Spring Boot 构建 REST API
  • SpringBoot教学管理平台源码设计开发
  • leetcode 24. 两两交换链表中的节点
  • 分库分表后复杂查询的应对之道:基于DTS实时性ES宽表构建技术实践
  • 简说Policy Gradient (1) —— 入门
  • [蓝桥杯 2025 省 B] 水质检测(暴力 )
  • python--------修改桌面文件内容
  • 第2章 神经网络的数学基础
  • 神经网络之激活函数:解锁非线性奥秘的关键
  • Linux开发工具【上】
  • 2025年LangChain(V0.3)开发与综合案例
  • 接口自动化工具如何选择?以及实战介绍
  • windows操作系统开机自启(自动启动) 运行窗口 shell:startup 指令调出开机自启文件夹
  • 驱动开发系列57 - Linux Graphics QXL显卡驱动代码分析(四)显示区域绘制
  • 使用原生javascript手动实现一个可选链运算符
  • [论文阅读]MCP Guardian: A Security-First Layer for Safeguarding MCP-Based AI System
  • 【Spring Boot 注解】@Configuration与@AutoConfiguration
  • vue2项目中使用pag格式动图
  • GMRES算法处理多个右端项的Block与PseudoBlock变体
  • 【已解决】Neo4j Desktop打不开,不断网解决
  • 一种基于条件生成对抗网络(cGAN)的CT重建算法
  • Hadoop架构再探讨
  • keil+vscode+腾讯ai助手
  • 【prometheus+Grafana篇】基于Prometheus+Grafana实现Linux操作系统的监控与可视化
  • 【程序员AI入门:基础】5.提示工程怎么释放LLM的潜力
  • WT2606B显示驱动TFT语音芯片IC:重塑电子锁交互体验的技术革新
  • 神经网络之训练的艺术:反向传播与常见问题解决之道
  • 数据库实验10 函数存储
  • Dify - Stable Diffusion
  • 《数据分析与可视化》(清华)ch-6 作业 三、绘图题