current position:Home>[fun bath] daily practice - "Li Kou: introduction to leetcode algorithm" (c + +): String - "longest common prefix - hypothetical sequential judgment method" 2021-12-16
[fun bath] daily practice - "Li Kou: introduction to leetcode algorithm" (c + +): String - "longest common prefix - hypothetical sequential judgment method" 2021-12-16
2022-01-26 23:37:04 【Playful bass】
【 Play bass 】 Practice every day ——《 Power button :LeetCode Introduction to algorithm 》(C++): character string ——「 The longest common prefix —— Hypothesis sequential judgment method 」 2021-12-16
Welcome to my WeChat official account. : Programming Pastor
ID:bianchengzhizhen
Timely sharing algorithm 、 Computer science and game programming
I am CSDN Blog home page :
https://blog.csdn.net/D16100?spm=1000.2115.3001.5343&type=blog
Welcome to exchange and study
subject :
author : Power button (LeetCode)
link :https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnmav1/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
Topic 1 : The longest common prefix
Write a function to find the longest common prefix in the string array .
If no common prefix exists , Returns an empty string “”.
Example 1:
Input :strs = [“flower”,“flow”,“flight”]
Output :“fl”
Example 2:
Input :strs = [“dog”,“racecar”,“car”]
Output :""
explain : Input does not have a common prefix .
Tips :
1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] It's only made up of lowercase letters
Related labels
character string
C++ Source program :
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
// I don't know why I use length() Compilation error
// First judge strs When the array does not exist
if (strs.size() == 0)
return "";
// Assume that the first string of the array is the public prefix of the second .
string pre = strs[0];
int i = 1; // At least two arrays , If only one , Then it itself is the longest public prefix , So from 1 Start
while(i < strs.size()){
while(strs[i].find(pre) != 0) // From the second string of the array ( Subscript to be 1) Start ,find The function looks for the existence of the first ( That is, the previous string ) The characters of . If there is , Will return the address of the first character , Return 0 It means that the first and second have the same common prefix
pre = pre.substr(0, pre.size() - 1 );// substr Intercept pre from 0 Subscript to length -1 The number of character . For example size() = 4, that substr(0,4-1), That is, intercept the subscript 0 Start length is 3 String , Subscript to be 0,1,2 This string of .
i++;
}
return pre;
}
};
Personal experience :
Read this question , A key factor must be understood —— That is, the prefix in the longest common prefix , That is, it can only be the prefix composed of the first few characters in each string . Make this point , Then you can know that what you have to consider is not very responsible .
We can directly assume that the first string of the array is the longest common prefix , And find out the longest common prefix between it and the second string . If there isn't the same , So long back “”, non-existent .
Related to knowledge :
- str1.find(str2) function
I.e. find str1 in str2 The first subscript address of the string
I am CSDN Blog has detailed knowledge explanation :
https://blog.csdn.net/D16100/article/details/121978209?spm=1001.2014.3001.5501
- str1.substr(a,b) function
Intercepting string str1 In the from a At the beginning b A string of length
I am CSDN Blog has detailed knowledge explanation :
https://blog.csdn.net/D16100/article/details/121978625
copyright notice
author[Playful bass],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262337009596.html
The sidebar is recommended
- Spring IOC container loading process
- [thinking] the difference between singleton mode and static method - object-oriented programming
- Hadoop environment setup (MySQL environment configuration)
- 10 minutes, using node JS creates a real-time early warning system for bad weather!
- Git tool
- Force deduction algorithm - 92 Reverse linked list II
- What is the sub problem of dynamic programming?
- C / C + +: static keyword summary
- Idea does not have the artifacts option when configuring Tomcat
- Anaconda can't open it
guess what you like
-
I don't know how to start this
-
Matlab simulation of transportation optimization algorithm based on PSO
-
MySQL slow log optimization
-
[Vue] as the window is stretched (larger, smaller, wider and higher), the text will not be displayed
-
Popular Linux distributions for embedded computing
-
Suzhou computer research
-
After installing SSL Certificate in Windows + tomcat, the domain name request is not successful. Please answer!!
-
Implementation time output and greetings of jQuery instance
-
The 72 year old uncle became popular. Wu Jing and Guo fan made his story into a film, which made countless dreamers blush
-
How to save computer research
Random recommended
- Springboot implements excel import and export, which is easy to use, and poi can be thrown away
- The final examination subjects of a class are mathematical programming, and the scores are sorted and output from high to low
- Two pronged approach, Tsinghua Professor Pro code JDK and hotspot source code notes, one-time learning to understand
- C + + recursive knapsack problem
- The use of GIT and GitHub and the latest git tutorial are easy to understand -- Video notes of crazy God speaking
- PostgreSQL statement query
- Ignition database test
- Context didn't understand why he got a high salary?, Nginxfair principle
- Bootstrap switch switch control user's guide, springcloud actual combat video
- A list that contains only strings. What other search methods can be used except sequential search
- [matlab path planning] multi ant colony algorithm grid map path planning [including GUI source code 650]
- [matlab path planning] improved genetic algorithm grid map path planning [including source code phase 525]
- Iinternet network path management system
- Appium settings app is not running after 5000ms
- Reactnative foundation - 07 (background image, status bar, statusbar)
- Reactnative foundation - 04 (custom rpx)
- If you want an embedded database (H2, hsql or Derby), please put it on the classpath
- When using stm32g070 Hal library, if you want to write to flash, you must perform an erase. If you don't let it, you can't write continuously.
- Linux checks where the software is installed and what files are installed
- SQL statement fuzzy query and time interval filtering
- 69. Sqrt (x) (c + + problem solving version with vs runnable source program)
- Fresh students are about to graduate. Do you choose Java development or big data?
- Java project: OA management system (java + SSM + bootstrap + MySQL + JSP)
- Titanic passenger survival prediction
- Vectorization of deep learning formula
- Configuration and use of private image warehouse of microservice architect docker
- Relearn JavaScript events
- For someone, delete return 1 and return 0
- How does Java dynamically obtain what type of data is passed? It is used to judge whether the data is the same, dynamic data type
- How does the database cow optimize SQL?
- [data structure] chain structure of binary tree (pre order traversal) (middle order traversal) (post order traversal) (sequence traversal)
- Webpack packaging optimization solution
- 5. Operation element
- Detailed explanation of red and black trees
- redhat7. 9 install database 19C
- Blue Bridge Cup notes: (the given elements are not repeated) complete arrangement (arrangement cannot be repeated, arrangement can be repeated)
- Detailed explanation of springboot default package scanning mechanism and @ componentscan specified scanning path
- How to solve the run-time exception of test times
- Detailed explanation of k8s management tool kubectl
- Android system view memory command