当前位置:网站首页>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
边栏推荐
猜你喜欢

Physical orbit simulation

Influence of higher order poles on waveform

什么是 Meebits?一个简短的解释

spiral matrix visit Search a 2D Matrix

How many rounds of deep learning training? How many iterations?

Opencv desaturation
![[JS component library] drag sorting component](/img/f9/4090b52da1a5784b834cb7dbbb948c.jpg)
[JS component library] drag sorting component

切线与切平面

Binary tree - right view

Deep learning model pruning
随机推荐
[JS component] custom paging
论文笔记:STMARL: A Spatio-Temporal Multi-AgentReinforcement Learning Approach for Cooperative Traffic
[latex] insérer une image
Aof persistence
[server data recovery] successful cases of data loss recovery during data migration between storage servers
Rotating camera
MCU serial port interrupt and message receiving and sending processing -- judge and control the received information
刘徽与《九章算术》《海岛算经》简介
Quick power explanation
Kotlin 协程的四种启动模式
Most elements leetcode
How many rounds of deep learning training? How many iterations?
Unity extension
Wal mechanism of MySQL
Can GPU acceleration pytorch work?
Five classic articles worth reading
kotlin 协程withContext切换线程
Common skills for quantitative investment - drawing 2: drawing the moving average
[Latex] 插入圖片
[virtual machine] notes on virtual machine environment problems