当前位置:网站首页>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 ()
边栏推荐
- nacos注册中心
- Redis-集群模式(主从复制模式,哨兵模式,集群化模式)
- Review: image saturation calculation formula and image signal-to-noise (PSNR) ratio calculation formula
- 家用 NAS 服务器(4)| MergerFS和SnapRaid数据定时备份
- 高防服务器防御的原理是什么
- ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution
- swinIR论文阅读笔记
- 无代码生产新模式探索
- Introduction to Grid Layout
- Stress testing and performance analysis of node projects
猜你喜欢
随机推荐
JUC(二)原子类:CAS、乐观锁、Unsafe和原子类
LeetCode brush topic series - 787 K station transfer within the cheapest flight
服务器的单机防御与集群防御
18 years of programmer career, read more than 200 programming books, pick out some essence to share with you
21 Day Learning Challenge Schedule
25K test old bird's 6-year experience in interviews, four types of companies, four types of questions...
nacos注册中心
Redis数据库
Redis-----非关系数据库
Linux CentOS8安装Redis6
测试环境要多少?从成本与效率说起
跨桌面端Web容器演进
关于 VS Code 优化启动性能的实践
程序员最重要的能力是什么?
LeetCode刷题系列 -- 10. 正则表达式匹配
双重for循环案例(用js打印九九乘法表)
eggjs controller层调用controller层解决方案
There are more and more talents in software testing. Why are people still reluctant to take the road of software testing?
navicat connects to MySQL and reports an error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
Detailed installation and configuration of golang environment