当前位置:网站首页>14. longest common prefix
14. longest common prefix
2022-06-22 23:39:00 【Front end plasterer】
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 : ["flower","flow","flight"]
Output : "fl"
All input contains only subtitles
solution 1:
Their thinking : Let's take one of them for comparison , Then iterate over the remaining array elements , Compare whether the same position is the same . When it's different , Return previously obtained results , End cycle
function longestCommonPrefix(strs){
let obj = {
};
let str = strs[0]
if(!str) return "";
let res = "";
for (let i = 0; i < str.length; i++) {
let flag = strs.every( item=> {
return item[i] === str[i]
})
if(flag){
res += str[i];
}else{
return res;
}
}
return res;
}
solution 2:
Their thinking : Take the first element of the array , Then loop through each character of the first element , Compare characters in the same position as other elements , When the same position of each array element is different , End cycle
function longestCommonPrefix(strs){
if(!strs.length) return "";
let idx = 0,n;
while(idx < strs[0].length){
n = 1;
while(n < strs.length ){
if(strs[n][idx] != strs[0][idx]){
return strs[0].substring(0,idx);
}
n++;
}
idx++;
}
return strs[0]
}
边栏推荐
- LeetCode_ Backtracking_ Dynamic programming_ Medium_ 131. split palindrome string
- c# sqlsugar,hisql,freesql orm框架全方位性能测试对比 sqlserver 性能测试
- Tp5.1 solving cross domain problems
- Spark RDD Programming Guide(2.4.3)
- 为 localStorage 添加过期时间
- flutter外包,承接flutter项目
- tp5.1解决跨域
- Summary of transport layer knowledge points
- 剑指 Offer 06. 从尾到头打印链表
- 剑指 Offer 11. 旋转数组的最小数字
猜你喜欢

DCC888 :SSA (static single assignment form)

【STM32技巧】使用STM32 HAL库的硬件I2C驱动RX8025T实时时钟芯片

别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!

10 Super VIM plug-ins, I can't put them down

Do domestic mobile phones turn apples? It turned out that it was realized by 100 yuan machine and sharp price reduction

ArcGIS application (20) the ArcGIS grid image symbol system prompts "this dataset does not have valid histogram required for classificati..."

2021-08-21

Leakcanary source code (2)

ArcGIS应用(二十)Arcgis 栅格图像符号系统提示“This dataset does not have valid histogram required for classificati…”

Ensure database and cache consistency
随机推荐
OJ每日一练——过滤多余的空格
OJ daily practice - word length
Spark SQL Generic Load/Save Functions(2.4.3)
Tp5.1 upload excel file and read its contents
wallys/WiFi6 MiniPCIe Module 2T2R 2 × 2.4GHz 2x5GHz
14. 最长公共前缀
Spark SQL 访问json和jdbc数据源
Spark SQL accessing JSON and JDBC data sources
SourceTree版本管理常用操作
Considerations for using redisson to operate distributed queues
弱电转职业网工难不难?华为售前工程师分享亲身经历
2021-04-05
2021-07-27
Spark SQL Generic Load/Save Functions(2.4.3)
2020-12-04
canvas生成海报
使用smart-doc自动生成接口文档
'dare not doubt the code, but have to doubt the code 'a network request timeout analysis
OJ daily practice - spanning 2020
保证数据库和缓存的一致性