当前位置:网站首页>[leetcode] 14. Longest public prefix
[leetcode] 14. Longest public prefix
2022-06-29 12:20:00 【Xiaoqu】
14、 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
Their thinking :
This question is not difficult , Just use the most popular solution , The strings in the array are compared one by one , Same take out , Different pass Just drop it .
- label : Linked list
- When the string array length is 0 The public prefix is empty , Go straight back to
- Make the longest public prefix ans The value of is the first string , To initialize
- Traversing the following string , In turn, match it with ans Compare , Find the common prefix , The end result is the longest common prefix
- If during the search ans Empty situation , Then the public prefix does not exist and directly returns
- Time complexity :O(s)O(s),s For the sum of the length of all strings
Reference code :
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length == 0)
return "";
String ans = strs[0];
for(int i =1;i<strs.length;i++) {
int j=0;
for(;j<ans.length() && j < strs[i].length();j++) {
if(ans.charAt(j) != strs[i].charAt(j))
break;
}
ans = ans.substring(0, j);
if(ans.equals(""))
return ans;
}
return ans;
}
}

边栏推荐
- GBase8s数据库select有ORDER BY 子句3
- ERP Kingdee for preparing BOM
- 《Go题库·14》WaitGroup的坑
- GBase8s数据库select有ORDER BY 子句4
- GBase8s数据库select有ORDER BY 子句6
- 《自卑与超越》生活对你应有的意义
- GBase8s数据库FOR READ ONLY 子句
- Wang Yingqi, founder of ones, talks to fortune (Chinese version): is there any excellent software in China?
- Syntax of gbase8s database incompatible with for update clause
- Jerry's configuration of TWS cross pairing [chapter]
猜你喜欢

智能垃圾桶(四)——树莓派pico实现超声波测距(HC-SR04)

AUTOCAD——文字显示方式、CAD怎么直接打开天正图纸

An interpretable geometric depth learning model for structure based protein binding site prediction

ERP编制物料清单 华夏

模糊图片变清晰,一键双色图片,快速整理本地图片...这8个在线图片工具申请加入你的收藏夹!

年轻就要醒着拼,年轻就要勇于尝试

面试突击61:说一下MySQL事务隔离级别?

When you are young, you should be awake to fight, and when you are young, you should have the courage to try

架构实战营第五模块课后作业

JVM之方法区
随机推荐
对p值的理解
Jerry's about TWS pairing mode configuration [chapter]
Numpy的ndarray数组基础
Jerry's configuration of TWS cross pairing [chapter]
& 4 express framework
Unified exception reporting practice based on bytecode
Gbase8s database select has an order by clause
Jerry's WiFi interferes with Bluetooth [chapter]
Gbase8s database select has order by Clause 6
百度云盘不限速下载大文件(2021-11亲测有效)
ERP preparation of bill of materials Huaxia
[VTK] MFC grid editor based on vtk8.2
GBase8s数据库INTO TEMP 子句创建临时表来保存查询结果。
ERP编制物料清单 华夏
Jerry's about TWS pairing mode configuration [chapter]
Gbase8s database select has order by Clause 6
Embedded database development programming (IV) -- DDL, DML
什么是外链和内链?
高校如何基于云原生构建面向未来的智慧校园?全栈云原生架构VS传统IT架构
Titanium dynamic technology: our Zadig landing Road