当前位置:网站首页>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 ()
边栏推荐
- 5年在职经验之谈:2年功能测试、3年自动化测试,从入门到不可自拔...
- ATM系统
- Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟
- Three methods of importing sql files in MySQL
- H5 access payment process - WeChat payment & Alipay payment
- 软件测试的需求人才越来越多,为什么大家还是不太愿意走软件测试的道路?
- 使用TinkerPop框架对GDB增删改查
- 【C语言】LeetCode26.删除有序数组中的重复项&&LeetCode88.合并两个有序数组
- 165.比较版本号
- 无代码生产新模式探索
猜你喜欢
随机推荐
Google 安装印象笔记剪藏插件
服务器的单机防御与集群防御
非关系型数据库MongoDB的特点及安装
swinIR论文阅读笔记
golang泛型
对node工程进行压力测试与性能分析
跨桌面端Web容器演进
面试官:设计“抖音”直播功能测试用例吧
Google Chrome(谷歌浏览器)安装使用
classSR论文阅读笔记
C语言入门实战(13):十进制数转二进制
Stress testing and performance analysis of node projects
浏览器的onload事件
说好的女程序员做测试有优势?面试十几家,被面试官虐哭~~
The company does not pay attention to software testing, and the new Ali P8 has written a test case writing specification for us
pytorch常用函数
为什么4个字节的float要比8个字节的long大呢?
双重for循环案例(用js打印九九乘法表)
51单片机外设篇:ADC
ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution