当前位置:网站首页>leetcode-14. Longest common prefix JS longitudinal scanning method
leetcode-14. Longest common prefix JS longitudinal scanning method
2022-07-06 09:22:00 【Qianfan at the front】
subject
Code
/** * @param {string[]} strs * @return {string} */
// Longitudinal scanning method
var longestCommonPrefix = function(strs) {
let strLen = strs[0].length; // The length of the first element
// Traverse the first element
for (let i = 0; i < strLen; i++) {
let str = strs[0][i];
// Traverse the entire array
for (let j = 0; j < strs.length; j++) {
if (strs[j][i] !== str) {
// If there is a different character , Then exit the loop
return strs[j].slice(0, i);
}
}
}
// End of cycle , Describe each character in the first element of the array , Other elements of the array have ,
// At this point, the first element of the array has been traversed , So this element is the longest common prefix
return strs[0];
};
边栏推荐
- QML type: locale, date
- QML type: overlay
- Global and Chinese market of electronic tubes 2022-2028: Research Report on technology, participants, trends, market size and share
- 【每日一题】搬运工 (DFS / DP)
- LeetCode41——First Missing Positive——hashing in place & swap
- [three storage methods of graph] just use adjacency matrix to go out
- 【shell脚本】——归档文件脚本
- Global and Chinese market of cup masks 2022-2028: Research Report on technology, participants, trends, market size and share
- Mise en œuvre de la quantification post - formation du bminf
- Redis之主从复制
猜你喜欢
LeetCode41——First Missing Positive——hashing in place & swap
基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
Intel Distiller工具包-量化实现2
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
Advanced Computer Network Review(3)——BBR
Ijcai2022 collection of papers (continuously updated)
Nacos installation and service registration
一改测试步骤代码就全写 为什么不试试用 Yaml实现数据驱动?
注意力机制的一种卷积替代方式
Kratos战神微服务框架(二)
随机推荐
CUDA implementation of self defined convolution attention operator
CSP student queue
Redis cluster
Global and Chinese markets for small seed seeders 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode:214. Shortest palindrome string
使用标签模板解决用户恶意输入的问题
CUDA实现focal_loss
What is an R-value reference and what is the difference between it and an l-value?
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
Advance Computer Network Review(1)——FatTree
QDialog
Redis core configuration
BMINF的後訓練量化實現
Basic usage of xargs command
SimCLR:NLP中的对比学习
[oc foundation framework] - < copy object copy >
注意力机制的一种卷积替代方式
Selenium+Pytest自动化测试框架实战(下)
LeetCode:26. Remove duplicates from an ordered array
Mathematical modeling 2004b question (transmission problem)