当前位置:网站首页>Leetcode-14- longest common prefix (simple)
Leetcode-14- longest common prefix (simple)
2022-06-13 00:59:00 【Didi dada】
14 The longest common prefix ( Simple )
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 .
- When the input is empty , non-existent
strs[0], therefore , If the input is blank, it needs to be discussed separately- The longest public prefix length is not greater than strs Shortest string length in
(1)C++
class Solution {
public:
string longestCommonPrefix(vector<string>& strs){
int n = strs.size();
if(n==0)
return "";
string s = "";
string temp = strs[0];
for(int i =1; i<n; i++){
if(temp.length()<strs[i].length())
temp = strs[i];
}
for(int j=0;j<temp.size();j++){
char temp = strs[0][j];
bool flag = true;
for(int i=1; i<n ;i++){
if(temp != strs[i][j])
flag = false;
}
if(flag)
s+=temp;
else
break;
}
return s;
}
};
(2)C++(C++ Boundary detection of non array strings in )
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if(strs.size() == 0)
return "";
string s = strs[0];
for(int i = 1; i < strs.size(); i++) {
for(int j = 0; j < s.length(); j++) {
if(s[j] != strs[i][j]) {
s = s.substr(0, j);
break;
}
}
}
return s;
}
};
(3)python
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if(len(strs)==0):
return ""
res = strs[0]
for each in strs:
if len(res)>len(each):
res = each
for i in range(len(strs)):
for j in range(len(res)):
if res[j]!= strs[i][j]:
res = res[:j]
break
return res
边栏推荐
- [JS] battle chess
- Dynamic planning - good article link
- Liu Hui and introduction to nine chapter arithmetic and island arithmetic
- Notes: the 11th and 12th generation mobile versions of Intel support the native thunderbolt4 interface, but the desktop version does not
- 深度学习训练多少轮?迭代多少次?
- Antdpro - protable realizes the linkage effect of two selection boxes
- [Latex] 插入圖片
- Build your own PE manually from winpe of ADK
- How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of DBCD, ROC, vroc, Cr and psy index strategy income
- MySQL exception: com mysql. jdbc. PacketTooBigException: Packet for query is too large(4223215 > 4194304)
猜你喜欢

生态聚合NFT来袭,Metaverse Ape引领Web 3.0元宇宙新范式革命
![[latex] insérer une image](/img/0b/3304aaa03d3fea3ebb93b0348c3131.png)
[latex] insérer une image

Matrix fast power

Hard (magnetic) disk (I)

Canvas game lower level 100

Pipeline流水线项目构建

How to solve the duplication problem when MySQL inserts data in batches?

Traditional machine learning classification model predicts the rise and fall of stock prices under more than 40 indicators

spiral matrix visit Search a 2D Matrix

5G工业网关在煤矿行业的应用优势
随机推荐
Liu Hui and introduction to nine chapter arithmetic and island arithmetic
[Latex] 插入图片
What is pytorch? Explain the basic concepts of pytorch
redis
[virtual machine] notes on virtual machine environment problems
Addition and modification of JPA
Rest at home today
【服务器数据恢复】存储服务器之间迁移数据时数据丢失恢复成功案例
[virtual machine] notes on virtual machine environment problems
3623. Merge two ordered arrays
Breadth first search for node editor runtime traversal
Bubble sort - alternate sort at both ends
Common skills of quantitative investment -- Drawing Part 1: Drawing stock closing price curve and ochl candle chart
今日睡眠质量记录74分
Notes: the 11th and 12th generation mobile versions of Intel support the native thunderbolt4 interface, but the desktop version does not
Canvas game 2048 free map size
What is dummy change?
How to handle different types of data
[JS component] create a custom horizontal and vertical scroll bar following the steam style
Aof persistence