网站首页 > 技术文章 正文
后面会继续写有关wifi的内容,最近继续算法练习
题目: 给定一个字符串 s,找到 s 中最长的回文子串。可以假设 s 的最大长度为 1000。
思路:根据回文特性,可以暴力计算但比较耗时。这里根据leetcode其他人介绍的中心检测法,来实现。即因为回文左右对称,即有中心点。遍历中心点,即可用n^2的时间复杂度求解。
代码:
res = '' if s is None or len(s)< 1: return res for i in range(0,len(s)): s1 = self.pointAroud(s,i,i) s2 = self.pointAroud(s,i,i+1) if len(s1) > len(s2): if len(s1) > len(res): res = s1 else: if len(s2) > len(res): res = s2 return res def pointAroud(self,s,left,right): while left > -1 and right < len(s) and s[left] == s[right]: left -= 1 right += 1 return s[left+1:right]
结果:
猜你喜欢
- 2025-05-28 RxJs 介绍
- 2025-05-28 19个基本的JavaScript面试问题及答案(都是实用技巧)免费送
- 2025-05-28 数组双指针直接秒杀七道题目
- 2025-05-28 确定有限状态机(DFA)-Leet
- 2025-05-28 互联网公司中:程序员大厂履历意味着什么?月薪35K?
- 2025-05-28 六十六、Leetcode数组系列(中篇)
- 2025-05-28 征服LeetCode,你的刷题之路就此开始,手把手带你了解算法奥秘
- 2025-05-28 C语言干货:函数知识详解(变量的作用域,全局变量,静态变量)
- 2025-05-28 Python 从入门到精通:一个月就够了
- 2025-05-28 如果你真的想学Python可以试试我的方法
- 261℃Python短文,Python中的嵌套条件语句(六)
- 261℃python笔记:for循环嵌套。end=""的作用,图形打印
- 260℃PythonNet:实现Python与.Net代码相互调用!
- 255℃Python实现字符串小写转大写并写入文件
- 254℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 110℃原来2025是完美的平方年,一起探索六种平方的算吧
- 94℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 87℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)