当前位置:网站首页>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 ()
边栏推荐
- How Navicat Connects to MySQL
- 保证家里和企业中的WIFI安全-附AC与AP组网实验
- PSQL function, predicate, CASE expression and set operations
- LeetCode brush topic series - 787 K station transfer within the cheapest flight
- 面试官:设计“抖音”直播功能测试用例吧
- The Go language learning notes - dealing with timeout - use the language from scratch from Context
- 家用 NAS 服务器(4)| MergerFS和SnapRaid数据定时备份
- 程序员最重要的能力是什么?
- 网安学习-内网渗透4
- 在腾讯做外包测试的那些日子.....
猜你喜欢
金山云团队分享 | 5000字读懂Presto如何与Alluxio搭配
Use the browser's local storage to realize the function of remembering the user name
Mysql数据库 | 基于Docker搭建Mysql-8.0以上版本主从实例实战
Review: image saturation calculation formula and image signal-to-noise (PSNR) ratio calculation formula
Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)
对node工程进行压力测试与性能分析
nacos注册中心
[C language] LeetCode26. Delete duplicates in an ordered array && LeetCode88. Merge two ordered arrays
5年在职经验之谈:2年功能测试、3年自动化测试,从入门到不可自拔...
APP Bluetooth connection test of test technology
随机推荐
leetcode每天5题-Day04
整合ssm(一)
Install and use Google Chrome
C language: Check for omissions and fill in vacancies (3)
How H5 realizes evoking APP
Features and installation of non-relational database MongoDB
Mysql implements optimistic locking
Constructors, member variables, local variables
51单片机外设篇:红外通信
线程基础(一)
Meta公司内部项目-RaptorX:将Presto性能提升10倍
深度学习——CNN实现MNIST手写数字的识别
区块元素、内联元素(<div>元素、span元素)
Navicat cannot connect to mysql super detailed processing method
为什么4个字节的float要比8个字节的long大呢?
APP Bluetooth connection test of test technology
Difference and analysis of CPU usage and load
LeetCode brush topic series - 787 K station transfer within the cheapest flight
Matlab paper illustration drawing template No. 41 - bubble chart (bubblechart)
kubernetes affinity, anti-affinity, taint, tolerance