当前位置:网站首页>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
边栏推荐
- Hard (magnetic) disk (II)
- 304. Merge two ordered arrays
- Dynamic planning - good article link
- Expression tree - medium order printout
- [JS component] customize the right-click menu
- STM32 USB Basics
- Bubble sort - alternate sort at both ends
- Common skills of quantitative investment -- Drawing Part 1: Drawing stock closing price curve and ochl candle chart
- Remove duplicates from an ordered array
- 【北亚服务器数据恢复】虚拟机文件丢失导致Hyper-V服务瘫痪的数据恢复案例
猜你喜欢
![[JS component] simulation framework](/img/f2/8d5bb7e0db55a87ce76c09fae03694.jpg)
[JS component] simulation framework

Jenkins持续集成操作

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

Wal mechanism of MySQL

Triangle wave and triangle wave convolution

Today's sleep quality record 74 points

spiral matrix visit Search a 2D Matrix
![[JS component] calendar](/img/20/71bb0c59da29b3cd3418e38cca39c0.jpg)
[JS component] calendar

深度学习每周期的步数多少合适?

How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic returns of BBI, MTM, obv, CCI and priceosc indicators
随机推荐
Common skills of quantitative investment - index part 2: detailed explanation of BOL (Bollinger line) index, its code implementation and drawing
Go simple read database
gpu加速pytorch能用吗?
Quantitative investment traditional index investment decision vs Monte Carlo simulation method
切线与切平面
[003] embedded learning: creating project templates - using stm32cubemx
Dynamic planning - good article link
[JS component] dazzle radio box and multi box
The grass is bearing seeds
[latex] insert picture
Zhouchuankai, Bank of Tianjin: from 0 to 1, my experience in implementing distributed databases
[JS component] browse progress bar
408 true question - division sequence
Pipeline流水线项目构建
Biological unlocking - Fingerprint entry process
Andersen global expands its business in northern Europe through cooperation agreements in Finland and Denmark
Jenkins持续集成操作
[virtual machine] notes on virtual machine environment problems
Traditional machine learning classification model predicts the rise and fall of stock prices under more than 40 indicators
Higherhrnet pre training model -- download from network disk