当前位置:网站首页>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
边栏推荐
- Biological unlocking - Fingerprint entry process
- gpu加速pytorch能用吗?
- sort
- How to handle different types of data
- Traditional machine learning classification model predicts the rise and fall of stock prices
- pytorch是什么?解释pytorch的基本概念
- 生物解锁--指纹录入流程
- Et5.0 configuring Excel
- Hard (magnetic) disk (I)
- What is the difference between pytorch and tensorflow?
猜你喜欢

Kotlin coroutine withcontext switch thread

Comparison of disk partition modes (MBR and GPT)
![[JS component library] drag sorting component](/img/f9/4090b52da1a5784b834cb7dbbb948c.jpg)
[JS component library] drag sorting component
![[JS component] dazzle radio box and multi box](/img/2a/00620bee312972db93e1db4313385f.jpg)
[JS component] dazzle radio box and multi box

Canvas random bubbling background
![[latex] insérer une image](/img/0b/3304aaa03d3fea3ebb93b0348c3131.png)
[latex] insérer une image
![[sca-cnn interpretation] spatial and channel wise attention](/img/4b/bdcdab17d531481bf0222a96ace065.png)
[sca-cnn interpretation] spatial and channel wise attention

What is pytorch? Explain the basic concepts of pytorch

sort
![[network protocol] problems and solutions in the use of LwIP](/img/25/d064a761724936b8f35ee0c779e597.jpg)
[network protocol] problems and solutions in the use of LwIP
随机推荐
How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic returns of BBI, MTM, obv, CCI and priceosc indicators
[JS component] create a custom horizontal and vertical scroll bar following the steam style
Breadth first search for node editor runtime traversal
Comparison of disk partition modes (MBR and GPT)
MySQL performance analysis - explain
Four startup modes of kotlin collaboration
Et5.0 simply transform referencecollectorieditor
Kotlin coroutine withcontext switch thread
Introduction to ROS from introduction to mastery (zero) tutorial
Google play console crash information collection
[backtrader source code analysis 7] analysis of the functions for calculating mean value, variance and standard deviation in mathsupport in backtrader (with low gold content)
[JS component] dazzle radio box and multi box
Opencv desaturation
Three threads print digital demo alternately
Lessons learned from the NLP part of the course "Baidu architects hands-on deep learning"
什么是 dummy change?
Andersen global expands its business in northern Europe through cooperation agreements in Finland and Denmark
Go simple read database
spiral matrix visit Search a 2D Matrix
【北亚服务器数据恢复】虚拟机文件丢失导致Hyper-V服务瘫痪的数据恢复案例