当前位置:网站首页>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];
};
边栏推荐
- What is an R-value reference and what is the difference between it and an l-value?
- Global and Chinese market of AVR series microcontrollers 2022-2028: Research Report on technology, participants, trends, market size and share
- Pytest参数化你不知道的一些使用技巧 /你不知道的pytest
- Digital people anchor 618 sign language with goods, convenient for 27.8 million people with hearing impairment
- Global and Chinese markets for small seed seeders 2022-2028: Research Report on technology, participants, trends, market size and share
- IDS cache preheating, avalanche, penetration
- Booking of tourism products in Gansu quadrupled: "green horse" became popular, and one room of B & B around Gansu museum was hard to find
- Servlet learning diary 8 - servlet life cycle and thread safety
- Advance Computer Network Review(1)——FatTree
- How to intercept the string correctly (for example, intercepting the stock in operation by applying the error information)
猜你喜欢
Pytest之收集用例规则与运行指定用例
QDialog
Redis geospatial
LeetCode:124. Maximum path sum in binary tree
Advanced Computer Network Review(4)——Congestion Control of MPTCP
BMINF的后训练量化实现
Redis之连接redis服务命令
基于B/S的影视创作论坛的设计与实现(附:源码 论文 sql文件 项目部署教程)
Advanced Computer Network Review(3)——BBR
Booking of tourism products in Gansu quadrupled: "green horse" became popular, and one room of B & B around Gansu museum was hard to find
随机推荐
AcWing 2456. 记事本
The five basic data structures of redis are in-depth and application scenarios
工作流—activiti7环境搭建
使用标签模板解决用户恶意输入的问题
【每日一题】搬运工 (DFS / DP)
一改测试步骤代码就全写 为什么不试试用 Yaml实现数据驱动?
Redis之哨兵模式
Pytest之收集用例规则与运行指定用例
Reids之删除策略
QML control type: Popup
Redis之哨兵模式
Different data-driven code executes the same test scenario
Global and Chinese market of AVR series microcontrollers 2022-2028: Research Report on technology, participants, trends, market size and share
Redis之性能指标、监控方式
postman之参数化详解
Booking of tourism products in Gansu quadrupled: "green horse" became popular, and one room of B & B around Gansu museum was hard to find
IDS' deletion policy
[OC-Foundation框架]-<字符串And日期与时间>
BN folding and its quantification
Redis之五大基础数据结构深入、应用场景