当前位置:网站首页>Longest public prefix of leetcode
Longest public prefix of leetcode
2022-07-02 20:35:00 【wx58c8fa5d0b356】
List of articles
- Example
LeetCode The longest public prefix of the brush question
Title Description
Write a function to find the longest common prefix in the string array .
If no common prefix exists , Returns an empty string “”.
Example
Example 1:
Input : [“flower”,“flow”,“flight”]
Output : “fl”
Example 2:
Input : [“dog”,“racecar”,“car”]
Output : “”
explain : Input does not have a common prefix .
explain :
All inputs contain only lowercase letters a-z .
Problem solving
Ideas
Take any character , from 0 Start to intercept string , See whether the interception result is the same as that of other strings .
If it's not the same , What I intercepted before is the longest public prefix .
Note that the characters you choose are not necessarily the shortest , To prevent the index from crossing the boundary , It needs to be verified .
Code
public static String longestCommonPrefix2(String[] strs) {
// Visibility verification
if (strs.length == 0) {
return "";
}
if (strs.length == 1) {
return strs[0];
}
// answer
String ans = "";
for (int i = 0; i < strs[0].length(); i++) {
String substring = strs[0].substring(0, i + 1);
for (int j = 1; j < strs.length; j++) {
if (substring.length() > strs[j].length()) {
return ans;
}
if (!substring.equals(strs[j].substring(0, i + 1))) {
return ans;
}
}
ans = substring;
}
return ans;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
A regular matching version is attached below , It's a little slow , It is not recommended to adopt
public static String longestCommonPrefix(String[] strs) {
// Shortest character
String shortest = strs[0];
for (int i = 1; i < strs.length; i++) {
if (strs[i].length() < shortest.length()) {
shortest = strs[i];
}
}
// One is "" The result is ""
if (shortest.length() == 0) {
return "";
}
for (int i = 0; i < shortest.length(); i++) {
String regex = "^" + shortest.substring(0, i + 1) + "\\w*$";
for (String element : strs) {
if (!Pattern.matches(regex, element)) {
return shortest.substring(0, i);
}
}
}
return shortest;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
边栏推荐
- SBT tutorial
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of sound quality head simulators in the global market in 2022
- 【Kubernetes系列】kubeadm reset初始化前后空间、内存使用情况对比
- Driverless learning (4): Bayesian filtering
- [译]深入了解现代web浏览器(一)
- Research Report on the overall scale, major manufacturers, major regions, products and applications of building automation power meters in the global market in 2022
- pytorch 模型保存的完整例子+pytorch 模型保存只保存可訓練參數嗎?是(+解决方案)
- Share several map bed websites for everyone to share pictures
- Implementing yolox from scratch: dataset class
- Research Report on the overall scale, major manufacturers, major regions, products and applications of capacitive voltage transformers in the global market in 2022
猜你喜欢
Friends who firmly believe that human memory is stored in macromolecular substances, please take a look
Overview of browser caching mechanism
Use graalvm native image to quickly expose jar code as a native shared library
[internship] solve the problem of too long request parameters
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training
AcWing 340. Solution to communication line problem (binary + double ended queue BFS for the shortest circuit)
After eight years of test experience and interview with 28K company, hematemesis sorted out high-frequency interview questions and answers
B端电商-订单逆向流程
Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
随机推荐
2021 v+ Quanzhen internet global innovation and Entrepreneurship Challenge, one of the top ten audio and video scene innovation and application pioneers
疫情封控65天,我的居家办公心得分享 | 社区征文
[fluent] dart technique (independent main function entry | nullable type determination | default value setting)
For (Auto A: b) and for (Auto & A: b) usage
Motivation! Big Liangshan boy a remporté le prix Zhibo! Un article de remerciement pour les internautes qui pleurent
Cs5268 perfectly replaces ag9321mcq typec multi in one docking station solution
After 65 days of closure and control of the epidemic, my home office experience sharing | community essay solicitation
[JS] get the search parameters of URL in hash mode
HDL design peripheral tools to reduce errors and help you take off!
Research Report on the overall scale, major manufacturers, major regions, products and applications of sliding door dampers in the global market in 2022
B-end e-commerce - reverse order process
[871. Minimum refueling times]
想请教一下,究竟有哪些劵商推荐?手机开户是安全么?
Redis sentinel cluster working principle and architecture deployment # yyds dry goods inventory #
I did a craniotomy experiment: talk about macromolecule coding theory and Lao Wang's fallacy from corpus callosum and frontal leukotomy
CRM客户关系管理系统
Research Report on the overall scale, major manufacturers, major regions, products and applications of friction dampers in the global market in 2022
Research Report on the overall scale, major manufacturers, major regions, products and applications of micro hydraulic cylinders in the global market in 2022
外包干了三年,废了...
Research Report on the overall scale, major manufacturers, major regions, products and applications of swivel chair gas springs in the global market in 2022