当前位置:网站首页>An advanced method for solving palindromes
An advanced method for solving palindromes
2022-08-02 06:31:00 【Fanshui 682】
The first is to give an example:
Find the number of palindromes in a string.
To get this problem, we know that the complexity of solving the palindrome by brute force is O(n)
i means the beginning character, j means the end character, and then judge whether it is a palindrome
O(n) for each operation, which is n^3, which is too big.
Then we find that the palindrome is symmetric, so if you start from the middle to find whether it is a palindrome, the time complexity can be reduced.So, the double pointer idea:
For i, there are two types, one lets (l=i, r=i) and the other lets (l=i-1, r=i), each judgment lets l--, r++, and then encountersThe string continues to execute and ++cnt, otherwise jump out.
What's good about it?
At this time, due to the symmetry of the string, the search for whether it is a string and the expansion of the search range are carried out at the same time. This has changed from the original j, k two layers to the idea of continuous extension on both sides.
This is an improvement I can think of to take advantage of the symmetry of the palindrome.
The back is not what my stupid brain can think of..
Let's put it here first, there are several palindrome ideas.
But first of all, it needs to be explained that these things should be used. For example, finding the longest palindrome can also be used to find the length of the palindrome, and finding the palindrome at the end of X can also be used to find the palindrome.).
If you don't use it, you will learn in vain
Advanced idea of palindrome: horse-drawn carriage, dp (jump search and non-jump search), palindrome tree.
The following content is slowly filled ()
边栏推荐
- apisix-Getting Started
- 卸载redis
- Go language study notes - grpc serverclient protobuf Go language from scratch
- leetcode 204. Count Primes 计数质数 (Easy)
- 回文串求解的进阶方法
- 【C语言】LeetCode26.删除有序数组中的重复项&&LeetCode88.合并两个有序数组
- 国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
- 【漫画】2021满分程序员行为对照表(最新版)
- H5 access payment process - WeChat payment & Alipay payment
- C语言中i++和++i在循环中的差异性
猜你喜欢

Navicat new database
![[PSQL] 窗口函数、GROUPING运算符](/img/95/5c9dc06539330db907d22f84544370.png)
[PSQL] 窗口函数、GROUPING运算符

【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台

Introduction to Grid Layout

Introduction to coredns

为什么4个字节的float要比8个字节的long大呢?

关于鸿蒙系统 JS UI 框架源码的分析

复盘:图像饱和度计算公式和图像信噪(PSNR)比计算公式

MySql copies data from one table to another table

Use the browser's local storage to realize the function of remembering the user name
随机推荐
25K测试老鸟6年经验的面试心得,四种公司、四种问题…
Go language study notes - grpc serverclient protobuf Go language from scratch
Three methods of importing sql files in MySQL
Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟
kubernetes affinity, anti-affinity, taint, tolerance
关于鸿蒙系统 JS UI 框架源码的分析
【OpenCV从入门到实践】图像处理技术[像素](全网最详细)
18 years of programmer career, read more than 200 programming books, pick out some essence to share with you
Use the browser's local storage to realize the function of remembering the user name
上海交大牵手淘宝成立媒体计算实验室:推动视频超分等关键技术发展
PSQL function, predicate, CASE expression and set operations
使用TinkerPop框架对GDB增删改查
classSR论文阅读笔记
MySQL数据表的基本操作和基于 MySQL数据表的基本操作的综合实例项目
21 Day Learning Challenge Schedule
nacos注册中心
Introduction to coredns
测试技术之APP蓝牙连接测试
Brush LeetCode topic series - 10. Regular expression match
pytorch常用函数